]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / genericrelation / DependencyResources.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java
new file mode 100644 (file)
index 0000000..5f5df71
--- /dev/null
@@ -0,0 +1,78 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.genericrelation;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.GenericRelationIndex;\r
+import org.simantics.db.service.QueryControl;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.scl.runtime.function.FunctionImpl4;\r
+import org.simantics.scl.runtime.function.UnsaturatedFunction2;\r
+\r
+/**\r
+ * dependencies:\r
+ *      (ReadGraph, Resource model, String query) -> List<Map<String, Object>>\r
+ *      (ReadGraph, Resource model, String query, Integer maxResults) -> List<Map<String, Object>>\r
+ * \r
+ * @author Antti Villberg\r
+ */\r
+public class DependencyResources extends FunctionImpl4<ReadGraph, Resource, String, Integer, Object> {\r
+\r
+    public static final String FIELD_MODEL    = "Model";\r
+    public static final String FIELD_PARENT   = "Parent";\r
+    public static final String FIELD_RESOURCE = "Resource";\r
+    public static final String FIELD_NAME     = "Name";\r
+    public static final String FIELD_TYPES    = "Types";\r
+\r
+    protected Resource getIndexRelation(ReadGraph graph) {\r
+        return Layer0X.getInstance(graph).DependenciesRelation;\r
+    }\r
+\r
+    protected String getBindingPattern() {\r
+        return Dependencies.getBindingPattern();\r
+    }\r
+\r
+    @Override\r
+    public Object apply(ReadGraph p0, Resource p1) {\r
+        return new UnsaturatedFunction2(this, p0, p1);\r
+    }\r
+\r
+    @Override\r
+    public Object apply(ReadGraph p0, Resource p1, String p2) {\r
+        return apply(p0, p1, p2, Integer.MAX_VALUE);\r
+    }\r
+\r
+    @Override\r
+    public Object apply(ReadGraph graph, final Resource model, final String query, Integer _maxResults) {\r
+        \r
+        try {\r
+\r
+            final GenericRelationIndex index = graph.adapt(getIndexRelation(graph), GenericRelationIndex.class);\r
+\r
+            // Listen\r
+            graph.syncRequest(new ExternalRequest(index, model));\r
+            \r
+            final int maxResults = _maxResults != null ? _maxResults : Integer.MAX_VALUE;\r
+            \r
+            QueryControl qc = graph.getService(QueryControl.class);\r
+            return index.queryResources(qc.getIndependentGraph(graph), query, getBindingPattern(), new Object[] { model }, maxResults);\r
+            \r
+        } catch (DatabaseException e) {\r
+            Logger.defaultLogError(e);\r
+            return null;\r
+        }\r
+    }\r
+\r
+}\r