]> gerrit.simantics Code Review - simantics/platform.git/blob - server/request/DefaultFields.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / server / request / DefaultFields.java
1 package org.simantics.document.server.request;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.databoard.Databoard;
5 import org.simantics.databoard.binding.Binding;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.request.VariableRead;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.document.base.ontology.DocumentationResource;
12 import org.simantics.document.server.DocumentServerUtils;
13 import org.simantics.document.server.JSONObject;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.structural.stubs.StructuralResource2;
16
17 /**
18  * @author Antti Villberg
19  * @since 1.30.0
20  */
21 public class DefaultFields extends VariableRead<JSONObject> {
22
23     public DefaultFields(Variable variable) {
24         super(variable);
25     }
26
27     @Override
28     public JSONObject perform(ReadGraph graph) throws DatabaseException {
29
30         Layer0 L0 = Layer0.getInstance(graph);
31         StructuralResource2.getInstance(graph);
32         DocumentationResource DOC = DocumentationResource.getInstance(graph);
33
34         String id = DocumentServerUtils.getId(graph, variable);
35
36         Binding jsonBinding = graph.getService(Databoard.class).getBindingUnchecked(JSONObject.class);
37         JSONObject object = new JSONObject(jsonBinding, id);
38
39         Resource type = variable.getType(graph);
40
41         Variable parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, variable);
42         while (parentConnectionPoint != null) {
43             Variable par = parentConnectionPoint.getParent(graph);
44             if(!DOC.Components_DummyContainer.equals(par.getType(graph))) break;
45             parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, par);
46         }
47
48         if(parentConnectionPoint != null) {
49             String parent = DocumentServerUtils.getId(graph, parentConnectionPoint.getParent(graph));
50             Resource cpPredicate = parentConnectionPoint.getPossiblePredicateResource(graph);
51             String i = null;
52             if (DOC.Relations_partN.equals(cpPredicate)) {
53                 i = DocumentServerUtils.findManualOrdinal(graph, variable);
54             } else {
55                 i = graph.getPossibleRelatedValue(cpPredicate, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
56             }
57             if (i == null) {
58                 i = "0";
59             }
60
61             object.addJSONField("parent", parent);
62             object.addJSONField("parentOrd", i);
63         } else {
64             if(DOC.Components_Root.equals(type) || graph.isInheritedFrom(type, DOC.Components_Root)) {
65                 object.addJSONField("parent", "root");
66                 object.addJSONField("parentOrd", "0");
67             } if(!graph.isInheritedFrom(type, DOC.Components_ParentlessComponent)) {
68                 object.addJSONField("parent", "");
69                 object.addJSONField("parentOrd", "0");
70             }
71         }
72
73         object.addJSONField("id", id);
74         object.addJSONField("type", graph.<String>getRelatedValue(type, L0.HasName, Bindings.STRING));
75         return object;
76
77     }
78
79 }