From e95e32c01362a9be338cb1c949dde9ff9909c6fa Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Fri, 2 Aug 2019 09:14:35 +0300 Subject: [PATCH] API for creating a new File Document gitlab #326 Change-Id: Ic22fcdc5600883eeb1469112f267a8f2c4df4e90 --- .../document/ui/actions/NewFileDocument.java | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) 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; + + } + } -- 2.47.1