package org.simantics.document.server.request; import java.util.ArrayList; import java.util.List; import org.simantics.databoard.Bindings; import org.simantics.databoard.Databoard; import org.simantics.databoard.binding.Binding; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.request.VariableRead; import org.simantics.db.layer0.variable.Variable; import org.simantics.document.base.ontology.DocumentationResource; import org.simantics.document.server.DocumentServerUtils; import org.simantics.document.server.JSONObject; import org.simantics.layer0.Layer0; import org.simantics.structural.stubs.StructuralResource2; class NodeRequestStatic extends VariableRead { public NodeRequestStatic(Variable node) { super(node); } public static class Headers extends VariableRead { public Headers(Variable variable) { super(variable); } @Override public JSONObject perform(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); StructuralResource2.getInstance(graph); DocumentationResource DOC = DocumentationResource.getInstance(graph); String id = DocumentServerUtils.getId(graph, variable); Binding jsonBinding = graph.getService(Databoard.class).getBindingUnchecked(JSONObject.class); JSONObject object = new JSONObject(jsonBinding, id); Resource type = variable.getType(graph); Variable parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, variable); while (parentConnectionPoint != null) { Variable par = parentConnectionPoint.getParent(graph); if(!DOC.Components_DummyContainer.equals(par.getType(graph))) break; parentConnectionPoint = DocumentServerUtils.getParentConnectionPoint(graph, par); } if(parentConnectionPoint != null) { String parent = DocumentServerUtils.getId(graph, parentConnectionPoint.getParent(graph)); Resource cpPredicate = parentConnectionPoint.getPossiblePredicateResource(graph); String i = null; if (DOC.Relations_partN.equals(cpPredicate)) { i = DocumentServerUtils.findManualOrdinal(graph, variable); } else { i = graph.getPossibleRelatedValue(cpPredicate, DOC.Document_ChildRelation_ordinal, Bindings.STRING); } if (i == null) { i = "0"; } object.addJSONField("parent", parent); object.addJSONField("parentOrd", i); } else { if(DOC.Components_Root.equals(type) || graph.isInheritedFrom(type, DOC.Components_Root)) { object.addJSONField("parent", "root"); object.addJSONField("parentOrd", "0"); } if(!graph.isInheritedFrom(type, DOC.Components_ParentlessComponent)) { object.addJSONField("parent", ""); object.addJSONField("parentOrd", "0"); } } object.addJSONField("id", id); object.addJSONField("type", graph.getRelatedValue(type, L0.HasName, Bindings.STRING)); return object; } } @Override public JSONObject perform(ReadGraph graph) throws DatabaseException { long s = System.nanoTime(); JSONObject base = graph.syncRequest(new Headers(variable)); JSONObject object = base.clone(); DocumentationResource DOC = DocumentationResource.getInstance(graph); for(Variable attrib : DocumentServerUtils.getStaticAttributes(graph, DOC, variable)) { String name = attrib.getName(graph); try { Object value = DocumentServerUtils.getValue(graph, attrib); object.addJSONField(name, value); } catch (Throwable t) { List errorList = object.getJSONField(NodeRequest.ERRORS); if(errorList == null) errorList = new ArrayList(); String errorMessage = NodeRequestUtils.formatErrorMessage(name, t); errorList.add(errorMessage); object.addJSONField(NodeRequest.ERRORS, errorList); } } if(DocumentRequest.PROFILE) { long dura = System.nanoTime()-s; System.err.println("NodeRequestStatic " + System.identityHashCode(this) + " " + variable.getURI(graph) + " in " + 1e-6*dura + "ms." + object); } return object; } }