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