Improvements to resolving primitive properties in document server 98/798/3
authorJussi Koskela <jussi.koskela@semantum.fi>
Wed, 2 Aug 2017 11:38:23 +0000 (14:38 +0300)
committerJani Simomaa <jani.simomaa@semantum.fi>
Sun, 6 Aug 2017 18:07:17 +0000 (21:07 +0300)
Catch and report property value evaluation exceptions, fixed typo in
dataDefinitions reading, always initialize property map, improved error
message formatting.

refs #7405
Change-Id: I4ba78bb0b91b05e1f04a65f4dc92fcfe06620f60

bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java
bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java
bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestUtils.java

index 4821bbd1ca0e4c48a1cbe71210d1e53acb2c20be..5e1f5cc30e798aff45343b8f8c5f4483b6d8a9cc 100644 (file)
@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
@@ -324,8 +325,24 @@ public class DocumentServerUtils {
                        for(Variable attrib : statics) {
                                String name = attrib.getName(graph);
                                try {
-                                   Object value = DocumentServerUtils.getValue(graph, attrib);
-                                   object.addJSONField(name, value);
+                                       if (name.equals(NodeRequest.PROPERTY_VALUE_EXCEPTIONS)) {
+                                       @SuppressWarnings("unchecked")
+                                               Map<String, Exception> exceptions = (Map<String, Exception>)DocumentServerUtils.getValue(graph, attrib);
+                                       
+                                       List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
+                                           if(errorList == null)
+                                               errorList = new ArrayList<String>();
+                                           
+                                   for (Map.Entry<String, Exception> entry : exceptions.entrySet()) {
+                                       String errorMessage = NodeRequestUtils.formatErrorMessage(entry.getKey(), entry.getValue());
+                                       errorList.add(errorMessage);
+                                   }
+                                       object.addJSONField(NodeRequest.ERRORS, errorList);
+                                       
+                                   } else {
+                                               Object value = DocumentServerUtils.getValue(graph, attrib);
+                                           object.addJSONField(name, value);
+                                   }
                                } catch (Throwable t) {
                                    List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
                                    if(errorList == null)
index 6ccffbe973ddecdeef07e4a8bcbdaebe3acf52e2..13444fdba944701e2454fea1174283d89045f8ca 100644 (file)
@@ -62,6 +62,7 @@ import org.simantics.document.server.io.CommandContextImpl;
 import org.simantics.document.server.io.CommandContextMutable;
 import org.simantics.document.server.io.CommandResult;
 import org.simantics.document.server.io.IConsole;
+import org.simantics.document.server.request.NodeRequest;
 import org.simantics.document.server.request.ServerSCLHandlerValueRequest;
 import org.simantics.document.server.request.ServerSCLValueRequest;
 import org.simantics.document.server.serverResponse.ServerResponse;
@@ -117,7 +118,35 @@ public class Functions {
 
     @SCLValue(type = "VariableMap")
     public static VariableMap primitiveProperties = new VariableMapImpl() {
+       private void storePropertyValueAndExceptions(ReadGraph graph, Variable parent, String name, Variable property, Map<String, Variable> map) {
+               try {
+                       Object value = property.getValue(graph);
+                               map.put(name, new ConstantPropertyVariable(parent, name, value, null));
+               } catch (DatabaseException e) {
+                       Variable propertyExceptions = map.get(NodeRequest.PROPERTY_VALUE_EXCEPTIONS);
+                       Map<String, Exception> exceptionMap;
+                       if (propertyExceptions == null) {
+                               exceptionMap = new TreeMap<String, Exception>();
+                               propertyExceptions = new ConstantPropertyVariable(parent, NodeRequest.PROPERTY_VALUE_EXCEPTIONS, exceptionMap, null);
+                               map.put(NodeRequest.PROPERTY_VALUE_EXCEPTIONS, propertyExceptions);
+                       } else {
+                               try {
+                                               exceptionMap = propertyExceptions.getValue(graph);
+                                       } catch (DatabaseException e1) {
+                                               Logger.defaultLogError(e1);
+                                               return;
+                                       }
+                       }
+                       String label = name;
+                       try {
+                               label = property.getLabel(graph);
+                       } catch (DatabaseException e2) {
 
+                       }
+                       exceptionMap.put(label, e);
+               }
+       }
+       
         @Override
         public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
             return All.getStandardPropertyDomainPropertyVariableFromValue(graph, context, name);
@@ -126,6 +155,8 @@ public class Functions {
         @Override
         public Map<String, Variable> getVariables(final ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
 
+            if(map == null) map = new HashMap<String,Variable>();
+
             Variable parent = context.getParent(graph);
 
             DocumentationResource DOC = DocumentationResource.getInstance(graph);
@@ -136,9 +167,8 @@ public class Functions {
                 for(Variable property : procedural.getProperties(graph/*, DocumentationResource.URIs.Document_AttributeRelation*/)) {
                     if(property instanceof StandardAssertedGraphPropertyVariable) {
                         StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
-                        if("datadefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)  || "pollingFunction".equals(ass.property.name)) {
-                            Object value = property.getPossibleValue(graph);
-                            if(value != null) map.put(ass.property.name, new ConstantPropertyVariable(parent, ass.property.name, value, null));
+                        if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)  || "pollingFunction".equals(ass.property.name)) {
+                               storePropertyValueAndExceptions(graph, parent, ass.property.name, property, map);
                         }
                         continue;
                     }
@@ -146,56 +176,47 @@ public class Functions {
                     if(predicate != null) {
                         PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
                         if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
-                            Variable prop = parent.getProperty(graph, predicate);
-                            Object value = prop.getValue(graph);
-                            if(map == null) map = new HashMap<String,Variable>();
-                            map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null));
+                               Variable prop = parent.getProperty(graph, predicate);
+                            storePropertyValueAndExceptions(graph, parent, info.name, prop, map);
                         }
                     }
                 }
 
             } else {
 
+                Resource parentRes = parent.getRepresents(graph);
+                {
+                       Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands);
+                       storePropertyValueAndExceptions(graph, parent, "commands", prop, map);
+                }
+
+                if (graph.getPossibleObject(parentRes, DOC.Properties_dataDefinitions) != null) {
+                       Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions);
+                       storePropertyValueAndExceptions(graph, parent, "dataDefinitions", prop, map);
+                }
+                
                 DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
                 PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
 
-                dqs.forEachDirectPersistentStatement(graph, parent.getRepresents(graph), foo);
+                dqs.forEachDirectPersistentStatement(graph, parentRes, foo);
 
                 for(Statement stm : foo.result) {
                     Resource predicate = stm.getPredicate();
                     PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
 
                     if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
-                        if(map == null) map = new HashMap<String,Variable>();
-                        Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate);
-                        Object value = prop.getValue(graph);
-                        map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null));
+                       Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate);
+                       storePropertyValueAndExceptions(graph, parent, info.name, prop, map);
                     } else {
                         Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
                         if(definition != null) {
-                            if(map == null) map = new HashMap<String,Variable>();
-                            try {
-                                PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
-                                Variable prop = new StandardGraphPropertyVariable(graph, parent, definition);
-                                map.put(info2.name, new PrimitiveValueVariable(parent, info2.name, prop));
-                            } catch (DatabaseException e) {
-                                Logger.defaultLogError(e);
-                            }
+                               PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
+                               Variable prop = new StandardGraphPropertyVariable(graph, parent, definition);
+                            map.put(info2.name, new PrimitiveValueVariable(parent, info2.name, prop));
                         }
                     }
                 }
