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