]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/queries/StructuralChildMapOfResource.java
For PSaaS
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / queries / StructuralChildMapOfResource.java
diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/queries/StructuralChildMapOfResource.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/queries/StructuralChildMapOfResource.java
new file mode 100644 (file)
index 0000000..4243c1d
--- /dev/null
@@ -0,0 +1,34 @@
+package org.simantics.structural2.queries;
+
+import java.util.Map;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.uri.UnescapedChildMapOfResource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.structural.stubs.StructuralResource2;
+
+public class StructuralChildMapOfResource extends ResourceRead<Map<String, Resource>> {
+
+    public StructuralChildMapOfResource(Resource resource) {
+        super(resource);
+    }
+
+    @Override
+    public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        Resource type = graph.getPossibleType(resource, STR.Component);
+        if(type != null) {
+            Resource definition = graph.getPossibleObject(type, STR.IsDefinedBy);
+            if(definition != null) {
+                Map<String, Resource> map = graph.syncRequest(new UnescapedChildMapOfResource(definition));
+                if (!map.isEmpty())
+                    return map;
+            } 
+        }
+        Map<String, Resource> directChildren = graph.syncRequest(new UnescapedChildMapOfResource(resource));
+        return directChildren;
+    }
+
+}
\ No newline at end of file