]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
API for creating a new File Document 52/3052/2
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 2 Aug 2019 06:14:35 +0000 (09:14 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 2 Aug 2019 12:12:27 +0000 (12:12 +0000)
gitlab #326

Change-Id: Ic22fcdc5600883eeb1469112f267a8f2c4df4e90

bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewFileDocument.java

index 0ab824e918bf0aa3f7f7cd95540cc1ca7438bbe3..783725414778ce7cd8588a2fb72f535c458f119d 100644 (file)
@@ -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;
+
+       }
+
 }