From: Antti Villberg Date: Fri, 2 Aug 2019 06:14:35 +0000 (+0300) Subject: API for creating a new File Document X-Git-Tag: v1.43.0~136^2~127 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=e95e32c01362a9be338cb1c949dde9ff9909c6fa;p=simantics%2Fplatform.git API for creating a new File Document gitlab #326 Change-Id: Ic22fcdc5600883eeb1469112f267a8f2c4df4e90 --- diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewFileDocument.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewFileDocument.java index 0ab824e91..783725414 100644 --- a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewFileDocument.java +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewFileDocument.java @@ -17,12 +17,12 @@ import org.simantics.layer0.Layer0; public class NewFileDocument implements ActionFactory { Resource relation; String defaultName; - + public NewFileDocument(ReadGraph graph, String relationUri, String defaultName) throws DatabaseException { relation = graph.getResource(relationUri); this.defaultName = defaultName; } - + @Override public Runnable create(Object target) { @@ -35,29 +35,38 @@ public class NewFileDocument implements ActionFactory { @Override public void run() { Simantics.getSession().asyncRequest(new WriteRequest() { - + @Override public void perform(WriteGraph graph) throws DatabaseException { - graph.markUndoPoint(); - - Layer0 l0 = Layer0.getInstance(graph); - - String name = NameUtils.findFreshName(graph, defaultName, resource, relation); - DocumentResource doc = DocumentResource.getInstance(graph); - - Resource fileResource = graph.newResource(); - graph.claim(fileResource, l0.InstanceOf, doc.FileDocument); - graph.claimLiteral(fileResource, l0.HasName, name); - graph.claim(resource, relation, fileResource); - try { - GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource); - } catch (IOException e) { - throw new DatabaseException(e); - } + create(graph, resource, relation, defaultName); } - + }); } }; } + + public static Resource create(WriteGraph graph, Resource resource, Resource relation, String defaultName) throws DatabaseException { + + graph.markUndoPoint(); + + Layer0 l0 = Layer0.getInstance(graph); + + String name = NameUtils.findFreshName(graph, defaultName, resource, relation); + DocumentResource doc = DocumentResource.getInstance(graph); + + Resource fileResource = graph.newResource(); + graph.claim(fileResource, l0.InstanceOf, doc.FileDocument); + graph.claimLiteral(fileResource, l0.HasName, name); + graph.claim(resource, relation, fileResource); + try { + GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource); + } catch (IOException e) { + throw new DatabaseException(e); + } + + return fileResource; + + } + }