]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequestBase.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / ServerSCLValueRequestBase.java
1 package org.simantics.document.server.request;
2
3 import org.simantics.db.ReadGraph;
4 import org.simantics.db.Resource;
5 import org.simantics.db.common.request.IndexRoot;
6 import org.simantics.db.common.request.PossibleTypedParent;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext;
9 import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest;
10 import org.simantics.document.base.ontology.DocumentationResource;
11 import org.simantics.structural2.scl.FindPossibleComponentTypeRequest;
12 import org.simantics.utils.datastructures.Pair;
13
14 abstract public class ServerSCLValueRequestBase<CompilationContext extends AbstractExpressionCompilationContext> extends AbstractExpressionCompilationRequest<CompilationContext, Object> {
15
16     static Pair<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component, Resource literal)  throws DatabaseException {
17         if(component != null) {
18             Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component));
19             if(type != null) {
20                 Resource root = graph.syncRequest(new IndexRoot(type));
21                 return Pair.make(type, root);
22             } else {
23                 Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document));
24                 if(doc != null) {
25                     Resource componentType = graph.getSingleType(doc);
26                     Resource root = graph.syncRequest(new IndexRoot(doc));
27                     return Pair.make(componentType, root);
28                 } else {
29                     Resource root = graph.syncRequest(new IndexRoot(component));
30                     return Pair.make(null, root);
31                 }
32             }
33             // TODO: For Antti to consider and fix later
34             // Introduced to handle procedural user components where component == null
35         } else if (literal != null) {
36             Resource root = graph.syncRequest(new IndexRoot(literal));
37             return Pair.make(null, root);
38         } else {
39             throw new DatabaseException("Couldn't resolve component type and root for component == null && literal == null");
40         }
41     }
42
43 }