]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGConfigurer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGConfigurer.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGConfigurer.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGConfigurer.java
new file mode 100644 (file)
index 0000000..34af0d1
--- /dev/null
@@ -0,0 +1,118 @@
+package org.simantics.db.layer0.util;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.NamedResource;\r
+import org.simantics.db.common.request.PossibleIndexRoot;\r
+import org.simantics.db.common.utils.CommonDBUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;\r
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2.RootSpec;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class TGConfigurer {\r
+\r
+       final private ReadGraph graph;\r
+       final Collection<RootSpec> roots = new ArrayList<RootSpec>();\r
+       final Map<Resource, ExtentStatus> preStatus = new HashMap<Resource, ExtentStatus>();\r
+       final boolean ignoreVirtualResources;\r
+       final boolean validate;\r
+       \r
+       public TGConfigurer(ReadGraph graph, boolean ignoreVirtualResources, boolean validate) {\r
+               this.graph = graph;\r
+               this.ignoreVirtualResources = ignoreVirtualResources;\r
+               this.validate = validate;\r
+       }\r
+       \r
+       public TGConfigurer(ReadGraph graph, boolean ignoreVirtualResources) {\r
+               this(graph, ignoreVirtualResources, true);\r
+       }\r
+\r
+       public TGConfigurer exclusions(Collection<Resource> exclusions) {\r
+               for(Resource r : exclusions) {\r
+                       preStatus.put(r, ExtentStatus.EXCLUDED);\r
+               }\r
+               return this;\r
+       }\r
+\r
+       public TGConfigurer roots(Collection<RootSpec> rs) throws DatabaseException {\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               for(RootSpec root : rs) {\r
+                       Resource nameResource = graph.getPossibleObject(root.resource, L0.HasName);\r
+                       if(nameResource != null) preStatus.put(nameResource, ExtentStatus.EXCLUDED);\r
+//                     String name = graph.getValue(nameResource, Bindings.STRING);\r
+                       roots.add(new RootSpec(root.resource, root.name, root.internal));\r
+                       preStatus.put(root.resource, ExtentStatus.INTERNAL);\r
+               }\r
+               return this;\r
+       }\r
+\r
+       public TGConfigurer roots2(Collection<Resource> rs) throws DatabaseException {\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               for(Resource root : rs) {\r
+                       Resource nameResource = graph.getPossibleObject(root, L0.HasName);\r
+                       String name = "<no name>";\r
+                       if(nameResource != null) {\r
+                               name = graph.getPossibleValue(nameResource, Bindings.STRING);\r
+                               if(name == null) name = "<no name>";\r
+                               preStatus.put(nameResource, ExtentStatus.EXCLUDED);\r
+                       }\r
+                       roots.add(new RootSpec(root, name, true));\r
+                       preStatus.put(root, ExtentStatus.INTERNAL);\r
+               }\r
+               return this;\r
+       }\r
+       \r
+       public TGConfigurer roots3(Collection<NamedResource> rs) throws DatabaseException {\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               for(NamedResource root : rs) {\r
+                       Resource nameResource = graph.getPossibleObject(root.getResource(), L0.HasName);\r
+//                     String name = graph.getValue(nameResource, Bindings.STRING);\r
+                       roots.add(new RootSpec(root.getResource(), root.getName(), true));\r
+                       preStatus.put(root.getResource(), ExtentStatus.INTERNAL);\r
+                       preStatus.put(nameResource, ExtentStatus.EXCLUDED);\r
+               }\r
+               return this;\r
+       }\r
+\r
+       public TransferableGraphConfiguration2 create() throws DatabaseException {\r
+\r
+               // Final preparation\r
+\r
+               // -Exclude root library if not explicitly included\r
+               if (!roots.contains(graph.getRootLibrary()))\r
+                       preStatus.put(graph.getRootLibrary(), ExtentStatus.EXCLUDED);\r
+\r
+               // -Exclude owners if not explicitly defined\r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               ArrayList<Resource> rootList = new ArrayList<Resource>();\r
+               for (RootSpec root : roots) {\r
+                       if(root.internal) {\r
+                               rootList.add(root.resource);\r
+                               for (Resource owner : graph.getObjects(root.resource, L0.IsOwnedBy)) {\r
+                                       ExtentStatus ownerStatus = preStatus.get(owner);\r
+                                       if(ownerStatus == null)\r
+                                               preStatus.put(owner, ExtentStatus.EXCLUDED);\r
+                               }\r
+                       } else {\r
+                               preStatus.put(root.resource, ExtentStatus.EXTERNAL);\r
+                       }\r
+               }\r
+               \r
+               Resource owner = CommonDBUtils.getNearestOwner(graph, rootList);\r
+               if(owner != null) {\r
+                       Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(owner));\r
+                       return new TransferableGraphConfiguration2(indexRoot, roots, preStatus, ignoreVirtualResources, validate);\r
+               } else {\r
+                       return new TransferableGraphConfiguration2(null, roots, preStatus, ignoreVirtualResources, validate);\r
+               }\r
+               \r
+       }\r
+       \r
+}\r