28
28
import java .util .List ;
29
29
import java .util .Map ;
30
30
import java .util .Objects ;
31
+ import java .util .stream .Collectors ;
31
32
32
33
import javax .faces .context .ExternalContext ;
33
34
import javax .faces .context .FacesContext ;
@@ -76,6 +77,8 @@ public class WorkflowForm extends BaseForm {
76
77
private static final String SVG_EXTENSION = ".svg" ;
77
78
private static final String SVG_DIAGRAM_URI = "svgDiagramURI" ;
78
79
private static final String XML_DIAGRAM_URI = "xmlDiagramURI" ;
80
+ private Map <String , URI > activeDiagramsUris = new HashMap <>();
81
+ private String activeWorkflowTitle ;
79
82
private final String workflowEditPath = MessageFormat .format (REDIRECT_PATH , "workflowEdit" );
80
83
private Integer roleId ;
81
84
private boolean migration ;
@@ -121,20 +124,19 @@ public void setWorkflowStatus(WorkflowStatus workflowStatus) {
121
124
public void readXMLDiagram () {
122
125
URI xmlDiagramURI = new File (
123
126
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 " ));
134
135
} catch (IOException e ) {
135
136
Helper .setErrorMessage (e .getLocalizedMessage (), logger , e );
136
137
}
137
138
}
139
+ return "" ;
138
140
}
139
141
140
142
/**
@@ -154,6 +156,11 @@ public String saveAndRedirect() {
154
156
if (!this .workflow .getTemplates ().isEmpty ()) {
155
157
updateTemplateTasks ();
156
158
}
159
+ if (Objects .nonNull (activeWorkflowTitle )
160
+ && !activeWorkflowTitle .equals (this .workflow .getTitle ())) {
161
+ deleteOldWorkflowFiles (activeWorkflowTitle );
162
+ activeWorkflowTitle = this .workflow .getTitle ();
163
+ }
157
164
if (migration ) {
158
165
migration = false ;
159
166
return MIGRATION_FORM_PATH + "&workflowId=" + workflow .getId ();
@@ -247,20 +254,33 @@ public void delete() {
247
254
} else {
248
255
try {
249
256
ServiceManager .getWorkflowService ().remove (this .workflow );
257
+ deleteOldWorkflowFiles (this .workflow .getTitle ());
250
258
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 ();
259
+ } catch (DataException e ) {
260
+ Helper .setErrorMessage (ERROR_DELETING , new Object [] {ObjectType .WORKFLOW .getTranslationSingular () },
261
+ logger , e );
262
+ }
263
+ }
264
+ }
256
265
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
+ if (fileService .fileExist (svgDiagramURI )) {
257
274
fileService .delete (svgDiagramURI );
275
+ }
276
+ if (fileService .fileExist (xmlDiagramURI )) {
258
277
fileService .delete (xmlDiagramURI );
259
- } catch (DataException | IOException e ) {
260
- Helper .setErrorMessage (ERROR_DELETING , new Object [] {ObjectType .WORKFLOW .getTranslationSingular () },
261
- logger , e );
262
278
}
279
+ } catch (IOException e ) {
280
+ Helper .setErrorMessage (ERROR_DELETING , new Object [] {ObjectType .WORKFLOW .getTranslationSingular () },
281
+ logger , e );
263
282
}
283
+
264
284
}
265
285
266
286
/**
@@ -287,6 +307,8 @@ private boolean saveFiles() throws IOException, WorkflowException {
287
307
Helper .setErrorMessage ("emptyWorkflow" );
288
308
return false ;
289
309
}
310
+ activeDiagramsUris .put (XML_DIAGRAM_URI , xmlDiagramURI );
311
+
290
312
291
313
Reader reader = new Reader (new ByteArrayInputStream (xmlDiagram .getBytes (StandardCharsets .UTF_8 )));
292
314
reader .validateWorkflowTasks ();
@@ -296,10 +318,20 @@ private boolean saveFiles() throws IOException, WorkflowException {
296
318
297
319
if (Objects .nonNull (svgDiagram )) {
298
320
saveFile (svgDiagramURI , svgDiagram );
321
+ activeDiagramsUris .put (SVG_DIAGRAM_URI , svgDiagramURI );
322
+ } else {
323
+ if (fileService .fileExist (activeDiagramsUris .get (SVG_DIAGRAM_URI ))) {
324
+ try (InputStream svgInputStream = ServiceManager .getFileService ().read (activeDiagramsUris .get (SVG_DIAGRAM_URI ))) {
325
+ svgDiagram = IOUtils .toString (svgInputStream , StandardCharsets .UTF_8 );
326
+ saveFile (svgDiagramURI , svgDiagram );
327
+ }
328
+ } else {
329
+ saveFile (svgDiagramURI , "" );
330
+ }
331
+ activeDiagramsUris .put (SVG_DIAGRAM_URI , svgDiagramURI );
299
332
}
300
333
saveFile (xmlDiagramURI , xmlDiagram );
301
334
}
302
-
303
335
return fileService .fileExist (xmlDiagramURI ) && fileService .fileExist (svgDiagramURI );
304
336
}
305
337
@@ -433,6 +465,8 @@ public void load(int id) {
433
465
Workflow workflow = ServiceManager .getWorkflowService ().getById (id );
434
466
setWorkflow (workflow );
435
467
setWorkflowStatus (workflow .getStatus ());
468
+ activeDiagramsUris = getDiagramUris (workflow .getTitle ());
469
+ activeWorkflowTitle = workflow .getTitle ();
436
470
readXMLDiagram ();
437
471
this .dataEditorSettingsDefined = this .dataEditorSettingService .areDataEditorSettingsDefinedForWorkflow (workflow );
438
472
} else {
0 commit comments