-
-                Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions);
-                Object value = prop.getPossibleValue(graph);
-                if(value != null) map.put("dataDefinitions", new ConstantPropertyVariable(parent, "dataDefinitions", value, null));
-                prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands);
-                value = prop.getPossibleValue(graph);
-                if(value != null) map.put("commands", new ConstantPropertyVariable(parent, "commands", value, null));
-
             }
-
-            if(map == null) return Collections.emptyMap();
-
             return map;
 
         }
index 98bd0fb821dc0286238da7a310c85149247633a0..d9418db9947d482fecd4daf547d85359363bea5c 100644 (file)
@@ -16,7 +16,8 @@ import org.simantics.utils.datastructures.Pair;
 public class NodeRequest extends VariableRead<JSONObject> {
 
     public static final String ERRORS = "Errors";
-
+    public static final String PROPERTY_VALUE_EXCEPTIONS = "_PropertyValueExceptions";
+    
        public NodeRequest(Variable node) {
                super(node);
        }
index ed0c54c968b8043e8fc7383655f8b9cf268a6236..00eb2a492ffd4243329e8b0d2706a7d4535d0d68 100644 (file)
@@ -24,10 +24,13 @@ public class NodeRequestUtils {
                if (t instanceof DocumentException) {
                        return t.getMessage();
                } else if (t instanceof MissingVariableException) {
-                       return "Evaluation of property '" + name + "' failed\n" +
-                                       t.getMessage();
+                       return name + ":\n" +
+                                       t.getMessage().replaceAll("(?m)^", "  ");
                } else if (t instanceof SCLDatabaseException) {
-                       return t.getMessage();
+                       StringBuilder sb = new StringBuilder();
+                       sb.append(name + ":\n");
+                       sb.append(t.getMessage().replaceAll("(?m)^", "  "));
+                       return sb.toString();
                } else if (t instanceof NotFoundException) {
                        return t.getMessage();
                } else if (t instanceof ImportFailureException) {