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.ByteArrayInputStream;
\r
15 import java.io.File;
\r
16 import java.io.FileInputStream;
\r
17 import java.io.FileOutputStream;
\r
18 import java.io.IOException;
\r
19 import java.io.InputStream;
\r
20 import java.nio.ByteBuffer;
\r
21 import java.nio.channels.FileChannel;
\r
22 import java.nio.file.Path;
\r
23 import java.util.Collection;
\r
24 import java.util.HashMap;
\r
25 import java.util.Map;
\r
27 import org.simantics.Simantics;
\r
28 import org.simantics.databoard.Bindings;
\r
29 import org.simantics.databoard.util.binary.RandomAccessBinary;
\r
30 import org.simantics.db.ReadGraph;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.db.WriteGraph;
\r
33 import org.simantics.db.common.request.ReadRequest;
\r
34 import org.simantics.db.common.request.WriteRequest;
\r
35 import org.simantics.db.common.request.WriteResultRequest;
\r
36 import org.simantics.db.common.utils.LiteralFileUtil;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.exception.DoesNotContainValueException;
\r
39 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
\r
40 import org.simantics.db.exception.NoSingleResultException;
\r
41 import org.simantics.db.exception.ServiceException;
\r
42 import org.simantics.db.request.Read;
\r
43 import org.simantics.db.service.ClusteringSupport;
\r
44 import org.simantics.graphfile.ontology.GraphFileResource;
\r
45 import org.simantics.layer0.Layer0;
\r
48 * @author Marko Luukkainen
\r
50 public class GraphFileUtil {
\r
53 public static boolean USE_RANDOM_ACCESS_BINARY = true;
\r
55 * Creates a temp file of a graphFile.
\r
58 * @throws DatabaseException
\r
60 public static File toTempFile(final Resource res)throws DatabaseException {
\r
61 return Simantics.getSession().syncRequest(new Read<File>() {
\r
63 public File perform(ReadGraph graph) throws DatabaseException {
\r
64 return toTempFile(graph, res);
\r
70 * Creates a temp file of a graphFile.
\r
74 * @throws DatabaseException
\r
76 public static File toTempFile(ReadGraph graph, Resource res)throws DatabaseException {
\r
78 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
79 String filename = graph.getRelatedValue(res, gf.HasResourceName);
\r
81 int index = filename.lastIndexOf(".");
\r
85 name = filename.substring(0,index);
\r
86 ext = filename.substring(index+1);
\r
90 if (name.length() < 3) {
\r
91 for (int i = name.length(); i < 3; i++)
\r
95 File file = File.createTempFile(name, "."+ ext);
\r
96 writeDataToFile(graph, res, file);
\r
98 } catch (Exception e) {
\r
99 throw new DatabaseException(e);
\r
103 public static void writeDataToFile(final Resource res, final File file) throws DatabaseException{
\r
104 Simantics.getSession().syncRequest(new ReadRequest() {
\r
107 public void run(ReadGraph graph) throws DatabaseException {
\r
109 writeDataToFile(graph, res, file);
\r
110 } catch (IOException e) {
\r
111 throw new DatabaseException(e);
\r
118 * Writes contents of a graphFile to file.
\r
122 * @throws DatabaseException
\r
123 * @throws IOException
\r
125 public static void writeDataToFile(ReadGraph graph, Resource res, File file) throws DatabaseException, IOException {
\r
127 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
128 if (USE_RANDOM_ACCESS_BINARY) {
\r
129 Resource filedata = graph.getSingleObject(res, gf.HasFiledata);
\r
130 LiteralFileUtil.copyRandomAccessBinaryToFile(graph, filedata, file);
\r
133 byte[] data = graph.getRelatedValue(res, gf.HasFiledata);
\r
134 FileOutputStream fos = new FileOutputStream(file);
\r
140 Long lastModified = graph.getPossibleRelatedValue(res, gf.LastModified);
\r
141 if (lastModified != null)
\r
142 file.setLastModified(lastModified);
\r
146 * Updates contents of a graphFile, including the name.
\r
149 * @throws DatabaseException
\r
151 public static void toGraph(final String filename, final Resource graphFile)throws DatabaseException {
\r
152 Simantics.getSession().syncRequest(new WriteRequest() {
\r
154 public void perform(WriteGraph graph) throws DatabaseException {
\r
156 toGraph(graph, filename,graphFile);
\r
157 } catch (IOException e) {
\r
158 throw new DatabaseException(e);
\r
165 * Updates contents of a graphFile, including the name.
\r
169 * @throws DatabaseException
\r
170 * @throws IOException
\r
172 public static void toGraph(WriteGraph graph, String filename, Resource graphFile) throws DatabaseException, IOException {
\r
173 File file = new File(filename);
\r
174 if (!file.exists())
\r
175 throw new IOException("File " + filename + " not found.");
\r
177 toGraph(graph, file, graphFile);
\r
181 * Updates contents of a graphFile, including the name.
\r
185 * @throws DatabaseException
\r
186 * @throws IOException
\r
188 public static void toGraph(WriteGraph graph, File file, Resource graphFile) throws DatabaseException, IOException {
\r
190 writeDataToGraph(graph, file, graphFile);
\r
191 String name = file.getName();
\r
192 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
193 graph.claimLiteral(graphFile, gf.HasResourceName, name);
\r
198 * Writes contents of a file to a graphFile (data and time stamp).
\r
202 * @throws IOException
\r
203 * @throws ManyObjectsForFunctionalRelationException
\r
204 * @throws ServiceException
\r
206 public static void writeDataToGraph(WriteGraph graph, File file, Resource graphFile) throws IOException, DatabaseException{
\r
207 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
208 if (USE_RANDOM_ACCESS_BINARY) {
\r
210 Resource fileData = graph.getPossibleObject(graphFile, gf.HasFiledata);
\r
211 RandomAccessBinary rab = null;
\r
212 if (fileData == null) {
\r
213 Layer0 l0 = Layer0.getInstance(graph);
\r
214 ClusteringSupport cs = graph.getService(ClusteringSupport.class);
\r
215 fileData = graph.newResource(cs.createCluster());
\r
216 graph.claim(fileData, l0.InstanceOf, l0.ByteArray);
\r
217 graph.claim(graphFile, gf.HasFiledata, fileData);
\r
218 rab = graph.createRandomAccessBinary(fileData, Bindings.BYTE_ARRAY.type(), null);
\r
220 rab = graph.getRandomAccessBinary(fileData);
\r
222 LiteralFileUtil.copyRandomAccessBinaryFromFile(file, rab);
\r
224 FileInputStream stream = new FileInputStream(file);
\r
225 FileChannel chan = stream.getChannel();
\r
226 long lsize = chan.size();
\r
227 if (lsize > Integer.MAX_VALUE)
\r
228 throw new IOException("File is too big");
\r
229 int size = (int)lsize;
\r
230 final byte[] array = new byte[size];
\r
231 ByteBuffer buf = ByteBuffer.wrap(array);
\r
233 size -= chan.read(buf);
\r
235 graph.claimLiteral(graphFile, gf.HasFiledata, array);
\r
240 graph.claimLiteral(graphFile, gf.LastModified, file.lastModified());
\r
244 public static void writeDataToGraph(WriteGraph graph, byte data[], Resource graphFile) throws IOException, DatabaseException {
\r
245 GraphFileResource gf = GraphFileResource.getInstance(graph);
\r
246 if (USE_RANDOM_ACCESS_BINARY) {
\r
247 Resource fileData = graph.getPossibleObject(graphFile, gf.HasFiledata);
\r
248 if (fileData == null) {
\r
249 Layer0 l0 = Layer0.getInstance(graph);
\r
250 ClusteringSupport cs = graph.getService(ClusteringSupport.class);
\r
251 fileData = graph.newResource(cs.createCluster());
\r
252 graph.claim(fileData, l0.InstanceOf, l0.ByteArray);
\r
253 graph.claim(graphFile, gf.HasFiledata, fileData);
\r
254 graph.createRandomAccessBinary(fileData, Bindings.BYTE_ARRAY.type(), data);
\r
256 InputStream input = new ByteArrayInputStream(data);
\r
257 LiteralFileUtil.copyStreamToRandomAccessBinary(graph, input, fileData);
\r
260 graph.claimLiteral(graphFile, gf.HasFiledata, data);
\r
262 graph.claimLiteral(graphFile, gf.LastModified, System.currentTimeMillis());
\r
266 * Writes contents of a file to a graphFile (data and time stamp).
\r
269 * @throws DatabaseException
\r
271 public static void writeDataToGraph(final File file, final Resource graphFile) throws DatabaseException {
\r
272 Simantics.getSession().syncRequest(new WriteRequest() {
\r
275 public void perform(WriteGraph graph) throws DatabaseException {
\r
277 writeDataToGraph(graph, file, graphFile);
\r
278 } catch (IOException e) {
\r
279 throw new DatabaseException(e);
\r
286 public static void syncFolderToGraph(WriteGraph g, File folder, Resource folderRes) throws Exception {
\r
287 File subFiles[] = folder.listFiles();
\r
288 Layer0 l0 = Layer0.getInstance(g);
\r
289 GraphFileResource gf = GraphFileResource.getInstance(g);
\r
290 Collection<Resource> subFileResources = g.getObjects(folderRes, gf.HasFile);
\r
291 Collection<Resource> subFolderResources = g.getObjects(folderRes, gf.HasFolder);
\r
293 Map<Resource,File> matching = new HashMap<Resource, File>();
\r
295 for (File f : subFiles) {
\r
296 String name = f.getName();
\r
297 if (f.isDirectory()) {
\r
298 Resource matchingFolder = findWithName(g, subFolderResources, name);
\r
300 if (matchingFolder != null) {
\r
301 if (matching.containsKey(matchingFolder))
\r
302 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
\r
304 matching.put(matchingFolder, f);
\r
305 syncFolderToGraph(g, f, matchingFolder);
\r
307 matchingFolder = g.newResource();
\r
308 g.claim(matchingFolder, l0.InstanceOf, gf.Folder);
\r
309 g.claimLiteral(matchingFolder, gf.HasResourceName, name);
\r
310 g.claimLiteral(matchingFolder, l0.HasName, name);
\r
311 g.claim(folderRes, gf.HasFolder, matchingFolder);
\r
312 matching.put(matchingFolder, f);
\r
313 syncFolderToGraph(g, f, matchingFolder);
\r
316 Resource fileRes = findWithName(g, subFileResources, name);
\r
317 if (fileRes != null) {
\r
318 if (matching.containsKey(fileRes))
\r
319 throw new Exception("Matching file already in use" + f.getAbsolutePath() + " " + fileRes);
\r
320 matching.put(fileRes, f);
\r
321 toGraph(g, f, fileRes);
\r
323 fileRes = g.newResource();
\r
324 g.claim(fileRes, l0.InstanceOf, gf.File);
\r
325 g.claimLiteral(fileRes, gf.HasResourceName, name);
\r
326 g.claimLiteral(fileRes, l0.HasName, name);
\r
327 g.claim(folderRes, gf.HasFile, fileRes);
\r
328 matching.put(fileRes, f);
\r
329 toGraph(g, f, fileRes);
\r
333 // delete resources, which have no matching file (or folder)
\r
334 for (Resource subFolder : subFolderResources) {
\r
335 if (!matching.containsKey(subFolder))
\r
339 for (Resource subFolder : subFileResources) {
\r
340 if (!matching.containsKey(subFolder))
\r
345 public static void writeFolderToDisk(ReadGraph g, Resource folderRes, File folder) throws DatabaseException, IOException{
\r
347 GraphFileResource gf = GraphFileResource.getInstance(g);
\r
348 for (Resource subFolder : g.getObjects(folderRes, gf.HasFolder)) {
\r
349 String name = g.getRelatedValue(subFolder, gf.HasResourceName);
\r
351 if (name.length() == 0)
\r
352 throw new DatabaseException("Empty folder name for " + subFolder);
\r
354 File newFolder = new File(folder.getAbsolutePath() + "/" + name);
\r
355 if (!newFolder.mkdir()) {
\r
356 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);
\r
358 writeFolderToDisk(g, subFolder, newFolder);
\r
360 for (Resource fileRes : g.getObjects(folderRes, gf.HasFile)) {
\r
361 String name = g.getRelatedValue(fileRes, gf.HasResourceName);
\r
362 File file = new File(folder.getAbsolutePath() + "/" + name);
\r
363 writeDataToFile(g, fileRes, file);
\r
367 public static void syncFolderToGraph(WriteGraph g, File folder, Resource folderRes, ToGraphHelper helper) throws Exception {
\r
368 File subFiles[] = folder.listFiles();
\r
369 GraphFileResource gf = GraphFileResource.getInstance(g);
\r
370 Collection<Resource> subFileResources = g.getObjects(folderRes, gf.HasFile);
\r
371 Collection<Resource> subFolderResources = g.getObjects(folderRes, gf.HasFolder);
\r
373 Map<Resource,File> matching = new HashMap<Resource, File>();
\r
375 for (File f : subFiles) {
\r
376 String name = f.getName();
\r
377 if (f.isDirectory()) {
\r
378 Resource matchingFolder = helper.findFolder(g, subFolderResources, name);
\r
380 if (matchingFolder != null) {
\r
381 if (matching.containsKey(matchingFolder))
\r
382 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
\r
384 matching.put(matchingFolder, f);
\r
385 syncFolderToGraph(g, f, matchingFolder,helper);
\r
387 matchingFolder = helper.createFolder(g, name);
\r
388 g.claim(folderRes, gf.HasFolder, matchingFolder);
\r
389 matching.put(matchingFolder, f);
\r
390 syncFolderToGraph(g, f, matchingFolder,helper);
\r
393 Resource fileRes = helper.findFile(g, subFileResources, name);
\r
394 if (fileRes != null) {
\r
395 if (matching.containsKey(fileRes))
\r
396 throw new Exception("Matching file already in use" + f.getAbsolutePath() + " " + fileRes);
\r
397 matching.put(fileRes, f);
\r
398 toGraph(g, f, fileRes);
\r
400 fileRes = helper.createFile(g, name);
\r
401 g.claim(folderRes, gf.HasFile, fileRes);
\r
402 matching.put(fileRes, f);
\r
403 toGraph(g, f, fileRes);
\r
407 // delete resources, which have no matching file (or folder)
\r
408 for (Resource subFolder : subFolderResources) {
\r
409 if (!matching.containsKey(subFolder))
\r
413 for (Resource subFolder : subFileResources) {
\r
414 if (!matching.containsKey(subFolder))
\r
419 public static interface ToGraphHelper {
\r
420 public Resource findFolder(ReadGraph g, Collection<Resource> subFolderResources, String name) throws DatabaseException;
\r
421 public Resource createFolder(WriteGraph g, String name) throws DatabaseException;
\r
422 public Resource findFile(ReadGraph g, Collection<Resource> subFileResources, String name) throws DatabaseException;
\r
423 public Resource createFile(WriteGraph g, String name) throws DatabaseException;
\r
426 public static interface ToDiskHelper {
\r
427 public String getName(ReadGraph g, Resource systemResource) throws DatabaseException;
\r
430 public static void writeFolderToDisk(ReadGraph g, Resource folderRes, File folder, ToDiskHelper helper) throws DatabaseException, IOException{
\r
431 GraphFileResource gf = GraphFileResource.getInstance(g);
\r
432 for (Resource subFolder : g.getObjects(folderRes, gf.HasFolder)) {
\r
433 String name = helper.getName(g, subFolder);
\r
435 if (name.length() == 0)
\r
436 throw new DatabaseException("Empty folder name for " + subFolder);
\r
438 File newFolder = new File(folder.getAbsolutePath() + "/" + name);
\r
439 if (!newFolder.mkdir()) {
\r
440 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);
\r
442 writeFolderToDisk(g, subFolder, newFolder, helper);
\r
444 for (Resource fileRes : g.getObjects(folderRes, gf.HasFile)) {
\r
445 String name = helper.getName(g, fileRes);
\r
446 File file = new File(folder.getAbsolutePath() + "/" + name);
\r
447 writeDataToFile(g, fileRes, file);
\r
451 public static interface ToDiskHelper2 extends ToDiskHelper {
\r
452 public Resource findFolder(ReadGraph g, Collection<Resource> subFolderResources, String name) throws DatabaseException;
\r
453 public Resource findFile(ReadGraph g, Collection<Resource> subFileResources, String name) throws DatabaseException;
\r
456 public static void syncFolderToDisk(ReadGraph g, Resource folderRes, File folder, ToDiskHelper2 helper) throws Exception {
\r
457 File subFiles[] = folder.listFiles();
\r
458 GraphFileResource gf = GraphFileResource.getInstance(g);
\r
459 Collection<Resource> subFileResources = g.getObjects(folderRes, gf.HasFile);
\r
460 Collection<Resource> subFolderResources = g.getObjects(folderRes, gf.HasFolder);
\r
462 Map<Resource,File> matching = new HashMap<Resource, File>();
\r
464 for (File f : subFiles) {
\r
465 String name = f.getName();
\r
466 if (f.isDirectory()) {
\r
467 Resource matchingFolder = helper.findFolder(g, subFolderResources, name);
\r
469 if (matchingFolder != null) {
\r
470 if (matching.containsKey(matchingFolder))
\r
471 throw new Exception("Matching folder already in use" + f.getAbsolutePath() + " " + matchingFolder);
\r
473 matching.put(matchingFolder, f);
\r
474 syncFolderToDisk(g, matchingFolder, f, helper);
\r
476 deleteDirectoryStructure(f);
\r
479 Resource fileRes = helper.findFile(g, subFileResources, name);
\r
480 if (fileRes != null) {
\r
481 if (matching.containsKey(fileRes))
\r
482 throw new Exception("Matching file already in use" + f.getAbsolutePath() + " " + fileRes);
\r
483 matching.put(fileRes, f);
\r
484 writeDataToFile(g, fileRes, f);
\r
487 throw new Exception("Cannot delete file " + f.getAbsolutePath());
\r
491 // create files and folders, which have no matching graphFile (or folder)
\r
492 for (Resource subFolder : subFolderResources) {
\r
493 if (!matching.containsKey(subFolder)) {
\r
494 String name = helper.getName(g, subFolder);
\r
496 if (name.length() == 0)
\r
497 throw new DatabaseException("Empty folder name for " + subFolder);
\r
499 File newFolder = new File(folder.getAbsolutePath() + "/" + name);
\r
500 if (!newFolder.mkdir()) {
\r
501 throw new DatabaseException("Could not create folder " + name + " for resource " + subFolder);
\r
503 writeFolderToDisk(g, subFolder, newFolder, helper);
\r
507 for (Resource fileRes : subFileResources) {
\r
508 if (!matching.containsKey(fileRes)) {
\r
509 String name = helper.getName(g, fileRes);
\r
510 File file = new File(folder.getAbsolutePath() + "/" + name);
\r
511 writeDataToFile(g, fileRes, file);
\r
519 public static Resource findWithName(ReadGraph g, Collection<Resource> resources, String name) throws ServiceException, NoSingleResultException, DoesNotContainValueException {
\r
520 GraphFileResource gf = GraphFileResource.getInstance(g);
\r
521 for (Resource r : resources)
\r
522 if (name.equals(g.getRelatedValue(r, gf.HasResourceName)))
\r
528 * Deletes the directory and all it contents.
\r
530 * @throws Exception
\r
532 public static void deleteDirectoryStructure(File dir) throws Exception{
\r
533 deleteDirectoryStructure(dir, true);
\r
537 * Deletes the directory's contents, but does not delete the directory.
\r
539 * @throws Exception
\r
541 public static void clearDirectoryStructure(File dir) throws Exception{
\r
542 deleteDirectoryStructure(dir, false);
\r
545 private static void deleteDirectoryStructure(File dir, boolean deleteDir) throws Exception{
\r
546 File subFiles[] = dir.listFiles();
\r
547 for (File f : subFiles) {
\r
548 if (f.isDirectory())
\r
549 deleteDirectoryStructure(f,true);
\r
552 throw new Exception("Cannot delete file " + f.getAbsolutePath());
\r
556 if (!dir.delete()) {
\r
557 throw new Exception("Cannot delete folder " + dir.getAbsolutePath());
\r
562 public static Resource createFileReference(final Resource parent, final Path path) throws DatabaseException {
\r
563 return Simantics.getSession().syncRequest(new WriteResultRequest<Resource>() {
\r
566 public Resource perform(WriteGraph graph) throws DatabaseException {
\r
567 Layer0 L0 = Layer0.getInstance(graph);
\r
568 GraphFileResource GF = GraphFileResource.getInstance(graph);
\r
569 Resource file = graph.newResource();
\r
570 graph.claim(file, L0.PartOf, parent);
\r
571 graph.claim(file, L0.InstanceOf, GF.File);
\r
572 String name = path.getFileName().toString();
\r
573 graph.claimLiteral(file, L0.HasName, name, Bindings.STRING);
\r
574 graph.claimLiteral(file, GF.SystemPath, path.toAbsolutePath().toString(), Bindings.STRING);
\r