1 package org.simantics.document.server;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
9 import java.util.SortedMap;
10 import java.util.TreeMap;
12 import org.simantics.databoard.Bindings;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.request.BinaryRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.request.VariableRead;
18 import org.simantics.db.layer0.variable.ProxyChildVariable;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.document.base.ontology.DocumentationResource;
22 import org.simantics.document.server.Functions.RootVariable;
23 import org.simantics.document.server.request.NodeRequest;
24 import org.simantics.document.server.request.NodeRequestUtils;
25 import org.simantics.structural2.variables.Connection;
26 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
27 import org.simantics.utils.strings.AlphanumComparator;
29 public class DocumentServerUtils {
31 public static Collection<Variable> getChildren(ReadGraph graph, Variable variable) throws DatabaseException {
33 DocumentationResource DOC = DocumentationResource.getInstance(graph);
35 ArrayList<Variable> result = new ArrayList<Variable>();
36 for(Variable property : variable.getProperties(graph)) {
37 Collection<String> classifications = property.getPossiblePropertyValue(graph, Variables.CLASSIFICATIONS);
38 if(classifications != null) {
39 if(classifications.contains(DocumentationResource.URIs.Document_ChildRelation)) {
40 Connection conn = property.getValue(graph);
41 Variable childConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, property, conn);
42 if(childConnectionPoint != null) {
43 result.add(childConnectionPoint.getParent(graph));
45 } else if (DOC.Relations_partN.equals(property.getPossiblePredicateResource(graph))) {
46 Connection conn = property.getValue(graph);
47 for (Variable childConnectionPoint : DocumentServerUtils.getOtherConnectionPoints(graph, property, conn)) {
48 result.add(childConnectionPoint.getParent(graph));
57 public static String findManualOrdinal(ReadGraph graph, Variable v) throws DatabaseException {
58 DocumentationResource DOC = DocumentationResource.getInstance(graph);
60 while (j == null && v != null) {
61 j = v.getPossiblePropertyValue(graph, DOC.Components_Component_manualOrdinal);
62 v = v.getParent(graph);
65 return Integer.toString(j);
71 public static Collection<Variable> getChildrenInOrdinalOrder(ReadGraph graph, Variable variable) throws DatabaseException {
72 DocumentationResource DOC = DocumentationResource.getInstance(graph);
74 SortedMap<String, Variable> childMap = new TreeMap<String, Variable>(AlphanumComparator.COMPARATOR);
76 for(Variable property : variable.getProperties(graph, DocumentationResource.URIs.Document_ChildRelation)) {
77 Resource cp = property.getPossiblePredicateResource(graph);
78 String i = graph.getRelatedValue(cp, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
79 Connection conn = property.getValue(graph);
80 Variable childConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, property, conn);
81 if(childConnectionPoint != null) {
82 childMap.put(i, childConnectionPoint.getParent(graph));
86 Variable property = variable.getPossibleProperty(graph, "partN");
87 if(property != null) {
88 Connection conn = property.getValue(graph);
89 for (Variable childConnectionPoint : DocumentServerUtils.getOtherConnectionPoints(graph, property, conn)) {
90 Variable child = childConnectionPoint.getParent(graph);
91 String i = findManualOrdinal(graph, child);
95 childMap.put(i, child);
99 return childMap.values();
103 public static Collection<Variable> collectNodes(ReadGraph graph, Variable variable, Collection<Variable> nodes) throws DatabaseException {
105 DocumentationResource DOC = DocumentationResource.getInstance(graph);
107 Resource type = variable.getPossibleType(graph);
108 if(type == null) return nodes;
110 if(!graph.isInheritedFrom(type, DOC.Components_Component)) return nodes;
112 Boolean enabled = variable.getPossiblePropertyValue(graph, DOC.Properties_exists, Bindings.BOOLEAN);
113 if(enabled != null && !enabled) return nodes;
115 if(graph.isInheritedFrom(type, DOC.Components_PrimitiveComponent)) {
118 for(Variable child : variable.getChildren(graph)) {
119 collectNodes(graph, child, nodes);
127 public static Variable getPossibleOtherConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
129 Collection<VariableConnectionPointDescriptor> descs = conn.getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null);
130 if(descs.size() != 2) return null;
132 for(VariableConnectionPointDescriptor desc : descs) {
133 if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
134 return desc.getVariable(graph);
141 public static Variable getPossibleChildConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
143 Collection<VariableConnectionPointDescriptor> descs = conn.getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null);
144 if(descs.size() != 2) return null;
146 DocumentationResource DOC = DocumentationResource.getInstance(graph);
148 for(VariableConnectionPointDescriptor desc : descs) {
149 Resource res = desc.getConnectionPointResource(graph);
150 if(graph.isInstanceOf(res, DOC.Relations_parentRelation)) continue;
151 //if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
152 return desc.getVariable(graph);
159 public static Collection<Variable> getOtherConnectionPoints(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
161 ArrayList<Variable> connectionPoints = new ArrayList<Variable>();
163 Collection<VariableConnectionPointDescriptor> descs = conn.getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null);
165 for(VariableConnectionPointDescriptor desc : descs) {
166 if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
167 connectionPoints.add(desc.getVariable(graph));
170 return connectionPoints;
174 public static Variable getPossibleCommandTriggerConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
176 Collection<Variable> cpts = conn.getConnectionPoints(graph, connectionPoint.getParent(graph), null);
178 Variable result = null;
180 for(Variable cpt : cpts) {
181 Set<String> classifications = cpt.getClassifications(graph);
182 if(classifications.contains(DocumentationResource.URIs.Relations_commandExecutorRelation)) {
183 if(result != null) throw new DatabaseException("Multiple executor connection points in command connection");
192 public static Collection<Variable> getPossibleOtherConnectionPoints(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
194 Collection<Variable> cpts = conn.getConnectionPoints(graph, connectionPoint.getParent(graph), null);
196 return Collections.emptyList();
198 ArrayList<Variable> result = new ArrayList<Variable>();
199 for(Variable cpt : cpts) {
200 if(!cpt.equals(connectionPoint))
206 public static String getId(ReadGraph graph, Variable node) throws DatabaseException {
208 if(node == null) return "root";
210 String uri = node.getURI(graph);
211 int l = uri.lastIndexOf(ProxyChildVariable.CONTEXT_END);
212 return uri.substring(l+4);
217 public static Object getValue(ReadGraph graph, Variable attrib) throws DatabaseException {
218 return graph.syncRequest(new DocumentValue(attrib));
221 public static Variable getParentConnectionPoint(ReadGraph graph, Variable component) throws DatabaseException {
223 Variable connectionPoint = component.getPossibleProperty(graph, "parent");
224 if(connectionPoint == null) {
225 DocumentationResource DOC = DocumentationResource.getInstance(graph);
226 Collection<Variable> cps = component.getProperties(graph, DOC.Relations_parentRelation);
227 if(cps.size() == 1) {
228 connectionPoint = cps.iterator().next();
234 Connection conn = connectionPoint.getValue(graph);
235 Variable otherCp = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, connectionPoint, conn);
236 if (otherCp != null) {
239 Variable parentCp = graph.sync(new BinaryRead<Variable, Connection, Variable>(connectionPoint.getParent(graph), conn) {
241 public Variable perform(ReadGraph graph) throws DatabaseException {
242 DocumentationResource DOC = DocumentationResource.getInstance(graph);
243 Collection<VariableConnectionPointDescriptor> descs = parameter2.getConnectionPointDescriptors(graph, parameter, null);
245 for(VariableConnectionPointDescriptor desc : descs) {
246 if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) {
247 return desc.getVariable(graph);
253 if (parentCp != null) {
262 public static Collection<Variable> getChildConnections(ReadGraph graph, Variable variable) throws DatabaseException {
263 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_ChildRelation);
266 /* Command sequence */
267 public static Collection<Variable> getTriggerCommands(ReadGraph graph, Variable variable) throws DatabaseException {
268 ArrayList<Variable> result = new ArrayList<Variable>();
269 DocumentationResource DOC = DocumentationResource.getInstance(graph);
270 for(Variable var : variable.getProperties(graph, DOC.Document_CommandRelation)) {
271 if(DOC.Relations_broadcasted.equals(var.getPredicateResource(graph))) continue;
277 public static Collection<Variable> getCommands(ReadGraph graph, Variable variable) throws DatabaseException {
278 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_CommandRelation);
281 /* Data definition */
282 public static Collection<Variable> getDataDefinitions(ReadGraph graph, Variable variable) throws DatabaseException {
283 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_DataDefinitionRelation);
287 public static Collection<Variable> getDataRelations(ReadGraph graph, Variable variable) throws DatabaseException {
288 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_DataRelation);
292 public static Collection<Variable> getAttributes(ReadGraph graph, DocumentationResource DOC, Variable variable) throws DatabaseException {
293 return variable.getProperties(graph, DOC.Document_AttributeRelation);
296 public static class AttributesRequest extends VariableRead<JSONObject> {
298 public AttributesRequest(Variable variable) {
303 public JSONObject perform(ReadGraph graph) throws DatabaseException {
305 DocumentationResource DOC = DocumentationResource.getInstance(graph);
307 DocumentProperties properties = variable.getPropertyValue(graph, DOC.Properties_primitiveProperties);
309 return computeStatic(graph, variable, properties);
313 JSONObject computeStatic(ReadGraph graph, Variable variable, DocumentProperties statics) throws DatabaseException {
315 JSONObject base = graph.syncRequest(new org.simantics.document.server.request.DefaultFields(variable));
316 JSONObject object = base.clone();
318 for(String name : statics.getKeys(graph, variable)) {
320 if (name.equals(NodeRequest.PROPERTY_VALUE_EXCEPTIONS)) {
321 @SuppressWarnings("unchecked")
322 Map<String, Exception> exceptions = (Map<String, Exception>)statics.getValue(graph, variable, name);//(Map<String, Exception>)DocumentServerUtils.getValue(graph, attrib);
324 List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
325 if(errorList == null)
326 errorList = new ArrayList<String>();
328 for (Map.Entry<String, Exception> entry : exceptions.entrySet()) {
329 String errorMessage = NodeRequestUtils.formatErrorMessage(entry.getKey(), entry.getValue());
330 errorList.add(errorMessage);
332 object.addJSONField(NodeRequest.ERRORS, errorList);
335 object.addJSONField(name, statics.getValue(graph, variable, name));
337 } catch (Throwable t) {
338 List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
339 if(errorList == null)
340 errorList = new ArrayList<String>();
342 String errorMessage = NodeRequestUtils.formatErrorMessage(name, t);
344 errorList.add(errorMessage);
345 object.addJSONField(NodeRequest.ERRORS, errorList);
356 public static Collection<Variable> getDynamicAttributes(ReadGraph graph, final DocumentationResource DOC, Variable variable) throws DatabaseException {
357 return Collections.emptyList();
360 public static Variable getPossibleDocumentRootVariable(ReadGraph graph, Variable documentPart) throws DatabaseException {
361 if(documentPart instanceof RootVariable) return documentPart;
362 Variable parent = documentPart.getParent(graph);
363 if(parent == null) return null;
364 return getPossibleDocumentRootVariable(graph, parent);