+package org.simantics.document.linking.ge;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;\r
+import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.document.linking.ontology.DocumentLink;\r
+import org.simantics.document.linking.utils.SourceLinkUtil;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * Rule for browsing properties and their document links.\r
+ * \r
+ * The rule has two modes, one list all properties, and another lists only properties that have document links.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class PropertyChildRule implements org.simantics.browsing.ui.model.children.ChildRule{\r
+ \r
+ private boolean includeAllProps = true;\r
+ private boolean showOnlyCheckable = false;\r
+ \r
+ public PropertyChildRule() {\r
+ \r
+ }\r
+ \r
+ public PropertyChildRule(boolean includeAllProps) {\r
+ this.includeAllProps = includeAllProps;\r
+ }\r
+ \r
+ public void setShowOnlyCheckable(boolean showOnlyCheckable) {\r
+ this.showOnlyCheckable = showOnlyCheckable;\r
+ }\r
+ \r
+ @Override\r
+ public boolean isCompatible(Class<?> contentType) {\r
+ return contentType.equals(Resource.class) || contentType.equals(Variable.class);\r
+ }\r
+ \r
+ public void setIncludeAllProps(boolean includeAllProps) {\r
+ this.includeAllProps = includeAllProps;\r
+ }\r
+ \r
+ @Override\r
+ public Collection<?> getChildren(ReadGraph graph, Object obj)\r
+ throws DatabaseException {\r
+\r
+ ArrayList<Variable> children = new ArrayList<Variable>();\r
+ \r
+ boolean isResource = true;\r
+ Resource resource = null;\r
+ Variable variable = null;\r
+ if (obj instanceof Resource) {\r
+ resource = (Resource)obj;\r
+ isResource = true;\r
+ try {\r
+ variable = graph.adapt(resource, Variable.class);\r
+ } catch (Throwable t) {\r
+ return children;\r
+ }\r
+ } else {\r
+ variable = (Variable)obj;\r
+ resource = variable.getPossibleRepresents(graph);\r
+ isResource = false;\r
+ }\r
+ if (resource == null)\r
+ return children;\r
+ \r
+ DocumentLink sl = DocumentLink.getInstance(graph);\r
+ if (graph.isInstanceOf(resource, sl.Source))\r
+ return children;\r
+ if (!graph.hasValue(resource)) {\r
+ if (includeAllProps) {\r
+ // reading all objects returns both asserted and instance specific properties.\r
+ // we must filter out asserted properties if there is a equivalent instance property. \r
+ Map<Resource,Statement> selectedProps = getProps(graph, resource);\r
+ for (Statement s : selectedProps.values()) {\r
+ children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));\r
+ }\r
+ } else {\r
+ \r
+ Set<Resource> linked = new HashSet<Resource>();\r
+ Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);\r
+ for (Resource source : sources) {\r
+ if (showOnlyCheckable && \r
+ (SourceLinkUtil.isValidSource(graph, source) &&\r
+ SourceLinkUtil.isUpToDateSource(graph, source)))\r
+ continue;\r
+ Resource rel = graph.getPossibleObject(source, sl.consernsRelation);\r
+ linked.add(rel);\r
+ }\r
+ Map<Resource,Statement> selectedProps = getProps(graph, resource);\r
+ for (Statement s : selectedProps.values()) {\r
+ if (linked.contains(s.getPredicate()))\r
+ children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));\r
+ }\r
+ \r
+ }\r
+ }\r
+ if (!isResource) {\r
+ //find sources declared on parentResource using consernsRelation\r
+ Variable parentVariable = variable.getParent(graph);\r
+ Resource parentRes = null;\r
+ Resource relation = null;\r
+ if (parentVariable != null) {\r
+ parentRes = parentVariable.getPossibleRepresents(graph);\r
+ relation = variable.getPossiblePredicateResource(graph);\r
+ }\r
+ \r
+ if (parentRes != null && relation != null) {\r
+ Collection<Resource> sources = graph.getObjects(parentRes, sl.hasSource);\r
+ for (Resource source : sources) {\r
+ Resource rel = graph.getPossibleObject(source, sl.consernsRelation);\r
+ if (rel != null && rel.equals(relation)) {\r
+ if (showOnlyCheckable && \r
+ (SourceLinkUtil.isValidSource(graph, source) &&\r
+ SourceLinkUtil.isUpToDateSource(graph, source)))\r
+ continue;\r
+ children.add(new StandardGraphChildVariable(variable, null, source));\r
+ }\r
+ }\r
+ }\r
+ //find sources declared on this resource, ie. sources that do not have consernsRelation.\r
+ Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);\r
+ for (Resource source : sources) {\r
+ Resource rel = graph.getPossibleObject(source, sl.consernsRelation);\r
+ if (rel == null) {\r
+ if (showOnlyCheckable && \r
+ (SourceLinkUtil.isValidSource(graph, source) &&\r
+ SourceLinkUtil.isUpToDateSource(graph, source)))\r
+ continue;\r
+ children.add(new StandardGraphChildVariable(variable, null, source));\r
+ }\r
+ }\r
+ while (children.remove(variable)) {\r
+ \r
+ }\r
+ }\r
+ \r
+\r
+ return children;\r
+ }\r
+ \r
+ private Map<Resource,Statement> getProps(ReadGraph graph, Resource resource) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ Map<Resource, Statement> selectedProps = new HashMap<Resource, Statement>();\r
+ Collection<Statement> propertyStatements = graph.getStatements(resource, l0.HasProperty);\r
+ for (Statement s : propertyStatements) {\r
+ if (!graph.isInstanceOf(s.getObject(), l0.Literal) && !graph.isInstanceOf(s.getObject(), l0.SCLValue))\r
+ continue;\r
+ if (selectedProps.containsKey(s.getPredicate())) {\r
+ if (!s.isAsserted(resource))\r
+ selectedProps.put(s.getPredicate(), s);\r
+ } else {\r
+ selectedProps.put(s.getPredicate(), s);\r
+ }\r
+ }\r
+ return selectedProps;\r
+ }\r
+ \r
+ @Override\r
+ public Collection<?> getParents(ReadGraph graph, Object child)\r
+ throws DatabaseException {\r
+ return new ArrayList<Resource>();\r
+ }\r
+\r
+}\r