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