From: Marko Luukkainen Date: Thu, 28 Jun 2018 14:40:26 +0000 (+0300) Subject: SCL API for GraphFile plug-in. X-Git-Tag: v1.43.0~136^2~450 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=24d45eec3a3bad14d1a275387c20a94cea594562;p=simantics%2Fplatform.git SCL API for GraphFile plug-in. gitlab #34 Change-Id: I81b9d41cfa392545df629bf9e7a6197c21effa58 --- diff --git a/bundles/org.simantics.graphfile/build.properties b/bundles/org.simantics.graphfile/build.properties index ff239f985..fe68d79ae 100644 --- a/bundles/org.simantics.graphfile/build.properties +++ b/bundles/org.simantics.graphfile/build.properties @@ -14,4 +14,5 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ plugin.xml,\ - adapters.xml + adapters.xml,\ + scl/ diff --git a/bundles/org.simantics.graphfile/scl/Simantics/GraphFile.scl b/bundles/org.simantics.graphfile/scl/Simantics/GraphFile.scl new file mode 100644 index 000000000..dd28870d3 --- /dev/null +++ b/bundles/org.simantics.graphfile/scl/Simantics/GraphFile.scl @@ -0,0 +1,41 @@ +import "Simantics/DB" +import "File" + + +importJava "org.simantics.graphfile.util.GraphFileUtil" where + "Writes contents of a file to DB, including the timestamp" + @JavaName writeDataToGraphSCLhack + writeDataToGraph:: File -> Resource -> () + + "Writes contents of a byte array to DB" + @JavaName writeDataToGraph + writeDataToGraphBytes:: ByteArray -> Resource -> () + + "Creates a temp file of a GraphFile resource" + @JavaName toTempFileSCLhack + toTempFile :: Resource -> File + + "Writes contents of a GraphFile into a regular File" + @JavaName writeDataToFileSCLhack + writeDataToFile :: Resource -> File -> () + + "Writes contents of a file to DB, including the filename and timestamp" + @JavaName toGraphSCLhack + toGraphWithPath :: String -> Resource -> () + + "Writes contents of a file to DB, including the filename and timestamp" + @JavaName toGraph + toGraph :: File -> Resource -> () + + "Returns contents of a GraphFile as a byte array" + @JavaName getData + getData :: Resource -> ByteArray + + "Returns contents of a GraphFile as a String" + @JavaName getDataAsString + getDataAsString :: Resource -> String + + + + + \ No newline at end of file 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 fbfcc3a14..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; @@ -578,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; + } + }