]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardAssertedGraphPropertyVariable.java
Check if root allows paste only for non-null target root to prevent NPE
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardAssertedGraphPropertyVariable.java
1 package org.simantics.db.layer0.variable;
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.databoard.Bindings;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.impl.ObjectArrayBinding;
13 import org.simantics.databoard.type.Datatype;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.function.All;
19 import org.simantics.db.layer0.util.Layer0Utils;
20 import org.simantics.layer0.Layer0;
21
22 public class StandardAssertedGraphPropertyVariable extends StandardGraphPropertyVariable {
23         
24     final public Resource object;
25         
26         transient private int hash = 0;
27
28         public StandardAssertedGraphPropertyVariable(ReadGraph graph, Variable parent, VariableNode node, Resource parentResource, Resource property, Resource object) throws DatabaseException {
29             super(graph, parent, node, parentResource, property);
30                 assert parent != null;
31                 assert property != null;
32                 this.object = object;
33         }
34         
35         @Override
36         public boolean isAsserted() {
37                 return true;
38         }
39
40         @Override
41         public void validate(ReadGraph graph) throws DatabaseException {
42         }
43
44         @Override
45         public String getName(ReadGraph graph) throws DatabaseException {
46                 return graph.getRelatedValue(property.predicate, graph.getService(Layer0.class).HasName, Bindings.STRING);
47         }
48
49         @Override
50         public String getLabel(ReadGraph graph) throws DatabaseException {
51                 return graph.getRelatedValue2(property.predicate, graph.getService(Layer0.class).HasLabel, parent);
52         }
53         
54 //      @Override
55 //      public Object getSerialized(ReadGraph graph) throws DatabaseException {
56 //              return property;
57 //      }
58
59         @Override
60         public Variable getParent(ReadGraph graph) throws DatabaseException {
61                 return parent;
62         }
63
64         @SuppressWarnings("unchecked")
65         @Override
66         public <T> T getValue(ReadGraph graph) throws DatabaseException {
67                 
68                 assertAlive(graph);
69                 
70                 return (T)getValueAccessor(graph).getValue(graph, this);
71         }
72
73         @SuppressWarnings("unchecked")
74         @Override
75         public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
76                 if (binding instanceof ObjectArrayBinding)
77                         return getValue(graph);
78                 
79                 assertAlive(graph);
80
81                 return (T)getValueAccessor(graph).getValue(graph, this, binding);
82                 
83         }
84         
85         @Override
86         public Resource getRepresents(ReadGraph graph) throws DatabaseException {
87                 
88                 assertAlive(graph);
89                 
90                 return object;
91                 
92         }
93
94         @Override
95         public Resource getPossibleRepresents(ReadGraph graph) throws DatabaseException {
96                 
97                 assertAlive(graph);
98                 
99                 return object;
100                 
101         }
102
103         @Override
104         public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
105                 
106                 assertAlive(graph);
107                 
108                 getValueAccessor(graph).setValue(graph, this, value, binding);
109                 
110         }
111         
112         @Override
113         public void setValue(WriteGraph graph, Object value) throws DatabaseException {
114                 
115                 assertAlive(graph);
116                 
117                 getValueAccessor(graph).setValue(graph, this, value);
118                 
119         }
120
121         @Override
122         public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
123                 try {
124                         return Layer0Utils.getDatatype(graph, this);
125                 } catch (DatabaseException e) {
126                         return null;
127                 }
128         }
129         
130         @Override
131         public String getUnit(ReadGraph graph) throws DatabaseException {
132                 try {
133                         return Layer0Utils.getUnit(graph, this);
134                 } catch (DatabaseException e) {
135                         return null;
136                 }
137         }
138
139         @Override
140         public Resource getPropertyResource(ReadGraph graph) {
141                 return property.predicate;
142         }
143         
144         @Override
145         public Resource getContainerResource(ReadGraph graph) throws DatabaseException {
146                 return parent.getRepresents(graph);
147         }
148
149         @Override
150         public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
151
152                 Map<String, Variable> result = new HashMap<String, Variable>();
153                 VariableMap map = getPossibleChildVariableMap(graph);
154                 if(map != null) map.getVariables(graph, this, result);
155                 return result.values();
156                 
157         }
158         
159         @Override
160         public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
161
162                 VariableMap map = getPossibleChildVariableMap(graph);
163                 if(map == null) return null;
164                 try {
165                         return map.getVariable(graph, this, name);
166                 } catch (DatabaseException e) {
167                         return null;
168                 }
169                 
170         }
171         
172         
173         @Override
174         protected Variable getPossibleDomainProperty(ReadGraph graph, String name) throws DatabaseException {
175
176                 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
177                 if(valueMap == null) return null;
178                 try {
179                         return valueMap.getVariable(graph, this, name);
180                 } catch (DatabaseException e) {
181                         return null;
182                 }
183                 
184         }
185
186         
187         
188         @Override
189         public Map<String, Variable> collectDomainProperties(ReadGraph graph, Map<String, Variable> properties) throws DatabaseException {
190
191                 VariableMap valueMap = getPossiblePropertyVariableMap(graph);
192                 if(valueMap == null) return properties;
193                 return valueMap.getVariables(graph, this, properties);
194                 
195         }
196         
197         @Override
198         public Variable getPredicate(ReadGraph graph) throws DatabaseException {
199                 return Variables.getVariable(graph, graph.getURI(property.predicate));
200         }
201         
202
203         @Override
204         public int hashCode() {
205                 if(hash == 0) {
206                         final int prime = 31;
207                         int result = 1;
208                         result = prime * result + parent.hashCode();
209                         result = prime * result + property.hashCode();
210                         result = prime * result + object.hashCode();
211                         hash =result;
212                 }
213                 return hash;
214         }
215
216         @Override
217         public boolean equals(Object obj) {
218                 if (this == obj)
219                         return true;
220                 if (obj == null)
221                         return false;
222                 if (getClass() != obj.getClass())
223                         return false;
224                 StandardAssertedGraphPropertyVariable other = (StandardAssertedGraphPropertyVariable) obj;
225                 if (!property.equals(other.property))
226                         return false;
227                 if (!object.equals(other.object))
228                         return false;
229                 if (!parent.equals(other.parent))
230                         return false;
231                 return parent.equals(other.parent);
232         }
233
234         @Override
235         protected Variable getNameVariable(ReadGraph graph) throws DatabaseException {
236                 Layer0 L0 = Layer0.getInstance(graph);
237                 return new StandardGraphPropertyVariable(graph, this, L0.HasName);
238         }
239         
240         private void assertAlive(ReadGraph graph) throws DatabaseException {
241         }
242         
243         protected ValueAccessor getValueAccessor(ReadGraph graph) throws DatabaseException {
244                 // First from literal
245             if(parentResource == null) return All.standardValueAccessor;
246             ValueAccessor accessor = property.valueAccessor;
247             if(accessor != null) return accessor;
248             else {
249                 System.err.println("No value accessor for " + getURI(graph));
250                 return All.standardValueAccessor;
251             }
252         }
253         
254         protected VariableMap getPossibleChildVariableMap(ReadGraph graph) throws DatabaseException {
255                 Resource value = getRepresents(graph);
256                 if(value == null) return null;
257                 return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainChildren, this);
258         }
259
260         protected VariableMap getPossiblePropertyVariableMap(ReadGraph graph) throws DatabaseException {
261                 Resource value = getRepresents(graph);
262                 if(value == null) return null;
263                 return graph.getPossibleRelatedValue2(value, Layer0.getInstance(graph).domainProperties, this);
264         }
265         
266         public Set<String> getClassifications(ReadGraph graph) throws DatabaseException {
267                 ArrayList<String> value = getPropertyValue(graph, "classifications"); 
268                 return new HashSet<String>(value);
269         }
270         
271 }