]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
Let the request processor handle the exceptions
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / Functions.java
index 8b68cc409cde83d0442ae081163c15e3dcf0071c..0a7e004f7dcab5e75740d176100c78d59c3dfb47 100644 (file)
@@ -38,6 +38,7 @@ import org.simantics.db.layer0.request.PropertyInfoRequest;
 import org.simantics.db.layer0.request.VariableProperty;
 import org.simantics.db.layer0.request.VariableRead;
 import org.simantics.db.layer0.request.VariableValueWithBinding;
+import org.simantics.db.layer0.scl.SCLDatabaseException;
 import org.simantics.db.layer0.variable.ConstantPropertyVariable;
 import org.simantics.db.layer0.variable.ProxyChildVariable;
 import org.simantics.db.layer0.variable.ProxySessionRequest;
@@ -62,6 +63,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;
@@ -73,6 +75,7 @@ import org.simantics.modeling.services.CaseInsensitiveComponentFunctionNamingStr
 import org.simantics.modeling.services.ComponentNamingStrategy;
 import org.simantics.operation.Layer0X;
 import org.simantics.project.IProject;
+import org.simantics.scl.compiler.module.repository.ImportFailureException;
 import org.simantics.scl.compiler.types.TCon;
 import org.simantics.scl.compiler.types.Type;
 import org.simantics.scl.compiler.types.Types;
@@ -95,11 +98,14 @@ import org.simantics.simulation.project.IExperimentManager;
 import org.simantics.structural2.variables.Connection;
 import org.simantics.structural2.variables.StandardProceduralChildVariable;
 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.map.hash.THashMap;
 
 public class Functions {
 
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Functions.class);
+    
     private static class PrimitivePropertyStatementsProcedure implements AsyncProcedure<DirectStatements> {
 
         public DirectStatements result;
@@ -117,7 +123,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 +160,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 +172,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)) {
-                            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,43 +181,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) {
+                               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;
 
         }
@@ -544,6 +583,7 @@ public class Functions {
        }
     }
 
+    @Deprecated
     public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException {
         return new EventHandler() {
             @Override
@@ -553,6 +593,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
 
        final Session session = graph.getSession();
@@ -585,15 +626,11 @@ public class Functions {
                                                        return (String)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -610,6 +647,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
 
        final Session session = graph.getSession();
@@ -642,15 +680,11 @@ public class Functions {
                                                        return (String)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -667,6 +701,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) {
 
        final Session session = graph.getSession();
@@ -693,15 +728,11 @@ public class Functions {
                                                        return (CommandResult)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -791,10 +822,7 @@ public class Functions {
                                                Object oldGraph = sclContext.put("graph", graph);
                                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
                                                try {
-                                                       Object response = fn.apply(parameters);
-                                                       return response;
-                                               } catch (Throwable t) {
-                                                       return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
+                                                       return fn.apply(parameters);
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
@@ -815,10 +843,7 @@ public class Functions {
                                                Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
 
                                                try {
-                                                       Object response = fn.apply(parameters);
-                                                       return response;
-                                               } catch (Throwable t) {
-                                                       return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
+                                                       return fn.apply(parameters);
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
@@ -860,6 +885,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) {
 
        final Session session = graph.getSession();
@@ -886,15 +912,11 @@ public class Functions {
                                                        return (CommandResult)response;
                                                    }
                                                    return null;
-                                               } catch (Throwable t) {
-                                                   t.printStackTrace();
                                                } finally {
                                                        sclContext.put("graph", oldGraph);
                                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                                }
 
-                                               return null;
-
                                            }
 
                                        });
@@ -911,6 +933,8 @@ public class Functions {
         };
     }
 
+
+    @Deprecated
     public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) {
 
         return new AbstractEventHandler() {
@@ -930,14 +954,10 @@ public class Functions {
                                        return (CommandResult)response;
                                }
                                return null;
-                       } catch (Throwable t) {
-                               t.printStackTrace();
                        } finally {
                                sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                        }
 
-                       return null;
-
                                } catch (Throwable e) {
                                        Logger.defaultLogError(e);
                                        return new org.simantics.document.server.serverResponse.Error(e.getMessage()); 
@@ -948,6 +968,7 @@ public class Functions {
         };
     }
 
+    @Deprecated
     public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) {
 
         return new AbstractEventHandler() {
@@ -974,8 +995,6 @@ public class Functions {
                                    if(response instanceof String) {
                                        result = (String)response;
                                    }
-                               } catch (Throwable t) {
-                                   t.printStackTrace();
                                } finally {
                                        sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
                                }
@@ -1271,4 +1290,27 @@ public class Functions {
        return graph.syncRequest(new PathExistsRequest(context));
     }
 
+    public static String compileDocumentSCLValueExpression(ReadGraph graph, Variable context) {
+        try {
+            ServerSCLValueRequest.validate(graph, context);
+            return "";
+        } catch (Exception e) {
+            return resolveIssueMessage(e);
+        }
+    }
+    
+    private static String resolveIssueMessage(Exception e) {
+        if (e instanceof ImportFailureException)
+            return "";
+        if (e.getCause() != null && e.getCause() instanceof ImportFailureException)
+            return "";
+        if (e instanceof SCLDatabaseException) {
+            SCLDatabaseException ee = (SCLDatabaseException) e;
+            return ee.getMessage();
+        }
+        if (LOGGER.isDebugEnabled())
+            LOGGER.debug("", e);
+        return e.getMessage();
+    }
+
 }
\ No newline at end of file