]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java
Simantics Console
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequest.java
index 09f1523a2ede4ba26ff3c4d9a52df435647d302f..adff8dab5b9ec87d1271e1ca02d1d3bea58e49a3 100644 (file)
@@ -1,14 +1,17 @@
 package org.simantics.document.server.request;
 
+import java.util.Collections;
 import java.util.Map;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.Statement;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
 import org.simantics.db.common.request.IndexRoot;
 import org.simantics.db.common.request.PossibleTypedParent;
 import org.simantics.db.common.request.UnaryRead;
+import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
@@ -70,7 +73,7 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
        }
 
        public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
-           this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p));
+           this(getComponentTypeAndRoot(graph, s, o), o, resolveExpectedValueType(graph, p));
        }
 
        private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Variable property)  throws DatabaseException {
@@ -88,7 +91,7 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                return Pair.make(parent.getType(graph), root);
        }
 
-       private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
+       private static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component, Resource literal)  throws DatabaseException {
                if(component != null) {
                        Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
                        if(type != null) {
@@ -102,14 +105,20 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                                        Resource root = graph.syncRequest(new IndexRoot(doc));
                                        return Pair.make(componentType, root);
                                } else {
-                                       System.err.println("component = " + component);
+                                       //System.err.println("component = " + component);
                                        Resource root = graph.syncRequest(new IndexRoot(component));
 //                                     Resource componentType = graph.getSingleType(doc);
                                        return Pair.make(null, root);
                                }
                        }
-               }
-               throw new IllegalStateException();
+        // TODO: For Antti to consider and fix later
+        // Introduced to handle procedural user components where component == null
+        } else if (literal != null) {
+            Resource root = graph.syncRequest(new IndexRoot(literal));
+            return Pair.make(null, root);
+        } else {
+            throw new DatabaseException("Couldn't resolve component type and root for component == null && literal == null");
+        }
        }
 
        public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
@@ -160,9 +169,16 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
                        public CompilationContext perform(ReadGraph graph)
                                        throws DatabaseException {
                                RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second));
-                               Map<String, ComponentTypeProperty> propertyMap =
+                               Map<String, ComponentTypeProperty> propertyMap;
+                               if (parameter.first != null) {
+                                   propertyMap =
                                                graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
                                                                TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                               } else {
+                                   // TODO: Antti to consider
+                                   // To handle procedural user components
+                                   propertyMap = Collections.emptyMap();
+                               }
                                return new CompilationContext(runtimeEnvironment, propertyMap);
                        }
                });
@@ -260,6 +276,20 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest<
     public static Function1<Object, Object> validate(ReadGraph graph, Variable context) throws DatabaseException {
         return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.instance());
     }
+
+    @Override
+    protected String getContextDescription(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        Statement possibleOwner = graph.getPossibleStatement(literal, L0.IsOwnedBy);
+        if(possibleOwner != null) {
+            String uri = graph.getPossibleURI(possibleOwner.getObject());
+            if(uri != null) {
+                String propertyName = NameUtils.getSafeName(graph, graph.getInverse(possibleOwner.getPredicate()));
+                return uri + "#" + propertyName;
+            }
+        }
+        return super.getContextDescription(graph);
+    }
     
     public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest {