From 4f87cc11bc1e4a56860f278798e41834ea1fbb3f Mon Sep 17 00:00:00 2001 From: Jussi Koskela Date: Wed, 2 Aug 2017 14:38:23 +0300 Subject: [PATCH] Improvements to resolving primitive properties in document server 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 --- .../document/server/DocumentServerUtils.java | 21 ++++- .../simantics/document/server/Functions.java | 83 ++++++++++++------- .../document/server/request/NodeRequest.java | 3 +- .../server/request/NodeRequestUtils.java | 9 +- 4 files changed, 79 insertions(+), 37 deletions(-) diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java index 4821bbd1c..5e1f5cc30 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java @@ -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 exceptions = (Map)DocumentServerUtils.getValue(graph, attrib); + + List errorList = object.getJSONField(NodeRequest.ERRORS); + if(errorList == null) + errorList = new ArrayList(); + + for (Map.Entry 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 errorList = object.getJSONField(NodeRequest.ERRORS); if(errorList == null) diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java index 6ccffbe97..13444fdba 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java @@ -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 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 exceptionMap; + if (propertyExceptions == null) { + exceptionMap = new TreeMap(); + 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 getVariables(final ReadGraph graph, Variable context, Map map) throws DatabaseException { + if(map == null) map = new HashMap(); + 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(); - 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(); - 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(); - 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; } diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java index 98bd0fb82..d9418db99 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java @@ -16,7 +16,8 @@ import org.simantics.utils.datastructures.Pair; public class NodeRequest extends VariableRead { public static final String ERRORS = "Errors"; - + public static final String PROPERTY_VALUE_EXCEPTIONS = "_PropertyValueExceptions"; + public NodeRequest(Variable node) { super(node); } diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestUtils.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestUtils.java index ed0c54c96..00eb2a492 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestUtils.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestUtils.java @@ -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) { -- 2.47.1