]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphPropertyVariable.java
Fix handling of property variables with no predicate resource.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardGraphPropertyVariable.java
index ed58556c7e570814bd4ce25e5473a6821f2c33bc..c9214e398c69577da69642536c8b932e67e5bc69 100644 (file)
@@ -21,6 +21,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.DatatypeNotFoundException;
 import org.simantics.db.exception.ValidationException;
 import org.simantics.db.layer0.exception.InvalidVariableException;
+import org.simantics.db.layer0.exception.MissingVariableException;
 import org.simantics.db.layer0.exception.MissingVariableValueException;
 import org.simantics.db.layer0.exception.PendingVariableException;
 import org.simantics.db.layer0.function.All;
@@ -35,12 +36,13 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
+
     private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphPropertyVariable.class);
-    
-       protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null,
-                       false, Collections.<String> emptySet(), null, null, null, null, null, null,
-                       Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
-                       null, false);
+
+    protected static final PropertyInfo NO_PROPERTY = new PropertyInfo(null, null, true,
+            false, false, Collections.<String> emptySet(), null, null, null, null, null, null,
+            Collections.<String, Pair<Resource, ChildReference>> emptyMap(),
+            null, false);
 
        final public Variable parent;
        final public Resource parentResource;
@@ -99,11 +101,15 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
        @Override
        public String getPossibleLabel(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       return null;
                return graph.getPossibleRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
        }
 
        @Override
        public String getLabel(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       throw new NoPredicateResourceException("No predicate resource for property " + getName(graph));
                return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent, Bindings.STRING);
        }
 
@@ -123,10 +129,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                   LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                           LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -144,10 +152,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                       LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -170,13 +180,10 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                if(represents == null)
                        throw new InvalidVariableException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
                return represents;
-//             return graph.getSingleObject(parentResource, property.predicate);
        }
 
        @Override
        public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
-//        if(parentResource == null) return null;
-//             return graph.getPossibleObject(parentResource, property.predicate);
                return represents;
        }
 
@@ -185,10 +192,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                       LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -202,10 +211,12 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                
                if(Development.DEVELOPMENT) {
                        if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
-                               String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
-                               if(error != null) {
-                                       LOGGER.error(error);
-                                       throw new ValidationException(error);
+                               if (property.predicate != null) {
+                                       String error = L0Validations.checkValueType(graph, parentResource, property.predicate);
+                                       if(error != null) {
+                                               LOGGER.error(error);
+                                               throw new ValidationException(error);
+                                       }
                                }
                        }
                }
@@ -226,16 +237,6 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
 
        @Override
        public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
-//<<<<<<< .mine
-//             
-//     Binding binding = getPossibleDefaultBinding(graph);
-//        if(binding != null) return binding.type();
-//
-//             Variant vt = getVariantValue(graph);
-//             binding = vt.getBinding();
-//             if(binding == null) throw new DatabaseException("No binding in Variant with value " + vt.getValue());
-//             return binding.type();
-//=======
                Datatype type;
                try {                   
                        type = getValueAccessor(graph).getDatatype(graph, this);
@@ -252,7 +253,6 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
                }
                
                return type;
-//>>>>>>> .r30794
                
        }
        
@@ -337,11 +337,15 @@ public class StandardGraphPropertyVariable extends AbstractPropertyVariable {
        
        @Override
        public Variable getPredicate(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       throw new MissingVariableException("No predicate for property " + getName(graph));
                return Variables.getVariable(graph, graph.getURI(property.predicate));
        }
        
        @Override
        public Resource getPredicateResource(ReadGraph graph) throws DatabaseException {
+               if (property.predicate == null)
+                       throw new NoPredicateResourceException("No predicate for property " + getName(graph));
                return property.predicate;
        }