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.UnaryRead;
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.request.NodeRequest;
23 import org.simantics.document.server.request.NodeRequestUtils;
24 import org.simantics.structural2.variables.Connection;
25 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
26 import org.simantics.utils.strings.AlphanumComparator;
28 public class DocumentServerUtils {
30 public static Collection<Variable> getChildren(ReadGraph graph, Variable variable) throws DatabaseException {
32 DocumentationResource DOC = DocumentationResource.getInstance(graph);
34 ArrayList<Variable> result = new ArrayList<Variable>();
35 for(Variable property : variable.getProperties(graph)) {
36 Collection<String> classifications = property.getPossiblePropertyValue(graph, Variables.CLASSIFICATIONS);
37 if(classifications != null) {
38 if(classifications.contains(DocumentationResource.URIs.Document_ChildRelation)) {
39 Connection conn = property.getValue(graph);
40 Variable childConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, property, conn);
41 if(childConnectionPoint != null) {
42 result.add(childConnectionPoint.getParent(graph));
44 } else if (DOC.Relations_partN.equals(property.getPossiblePredicateResource(graph))) {
45 Connection conn = property.getValue(graph);
46 for (Variable childConnectionPoint : DocumentServerUtils.getOtherConnectionPoints(graph, property, conn)) {
47 result.add(childConnectionPoint.getParent(graph));
56 public static String findManualOrdinal(ReadGraph graph, Variable v) throws DatabaseException {
57 DocumentationResource DOC = DocumentationResource.getInstance(graph);
59 while (j == null && v != null) {
60 j = v.getPossiblePropertyValue(graph, DOC.Components_Component_manualOrdinal);
61 v = v.getParent(graph);
64 return Integer.toString(j);
70 public static Collection<Variable> getChildrenInOrdinalOrder(ReadGraph graph, Variable variable) throws DatabaseException {
71 DocumentationResource DOC = DocumentationResource.getInstance(graph);
73 SortedMap<String, Variable> childMap = new TreeMap<String, Variable>(AlphanumComparator.COMPARATOR);
75 for(Variable property : variable.getProperties(graph, DocumentationResource.URIs.Document_ChildRelation)) {
76 Resource cp = property.getPossiblePredicateResource(graph);
77 String i = graph.getRelatedValue(cp, DOC.Document_ChildRelation_ordinal, Bindings.STRING);
78 Connection conn = property.getValue(graph);
79 Variable childConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, property, conn);
80 if(childConnectionPoint != null) {
81 childMap.put(i, childConnectionPoint.getParent(graph));
85 Variable property = variable.getPossibleProperty(graph, "partN");
86 if(property != null) {
87 Connection conn = property.getValue(graph);
88 for (Variable childConnectionPoint : DocumentServerUtils.getOtherConnectionPoints(graph, property, conn)) {
89 Variable child = childConnectionPoint.getParent(graph);
90 String i = findManualOrdinal(graph, child);
94 childMap.put(i, child);
98 return childMap.values();
102 public static Collection<Variable> collectNodes(ReadGraph graph, Variable variable, Collection<Variable> nodes) throws DatabaseException {
104 DocumentationResource DOC = DocumentationResource.getInstance(graph);
106 Resource type = variable.getPossibleType(graph);
107 if(type == null) return nodes;
109 if(!graph.isInheritedFrom(type, DOC.Components_Component)) return nodes;
111 Boolean enabled = variable.getPossiblePropertyValue(graph, DOC.Properties_exists, Bindings.BOOLEAN);
112 if(enabled != null && !enabled) return nodes;
114 if(graph.isInheritedFrom(type, DOC.Components_PrimitiveComponent)) {
117 for(Variable child : variable.getChildren(graph)) {
118 collectNodes(graph, child, nodes);
126 public static Variable getPossibleOtherConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
128 Collection<VariableConnectionPointDescriptor> descs = conn.getConnection2().getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null);
129 if(descs.size() != 2) return null;
131 for(VariableConnectionPointDescriptor desc : descs) {
132 if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
133 return desc.getVariable(graph);
140 public static Variable getPossibleChildConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
142 Collection<VariableConnectionPointDescriptor> descs = conn.getConnection2().getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null);
143 if(descs.size() != 2) return null;
145 DocumentationResource DOC = DocumentationResource.getInstance(graph);
147 for(VariableConnectionPointDescriptor desc : descs) {
148 Resource res = desc.getConnectionPointResource(graph);
149 if(graph.isInstanceOf(res, DOC.Relations_parentRelation)) continue;
150 //if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
151 return desc.getVariable(graph);
158 public static Collection<Variable> getOtherConnectionPoints(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
160 ArrayList<Variable> connectionPoints = new ArrayList<Variable>();
162 Collection<VariableConnectionPointDescriptor> descs = conn.getConnectionPointDescriptors(graph, null);
164 for(VariableConnectionPointDescriptor desc : descs) {
165 if(desc.isFlattenedFrom(graph, connectionPoint)) continue;
166 connectionPoints.add(desc.getVariable(graph));
169 return connectionPoints;
173 public static Variable getPossibleCommandTriggerConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
175 Collection<Variable> cpts = conn.getConnection2().getConnectionPoints(graph, connectionPoint.getParent(graph), null);
177 Variable result = null;
179 for(Variable cpt : cpts) {
180 Set<String> classifications = cpt.getClassifications(graph);
181 if(classifications.contains(DocumentationResource.URIs.Relations_commandExecutorRelation)) {
182 if(result != null) throw new DatabaseException("Multiple executor connection points in command connection");
191 public static Collection<Variable> getPossibleOtherConnectionPoints(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException {
193 Collection<Variable> cpts = conn.getConnection2().getConnectionPoints(graph, connectionPoint.getParent(graph), null);
195 return Collections.emptyList();
197 ArrayList<Variable> result = new ArrayList<Variable>();
198 for(Variable cpt : cpts) {
199 if(!cpt.equals(connectionPoint))
205 public static String getId(ReadGraph graph, Variable node) throws DatabaseException {
207 if(node == null) return "root";
209 String name = node.getName(graph);
210 if(ProxyChildVariable.CONTEXT_END.equals(name)) return "";
212 String parentId = getId(graph, node.getParent(graph));
213 if(parentId.isEmpty()) return name;
214 else return parentId + "/" + name;
221 public static Object getValue(ReadGraph graph, Variable attrib) throws DatabaseException {
222 return graph.syncRequest(new DocumentValue(attrib));
225 public static Variable getParentConnectionPoint(ReadGraph graph, Variable component) throws DatabaseException {
227 Variable connectionPoint = component.getPossibleProperty(graph, "parent");
228 if(connectionPoint == null) {
229 DocumentationResource DOC = DocumentationResource.getInstance(graph);
230 Collection<Variable> cps = component.getProperties(graph, DOC.Relations_parentRelation);
231 if(cps.size() == 1) {
232 connectionPoint = cps.iterator().next();
238 Connection conn = connectionPoint.getValue(graph);
239 Variable otherCp = DocumentServerUtils.getPossibleOtherConnectionPoint(graph, connectionPoint, conn);
240 if (otherCp != null) {
243 Variable parentCp = graph.sync(new UnaryRead<Connection, Variable>(conn) {
245 public Variable perform(ReadGraph graph) throws DatabaseException {
246 DocumentationResource DOC = DocumentationResource.getInstance(graph);
247 Collection<VariableConnectionPointDescriptor> descs = parameter.getConnectionPointDescriptors(graph, null);
249 for(VariableConnectionPointDescriptor desc : descs) {
250 if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) {
251 return desc.getVariable(graph);
257 if (parentCp != null) {
266 public static Collection<Variable> getChildConnections(ReadGraph graph, Variable variable) throws DatabaseException {
267 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_ChildRelation);
270 /* Command sequence */
271 public static Collection<Variable> getTriggerCommands(ReadGraph graph, Variable variable) throws DatabaseException {
272 ArrayList<Variable> result = new ArrayList<Variable>();
273 DocumentationResource DOC = DocumentationResource.getInstance(graph);
274 for(Variable var : variable.getProperties(graph, DOC.Document_CommandRelation)) {
275 if(DOC.Relations_broadcasted.equals(var.getPredicateResource(graph))) continue;
281 public static Collection<Variable> getCommands(ReadGraph graph, Variable variable) throws DatabaseException {
282 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_CommandRelation);
285 /* Data definition */
286 public static Collection<Variable> getDataDefinitions(ReadGraph graph, Variable variable) throws DatabaseException {
287 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_DataDefinitionRelation);
291 public static Collection<Variable> getDataRelations(ReadGraph graph, Variable variable) throws DatabaseException {
292 return variable.getProperties(graph, DocumentationResource.getInstance(graph).Document_DataRelation);
296 public static Collection<Variable> getAttributes(ReadGraph graph, DocumentationResource DOC, Variable variable) throws DatabaseException {
297 return variable.getProperties(graph, DOC.Document_AttributeRelation);
300 public static class AttributesRequest extends VariableRead<JSONObject> {
302 public AttributesRequest(Variable variable) {
307 public JSONObject perform(ReadGraph graph) throws DatabaseException {
308 DocumentProperties properties = variable.getPropertyValue(graph, "primitiveProperties");
309 return computeStatic(graph, variable, properties);
312 JSONObject computeStatic(ReadGraph graph, Variable variable, DocumentProperties statics) throws DatabaseException {
314 JSONObject base = graph.syncRequest(new org.simantics.document.server.request.DefaultFields(variable));
315 JSONObject object = base.clone();
317 for(String name : statics.getKeys(graph, variable)) {
319 if (name.equals(NodeRequest.PROPERTY_VALUE_EXCEPTIONS)) {
320 @SuppressWarnings("unchecked")
321 Map<String, Exception> exceptions = (Map<String, Exception>)statics.getValue(graph, variable, name);
323 List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
324 if(errorList == null)
325 errorList = new ArrayList<>();
327 for (Map.Entry<String, Exception> entry : exceptions.entrySet()) {
328 String errorMessage = NodeRequestUtils.formatErrorMessage(entry.getKey(), entry.getValue());
329 errorList.add(errorMessage);
331 object.addJSONField(NodeRequest.ERRORS, errorList);
333 object.addJSONField(name, statics.getValue(graph, variable, name));
335 } catch (Throwable t) {
336 List<String> errorList = object.getJSONField(NodeRequest.ERRORS);
337 if(errorList == null)
338 errorList = new ArrayList<>(1);
340 String errorMessage = NodeRequestUtils.formatErrorMessage(name, t);
341 errorList.add(errorMessage);
342 object.addJSONField(NodeRequest.ERRORS, errorList);
353 public static Collection<Variable> getDynamicAttributes(ReadGraph graph, final DocumentationResource DOC, Variable variable) throws DatabaseException {
354 return Collections.emptyList();
357 public static Variable getPossibleDocumentRootVariable(ReadGraph graph, Variable documentPart) throws DatabaseException {
358 if(ProxyChildVariable.CONTEXT_END.equals(documentPart.getName(graph))) return documentPart;
359 Variable parent = documentPart.getParent(graph);
360 if(parent == null) return null;
361 return getPossibleDocumentRootVariable(graph, parent);