]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/HeadlessComponentTypePropertiesResultRequest.java
Support for creating shared ontology dump to git
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / utils / HeadlessComponentTypePropertiesResultRequest.java
index a2f7138b44bdd048c8b5dee813d1d9e9bfaaee85..757643dbdfded2aea7b3c2e9a307132fef4f040f 100644 (file)
@@ -52,12 +52,70 @@ public class HeadlessComponentTypePropertiesResultRequest extends UniqueRead<Com
     protected void readSectionSpecificData(ReadGraph graph, ComponentTypeViewerPropertyInfo info) throws DatabaseException {
     }
     
+    public static ComponentTypeViewerPropertyInfo readPropertyInfo(ReadGraph graph, Resource relation, Resource componentType, boolean typeIsImmutable) throws DatabaseException {
+
+        Layer0 L0 = Layer0.getInstance(graph);
+        Layer0X L0X = Layer0X.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+        String name = graph.getRelatedValue(relation, L0.HasName);
+        String type =  graph.getPossibleRelatedValue(relation, L0.RequiresValueType);
+        String label = graph.getPossibleRelatedValue(relation, L0.HasLabel);
+        if (label == null)
+            label = ""; //$NON-NLS-1$
+        String description = graph.getPossibleRelatedValue(relation, L0.HasDescription);
+        if (description == null)
+            description = ""; //$NON-NLS-1$
+        NumberType numberType = null;
+        if(type == null)
+            type = "Double"; //$NON-NLS-1$
+        String unit = graph.getPossibleRelatedValue(relation, L0X.HasUnit, Bindings.STRING);
+        String defaultValue = "0"; //$NON-NLS-1$
+        String expression = null;
+
+        if(componentType != null) {
+            for(Resource assertion : graph.getAssertedObjects(componentType, relation)) {
+                try {
+                    expression = graph.getPossibleRelatedValue(assertion, L0.SCLValue_expression, Bindings.STRING);
+                    if(expression != null) {
+                        defaultValue = "=" + expression; //$NON-NLS-1$
+                    } else if (graph.sync(new IsEnumeratedValue(assertion))) {
+                        defaultValue = CommonDBUtils.getEnumerationValueName(graph, assertion);
+                    } else {
+                        Datatype dt = getPossibleDatatype(graph, assertion);
+                        if (dt == null)
+                            continue;
+                        if (dt instanceof NumberType)
+                            numberType = (NumberType) dt;
+                        Binding binding = Bindings.getBinding(dt);
+                        Object value = graph.getValue(assertion, binding);
+                        try {
+                            defaultValue = binding.toString(value, true);
+                        } catch (BindingException e) {
+                            ErrorLogger.defaultLogError(e);
+                        }
+                    }
+                } catch(DatabaseException e) {
+                    ErrorLogger.defaultLogError(e);
+                }
+            }
+        }
+
+        String valid = expression != null ? validateMonitorExpression(graph, componentType, relation, expression) : null; 
+
+        boolean immutable = typeIsImmutable || graph.isImmutable(relation);
+        ComponentTypeViewerPropertyInfo info =
+                new ComponentTypeViewerPropertyInfo(relation, name, type, defaultValue, numberType, unit, label, description, expression, valid, immutable);
+
+        return info;
+            
+    }
+    
     @Override
     public ComponentTypePropertiesResult perform(ReadGraph graph) throws DatabaseException {
         List<ComponentTypeViewerPropertyInfo> result = new ArrayList<>();
         List<NamedResource> connectionPoints = new ArrayList<>();
         Layer0 L0 = Layer0.getInstance(graph);
-        Layer0X L0X = Layer0X.getInstance(graph);
         StructuralResource2 STR = StructuralResource2.getInstance(graph);
 
         boolean typeIsImmutable = graph.isImmutable(componentType)
@@ -67,67 +125,22 @@ public class HeadlessComponentTypePropertiesResultRequest extends UniqueRead<Com
 
         for(Resource relation : graph.getObjects(componentType, L0.DomainOf)) {
             if(graph.isSubrelationOf(relation, L0.HasProperty)) {
-                String name = graph.getRelatedValue(relation, L0.HasName);
-                String type =  graph.getPossibleRelatedValue(relation, L0.RequiresValueType);
-                String label = graph.getPossibleRelatedValue(relation, L0.HasLabel);
-                if (label == null)
-                    label = ""; //$NON-NLS-1$
-                String description = graph.getPossibleRelatedValue(relation, L0.HasDescription);
-                if (description == null)
-                    description = ""; //$NON-NLS-1$
-                NumberType numberType = null;
-                if(type == null)
-                    type = "Double"; //$NON-NLS-1$
-                String unit = graph.getPossibleRelatedValue(relation, L0X.HasUnit, Bindings.STRING);
-                String defaultValue = "0"; //$NON-NLS-1$
-                String expression = null;
-                
-                for(Resource assertion : graph.getAssertedObjects(componentType, relation)) {
-                    try {
-                        expression = graph.getPossibleRelatedValue(assertion, L0.SCLValue_expression, Bindings.STRING);
-                        if(expression != null) {
-                               defaultValue = "=" + expression; //$NON-NLS-1$
-                        } else if (graph.sync(new IsEnumeratedValue(assertion))) {
-                            defaultValue = CommonDBUtils.getEnumerationValueName(graph, assertion);
-                        } else {
-                            Datatype dt = getPossibleDatatype(graph, assertion);
-                            if (dt == null)
-                                continue;
-                            if (dt instanceof NumberType)
-                                numberType = (NumberType) dt;
-                            Binding binding = Bindings.getBinding(dt);
-                            Object value = graph.getValue(assertion, binding);
-                            try {
-                                defaultValue = binding.toString(value, true);
-                            } catch (BindingException e) {
-                                ErrorLogger.defaultLogError(e);
-                            }
-                        }
-                    } catch(DatabaseException e) {
-                        ErrorLogger.defaultLogError(e);
-                    }
-                }
-                
-                String valid = expression != null ? validateMonitorExpression(graph, componentType, relation, expression) : null; 
-
-                boolean immutable = typeIsImmutable || graph.isImmutable(relation);
-                ComponentTypeViewerPropertyInfo info =
-                        new ComponentTypeViewerPropertyInfo(relation, name, type, defaultValue, numberType, unit, label, description, expression, valid, immutable);
-                
+                ComponentTypeViewerPropertyInfo info = readPropertyInfo(graph, relation, componentType, typeIsImmutable);
                 readSectionSpecificData(graph, info);
-
                 result.add(info);
-
             } else if (graph.isInstanceOf(relation, STR.ConnectionRelation)) {
                 NamedResource nr = new NamedResource(NameUtils.getSafeName(graph, relation), relation);
                 connectionPoints.add(nr);
             }
         }
+        
         Collections.sort(result);
+        
         return new ComponentTypePropertiesResult(result, connectionPoints, typeIsImmutable);
+        
     }
     
-    private Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
+    private static Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
         Binding binding = Bindings.getBindingUnchecked(Datatype.class);
         for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {
             Datatype dt = graph.getPossibleValue(dataTypeResource, binding);