]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / Functions.java
index 6ec2e6734a5a70808dcb02d6ef9e05c43a3a97ef..8dea97ad118da8235b185d56b0e0ce539d9d7f0a 100644 (file)
@@ -8,6 +8,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import org.simantics.Simantics;
@@ -15,6 +16,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.databoard.Datatypes;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.type.Datatype;
+import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.DirectStatements;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.RequestProcessor;
@@ -23,13 +25,16 @@ import org.simantics.db.Session;
 import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.primitiverequest.Adapter;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.BinaryRead;
+import org.simantics.db.common.request.ResourceAsyncRead;
 import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.request.WriteResultRequest;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ServiceException;
 import org.simantics.db.layer0.function.All;
 import org.simantics.db.layer0.request.ProjectModels;
 import org.simantics.db.layer0.request.PropertyInfo;
@@ -49,6 +54,7 @@ import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.VariableMap;
 import org.simantics.db.layer0.variable.VariableMapImpl;
 import org.simantics.db.layer0.variable.Variables;
+import org.simantics.db.procedure.AsyncProcedure;
 import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.document.base.ontology.DocumentationResource;
 import org.simantics.document.server.bean.Command;
@@ -158,8 +164,8 @@ public class Functions {
                         StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
                         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;
                         }
-                        continue;
                     }
                     Resource predicate = property.getPossiblePredicateResource(graph);
                     if(predicate != null) {
@@ -212,28 +218,113 @@ public class Functions {
 
     };
 
-    static class StandardDocumentProperties implements DocumentProperties {
+    static class DocumentPropertyKeys extends ResourceAsyncRead<List<String>> {
+
+        protected DocumentPropertyKeys(Resource resource) {
+            super(resource);
+        }
 
         @Override
-        public Collection<String> getKeys(ReadGraph graph, Variable context) throws DatabaseException {
-            
+        public void perform(AsyncReadGraph graph, final AsyncProcedure<List<String>> procedure) {
+
+            final List<String> result = new ArrayList<>();
+
             DocumentationResource DOC = DocumentationResource.getInstance(graph);
-            StandardGraphPropertyVariable asd = new StandardGraphPropertyVariable(graph, context, DOC.Properties_primitiveProperties);
-            Map<String, Variable> ps = primitiveProperties.getVariables(graph, asd, null);
-            return ps.keySet();
+
+            try {
+                if(graph.hasStatement(resource, DOC.Properties_commands))
+                    result.add("commands");
+                if(graph.hasStatement(resource, DOC.Properties_dataDefinitions))
+                    result.add("dataDefinitions");
+            } catch(ServiceException e) {
+                LOGGER.info(e.getMessage(), e);
+            }
             
+            graph.forEachDirectPredicate(resource, new AsyncProcedure<Set<Resource>>() {
+
+                @Override
+                public void execute(AsyncReadGraph graph, Set<Resource> predicates) {
+                    
+                    for(Resource predicate : predicates) {
+
+                        try {
+
+                            PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+
+                            if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                                result.add(info.name);
+                            } else {
+                                Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
+                                if(definition != null) {
+                                    PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
+                                    result.add(info2.name);
+                                }
+                            }
+
+                        } catch (DatabaseException e) {
+                            LOGGER.info(e.getMessage(), e);
+                        }
+
+                    }
+
+                    procedure.execute(graph, result);
+                    
+                }
+
+                @Override
+                public void exception(AsyncReadGraph graph, Throwable throwable) {
+                    LOGGER.info(throwable.getMessage(), throwable);
+                    procedure.exception(graph, throwable);
+                }
+                
+            });
+
+        }
+
+    }
+
+    static class StandardDocumentProperties implements DocumentProperties {
+
+        @Override
+        public Collection<String> getKeys(ReadGraph graph, Variable parent) throws DatabaseException {
+
+            if(parent instanceof StandardProceduralChildVariable) {
+
+                StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent;
+                List<String> result = new ArrayList<>();
+                for(Variable property : procedural.getProperties(graph)) {
+                    if(property instanceof StandardAssertedGraphPropertyVariable) {
+                        StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
+                        if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)  || "pollingFunction".equals(ass.property.name)) {
+                            result.add(ass.property.name);
+                            continue;
+                        }
+                    }
+                    Resource predicate = property.getPossiblePredicateResource(graph);
+                    if(predicate != null) {
+                        PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
+                        if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
+                            result.add(info.name);
+                        }
+                    }
+                }
+
+                return result;
+
+            } else {
+
+                Resource parentRes = parent.getRepresents(graph);
+                return graph.syncRequest(new DocumentPropertyKeys(parentRes), TransientCacheAsyncListener.instance());
+
+            }
+
         }
 
         @Override
         public Object getValue(ReadGraph graph, Variable context, String key) throws DatabaseException {
-            
-            DocumentationResource DOC = DocumentationResource.getInstance(graph);
-            StandardGraphPropertyVariable asd = new StandardGraphPropertyVariable(graph, context, DOC.Properties_primitiveProperties);
-            Map<String, Variable> ps = primitiveProperties.getVariables(graph, asd, null);
-            return ps.get(key).getValue(graph);
-            
+            return context.getPropertyValue(graph, key);
         }
-        
+
     }
     
     public static DocumentProperties primitiveProperties() throws DatabaseException {
@@ -1290,6 +1381,15 @@ public class Functions {
         }
     }
     
+    public static String compileDocumentSCLHandlerValueExpression(ReadGraph graph, Variable context) {
+        try {
+            ServerSCLHandlerValueRequest.compile(graph, context);
+            return "";
+        } catch (Exception e) {
+            return resolveIssueMessage(e);
+        }
+    }
+
     private static String resolveIssueMessage(Exception e) {
         if (e instanceof ImportFailureException)
             return "";