]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java
4c4b6d5760a6f40b68533dba92a67b741585fc27
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scl / SCLUtil.java
1 package org.simantics.plant3d.scl;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.request.Read;
8 import org.simantics.objmap.graph.IMapping;
9 import org.simantics.objmap.graph.Mappings;
10 import org.simantics.objmap.graph.schema.IMappingSchema;
11 import org.simantics.plant3d.ontology.Plant3D;
12 import org.simantics.plant3d.scenegraph.P3DRootNode;
13 import org.simantics.plant3d.scenegraph.SchemaBuilder;
14 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
15 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
16 import org.simantics.plant3d.utils.ComponentUtils;
17 import org.simantics.plant3d.utils.P3DUtil;
18
19 public class SCLUtil {
20
21         public static interface SchemaProvider {
22                 IMappingSchema<Resource,Object> get(ReadGraph graph) throws DatabaseException;
23         }
24
25         public static interface NodeMapProvider {
26                 P3DScriptNodeMap get(IMapping<Resource, Object> mapping, P3DRootNode rootNode);
27         }
28         
29         public static P3DScriptNodeMap load(final Resource root) throws DatabaseException {
30                 preload(Plant3D.URIs.Builtin);
31
32                 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
33                 NodeMapProvider mapProvider = (mapping, rootNode) -> new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode);
34                 return load(root, schemaProvider, mapProvider);
35         }
36
37         public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException {
38                 preload(Plant3D.URIs.Builtin);
39                 
40                 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
41                 return loadReadOnly(root, schemaProvider);
42         }
43
44         public static void preload(String symbolSet) throws DatabaseException {
45                 try {
46                         ControlPointFactory.preloadCache(symbolSet);
47                         ComponentUtils.preloadCache();
48                 } catch (Exception e) {
49                         throw new DatabaseException(e);
50                 }
51         }
52
53         public static <NodeMap extends P3DScriptNodeMap> NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
54                 return Simantics.getSession().syncRequest(new Read<NodeMap>() {
55                         @Override
56                         public NodeMap perform(ReadGraph graph) throws DatabaseException {
57                                 PipingRules.setEnabled(false);
58                                 IMappingSchema<Resource, Object> schema = schemaProvider.get(graph);
59                                 IMapping<Resource, Object> mapping = Mappings.createWithListening(schema);
60                                 P3DRootNode rootNode = (P3DRootNode) mapping.map(graph, root);
61                                 rootNode.setMapping((IMapping)mapping);
62                                 try {
63                                         P3DUtil.finalizeDBLoad(rootNode);
64                                         // FIXME: Something needs to be done here...
65                                         P3DUtil.finalizeDBLoad2(rootNode);
66                                 } catch (Exception e) {
67                                         throw new DatabaseException(e);
68                                 }
69                                 
70                                 @SuppressWarnings("unchecked")
71                                 NodeMap nodeMap = (NodeMap) mapProvider.get(mapping, rootNode);
72                                 return nodeMap;
73                         }
74                 });
75         }
76
77         public static <Root extends P3DRootNode> Root loadReadOnly(final Resource root, SchemaProvider schemaProvider)
78                         throws DatabaseException {
79                 return Simantics.getSession().syncRequest(new Read<Root>() {
80                         @Override
81                         public Root perform(ReadGraph graph) throws DatabaseException {
82                                 PipingRules.setEnabled(false);
83                                 IMappingSchema<Resource, Object> schema = schemaProvider.get(graph);
84                                 IMapping<Resource, Object> mapping = Mappings.createWithoutListening(schema);
85                                 @SuppressWarnings("unchecked")
86                                 Root rootNode = (Root) mapping.map(graph, root);
87                                 rootNode.setMapping((IMapping)mapping);
88                                 try {
89                                         P3DUtil.finalizeDBLoad(rootNode);
90                                         P3DUtil.finalizeDBLoad2(rootNode);
91                                 } catch (Exception e) {
92                                         throw new DatabaseException(e);
93                                 }
94                                 
95                                 return rootNode;
96                         }
97                 });
98         }
99
100 }