1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphfile.util;
14 import java.io.ByteArrayInputStream;
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;
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;
48 * @author Marko Luukkainen
50 public class GraphFileUtil {
53 public static boolean USE_RANDOM_ACCESS_BINARY = true;
55 * Creates a temp file of a graphFile.
58 * @throws DatabaseException
60 public static File toTempFile(final Resource res)throws DatabaseException {
61 return Simantics.getSession().syncRequest(new Read<File>() {
63 public File perform(ReadGraph graph) throws DatabaseException {
64 return toTempFile(graph, res);
70 * Creates a temp file of a graphFile.
74 * @throws DatabaseException
76 public static File toTempFile(ReadGraph graph, Resource res)throws DatabaseException {
78 GraphFileResource gf = GraphFileResource.getInstance(graph);
79 String filename = graph.getRelatedValue(res, gf.HasResourceName);
81 int index = filename.lastIndexOf(".");
85 name = filename.substring(0,index);
86 ext = filename.substring(index+1);
90 if (name.length() < 3) {
91 for (int i = name.length(); i < 3; i++)
95 File file = File.createTempFile(name, "."+ ext);
96 writeDataToFile(graph, res, file);
98 } catch (Exception e) {
99 throw new DatabaseException(e);
103 public static void writeDataToFile(final Resource res, final File file) throws DatabaseException{
104 Simantics.getSession().syncRequest(new ReadRequest() {
107 public void run(ReadGraph graph) throws DatabaseException {
109 writeDataToFile(graph, res, file);
110 } catch (IOException e) {
111 throw new DatabaseException(e);
118 * Writes contents of a graphFile to file.
122 * @throws DatabaseException
123 * @throws IOException
125 public static void writeDataToFile(ReadGraph graph, Resource res, File file) throws DatabaseException, IOException {
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);
133 byte[] data = graph.getRelatedValue(res, gf.HasFiledata);
134 FileOutputStream fos = new FileOutputStream(file);
140 Long lastModified = graph.getPossibleRelatedValue(res, gf.LastModified);
141 if (lastModified != null)
142 file.setLastModified(lastModified);
146 * Updates contents of a graphFile, including the name.
149 * @throws DatabaseException
151 public static void toGraph(final String filename, final Resource graphFile)throws DatabaseException {
152 Simantics.getSession().syncRequest(new WriteRequest() {
154 public void perform(WriteGraph graph) throws DatabaseException {
156 toGraph(graph, filename,graphFile);
157 } catch (IOException e) {
158 throw new DatabaseException(e);
165 * Updates contents of a graphFile, including the name.
169 * @throws DatabaseException
170 * @throws IOException
172 public static void toGraph(WriteGraph graph, String filename, Resource graphFile) throws DatabaseException, IOException {
173 File file = new File(filename);
175 throw new IOException("File " + filename + " not found.");
177 toGraph(graph, file, graphFile);
181 * Updates contents of a graphFile, including the name.
185 * @throws DatabaseException
186 * @throws IOException
188 public static void toGraph(WriteGraph graph, File file, Resource graphFile) throws DatabaseException, IOException {
190 writeDataToGraph(graph, file, graphFile);
191 String name = file.getName();
192 GraphFileResource gf = GraphFileResource.getInstance(graph);
193 graph.claimLiteral(graphFile, gf.HasResourceName, name);
198 * Writes contents of a file to a graphFile (data and time stamp).
202 * @throws IOException
203 * @throws ManyObjectsForFunctionalRelationException
204 * @throws ServiceException
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) {
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);
220 rab = graph.getRandomAccessBinary(fileData);
222 LiteralFileUtil.copyRandomAccessBinaryFromFile(file, rab);
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);
233 size -= chan.read(buf);
235 graph.claimLiteral(graphFile, gf.HasFiledata, array);
240 graph.claimLiteral(graphFile, gf.LastModified, file.lastModified());
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);
256 InputStream input = new ByteArrayInputStream(data);
257 LiteralFileUtil.copyStreamToRandomAccessBinary(graph, input, fileData);
260 graph.claimLiteral(graphFile, gf.HasFiledata, data);
262 graph.claimLiteral(graphFile, gf.LastModified, System.currentTimeMillis());
266 * Writes contents of a file to a graphFile (data and time stamp).
269 * @throws DatabaseException
271 public static void writeDataToGraph(final File file, final Resource graphFile) throws DatabaseException {
272 Simantics.getSession().syncRequest(new WriteRequest() {
275 public void perform(WriteGraph graph) throws DatabaseException {
277 writeDataToGraph(graph, file, graphFile);
278 } catch (IOException e) {
279 throw new DatabaseException(e);
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);
293 Map<Resource,File> matching = new HashMap<Resource, File>();
295 for (File f : subFiles) {
296 String name = f.getName();
297 if (f.isDirectory()) {
298 Resource matchingFolder = findWithName(g, subFolderResources, name);
300 if (matchingFolder != null) {
301 if (matching.containsKey(matchingFolder))
302 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
304 matching.put(matchingFolder, f);
305 syncFolderToGraph(g, f, matchingFolder);
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);
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);
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);
333 // delete resources, which have no matching file (or folder)
334 for (Resource subFolder : subFolderResources) {
335 if (!matching.containsKey(subFolder))
339 for (Resource subFolder : subFileResources) {
340 if (!matching.containsKey(subFolder))
345 public static void writeFolderToDisk(ReadGraph g, Resource folderRes, File folder) throws DatabaseException, IOException{
347 GraphFileResource gf = GraphFileResource.getInstance(g);
348 for (Resource subFolder : g.getObjects(folderRes, gf.HasFolder)) {
349 String name = g.getRelatedValue(subFolder, gf.HasResourceName);
351 if (name.length() == 0)
352 throw new DatabaseException("Empty folder name for " + subFolder);
354 File newFolder = new File(folder.getAbsolutePath() + "/" + name);
355 if (!newFolder.mkdir()) {
356 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);
358 writeFolderToDisk(g, subFolder, newFolder);
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);
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);
373 Map<Resource,File> matching = new HashMap<Resource, File>();
375 for (File f : subFiles) {
376 String name = f.getName();
377 if (f.isDirectory()) {
378 Resource matchingFolder = helper.findFolder(g, subFolderResources, name);
380 if (matchingFolder != null) {
381 if (matching.containsKey(matchingFolder))
382 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
384 matching.put(matchingFolder, f);
385 syncFolderToGraph(g, f, matchingFolder,helper);
387 matchingFolder = helper.createFolder(g, name);
388 g.claim(folderRes, gf.HasFolder, matchingFolder);
389 matching.put(matchingFolder, f);
390 syncFolderToGraph(g, f, matchingFolder,helper);
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);
400 fileRes = helper.createFile(g, name);
401 g.claim(folderRes, gf.HasFile, fileRes);
402 matching.put(fileRes, f);
403 toGraph(g, f, fileRes);
407 // delete resources, which have no matching file (or folder)
408 for (Resource subFolder : subFolderResources) {
409 if (!matching.containsKey(subFolder))
413 for (Resource subFolder : subFileResources) {
414 if (!matching.containsKey(subFolder))
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;
426 public static interface ToDiskHelper {
427 public String getName(ReadGraph g, Resource systemResource) throws DatabaseException;
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);
435 if (name.length() == 0)
436 throw new DatabaseException("Empty folder name for " + subFolder);
438 File newFolder = new File(folder.getAbsolutePath() + "/" + name);
439 if (!newFolder.mkdir()) {
440 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);
442 writeFolderToDisk(g, subFolder, newFolder, helper);
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);
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;
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);
462 Map<Resource,File> matching = new HashMap<Resource, File>();
464 for (File f : subFiles) {
465 String name = f.getName();
466 if (f.isDirectory()) {
467 Resource matchingFolder = helper.findFolder(g, subFolderResources, name);
469 if (matchingFolder != null) {
470 if (matching.containsKey(matchingFolder))
471 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
473 matching.put(matchingFolder, f);
474 syncFolderToDisk(g, matchingFolder, f, helper);
476 deleteDirectoryStructure(f);
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);
487 throw new Exception("Cannot delete file " + f.getAbsolutePath());
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);
496 if (name.length() == 0)
497 throw new DatabaseException("Empty folder name for " + subFolder);
499 File newFolder = new File(folder.getAbsolutePath() + "/" + name);
500 if (!newFolder.mkdir()) {
501 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);
503 writeFolderToDisk(g, subFolder, newFolder, helper);
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);
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)))
528 * Deletes the directory and all it contents.
532 public static void deleteDirectoryStructure(File dir) throws Exception{
533 deleteDirectoryStructure(dir, true);
537 * Deletes the directory's contents, but does not delete the directory.
541 public static void clearDirectoryStructure(File dir) throws Exception{
542 deleteDirectoryStructure(dir, false);
545 private static void deleteDirectoryStructure(File dir, boolean deleteDir) throws Exception{
546 File subFiles[] = dir.listFiles();
547 for (File f : subFiles) {
549 deleteDirectoryStructure(f,true);
552 throw new Exception("Cannot delete file " + f.getAbsolutePath());
557 throw new Exception("Cannot delete folder " + dir.getAbsolutePath());
562 public static Resource createFileReference(final Resource parent, final Path path) throws DatabaseException {
563 return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
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);