]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/util/GraphFileUtil.java
Sync git svn branch with SVN repository r33345.
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / util / GraphFileUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphfile.util;\r
13 \r
14 import java.io.ByteArrayInputStream;\r
15 import java.io.File;\r
16 import java.io.FileInputStream;\r
17 import java.io.FileOutputStream;\r
18 import java.io.IOException;\r
19 import java.io.InputStream;\r
20 import java.nio.ByteBuffer;\r
21 import java.nio.channels.FileChannel;\r
22 import java.nio.file.Path;\r
23 import java.util.Collection;\r
24 import java.util.HashMap;\r
25 import java.util.Map;\r
26 \r
27 import org.simantics.Simantics;\r
28 import org.simantics.databoard.Bindings;\r
29 import org.simantics.databoard.util.binary.RandomAccessBinary;\r
30 import org.simantics.db.ReadGraph;\r
31 import org.simantics.db.Resource;\r
32 import org.simantics.db.WriteGraph;\r
33 import org.simantics.db.common.request.ReadRequest;\r
34 import org.simantics.db.common.request.WriteRequest;\r
35 import org.simantics.db.common.request.WriteResultRequest;\r
36 import org.simantics.db.common.utils.LiteralFileUtil;\r
37 import org.simantics.db.exception.DatabaseException;\r
38 import org.simantics.db.exception.DoesNotContainValueException;\r
39 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
40 import org.simantics.db.exception.NoSingleResultException;\r
41 import org.simantics.db.exception.ServiceException;\r
42 import org.simantics.db.request.Read;\r
43 import org.simantics.db.service.ClusteringSupport;\r
44 import org.simantics.graphfile.ontology.GraphFileResource;\r
45 import org.simantics.layer0.Layer0;\r
46 \r
47 /**\r
48  * @author Marko Luukkainen\r
49  */\r
50 public class GraphFileUtil {\r
51         \r
52         \r
53         public static boolean USE_RANDOM_ACCESS_BINARY = true;\r
54         /**\r
55          * Creates a temp file of a graphFile.\r
56          * @param res\r
57          * @return\r
58          * @throws DatabaseException\r
59          */\r
60         public static File toTempFile(final Resource res)throws DatabaseException {\r
61                 return Simantics.getSession().syncRequest(new Read<File>() {\r
62                         @Override\r
63                         public File perform(ReadGraph graph) throws DatabaseException {\r
64                                 return toTempFile(graph, res);\r
65                         }\r
66                 });\r
67         }\r
68 \r
69         /**\r
70          * Creates a temp file of a graphFile.\r
71          * @param graph\r
72          * @param res\r
73          * @return\r
74          * @throws DatabaseException\r
75          */\r
76         public static File toTempFile(ReadGraph graph, Resource res)throws DatabaseException {\r
77 \r
78                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
79                 String filename = graph.getRelatedValue(res, gf.HasResourceName);\r
80                 \r
81                 int index = filename.lastIndexOf(".");\r
82                 String name = "";\r
83                 String ext = "";\r
84                 if (index > 0) {\r
85                         name = filename.substring(0,index);\r
86                         ext = filename.substring(index+1);\r
87                 } else {\r
88                         name = filename;\r
89                 }\r
90                 if (name.length() < 3) {\r
91                         for (int i = name.length(); i < 3; i++)\r
92                                 name += "_";\r
93                 }\r
94                 try {\r
95                         File file = File.createTempFile(name, "."+ ext);\r
96                         writeDataToFile(graph, res, file);\r
97                         return file;\r
98                 } catch (Exception e) {\r
99                         throw new DatabaseException(e);\r
100                 }\r
101         }\r
102         \r
103         public static void writeDataToFile(final Resource res, final File file) throws DatabaseException{\r
104                 Simantics.getSession().syncRequest(new ReadRequest() {\r
105                         \r
106                         @Override\r
107                         public void run(ReadGraph graph) throws DatabaseException {\r
108                                 try {\r
109                                         writeDataToFile(graph, res, file);\r
110                                 } catch (IOException e) {\r
111                                         throw new DatabaseException(e);\r
112                                 }\r
113                         }\r
114                 });\r
115         }\r
116         \r
117         /**\r
118          * Writes contents of a graphFile to file.\r
119          * @param graph\r
120          * @param res\r
121          * @param file\r
122          * @throws DatabaseException\r
123          * @throws IOException\r
124          */\r
125         public static void writeDataToFile(ReadGraph graph, Resource res, File file) throws DatabaseException, IOException {\r
126                 \r
127                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
128                 if (USE_RANDOM_ACCESS_BINARY) {\r
129                         Resource filedata = graph.getSingleObject(res, gf.HasFiledata);\r
130                         LiteralFileUtil.copyRandomAccessBinaryToFile(graph, filedata, file);\r
131                 } else {\r
132                 \r
133                         byte[] data = graph.getRelatedValue(res, gf.HasFiledata);\r
134                         FileOutputStream fos = new FileOutputStream(file);\r
135                         fos.write(data);\r
136                         fos.flush();\r
137                         fos.close();\r
138                 \r
139                 }\r
140                 Long lastModified = graph.getPossibleRelatedValue(res, gf.LastModified);\r
141                 if (lastModified != null)\r
142                         file.setLastModified(lastModified);\r
143         }\r
144 \r
145         /**\r
146          * Updates contents of a graphFile, including the name.\r
147          * @param filename\r
148          * @param graphFile\r
149          * @throws DatabaseException\r
150          */\r
151         public static void toGraph(final String filename, final Resource graphFile)throws DatabaseException {\r
152                 Simantics.getSession().syncRequest(new WriteRequest() {\r
153                         @Override\r
154                         public void perform(WriteGraph graph) throws DatabaseException {\r
155                                 try {\r
156                                         toGraph(graph, filename,graphFile);\r
157                                 } catch (IOException e) {\r
158                                         throw new DatabaseException(e);\r
159                                 }\r
160                         }\r
161                 });\r
162         }\r
163         \r
164         /**\r
165          * Updates contents of a graphFile, including the name.\r
166          * @param graph\r
167          * @param filename\r
168          * @param graphFile\r
169          * @throws DatabaseException\r
170          * @throws IOException\r
171          */\r
172         public static void toGraph(WriteGraph graph, String filename, Resource graphFile) throws DatabaseException, IOException {\r
173                 File file = new File(filename);\r
174                 if (!file.exists())\r
175                         throw new IOException("File " + filename + " not found.");\r
176                 \r
177                 toGraph(graph, file, graphFile);\r
178         }\r
179         \r
180         /**\r
181          * Updates contents of a graphFile, including the name.\r
182          * @param graph\r
183          * @param file\r
184          * @param graphFile\r
185          * @throws DatabaseException\r
186          * @throws IOException\r
187          */\r
188         public static void toGraph(WriteGraph graph, File file, Resource graphFile) throws DatabaseException, IOException {\r
189 \r
190                 writeDataToGraph(graph, file, graphFile);\r
191                 String name = file.getName();\r
192                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
193                 graph.claimLiteral(graphFile, gf.HasResourceName, name);\r
194                 \r
195         }\r
196         \r
197         /**\r
198          * Writes contents of a file to a graphFile (data and time stamp).\r
199          * @param graph\r
200          * @param file\r
201          * @param graphFile\r
202          * @throws IOException\r
203          * @throws ManyObjectsForFunctionalRelationException\r
204          * @throws ServiceException\r
205          */\r
206         public static void writeDataToGraph(WriteGraph graph, File file, Resource graphFile) throws IOException, DatabaseException{\r
207                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
208                 if (USE_RANDOM_ACCESS_BINARY) {\r
209                         \r
210                         Resource fileData = graph.getPossibleObject(graphFile, gf.HasFiledata);\r
211                         RandomAccessBinary rab = null;\r
212                         if (fileData == null) {\r
213                                 Layer0 l0 = Layer0.getInstance(graph);\r
214                                 ClusteringSupport cs = graph.getService(ClusteringSupport.class);\r
215                                 fileData = graph.newResource(cs.createCluster());\r
216                                 graph.claim(fileData, l0.InstanceOf, l0.ByteArray);\r
217                                 graph.claim(graphFile, gf.HasFiledata, fileData);\r
218                                 rab = graph.createRandomAccessBinary(fileData, Bindings.BYTE_ARRAY.type(), null);\r
219                         } else {\r
220                                 rab = graph.getRandomAccessBinary(fileData);\r
221                         }\r
222                         LiteralFileUtil.copyRandomAccessBinaryFromFile(file, rab);\r
223                 } else {\r
224                         FileInputStream stream = new FileInputStream(file);\r
225                         FileChannel chan = stream.getChannel();\r
226                         long lsize = chan.size();\r
227                         if (lsize > Integer.MAX_VALUE)\r
228                                 throw new IOException("File is too big");\r
229                         int size = (int)lsize;\r
230                         final byte[] array = new byte[size];\r
231                         ByteBuffer buf = ByteBuffer.wrap(array);\r
232                         while (size > 0)\r
233                                 size -= chan.read(buf);\r
234                         \r
235                         graph.claimLiteral(graphFile, gf.HasFiledata, array);\r
236                         chan.close();\r
237                         stream.close();\r
238                 }\r
239 \r
240                 graph.claimLiteral(graphFile, gf.LastModified, file.lastModified());\r
241                 \r
242         }\r
243         \r
244         public static void writeDataToGraph(WriteGraph graph, byte data[], Resource graphFile) throws IOException, DatabaseException {\r
245                 GraphFileResource gf = GraphFileResource.getInstance(graph);\r
246                 if (USE_RANDOM_ACCESS_BINARY) {\r
247                         Resource fileData = graph.getPossibleObject(graphFile, gf.HasFiledata);\r
248                         if (fileData == null) {\r
249                                 Layer0 l0 = Layer0.getInstance(graph);\r
250                                 ClusteringSupport cs = graph.getService(ClusteringSupport.class);\r
251                                 fileData = graph.newResource(cs.createCluster());\r
252                                 graph.claim(fileData, l0.InstanceOf, l0.ByteArray);\r
253                                 graph.claim(graphFile, gf.HasFiledata, fileData);\r
254                                 graph.createRandomAccessBinary(fileData, Bindings.BYTE_ARRAY.type(), data);\r
255                         } else {\r
256                                 InputStream input = new ByteArrayInputStream(data);\r
257                                 LiteralFileUtil.copyStreamToRandomAccessBinary(graph, input, fileData);\r
258                         }\r
259                 } else {\r
260                         graph.claimLiteral(graphFile, gf.HasFiledata, data);\r
261                 }\r
262                 graph.claimLiteral(graphFile, gf.LastModified, System.currentTimeMillis());\r
263         }\r
264         \r
265         /**\r
266          * Writes contents of a file to a graphFile (data and time stamp).\r
267          * @param file\r
268          * @param graphFile\r
269          * @throws DatabaseException\r
270          */\r
271         public static void writeDataToGraph(final File file, final Resource graphFile) throws DatabaseException {\r
272                 Simantics.getSession().syncRequest(new WriteRequest() {\r
273                         \r
274                         @Override\r
275                         public void perform(WriteGraph graph) throws DatabaseException {\r
276                                 try {\r
277                                         writeDataToGraph(graph, file, graphFile);\r
278                                 } catch (IOException e) {\r
279                                         throw new DatabaseException(e);\r
280                                 }\r
281                                 \r
282                         }\r
283                 });\r
284         }\r
285         \r
286         public static void syncFolderToGraph(WriteGraph g, File folder, Resource folderRes) throws Exception {\r
287                 File subFiles[] = folder.listFiles();\r
288                 Layer0 l0 = Layer0.getInstance(g);\r
289                 GraphFileResource gf = GraphFileResource.getInstance(g);\r
290                 Collection<Resource> subFileResources = g.getObjects(folderRes, gf.HasFile);\r
291                 Collection<Resource> subFolderResources = g.getObjects(folderRes, gf.HasFolder);\r
292                                 \r
293                 Map<Resource,File> matching = new HashMap<Resource, File>();\r
294                 \r
295                 for (File f : subFiles) {\r
296                         String name = f.getName();\r
297                         if (f.isDirectory()) {\r
298                                 Resource matchingFolder = findWithName(g, subFolderResources, name);\r
299 \r
300                                 if (matchingFolder != null) {\r
301                                         if (matching.containsKey(matchingFolder))\r
302                                                 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);\r
303                                         \r
304                                         matching.put(matchingFolder, f);\r
305                                         syncFolderToGraph(g, f, matchingFolder);\r
306                                 } else {\r
307                                         matchingFolder = g.newResource();\r
308                                         g.claim(matchingFolder, l0.InstanceOf, gf.Folder);\r
309                                         g.claimLiteral(matchingFolder, gf.HasResourceName, name);\r
310                                         g.claimLiteral(matchingFolder, l0.HasName, name);\r
311                                         g.claim(folderRes, gf.HasFolder, matchingFolder);\r
312                                         matching.put(matchingFolder, f);\r
313                                         syncFolderToGraph(g, f, matchingFolder);\r
314                                 }\r
315                         } else { //file\r
316                                 Resource fileRes = findWithName(g, subFileResources, name);\r
317                                 if (fileRes != null) {\r
318                                         if (matching.containsKey(fileRes))\r
319                                                 throw new Exception("Matching file already in use" + f.getAbsolutePath() + " " + fileRes);\r
320                                         matching.put(fileRes, f);\r
321                                         toGraph(g, f, fileRes);\r
322                                 } else {\r
323                                         fileRes = g.newResource();\r
324                                         g.claim(fileRes, l0.InstanceOf, gf.File);\r
325                                         g.claimLiteral(fileRes, gf.HasResourceName, name);\r
326                                         g.claimLiteral(fileRes, l0.HasName, name);\r
327                                         g.claim(folderRes, gf.HasFile, fileRes);\r
328                                         matching.put(fileRes, f);\r
329                                         toGraph(g, f, fileRes);\r
330                                 }\r
331                         }\r
332                 }\r
333                 // delete resources, which have no matching file (or folder)\r
334                 for (Resource subFolder : subFolderResources) {\r
335                         if (!matching.containsKey(subFolder))\r
336                                 g.deny(subFolder);\r
337                 }\r
338                 \r
339                 for (Resource subFolder : subFileResources) {\r
340                         if (!matching.containsKey(subFolder))\r
341                                 g.deny(subFolder);\r
342                 }\r
343         }\r
344         \r
345         public static void writeFolderToDisk(ReadGraph g, Resource folderRes, File folder) throws DatabaseException, IOException{\r
346 \r
347                 GraphFileResource gf = GraphFileResource.getInstance(g);\r
348                 for (Resource subFolder : g.getObjects(folderRes, gf.HasFolder)) {\r
349                         String name = g.getRelatedValue(subFolder, gf.HasResourceName);\r
350 \r
351                         if (name.length() == 0)\r
352                                 throw new DatabaseException("Empty folder name for " + subFolder);\r
353                         \r
354                         File newFolder = new File(folder.getAbsolutePath() + "/" + name);\r
355                         if (!newFolder.mkdir()) {\r
356                                 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);\r
357                         }\r
358                         writeFolderToDisk(g, subFolder, newFolder);\r
359                 }\r
360                 for (Resource fileRes : g.getObjects(folderRes, gf.HasFile)) {\r
361                         String name = g.getRelatedValue(fileRes, gf.HasResourceName);\r
362                         File file = new File(folder.getAbsolutePath() + "/" + name);\r
363                         writeDataToFile(g, fileRes, file);\r
364                 }\r
365         }\r
366         \r
367         public static void syncFolderToGraph(WriteGraph g, File folder, Resource folderRes, ToGraphHelper helper) throws Exception {\r
368                 File subFiles[] = folder.listFiles();\r
369                 GraphFileResource gf = GraphFileResource.getInstance(g);\r
370                 Collection<Resource> subFileResources = g.getObjects(folderRes, gf.HasFile);\r
371                 Collection<Resource> subFolderResources = g.getObjects(folderRes, gf.HasFolder);\r
372                                 \r
373                 Map<Resource,File> matching = new HashMap<Resource, File>();\r
374                 \r
375                 for (File f : subFiles) {\r
376                         String name = f.getName();\r
377                         if (f.isDirectory()) {\r
378                                 Resource matchingFolder = helper.findFolder(g, subFolderResources, name);\r
379 \r
380                                 if (matchingFolder != null) {\r
381                                         if (matching.containsKey(matchingFolder))\r
382                                                 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);\r
383                                         \r
384                                         matching.put(matchingFolder, f);\r
385                                         syncFolderToGraph(g, f, matchingFolder,helper);\r
386                                 } else {\r
387                                         matchingFolder = helper.createFolder(g, name);\r
388                                         g.claim(folderRes, gf.HasFolder, matchingFolder);\r
389                                         matching.put(matchingFolder, f);\r
390                                         syncFolderToGraph(g, f, matchingFolder,helper);\r
391                                 }\r
392                         } else { //file\r
393                                 Resource fileRes = helper.findFile(g, subFileResources, name);\r
394                                 if (fileRes != null) {\r
395                                         if (matching.containsKey(fileRes))\r
396                                                 throw new Exception("Matching file already in use" + f.getAbsolutePath() + " " + fileRes);\r
397                                         matching.put(fileRes, f);\r
398                                         toGraph(g, f, fileRes);\r
399                                 } else {\r
400                                         fileRes = helper.createFile(g, name);\r
401                                         g.claim(folderRes, gf.HasFile, fileRes);\r
402                                         matching.put(fileRes, f);\r
403                                         toGraph(g, f, fileRes);\r
404                                 }\r
405                         }\r
406                 }\r
407                 // delete resources, which have no matching file (or folder)\r
408                 for (Resource subFolder : subFolderResources) {\r
409                         if (!matching.containsKey(subFolder))\r
410                                 g.deny(subFolder);\r
411                 }\r
412                 \r
413                 for (Resource subFolder : subFileResources) {\r
414                         if (!matching.containsKey(subFolder))\r
415                                 g.deny(subFolder);\r
416                 }\r
417         }\r
418         \r
419         public static interface ToGraphHelper {\r
420                 public Resource findFolder(ReadGraph g, Collection<Resource> subFolderResources, String name) throws DatabaseException;\r
421                 public Resource createFolder(WriteGraph g, String name) throws DatabaseException;\r
422                 public Resource findFile(ReadGraph g, Collection<Resource> subFileResources, String name) throws DatabaseException;\r
423                 public Resource createFile(WriteGraph g, String name) throws DatabaseException;\r
424         }\r
425         \r
426         public static interface ToDiskHelper {\r
427                 public String getName(ReadGraph g, Resource systemResource) throws DatabaseException;\r
428         }\r
429         \r
430         public static void writeFolderToDisk(ReadGraph g, Resource folderRes, File folder, ToDiskHelper helper) throws DatabaseException, IOException{\r
431                 GraphFileResource gf = GraphFileResource.getInstance(g);\r
432                 for (Resource subFolder : g.getObjects(folderRes, gf.HasFolder)) {\r
433                         String name = helper.getName(g, subFolder);\r
434 \r
435                         if (name.length() == 0)\r
436                                 throw new DatabaseException("Empty folder name for " + subFolder);\r
437                         \r
438                         File newFolder = new File(folder.getAbsolutePath() + "/" + name);\r
439                         if (!newFolder.mkdir()) {\r
440                                 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);\r
441                         }\r
442                         writeFolderToDisk(g, subFolder, newFolder, helper);\r
443                 }\r
444                 for (Resource fileRes : g.getObjects(folderRes, gf.HasFile)) {\r
445                         String name = helper.getName(g, fileRes);\r
446                         File file = new File(folder.getAbsolutePath() + "/" + name);\r
447                         writeDataToFile(g, fileRes, file);\r
448                 }\r
449         }\r
450         \r
451         public static interface ToDiskHelper2 extends ToDiskHelper {\r
452                 public Resource findFolder(ReadGraph g, Collection<Resource> subFolderResources, String name) throws DatabaseException;\r
453                 public Resource findFile(ReadGraph g, Collection<Resource> subFileResources, String name) throws DatabaseException;\r
454         }\r
455         \r
456         public static void syncFolderToDisk(ReadGraph g, Resource folderRes, File folder, ToDiskHelper2 helper) throws Exception {\r
457                 File subFiles[] = folder.listFiles();\r
458                 GraphFileResource gf = GraphFileResource.getInstance(g);\r
459                 Collection<Resource> subFileResources = g.getObjects(folderRes, gf.HasFile);\r
460                 Collection<Resource> subFolderResources = g.getObjects(folderRes, gf.HasFolder);\r
461                                 \r
462                 Map<Resource,File> matching = new HashMap<Resource, File>();\r
463                 \r
464                 for (File f : subFiles) {\r
465                         String name = f.getName();\r
466                         if (f.isDirectory()) {\r
467                                 Resource matchingFolder = helper.findFolder(g, subFolderResources, name);\r
468 \r
469                                 if (matchingFolder != null) {\r
470                                         if (matching.containsKey(matchingFolder))\r
471                                                 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);\r
472                                         \r
473                                         matching.put(matchingFolder, f);\r
474                                         syncFolderToDisk(g, matchingFolder, f, helper);\r
475                                 } else {\r
476                                         deleteDirectoryStructure(f);\r
477                                 }\r
478                         } else { //file\r
479                                 Resource fileRes = helper.findFile(g, subFileResources, name);\r
480                                 if (fileRes != null) {\r
481                                         if (matching.containsKey(fileRes))\r
482                                                 throw new Exception("Matching file already in use" + f.getAbsolutePath() + " " + fileRes);\r
483                                         matching.put(fileRes, f);\r
484                                         writeDataToFile(g, fileRes, f);\r
485                                 } else {\r
486                                         if (!f.delete())\r
487                                         throw new Exception("Cannot delete file " + f.getAbsolutePath());\r
488                                 }\r
489                         }\r
490                 }\r
491                 // create files and folders, which have no matching graphFile (or folder)\r
492                 for (Resource subFolder : subFolderResources) {\r
493                         if (!matching.containsKey(subFolder)) {\r
494                                 String name = helper.getName(g, subFolder);\r
495 \r
496                                 if (name.length() == 0)\r
497                                         throw new DatabaseException("Empty folder name for " + subFolder);\r
498                                 \r
499                                 File newFolder = new File(folder.getAbsolutePath() + "/" + name);\r
500                                 if (!newFolder.mkdir()) {\r
501                                         throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);\r
502                                 }\r
503                                 writeFolderToDisk(g, subFolder, newFolder, helper);\r
504                         }\r
505                 }\r
506                 \r
507                 for (Resource fileRes : subFileResources) {\r
508                         if (!matching.containsKey(fileRes)) {\r
509                                 String name = helper.getName(g, fileRes);\r
510                                 File file = new File(folder.getAbsolutePath() + "/" + name);\r
511                                 writeDataToFile(g, fileRes, file);\r
512                         }\r
513                 }\r
514                 \r
515         }\r
516         \r
517         \r
518         \r
519         public static Resource findWithName(ReadGraph g, Collection<Resource> resources, String name) throws ServiceException, NoSingleResultException, DoesNotContainValueException {\r
520                 GraphFileResource gf = GraphFileResource.getInstance(g);\r
521                 for (Resource r : resources)\r
522                         if (name.equals(g.getRelatedValue(r, gf.HasResourceName)))\r
523                                 return r;\r
524                 return null;\r
525         }\r
526         \r
527         /**\r
528          * Deletes the directory and all it contents.\r
529          * @param dir\r
530          * @throws Exception\r
531          */\r
532         public static void deleteDirectoryStructure(File dir) throws Exception{\r
533                 deleteDirectoryStructure(dir, true);\r
534         }\r
535         \r
536         /**\r
537          * Deletes the directory's contents, but does not delete the directory.\r
538          * @param dir\r
539          * @throws Exception\r
540          */\r
541         public static void clearDirectoryStructure(File dir) throws Exception{\r
542                 deleteDirectoryStructure(dir, false);\r
543         }\r
544         \r
545         private static void deleteDirectoryStructure(File dir, boolean deleteDir) throws Exception{\r
546                 File subFiles[] = dir.listFiles();\r
547                 for (File f : subFiles) {\r
548                         if (f.isDirectory())\r
549                                 deleteDirectoryStructure(f,true);\r
550                         else\r
551                                 if (!f.delete()) {\r
552                                         throw new Exception("Cannot delete file " + f.getAbsolutePath());\r
553                                 }\r
554                 }\r
555                 if (deleteDir) {\r
556                         if (!dir.delete()) {\r
557                                 throw new Exception("Cannot delete folder " + dir.getAbsolutePath());\r
558                         }\r
559                 }\r
560         }\r
561 \r
562     public static Resource createFileReference(final Resource parent, final Path path) throws DatabaseException {\r
563         return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {\r
564 \r
565             @Override\r
566             public Resource perform(WriteGraph graph) throws DatabaseException {\r
567                 Layer0 L0 = Layer0.getInstance(graph);\r
568                 GraphFileResource GF = GraphFileResource.getInstance(graph);\r
569                 Resource file = graph.newResource();\r
570                 graph.claim(file, L0.PartOf, parent);\r
571                 graph.claim(file, L0.InstanceOf, GF.File);\r
572                 String name = path.getFileName().toString();\r
573                 graph.claimLiteral(file, L0.HasName, name, Bindings.STRING);\r
574                 graph.claimLiteral(file, GF.SystemPath, path.toAbsolutePath().toString(), Bindings.STRING);\r
575                 return file;\r
576             }\r
577         });\r
578     }\r
579 }\r