|
23 | 23 | import java.io.File; |
24 | 24 | import java.io.IOException; |
25 | 25 | import java.io.InputStream; |
| 26 | +import java.nio.file.FileAlreadyExistsException; |
26 | 27 | import java.nio.file.Files; |
27 | 28 | import java.nio.file.Path; |
28 | 29 | import java.nio.file.Paths; |
|
39 | 40 | @Service |
40 | 41 | public class GraphVizService { |
41 | 42 |
|
42 | | - private final String graphvizStorage; |
| 43 | + private final String graphvizStorage; |
43 | 44 |
|
44 | | - @Autowired |
45 | | - public GraphVizService(@Value("${graphvizStorage}") String graphvizStorage) { |
46 | | - this.graphvizStorage = graphvizStorage; |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * Generate a graph in a specified format using GraphViz |
51 | | - * @param dot The DOT source |
52 | | - * @param format The format for the graph to be generated in, e.g. "svg", "png", "dot" |
53 | | - * @return An InputStream containing the graph in desired image format. |
54 | | - */ |
55 | | - public InputStream getGraphStream(String dot, String format) { |
56 | | - // Generate graphviz image if it does not already exist |
57 | | - GraphViz gv = new GraphViz(); |
58 | | - |
59 | | - // Different DPI and transparency for svg files |
60 | | - if (format.equals("svg")) { |
61 | | - gv.decreaseDpi(); |
62 | | - gv.decreaseDpi(); |
63 | | - gv.decreaseDpi(); |
64 | | - dot = dot.replace("bgcolor = \"#eeeeee\"", "bgcolor = \"transparent\""); |
65 | | - } |
66 | | - |
67 | | - byte[] dotBytes = gv.getGraph(dot, format, "dot"); |
68 | | - return new ByteArrayInputStream(dotBytes); |
69 | | - } |
70 | | - |
| 45 | + @Autowired |
| 46 | + public GraphVizService(@Value("${graphvizStorage}") String graphvizStorage) { |
| 47 | + this.graphvizStorage = graphvizStorage; |
| 48 | + } |
71 | 49 |
|
72 | | - /** |
73 | | - * Generate a graph in a specified format using GraphViz |
74 | | - * @param fileName The relative name of the file to be generated in the graphvizStorage directory |
75 | | - * @param dot The DOT source |
76 | | - * @param format The format for the graph to be generated in, e.g. "svg", "png", "dot" |
77 | | - * @return The file containing the graph |
78 | | - * @throws IOException if the writing failed (e.g. out of disk space) |
79 | | - */ |
80 | | - public Path getGraphPath(String fileName, String dot, String format) throws IOException { |
81 | | - // Generate graphviz image if it does not already exist |
82 | | - Path out = Paths.get(graphvizStorage).resolve(fileName); |
83 | | - if (!Files.exists(out)) { |
84 | | - Files.copy(getGraphStream(dot, format), out); |
85 | | - } |
86 | | - return out; |
87 | | - } |
| 50 | + /** |
| 51 | + * Generate a graph in a specified format using GraphViz |
| 52 | + * |
| 53 | + * @param dot The DOT source |
| 54 | + * @param format The format for the graph to be generated in, e.g. "svg", "png", |
| 55 | + * "dot" |
| 56 | + * @return An InputStream containing the graph in desired image format. |
| 57 | + */ |
| 58 | + public InputStream getGraphStream(String dot, String format) { |
| 59 | + // Generate graphviz image if it does not already exist |
| 60 | + GraphViz gv = new GraphViz(); |
88 | 61 |
|
89 | | - /** |
90 | | - * Delete the cache of workflow images for a workflow |
91 | | - * TODO: Make this more dynamic in some way, either store in workflow object or clear folder |
92 | | - * @param workflowID The ID of the workflow used for assuming file locations |
93 | | - */ |
94 | | - public void deleteCache(String workflowID) { |
95 | | - File graphvizSvg = new File(graphvizStorage + "/" + workflowID + ".svg"); |
96 | | - graphvizSvg.delete(); |
97 | | - File graphvizPng = new File(graphvizStorage + "/" + workflowID + ".png"); |
98 | | - graphvizPng.delete(); |
99 | | - File graphvizXdot = new File(graphvizStorage + "/" + workflowID + ".dot"); |
100 | | - graphvizXdot.delete(); |
101 | | - } |
| 62 | + // Different DPI and transparency for svg files |
| 63 | + if (format.equals("svg")) { |
| 64 | + gv.decreaseDpi(); |
| 65 | + gv.decreaseDpi(); |
| 66 | + gv.decreaseDpi(); |
| 67 | + dot = dot.replace("bgcolor = \"#eeeeee\"", "bgcolor = \"transparent\""); |
| 68 | + } |
| 69 | + |
| 70 | + byte[] dotBytes = gv.getGraph(dot, format, "dot"); |
| 71 | + return new ByteArrayInputStream(dotBytes); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Generate a graph in a specified format using GraphViz |
| 76 | + * |
| 77 | + * @param fileName The relative name of the file to be generated in the |
| 78 | + * graphvizStorage directory |
| 79 | + * @param dot The DOT source |
| 80 | + * @param format The format for the graph to be generated in, e.g. "svg", |
| 81 | + * "png", "dot" |
| 82 | + * @return The file containing the graph |
| 83 | + * @throws IOException if the writing failed (e.g. out of disk space) |
| 84 | + */ |
| 85 | + public Path getGraphPath(String fileName, String dot, String format) throws IOException { |
| 86 | + // Generate graphviz image if it does not already exist |
| 87 | + Path out = Paths.get(graphvizStorage).resolve(fileName); |
| 88 | + try { |
| 89 | + if (!Files.exists(out)) { |
| 90 | + Files.copy(getGraphStream(dot, format), out); |
| 91 | + } |
| 92 | + } catch (FileAlreadyExistsException ex) { |
| 93 | + return out; |
| 94 | + } |
| 95 | + return out; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Delete the cache of workflow images for a workflow TODO: Make this more |
| 100 | + * dynamic in some way, either store in workflow object or clear folder |
| 101 | + * |
| 102 | + * @param workflowID The ID of the workflow used for assuming file locations |
| 103 | + */ |
| 104 | + public void deleteCache(String workflowID) { |
| 105 | + File graphvizSvg = new File(graphvizStorage + "/" + workflowID + ".svg"); |
| 106 | + graphvizSvg.delete(); |
| 107 | + File graphvizPng = new File(graphvizStorage + "/" + workflowID + ".png"); |
| 108 | + graphvizPng.delete(); |
| 109 | + File graphvizXdot = new File(graphvizStorage + "/" + workflowID + ".dot"); |
| 110 | + graphvizXdot.delete(); |
| 111 | + } |
102 | 112 | } |
0 commit comments