X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graphfile%2Fsrc%2Forg%2Fsimantics%2Fgraphfile%2Futil%2FGraphFileUtil.java;h=637a5d0ae3a1976f05a002d9a9096b530018fb55;hb=HEAD;hp=76003e023d778a1a73826152e715190400ac31ea;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graphfile/src/org/simantics/graphfile/util/GraphFileUtil.java b/bundles/org.simantics.graphfile/src/org/simantics/graphfile/util/GraphFileUtil.java index 76003e023..637a5d0ae 100644 --- a/bundles/org.simantics.graphfile/src/org/simantics/graphfile/util/GraphFileUtil.java +++ b/bundles/org.simantics.graphfile/src/org/simantics/graphfile/util/GraphFileUtil.java @@ -12,6 +12,7 @@ package org.simantics.graphfile.util; import java.io.ByteArrayInputStream; +import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -34,6 +35,7 @@ import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.request.WriteResultRequest; import org.simantics.db.common.utils.LiteralFileUtil; +import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.DoesNotContainValueException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; @@ -41,6 +43,7 @@ import org.simantics.db.exception.NoSingleResultException; import org.simantics.db.exception.ServiceException; import org.simantics.db.request.Read; import org.simantics.db.service.ClusteringSupport; +import org.simantics.db.service.TransferableGraphSupport; import org.simantics.graphfile.ontology.GraphFileResource; import org.simantics.layer0.Layer0; @@ -76,7 +79,9 @@ public class GraphFileUtil { public static File toTempFile(ReadGraph graph, Resource res)throws DatabaseException { GraphFileResource gf = GraphFileResource.getInstance(graph); - String filename = graph.getRelatedValue(res, gf.HasResourceName); + String filename = graph.getPossibleRelatedValue(res, gf.HasResourceName); + if (filename == null) + filename = graph.getRelatedValue(res, Layer0.getInstance(graph).HasName); int index = filename.lastIndexOf("."); String name = ""; @@ -576,4 +581,52 @@ public class GraphFileUtil { } }); } + + /** + * SCL java binding cannot make difference between methods that have two versions, one with Read/WriteGraph, and one without. + * Hence we have to introduce a set of methods which have no alternate versions. + */ + + + public static void writeDataToGraphSCLhack(WriteGraph graph, File file, Resource graphFile) throws IOException, DatabaseException{ + writeDataToGraph(graph, file, graphFile); + + } + + public static File toTempFileSCLhack(ReadGraph graph, Resource res)throws DatabaseException { + return toTempFile(graph, res); + } + + public static void writeDataToFileSCLhack(ReadGraph graph, Resource res, File file) throws DatabaseException, IOException { + writeDataToFile(graph, res, file); + } + + public static void toGraphSCLhack(WriteGraph graph, String filename, Resource graphFile) throws DatabaseException, IOException { + toGraph(graph, filename, graphFile); + } + + + public static byte[] getData(ReadGraph graph, Resource graphFile) throws DatabaseException{ + GraphFileResource GF = GraphFileResource.getInstance(graph); + Resource fileData = graph.getSingleObject(graphFile, GF.HasFiledata); + TransferableGraphSupport tgs = graph.getService(TransferableGraphSupport.class); + InputStream input = tgs.getValueStream(graph, fileData); + DataInputStream di = new DataInputStream(input); + try { + int length = di.readInt(); + byte[] content = new byte[length]; + di.read(content); + return content; + } catch (IOException e) { + Logger.defaultLogError(e); + } + return null; + } + + public static String getDataAsString(ReadGraph graph, Resource graphFile) throws DatabaseException{ + byte[] content = getData(graph, graphFile); + String s = new String(content); + return s; + } + }