2828import java .util .List ;
2929import java .util .Map ;
3030import java .util .Objects ;
31+ import java .util .stream .Collectors ;
3132
3233import javax .faces .context .ExternalContext ;
3334import javax .faces .context .FacesContext ;
@@ -76,6 +77,8 @@ public class WorkflowForm extends BaseForm {
7677 private static final String SVG_EXTENSION = ".svg" ;
7778 private static final String SVG_DIAGRAM_URI = "svgDiagramURI" ;
7879 private static final String XML_DIAGRAM_URI = "xmlDiagramURI" ;
80+ private Map <String , URI > activeDiagramsUris = new HashMap <>();
81+ private String activeWorkflowTitle ;
7982 private final String workflowEditPath = MessageFormat .format (REDIRECT_PATH , "workflowEdit" );
8083 private Integer roleId ;
8184 private boolean migration ;
@@ -121,20 +124,19 @@ public void setWorkflowStatus(WorkflowStatus workflowStatus) {
121124 public void readXMLDiagram () {
122125 URI xmlDiagramURI = new File (
123126 ConfigCore .getKitodoDiagramDirectory () + encodeXMLDiagramName (this .workflow .getTitle ())).toURI ();
124- if (fileService .fileExist (xmlDiagramURI )) {
125- try (InputStream inputStream = fileService .read (xmlDiagramURI );
126- BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream ))) {
127- StringBuilder sb = new StringBuilder ();
128- String line = bufferedReader .readLine ();
129- while (Objects .nonNull (line )) {
130- sb .append (line ).append ("\n " );
131- line = bufferedReader .readLine ();
132- }
133- xmlDiagram = sb .toString ();
127+ xmlDiagram = readFileContent (xmlDiagramURI );
128+ }
129+
130+ private String readFileContent (URI fileUri ) {
131+ if (fileService .fileExist (fileUri )) {
132+ try (InputStream inputStream = fileService .read (fileUri );
133+ BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream ))) {
134+ return reader .lines ().collect (Collectors .joining ("\n " ));
134135 } catch (IOException e ) {
135136 Helper .setErrorMessage (e .getLocalizedMessage (), logger , e );
136137 }
137138 }
139+ return "" ;
138140 }
139141
140142 /**
@@ -154,6 +156,11 @@ public String saveAndRedirect() {
154156 if (!this .workflow .getTemplates ().isEmpty ()) {
155157 updateTemplateTasks ();
156158 }
159+ if (Objects .nonNull (activeWorkflowTitle )
160+ && !activeWorkflowTitle .equals (this .workflow .getTitle ())) {
161+ deleteOldWorkflowFiles (activeWorkflowTitle );
162+ activeWorkflowTitle = this .workflow .getTitle ();
163+ }
157164 if (migration ) {
158165 migration = false ;
159166 return MIGRATION_FORM_PATH + "&workflowId=" + workflow .getId ();
@@ -247,22 +254,31 @@ public void delete() {
247254 } else {
248255 try {
249256 ServiceManager .getWorkflowService ().remove (this .workflow );
257+ deleteOldWorkflowFiles (this .workflow .getTitle ());
250258
251- String diagramDirectory = ConfigCore .getKitodoDiagramDirectory ();
252- URI svgDiagramURI = new File (
253- diagramDirectory + decodeXMLDiagramName (this .workflow .getTitle ()) + SVG_EXTENSION ).toURI ();
254- URI xmlDiagramURI = new File (diagramDirectory + encodeXMLDiagramName (this .workflow .getTitle ()))
255- .toURI ();
256-
257- fileService .delete (svgDiagramURI );
258- fileService .delete (xmlDiagramURI );
259- } catch (DataException | IOException e ) {
259+ } catch (DataException e ) {
260260 Helper .setErrorMessage (ERROR_DELETING , new Object [] {ObjectType .WORKFLOW .getTranslationSingular () },
261261 logger , e );
262262 }
263263 }
264264 }
265265
266+ private void deleteOldWorkflowFiles (String oldDiagramTitle ) {
267+ try {
268+ String diagramDirectory = ConfigCore .getKitodoDiagramDirectory ();
269+ URI svgDiagramURI = new File (
270+ diagramDirectory + decodeXMLDiagramName (oldDiagramTitle ) + SVG_EXTENSION ).toURI ();
271+ URI xmlDiagramURI = new File (diagramDirectory + encodeXMLDiagramName (oldDiagramTitle ))
272+ .toURI ();
273+ fileService .delete (svgDiagramURI );
274+ fileService .delete (xmlDiagramURI );
275+ } catch (IOException e ) {
276+ Helper .setErrorMessage (ERROR_DELETING , new Object [] {ObjectType .WORKFLOW .getTranslationSingular () },
277+ logger , e );
278+ }
279+
280+ }
281+
266282 /**
267283 * Save content of the diagram files.
268284 *
@@ -287,6 +303,8 @@ private boolean saveFiles() throws IOException, WorkflowException {
287303 Helper .setErrorMessage ("emptyWorkflow" );
288304 return false ;
289305 }
306+ activeDiagramsUris .put (XML_DIAGRAM_URI , xmlDiagramURI );
307+
290308
291309 Reader reader = new Reader (new ByteArrayInputStream (xmlDiagram .getBytes (StandardCharsets .UTF_8 )));
292310 reader .validateWorkflowTasks ();
@@ -296,10 +314,20 @@ private boolean saveFiles() throws IOException, WorkflowException {
296314
297315 if (Objects .nonNull (svgDiagram )) {
298316 saveFile (svgDiagramURI , svgDiagram );
317+ activeDiagramsUris .put (SVG_DIAGRAM_URI , svgDiagramURI );
318+ } else {
319+ if (fileService .fileExist (activeDiagramsUris .get (SVG_DIAGRAM_URI ))) {
320+ try (InputStream svgInputStream = ServiceManager .getFileService ().read (activeDiagramsUris .get (SVG_DIAGRAM_URI ))) {
321+ svgDiagram = IOUtils .toString (svgInputStream , StandardCharsets .UTF_8 );
322+ saveFile (svgDiagramURI , svgDiagram );
323+ }
324+ } else {
325+ saveFile (svgDiagramURI , "" );
326+ }
327+ activeDiagramsUris .put (SVG_DIAGRAM_URI , svgDiagramURI );
299328 }
300329 saveFile (xmlDiagramURI , xmlDiagram );
301330 }
302-
303331 return fileService .fileExist (xmlDiagramURI ) && fileService .fileExist (svgDiagramURI );
304332 }
305333
@@ -433,6 +461,8 @@ public void load(int id) {
433461 Workflow workflow = ServiceManager .getWorkflowService ().getById (id );
434462 setWorkflow (workflow );
435463 setWorkflowStatus (workflow .getStatus ());
464+ activeDiagramsUris = getDiagramUris (workflow .getTitle ());
465+ activeWorkflowTitle = workflow .getTitle ();
436466 readXMLDiagram ();
437467 this .dataEditorSettingsDefined = this .dataEditorSettingService .areDataEditorSettingsDefinedForWorkflow (workflow );
438468 } else {
0 commit comments