]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfoRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PropertyInfoRequest.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfoRequest.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfoRequest.java
new file mode 100644 (file)
index 0000000..f6d9b95
--- /dev/null
@@ -0,0 +1,120 @@
+package org.simantics.db.layer0.request;\r
+\r
+import gnu.trove.map.hash.THashMap;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.accessor.reference.IndexReference;\r
+import org.simantics.databoard.accessor.reference.NameReference;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.layer0.variable.ValueAccessor;\r
+import org.simantics.db.layer0.variable.VariableBuilder;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+final public class PropertyInfoRequest extends ResourceRead<PropertyInfo> {\r
+\r
+       public PropertyInfoRequest(Resource resource) {\r
+               super(resource);\r
+       }\r
+\r
+       @Override\r
+       public PropertyInfo perform(ReadGraph graph) throws DatabaseException {\r
+               \r
+               Layer0 L0 = Layer0.getInstance(graph);\r
+               Layer0X L0X = Layer0X.getInstance(graph);\r
+               \r
+               boolean isHasProperty = graph.isSubrelationOf(resource, L0.HasProperty);\r
+               \r
+               String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);\r
+               if(name != null) name = name.intern();\r
+               \r
+               Set<String> classifications = graph.sync(new ClassificationsRequest(graph.getPrincipalTypes(resource)));\r
+               VariableBuilder variableBuilder = graph.getPossibleAdapter(resource, VariableBuilder.class);\r
+               \r
+               Datatype requiredDataType = graph.getPossibleRelatedValue(resource, L0X.RequiresDataType, Layer0Utils.datatype_binging);\r
+               \r
+               String definedUnit = graph.getPossibleRelatedValue(resource, L0X.HasUnit, Bindings.STRING);\r
+               \r
+               Resource literalRange = graph.getPossibleObject(resource, L0.HasRange);\r
+               String requiredValueType = graph.getPossibleRelatedValue(resource, L0.RequiresValueType, Bindings.STRING);\r
+               //System.out.println(name + " -> " + requiredValueType);\r
+               \r
+               ValueAccessor accessor = graph.getPossibleRelatedValue2(resource, Layer0.getInstance(graph).valueAccessor, resource);\r
+               \r
+               boolean hasEnumerationRange = graph.syncRequest(new HasEnumerationRange(resource));\r
+\r
+               Collection<Resource> ranges = graph.getObjects(resource, L0.HasRange);\r
+               if (!ranges.isEmpty()) {\r
+                       for (Resource range : ranges) {\r
+                               if (requiredValueType == null) {\r
+                                       // Check if the single range defines value type\r
+                                       Collection<Resource> valueTypes = graph.getAssertedObjects(range, L0.HasValueType);\r
+                                       if (valueTypes.size() > 0) {\r
+                                               for (Resource valueType : valueTypes) {\r
+                                                       String vt = graph.getPossibleValue(valueType, Bindings.STRING);\r
+                                                       if (vt != null && !vt.isEmpty()) {\r
+                                                               requiredValueType = vt;\r
+                                                               continue;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               }\r
+\r
+                               Collection<Resource> subliterals = graph.getObjects(range, L0.HasSubliteralPredicate);\r
+                               if(!subliterals.isEmpty()) {\r
+                                       Map<String,Pair<Resource, ChildReference>> map = new THashMap<String,Pair<Resource, ChildReference>>(); \r
+                                       for(Resource p : subliterals) {\r
+                                               String pN = graph.getPossibleRelatedValue(p, L0.HasName, Bindings.STRING);\r
+                                               if(pN == null) continue;\r
+                                               if(pN.startsWith("n-")) {\r
+                                                       ChildReference r = new NameReference(pN.substring(2));\r
+                                                       map.put(pN, Pair.make(p, r));\r
+                                               } else if (pN.startsWith("i-")) {\r
+                                                       ChildReference r = new IndexReference(Integer.parseInt(pN.substring(2)));\r
+                                                       map.put(pN, Pair.make(p, r));\r
+                                               }\r
+                                       }\r
+                                       \r
+                                       return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, map, accessor, hasEnumerationRange);\r
+                                       \r
+                               }\r
+                       }\r
+               }\r
+               \r
+               return PropertyInfo.make(graph, resource, name, isHasProperty, classifications, variableBuilder, literalRange, requiredDataType, definedUnit, requiredValueType, Collections.<String,Pair<Resource,ChildReference>>emptyMap(), accessor, hasEnumerationRange);\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (!(object instanceof PropertyInfoRequest))\r
+            return false;\r
+        PropertyInfoRequest r = (PropertyInfoRequest)object;\r
+        return r.resource.equals(resource);\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+               return resource.hashCode() + classHash;\r
+       }\r
+       \r
+       private static int classHash = 31*PropertyInfo.class.hashCode();\r
+\r
+}\r