]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Refactoring of SCLUtil to allow customization 27/3427/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 30 Oct 2019 13:53:09 +0000 (15:53 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Wed, 30 Oct 2019 13:53:09 +0000 (15:53 +0200)
Change-Id: Ic491c6424b96c53da35ac61fb1ace9239047bb3b

org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java

index 1070803046ec1e38eed5279682c03bc2320dc805..cdbf6626b197548c624f65e57b6f2d7b58cbdff1 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.plant3d.scl;
 
-
 import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
@@ -18,21 +17,47 @@ import org.simantics.plant3d.utils.ComponentUtils;
 import org.simantics.plant3d.utils.P3DUtil;
 
 public class SCLUtil {
+
+       public static interface SchemaProvider {
+               IMappingSchema<Resource,Object> get(ReadGraph graph) throws DatabaseException;
+       }
+
+       public static interface NodeMapProvider {
+               P3DScriptNodeMap get(IMapping<Resource, Object> mapping, P3DRootNode rootNode);
+       }
        
        public static P3DScriptNodeMap load(final Resource root) throws DatabaseException {
+               preload(Plant3D.URIs.Builtin);
+
+               SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
+               NodeMapProvider mapProvider = (mapping, rootNode) -> new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode);
+               return load(root, schemaProvider, mapProvider);
+       }
+
+       public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException {
+               preload(Plant3D.URIs.Builtin);
+               
+               SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
+               return loadReadOnly(root, schemaProvider);
+       }
+
+       public static void preload(String symbolSet) throws DatabaseException {
                try {
-                       ControlPointFactory.preloadCache(Plant3D.URIs.Builtin);
+                       ControlPointFactory.preloadCache(symbolSet);
                        ComponentUtils.preloadCache();
                } catch (Exception e) {
                        throw new DatabaseException(e);
                }
-               return Simantics.getSession().syncRequest(new Read<P3DScriptNodeMap>() {
+       }
+
+       public static <NodeMap extends P3DScriptNodeMap> NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
+               return Simantics.getSession().syncRequest(new Read<NodeMap>() {
                        @Override
-                       public P3DScriptNodeMap perform(ReadGraph graph) throws DatabaseException {
+                       public NodeMap perform(ReadGraph graph) throws DatabaseException {
                                PipingRules.setEnabled(false);
-                               IMappingSchema<Resource, Object> schema = SchemaBuilder.getSchema(graph);
+                               IMappingSchema<Resource, Object> schema = schemaProvider.get(graph);
                                IMapping<Resource, Object> mapping = Mappings.createWithListening(schema);
-                               P3DRootNode rootNode = (P3DRootNode)mapping.map(graph, root);
+                               P3DRootNode rootNode = (P3DRootNode) mapping.map(graph, root);
                                try {
                                        P3DUtil.finalizeDBLoad(rootNode);
                                        // FIXME: Something needs to be done here...
@@ -40,28 +65,24 @@ public class SCLUtil {
                                } catch (Exception e) {
                                        throw new DatabaseException(e);
                                }
-                               P3DScriptNodeMap nodeMap = new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode);
-                               return nodeMap;
                                
+                               @SuppressWarnings("unchecked")
+                               NodeMap nodeMap = (NodeMap) mapProvider.get(mapping, rootNode);
+                               return nodeMap;
                        }
                });
-               
        }
-       
-       public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException {
-               try {
-                       ControlPointFactory.preloadCache(Plant3D.URIs.Builtin);
-                       ComponentUtils.preloadCache();
-               } catch (Exception e) {
-                       throw new DatabaseException(e);
-               }
-               return Simantics.getSession().syncRequest(new Read<P3DRootNode>() {
+
+       public static <Root extends P3DRootNode> Root loadReadOnly(final Resource root, SchemaProvider schemaProvider)
+                       throws DatabaseException {
+               return Simantics.getSession().syncRequest(new Read<Root>() {
                        @Override
-                       public P3DRootNode perform(ReadGraph graph) throws DatabaseException {
+                       public Root perform(ReadGraph graph) throws DatabaseException {
                                PipingRules.setEnabled(false);
-                               IMappingSchema<Resource, Object> schema = SchemaBuilder.getSchema(graph);
+                               IMappingSchema<Resource, Object> schema = schemaProvider.get(graph);
                                IMapping<Resource, Object> mapping = Mappings.createWithoutListening(schema);
-                               P3DRootNode rootNode = (P3DRootNode)mapping.map(graph, root);
+                               @SuppressWarnings("unchecked")
+                               Root rootNode = (Root) mapping.map(graph, root);
                                try {
                                        P3DUtil.finalizeDBLoad(rootNode);
                                        P3DUtil.finalizeDBLoad2(rootNode);
@@ -69,7 +90,6 @@ public class SCLUtil {
                                        throw new DatabaseException(e);
                                }
                                return rootNode;
-                               
                        }
                });
        }