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