Skip to content

Commit

Permalink
Ensure we always save an SVG file
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Feb 10, 2025
1 parent 3bcfa7b commit ba1f43e
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions Kitodo/src/main/java/org/kitodo/production/forms/WorkflowForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class WorkflowForm extends BaseForm {
private static final String SVG_EXTENSION = ".svg";
private static final String SVG_DIAGRAM_URI = "svgDiagramURI";
private static final String XML_DIAGRAM_URI = "xmlDiagramURI";
private Map<String, URI> activeDiagramsUris = new HashMap<>();
private String activeWorkflowTitle;
private final String workflowEditPath = MessageFormat.format(REDIRECT_PATH, "workflowEdit");
private Integer roleId;
private boolean migration;
Expand Down Expand Up @@ -154,6 +156,11 @@ public String saveAndRedirect() {
if (!this.workflow.getTemplates().isEmpty()) {
updateTemplateTasks();
}
if (Objects.nonNull(activeWorkflowTitle)
&& !activeWorkflowTitle.equals(this.workflow.getTitle())) {
deleteOldWorkflowFiles(activeWorkflowTitle);
activeWorkflowTitle = this.workflow.getTitle();
}
if (migration) {
migration = false;
return MIGRATION_FORM_PATH + "&workflowId=" + workflow.getId();
Expand Down Expand Up @@ -247,22 +254,31 @@ public void delete() {
} else {
try {
ServiceManager.getWorkflowService().remove(this.workflow);
deleteOldWorkflowFiles(this.workflow.getTitle());

String diagramDirectory = ConfigCore.getKitodoDiagramDirectory();
URI svgDiagramURI = new File(
diagramDirectory + decodeXMLDiagramName(this.workflow.getTitle()) + SVG_EXTENSION).toURI();
URI xmlDiagramURI = new File(diagramDirectory + encodeXMLDiagramName(this.workflow.getTitle()))
.toURI();

fileService.delete(svgDiagramURI);
fileService.delete(xmlDiagramURI);
} catch (DataException | IOException e) {
} catch (DataException e) {
Helper.setErrorMessage(ERROR_DELETING, new Object[] {ObjectType.WORKFLOW.getTranslationSingular() },
logger, e);
}
}
}

private void deleteOldWorkflowFiles(String oldDiagramTitle) {
try {
String diagramDirectory = ConfigCore.getKitodoDiagramDirectory();
URI svgDiagramURI = new File(
diagramDirectory + decodeXMLDiagramName(oldDiagramTitle) + SVG_EXTENSION).toURI();
URI xmlDiagramURI = new File(diagramDirectory + encodeXMLDiagramName(oldDiagramTitle))
.toURI();
fileService.delete(svgDiagramURI);
fileService.delete(xmlDiagramURI);
} catch (IOException e) {
Helper.setErrorMessage(ERROR_DELETING, new Object[] {ObjectType.WORKFLOW.getTranslationSingular() },
logger, e);
}

}

/**
* Save content of the diagram files.
*
Expand All @@ -287,6 +303,8 @@ private boolean saveFiles() throws IOException, WorkflowException {
Helper.setErrorMessage("emptyWorkflow");
return false;
}
activeDiagramsUris.put(XML_DIAGRAM_URI, xmlDiagramURI);


Reader reader = new Reader(new ByteArrayInputStream(xmlDiagram.getBytes(StandardCharsets.UTF_8)));
reader.validateWorkflowTasks();
Expand All @@ -296,10 +314,20 @@ private boolean saveFiles() throws IOException, WorkflowException {

if (Objects.nonNull(svgDiagram)) {
saveFile(svgDiagramURI, svgDiagram);
activeDiagramsUris.put(SVG_DIAGRAM_URI, svgDiagramURI);
} else {
if (fileService.fileExist(activeDiagramsUris.get(SVG_DIAGRAM_URI))) {
try (InputStream svgInputStream = ServiceManager.getFileService().read(activeDiagramsUris.get(SVG_DIAGRAM_URI))) {
svgDiagram = IOUtils.toString(svgInputStream, StandardCharsets.UTF_8);
saveFile(svgDiagramURI, svgDiagram);
}
} else {
saveFile(svgDiagramURI, "");
}
activeDiagramsUris.put(SVG_DIAGRAM_URI, svgDiagramURI);
}
saveFile(xmlDiagramURI, xmlDiagram);
}

return fileService.fileExist(xmlDiagramURI) && fileService.fileExist(svgDiagramURI);
}

Expand Down Expand Up @@ -433,6 +461,8 @@ public void load(int id) {
Workflow workflow = ServiceManager.getWorkflowService().getById(id);
setWorkflow(workflow);
setWorkflowStatus(workflow.getStatus());
activeDiagramsUris = getDiagramUris(workflow.getTitle());
activeWorkflowTitle = workflow.getTitle();
readXMLDiagram();
this.dataEditorSettingsDefined = this.dataEditorSettingService.areDataEditorSettingsDefinedForWorkflow(workflow);
} else {
Expand Down

0 comments on commit ba1f43e

Please sign in to comment.