]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/property/Properties.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / property / Properties.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/property/Properties.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/property/Properties.java
new file mode 100644 (file)
index 0000000..5d5a118
--- /dev/null
@@ -0,0 +1,68 @@
+package org.simantics.db.layer0.property;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.operation.Layer0X;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ * TODO: consider removing since this doesn't seem to be used.\r
+ */\r
+public class Properties {\r
+       \r
+       public static <T> T getPossibleRelatedValue(ReadGraph graph, Resource resource, Resource relation, Binding binding) throws DatabaseException {\r
+               \r
+               T result = graph.getPossibleRelatedValue(resource, relation, binding);\r
+               if(result != null) return result;\r
+               else {\r
+                       \r
+                   Layer0X L0X = Layer0X.getInstance(graph);\r
+                       ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();\r
+                       for(Statement stm : graph.getStatements(resource, L0X.ObtainsProperty)) {\r
+                               Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);\r
+                               order.add(new OrderedResource(position, stm.getObject()));\r
+                       }\r
+                       Collections.sort(order);\r
+                       for(OrderedResource or : order) {\r
+                               result = getPossibleRelatedValue(graph, or.r, relation, binding);\r
+                               if(result != null) return result;\r
+                       }\r
+                       \r
+                       return null;\r
+                       \r
+               }\r
+               \r
+       }\r
+\r
+       public static <T> T getPossibleRelatedAdapter(ReadGraph graph, Resource resource, Resource relation, Class<T> clazz) throws DatabaseException {\r
+\r
+               T result = graph.getPossibleRelatedAdapter(resource, relation, clazz);\r
+               if(result != null) return result;\r
+               else {\r
+                       \r
+                   Layer0X L0X = Layer0X.getInstance(graph);\r
+                       ArrayList<OrderedResource> order = new ArrayList<OrderedResource>();\r
+                       for(Statement stm : graph.getStatements(resource, L0X.ObtainsProperty)) {\r
+                               Integer position = graph.getRelatedValue(stm.getPredicate(), L0X.NaturalNumberOrderRelation, Bindings.INTEGER);\r
+                               order.add(new OrderedResource(position, stm.getObject()));\r
+                       }\r
+                       Collections.sort(order);\r
+                       for(OrderedResource or : order) {\r
+                               result = getPossibleRelatedAdapter(graph, or.r, relation, clazz);\r
+                               if(result != null) return result;\r
+                       }\r
+                       \r
+                       return null;\r
+                       \r
+               }\r
+               \r
+       }\r
+\r
+}\r