Variable optimizations for documents (Simupedia) 56/2456/3
authorAntti Villberg <antti.villberg@semantum.fi>
Tue, 13 Nov 2018 06:02:59 +0000 (08:02 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 13 Nov 2018 08:47:58 +0000 (08:47 +0000)
* A request for obtaining defined structural children

gitlab #169

Change-Id: I1977b3df10998fc3b52ada098105ad3e66663018

bundles/org.simantics.structural2/src/org/simantics/structural2/queries/StructuralChildMapOfResource.java [new file with mode: 0644]

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..15da98b
--- /dev/null
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+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;
+
+/**
+ * @author Antti Villberg
+ * @since 1.36.0
+ */
+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;
+            }
+        }
+        return graph.syncRequest(new UnescapedChildMapOfResource(resource));
+    }
+
+}
\ No newline at end of file