1 package org.simantics.document.server.request;
3 import java.util.ArrayList;
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;
20 class NodeRequestStatic extends VariableRead<JSONObject> {
22 public NodeRequestStatic(Variable node) {
26 public static class Headers extends VariableRead<JSONObject> {
28 public Headers(Variable variable) {
33 public JSONObject perform(ReadGraph graph) throws DatabaseException {
35 Layer0 L0 = Layer0.getInstance(graph);
36 StructuralResource2.getInstance(graph);
37 DocumentationResource DOC = DocumentationResource.getInstance(graph);
39 String id = DocumentServerUtils.getId(graph, variable);
41 Binding jsonBinding = graph.getService(Databoard.class).getBindingUnchecked(JSONObject.class);
42 JSONObject object = new JSONObject(jsonBinding, id);
44 Resource type = variable.getType(graph);
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);
53 if(parentConnectionPoint != null) {
54 String parent = DocumentServerUtils.getId(graph, parentConnectionPoint.getParent(graph));
55 Resource cpPredicate = parentConnectionPoint.getPossiblePredicateResource(graph);
57 if (DOC.Relations_partN.equals(cpPredicate)) {
58 i = DocumentServerUtils.findManualOrdinal(graph, variable);
60 i = graph.getPossibleRelatedValue(cpPredicate, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
66 object.addJSONField("parent", parent);
67 object.addJSONField("parentOrd", i);
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");
78 object.addJSONField("id", id);
79 object.addJSONField("type", graph.<String>getRelatedValue(type, L0.HasName, Bindings.STRING));
87 public JSONObject perform(ReadGraph graph) throws DatabaseException {
89 long s = System.nanoTime();
91 JSONObject base = graph.syncRequest(new Headers(variable));
92 JSONObject object = base.clone();
94 DocumentationResource DOC = DocumentationResource.getInstance(graph);
96 for(Variable attrib : DocumentServerUtils.getStaticAttributes(graph, DOC, variable)) {
97 String name = attrib.getName(graph);
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>();
106 String errorMessage = NodeRequestUtils.formatErrorMessage(name, t);
108 errorList.add(errorMessage);
109 object.addJSONField(NodeRequest.ERRORS, errorList);
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);