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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graphfile.util;
\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
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
46 * @author Marko Luukkainen
\r
48 public class GraphFileUtil {
\r
51 public static boolean USE_RANDOM_ACCESS_BINARY = true;
\r
53 * Creates a temp file of a graphFile.
\r
56 * @throws DatabaseException
\r
58 public static File toTempFile(final Resource res)throws DatabaseException {
\r
59 return Simantics.getSession().syncRequest(new Read<File>() {
\r
61 public File perform(ReadGraph graph) throws DatabaseException {
\r
62 return toTempFile(graph, res);
\r
68 * Creates a temp file of a graphFile.
\r
72 * @throws DatabaseException
\r
74 public static File toTempFile(ReadGraph graph, Resource res)throws DatabaseException {
\r
76 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
77 String filename = graph.getRelatedValue(res, gf.HasResourceName);
\r
79 int index = filename.lastIndexOf(".");
\r
83 name = filename.substring(0,index);
\r
84 ext = filename.substring(index+1);
\r
88 if (name.length() < 3) {
\r
89 for (int i = name.length(); i < 3; i++)
\r
93 File file = File.createTempFile(name, "."+ ext);
\r
94 writeDataToFile(graph, res, file);
\r
96 } catch (Exception e) {
\r
97 throw new DatabaseException(e);
\r
101 public static void writeDataToFile(final Resource res, final File file) throws DatabaseException{
\r
102 Simantics.getSession().syncRequest(new ReadRequest() {
\r
105 public void run(ReadGraph graph) throws DatabaseException {
\r
107 writeDataToFile(graph, res, file);
\r
108 } catch (IOException e) {
\r
109 throw new DatabaseException(e);
\r
116 * Writes contents of a graphFile to file.
\r
120 * @throws DatabaseException
\r
121 * @throws IOException
\r
123 public static void writeDataToFile(ReadGraph graph, Resource res, File file) throws DatabaseException, IOException {
\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
131 byte[] data = graph.getRelatedValue(res, gf.HasFiledata);
\r
132 FileOutputStream fos = new FileOutputStream(file);
\r
138 Long lastModified = graph.getPossibleRelatedValue(res, gf.LastModified);
\r
139 if (lastModified != null)
\r
140 file.setLastModified(lastModified);
\r
144 * Updates contents of a graphFile, including the name.
\r
147 * @throws DatabaseException
\r
149 public static void toGraph(final String filename, final Resource graphFile)throws DatabaseException {
\r
150 Simantics.getSession().syncRequest(new WriteRequest() {
\r
152 public void perform(WriteGraph graph) throws DatabaseException {
\r
154 toGraph(graph, filename,graphFile);
\r
155 } catch (IOException e) {
\r
156 throw new DatabaseException(e);
\r
163 * Updates contents of a graphFile, including the name.
\r
167 * @throws DatabaseException
\r
168 * @throws IOException
\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
175 toGraph(graph, file, graphFile);
\r
179 * Updates contents of a graphFile, including the name.
\r
183 * @throws DatabaseException
\r
184 * @throws IOException
\r
186 public static void toGraph(WriteGraph graph, File file, Resource graphFile) throws DatabaseException, IOException {
\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
196 * Writes contents of a file to a graphFile (data and time stamp).
\r
200 * @throws IOException
\r
201 * @throws ManyObjectsForFunctionalRelationException
\r
202 * @throws ServiceException
\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
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
218 rab = graph.getRandomAccessBinary(fileData);
\r
220 LiteralFileUtil.copyRandomAccessBinaryFromFile(file, rab);
\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
231 size -= chan.read(buf);
\r
233 graph.claimLiteral(graphFile, gf.HasFiledata, array);
\r
238 graph.claimLiteral(graphFile, gf.LastModified, file.lastModified());
\r
242 public static void writeDataToGraph(WriteGraph graph, byte data[], Resource graphFile) throws IOException, ManyObjectsForFunctionalRelationException, ServiceException {
\r
244 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
245 graph.claimLiteral(graphFile, gf.HasFiledata, data);
\r
246 graph.claimLiteral(graphFile, gf.LastModified, 0L);
\r
250 * Writes contents of a file to a graphFile (data and time stamp).
\r
253 * @throws DatabaseException
\r
255 public static void writeDataToGraph(final File file, final Resource graphFile) throws DatabaseException {
\r
256 Simantics.getSession().syncRequest(new WriteRequest() {
\r
259 public void perform(WriteGraph graph) throws DatabaseException {
\r
261 writeDataToGraph(graph, file, graphFile);
\r
262 } catch (IOException e) {
\r
263 throw new DatabaseException(e);
\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
277 Map<Resource,File> matching = new HashMap<Resource, File>();
\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
284 if (matchingFolder != null) {
\r
285 if (matching.containsKey(matchingFolder))
\r
286 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
\r
288 matching.put(matchingFolder, f);
\r
289 syncFolderToGraph(g, f, matchingFolder);
\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
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
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
317 // delete resources, which have no matching file (or folder)
\r
318 for (Resource subFolder : subFolderResources) {
\r
319 if (!matching.containsKey(subFolder))
\r
323 for (Resource subFolder : subFileResources) {
\r
324 if (!matching.containsKey(subFolder))
\r
329 public static void writeFolderToDisk(ReadGraph g, Resource folderRes, File folder) throws DatabaseException, IOException{
\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
335 if (name.length() == 0)
\r
336 throw new DatabaseException("Empty folder name for " + subFolder);
\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
342 writeFolderToDisk(g, subFolder, newFolder);
\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
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
357 Map<Resource,File> matching = new HashMap<Resource, File>();
\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
364 if (matchingFolder != null) {
\r
365 if (matching.containsKey(matchingFolder))
\r
366 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
\r
368 matching.put(matchingFolder, f);
\r
369 syncFolderToGraph(g, f, matchingFolder,helper);
\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
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
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
391 // delete resources, which have no matching file (or folder)
\r
392 for (Resource subFolder : subFolderResources) {
\r
393 if (!matching.containsKey(subFolder))
\r
397 for (Resource subFolder : subFileResources) {
\r
398 if (!matching.containsKey(subFolder))
\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
410 public static interface ToDiskHelper {
\r
411 public String getName(ReadGraph g, Resource systemResource) throws DatabaseException;
\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
419 if (name.length() == 0)
\r
420 throw new DatabaseException("Empty folder name for " + subFolder);
\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
426 writeFolderToDisk(g, subFolder, newFolder, helper);
\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
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
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
446 Map<Resource,File> matching = new HashMap<Resource, File>();
\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
453 if (matchingFolder != null) {
\r
454 if (matching.containsKey(matchingFolder))
\r
455 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
\r
457 matching.put(matchingFolder, f);
\r
458 syncFolderToDisk(g, matchingFolder, f, helper);
\r
460 deleteDirectoryStructure(f);
\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
471 throw new Exception("Cannot delete file " + f.getAbsolutePath());
\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
480 if (name.length() == 0)
\r
481 throw new DatabaseException("Empty folder name for " + subFolder);
\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
487 writeFolderToDisk(g, subFolder, newFolder, helper);
\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
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
512 * Deletes the directory and all it contents.
\r
514 * @throws Exception
\r
516 public static void deleteDirectoryStructure(File dir) throws Exception{
\r
517 deleteDirectoryStructure(dir, true);
\r
521 * Deletes the directory's contents, but does not delete the directory.
\r
523 * @throws Exception
\r
525 public static void clearDirectoryStructure(File dir) throws Exception{
\r
526 deleteDirectoryStructure(dir, false);
\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
536 throw new Exception("Cannot delete file " + f.getAbsolutePath());
\r
540 if (!dir.delete()) {
\r
541 throw new Exception("Cannot delete folder " + dir.getAbsolutePath());
\r
546 public static Resource createFileReference(final Resource parent, final Path path) throws DatabaseException {
\r
547 return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
\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