]> gerrit.simantics Code Review - simantics/platform.git/blob
8a65fb1fc33c93d12ce8d60ecfcab9ca59d59b52
[simantics/platform.git] /
1 package org.simantics.document.server.request;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.Databoard;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.request.VariableRead;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.document.base.ontology.DocumentationResource;
15 import org.simantics.document.server.DocumentServerUtils;
16 import org.simantics.document.server.JSONObject;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.structural.stubs.StructuralResource2;
19
20 class NodeRequestStatic extends VariableRead<JSONObject> {
21
22         public NodeRequestStatic(Variable node) {
23                 super(node);
24         }
25
26         public static class Headers extends VariableRead<JSONObject> {
27
28                 public Headers(Variable variable) {
29                         super(variable);
30                 }
31
32                 @Override
33                 public JSONObject perform(ReadGraph graph) throws DatabaseException {
34
35                         Layer0 L0 = Layer0.getInstance(graph);
36                         StructuralResource2.getInstance(graph);
37                         DocumentationResource DOC = DocumentationResource.getInstance(graph);
38
39                         String id = DocumentServerUtils.getId(graph, variable);
40                         
41                         Binding jsonBinding = graph.getService(Databoard.class).getBindingUnchecked(JSONObject.class);
42                         JSONObject object = new JSONObject(jsonBinding, id);
43
44                         Resource type = variable.getType(graph);
45
46                         Variable parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, variable);
47                         while (parentConnectionPoint != null) {
48                                 Variable par = parentConnectionPoint.getParent(graph);
49                                 if(!DOC.Components_DummyContainer.equals(par.getType(graph))) break;
50                                 parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, par);
51                         }
52                         
53                         if(parentConnectionPoint != null) {
54                                 String parent = DocumentServerUtils.getId(graph, parentConnectionPoint.getParent(graph));
55                                 Resource cpPredicate = parentConnectionPoint.getPossiblePredicateResource(graph);
56                                 String i = null;
57                                 if (DOC.Relations_partN.equals(cpPredicate)) {
58                                         i = DocumentServerUtils.findManualOrdinal(graph, variable);
59                                 } else {
60                                         i = graph.getPossibleRelatedValue(cpPredicate, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
61                                 }
62                                 if (i == null) {
63                                         i = "0";
64                                 }
65                                 
66                                 object.addJSONField("parent", parent);
67                                 object.addJSONField("parentOrd", i);
68                         } else {
69                             if(DOC.Components_Root.equals(type) || graph.isInheritedFrom(type, DOC.Components_Root)) {
70                                 object.addJSONField("parent", "root");
71                                 object.addJSONField("parentOrd", "0");
72                             } if(!graph.isInheritedFrom(type, DOC.Components_ParentlessComponent)) {
73                                 object.addJSONField("parent", "");
74                                 object.addJSONField("parentOrd", "0");
75                             }
76                         }
77
78                         object.addJSONField("id", id);
79                         object.addJSONField("type", graph.<String>getRelatedValue(type, L0.HasName, Bindings.STRING));
80                         return object;
81                         
82                 }
83                 
84         }
85         
86         @Override
87         public JSONObject perform(ReadGraph graph) throws DatabaseException {
88
89                 long s = System.nanoTime();
90
91                 JSONObject base = graph.syncRequest(new Headers(variable));
92                 JSONObject object = base.clone();
93                 
94                 DocumentationResource DOC = DocumentationResource.getInstance(graph);
95                 
96                 for(Variable attrib : DocumentServerUtils.getStaticAttributes(graph, DOC, variable)) {
97                         String name = attrib.getName(graph);
98                         try {
99                             Object value = DocumentServerUtils.getValue(graph, attrib);
100                             object.addJSONField(name, value);
101                         } catch (Throwable t) {
102                             List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
103                             if(errorList == null)
104                                 errorList = new ArrayList<String>();
105                             
106                     String errorMessage = NodeRequestUtils.formatErrorMessage(name, t);
107                     
108                             errorList.add(errorMessage);
109                             object.addJSONField(NodeRequest.ERRORS, errorList);
110                         }
111
112                 }
113
114         if(DocumentRequest.PROFILE) {
115             long dura = System.nanoTime()-s;
116             System.err.println("NodeRequestStatic " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms." + object);
117         }
118                 
119                 return object;
120
121         }               
122
123 }