]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/ge/PropertyChildRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / ge / PropertyChildRule.java
1 package org.simantics.document.linking.ge;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.Statement;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
15 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.document.linking.ontology.DocumentLink;
18 import org.simantics.document.linking.utils.SourceLinkUtil;
19 import org.simantics.layer0.Layer0;
20
21 /**
22  * Rule for browsing properties and their document links.
23  * 
24  * The rule has two modes, one list all properties, and another lists only properties that have document links.
25  * 
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
27  *
28  */
29 public class PropertyChildRule implements org.simantics.browsing.ui.model.children.ChildRule{
30         
31         private boolean includeAllProps = true;
32         private boolean showOnlyCheckable = false;
33         
34         public PropertyChildRule() {
35                 
36         }
37         
38         public PropertyChildRule(boolean includeAllProps) {
39                 this.includeAllProps = includeAllProps;
40         }
41         
42         public void setShowOnlyCheckable(boolean showOnlyCheckable) {
43                 this.showOnlyCheckable = showOnlyCheckable;
44         }
45         
46         @Override
47         public boolean isCompatible(Class<?> contentType) {
48                 return contentType.equals(Resource.class) || contentType.equals(Variable.class);
49         }
50         
51         public void setIncludeAllProps(boolean includeAllProps) {
52                 this.includeAllProps = includeAllProps;
53         }
54         
55         @Override
56         public Collection<?> getChildren(ReadGraph graph, Object obj)
57                         throws DatabaseException {
58
59                 ArrayList<Variable> children = new ArrayList<Variable>();
60                 
61                 boolean isResource = true;
62                 Resource resource = null;
63                 Variable variable = null;
64                 if (obj instanceof Resource) {
65                         resource = (Resource)obj;
66                         isResource = true;
67                         try {
68                                 variable = graph.adapt(resource, Variable.class);
69                         } catch (Throwable t) {
70                                 return children;
71                         }
72                 } else {
73                         variable = (Variable)obj;
74                         resource = variable.getPossibleRepresents(graph);
75                         isResource = false;
76                 }
77                 if (resource == null)
78                         return children;
79         
80                 DocumentLink sl = DocumentLink.getInstance(graph);
81                 if (graph.isInstanceOf(resource, sl.Source))
82                         return children;
83                 if (!graph.hasValue(resource)) {
84                         if (includeAllProps) {
85                                 // reading all objects returns both asserted and instance specific properties.
86                                 // we must filter out asserted properties if there is a equivalent instance property. 
87                                 Map<Resource,Statement> selectedProps = getProps(graph, resource);
88                                 for (Statement s : selectedProps.values()) {
89                                         children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));
90                                 }
91                         } else {
92                                 
93                                 Set<Resource> linked = new HashSet<Resource>();
94                                 Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);
95                                 for (Resource source : sources) {
96                                         if (showOnlyCheckable && 
97                                            (SourceLinkUtil.isValidSource(graph, source) &&
98                                             SourceLinkUtil.isUpToDateSource(graph, source)))
99                                                 continue;
100                                         Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
101                                         linked.add(rel);
102                                 }
103                                 Map<Resource,Statement> selectedProps = getProps(graph, resource);
104                                 for (Statement s : selectedProps.values()) {
105                                         if (linked.contains(s.getPredicate()))
106                                                 children.add(new StandardGraphPropertyVariable(graph, variable, s.getPredicate()));
107                                 }
108                                         
109                         }
110                 }
111                 if (!isResource) {
112                         //find sources declared on parentResource using consernsRelation
113                         Variable parentVariable = variable.getParent(graph);
114                         Resource parentRes = null;
115                         Resource relation = null;
116                         if (parentVariable != null) {
117                                 parentRes = parentVariable.getPossibleRepresents(graph);
118                                 relation = variable.getPossiblePredicateResource(graph);
119                         }
120                         
121                         if (parentRes != null && relation != null) {
122                                 Collection<Resource> sources = graph.getObjects(parentRes, sl.hasSource);
123                                 for (Resource source : sources) {
124                                         Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
125                                         if (rel != null && rel.equals(relation)) {
126                                                 if (showOnlyCheckable && 
127                                                    (SourceLinkUtil.isValidSource(graph, source) &&
128                                                         SourceLinkUtil.isUpToDateSource(graph, source)))
129                                                         continue;
130                                                 children.add(new StandardGraphChildVariable(variable, null, source));
131                                         }
132                                 }
133                         }
134                         //find sources declared on this resource, ie. sources that do not have consernsRelation.
135                         Collection<Resource> sources = graph.getObjects(resource, sl.hasSource);
136                         for (Resource source : sources) {
137                                 Resource rel = graph.getPossibleObject(source, sl.consernsRelation);
138                                 if (rel == null) {
139                                         if (showOnlyCheckable && 
140                                            (SourceLinkUtil.isValidSource(graph, source) &&
141                                                 SourceLinkUtil.isUpToDateSource(graph, source)))
142                                                 continue;
143                                         children.add(new StandardGraphChildVariable(variable, null, source));
144                                 }
145                         }
146                         while (children.remove(variable)) {
147                                 
148                         }
149                 }
150                 
151
152                 return children;
153         }
154         
155         private Map<Resource,Statement> getProps(ReadGraph graph, Resource resource) throws DatabaseException {
156                 Layer0 l0 = Layer0.getInstance(graph);
157                 Map<Resource, Statement> selectedProps = new HashMap<Resource, Statement>();
158                 Collection<Statement> propertyStatements = graph.getStatements(resource, l0.HasProperty);
159                 for (Statement s : propertyStatements) {
160                         if (!graph.isInstanceOf(s.getObject(), l0.Literal) && !graph.isInstanceOf(s.getObject(), l0.SCLValue))
161                                 continue;
162                         if (selectedProps.containsKey(s.getPredicate())) {
163                                 if (!s.isAsserted(resource))
164                                         selectedProps.put(s.getPredicate(), s);
165                         } else {
166                                 selectedProps.put(s.getPredicate(), s);
167                         }
168                 }
169                 return selectedProps;
170         }
171         
172         @Override
173         public Collection<?> getParents(ReadGraph graph, Object child)
174                         throws DatabaseException {
175                 return new ArrayList<Resource>();
176         }
177
178 }