Skip to content

Commit

Permalink
Further adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Feb 7, 2025
1 parent 67aaf88 commit e47c4ad
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions Kitodo/src/main/java/org/kitodo/production/forms/WorkflowForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,19 @@ public void setWorkflowStatus(WorkflowStatus workflowStatus) {
public void readXMLDiagram() {
URI xmlDiagramURI = new File(
ConfigCore.getKitodoDiagramDirectory() + encodeXMLDiagramName(this.workflow.getTitle())).toURI();

try (InputStream inputStream = fileService.read(xmlDiagramURI);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder sb = new StringBuilder();
String line = bufferedReader.readLine();
while (Objects.nonNull(line)) {
sb.append(line).append("\n");
line = bufferedReader.readLine();
if (fileService.fileExist(xmlDiagramURI)) {
try (InputStream inputStream = fileService.read(xmlDiagramURI);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder sb = new StringBuilder();
String line = bufferedReader.readLine();
while (Objects.nonNull(line)) {
sb.append(line).append("\n");
line = bufferedReader.readLine();
}
xmlDiagram = sb.toString();
} catch (IOException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
xmlDiagram = sb.toString();
} catch (IOException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
}

Expand Down Expand Up @@ -371,27 +372,29 @@ public String duplicate(Integer itemId) {
Map<String, URI> diagramsUris = getDiagramUris(baseWorkflow.getTitle());

URI xmlDiagramURI = diagramsUris.get(XML_DIAGRAM_URI);
URI svgDiagramURI = diagramsUris.get(SVG_DIAGRAM_URI);

this.workflow = ServiceManager.getWorkflowService().duplicateWorkflow(baseWorkflow);
setWorkflowStatus(WorkflowStatus.DRAFT);
Map<String, URI> diagramsCopyUris = getDiagramUris();

URI xmlDiagramCopyURI = diagramsCopyUris.get(XML_DIAGRAM_URI);

// Read XML diagram
try (InputStream xmlInputStream = ServiceManager.getFileService().read(xmlDiagramURI)) {
this.xmlDiagram = IOUtils.toString(xmlInputStream, StandardCharsets.UTF_8);
saveFile(xmlDiagramCopyURI, this.xmlDiagram);
} catch (IOException e) {
Helper.setErrorMessage("unableToDuplicateWorkflow", logger, e);
return this.stayOnCurrentPage;
}
// Read SVG diagram (use a separate input stream)
try (InputStream svgInputStream = ServiceManager.getFileService().read(svgDiagramURI)) {
this.svgDiagram = IOUtils.toString(svgInputStream, StandardCharsets.UTF_8);
}
// Store duplicated workflow in Flash scope
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.getFlash().put("duplicatedWorkflow", this.workflow);
externalContext.getFlash().put("xmlDiagram", this.xmlDiagram);
externalContext.getFlash().put("svgDiagram", this.svgDiagram);

return workflowEditPath + "&id=0";

} catch (DAOException e) {

} catch (IOException | DAOException e) {
Helper.setErrorMessage(ERROR_DUPLICATE, new Object[] {ObjectType.WORKFLOW.getTranslationSingular() },
logger, e);
return this.stayOnCurrentPage;
Expand Down Expand Up @@ -423,21 +426,24 @@ public void setWorkflowById(int id) {
*/
public void load(int id) {
try {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> flash = externalContext.getFlash();
// Check if duplicated workflow is stored in Flash scope
if (flash.containsKey("duplicatedWorkflow")) {
this.workflow = (Workflow) flash.get("duplicatedWorkflow");
setWorkflowStatus(workflow.getStatus());
readXMLDiagram();
this.dataEditorSettingsDefined = this.dataEditorSettingService.areDataEditorSettingsDefinedForWorkflow(workflow);
} else if (id > 0) {
if (id > 0) {
// Normal case: Load workflow from database
Workflow workflow = ServiceManager.getWorkflowService().getById(id);
setWorkflow(workflow);
setWorkflowStatus(workflow.getStatus());
readXMLDiagram();
this.dataEditorSettingsDefined = this.dataEditorSettingService.areDataEditorSettingsDefinedForWorkflow(workflow);
} else {
// Check if duplicated workflow is stored in Flash scope
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> flash = externalContext.getFlash();
if (flash.containsKey("duplicatedWorkflow")) {
this.workflow = (Workflow) flash.get("duplicatedWorkflow");
this.xmlDiagram = (String) flash.get("xmlDiagram");
this.svgDiagram = (String) flash.get("svgDiagram");
setWorkflowStatus(workflow.getStatus());
this.dataEditorSettingsDefined = this.dataEditorSettingService.areDataEditorSettingsDefinedForWorkflow(workflow);
}
}
setSaveDisabled(false);
} catch (DAOException e) {
Expand Down

0 comments on commit e47c4ad

Please sign in to comment.