1 package org.simantics.db.layer0.variable;
\r
3 import org.simantics.databoard.Bindings;
\r
4 import org.simantics.databoard.accessor.reference.ChildReference;
\r
5 import org.simantics.databoard.accessor.reference.IndexReference;
\r
6 import org.simantics.databoard.accessor.reference.NameReference;
\r
7 import org.simantics.databoard.adapter.AdaptException;
\r
8 import org.simantics.databoard.binding.ArrayBinding;
\r
9 import org.simantics.databoard.binding.Binding;
\r
10 import org.simantics.databoard.binding.RecordBinding;
\r
11 import org.simantics.databoard.binding.error.BindingException;
\r
12 import org.simantics.databoard.type.ArrayType;
\r
13 import org.simantics.databoard.type.Datatype;
\r
14 import org.simantics.databoard.type.RecordType;
\r
15 import org.simantics.db.ReadGraph;
\r
16 import org.simantics.db.Resource;
\r
17 import org.simantics.db.WriteGraph;
\r
18 import org.simantics.db.exception.DatabaseException;
\r
19 import org.simantics.db.exception.RuntimeDatabaseException;
\r
21 public class SubliteralPropertyVariable extends StandardGraphPropertyVariable {
\r
23 private final ChildReference reference;
\r
25 public SubliteralPropertyVariable(ReadGraph graph, Variable parent, Resource predicate, ChildReference reference) throws DatabaseException {
\r
26 super(graph, parent, null, predicate);
\r
27 this.reference = reference;
\r
31 public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
\r
32 Datatype parentType = parent.getDatatype(graph);
\r
33 if(parentType instanceof ArrayType) {
\r
34 ArrayType at = (ArrayType)parentType;
\r
35 return at.getComponentType(0);
\r
36 } else if(parentType instanceof RecordType) {
\r
37 RecordType rt = (RecordType)parentType;
\r
38 NameReference nr = (NameReference)reference;
\r
39 return rt.getComponent(nr.name).type;
\r
44 @SuppressWarnings("unchecked")
\r
46 public <T> T getValue(ReadGraph graph) throws DatabaseException {
\r
47 Object parentValue = parent.getValue(graph);
\r
48 Datatype parentType = parent.getDatatype(graph);
\r
49 Binding parentBinding = Bindings.getBinding(parentType);
\r
50 if (parentType instanceof ArrayType) {
\r
51 ArrayBinding ab = (ArrayBinding)parentBinding;
\r
52 IndexReference ref = (IndexReference)reference;
\r
54 return (T)ab.get(parentValue, ref.index);
\r
55 } catch (IndexOutOfBoundsException e) {
\r
56 throw new RuntimeDatabaseException(e);
\r
57 } catch (BindingException e) {
\r
58 throw new RuntimeDatabaseException(e);
\r
60 } else if (parentType instanceof RecordType) {
\r
61 RecordBinding rb = (RecordBinding)parentBinding;
\r
62 NameReference ref = (NameReference)reference;
\r
64 return (T)rb.getComponentObject(parentValue, ref.name);
\r
65 } catch (IndexOutOfBoundsException e) {
\r
66 throw new RuntimeDatabaseException(e);
\r
67 } catch (BindingException e) {
\r
68 throw new RuntimeDatabaseException(e);
\r
72 throw new RuntimeDatabaseException("parent variable data type " + parentType + " is not an ArrayType");
\r
75 @SuppressWarnings("unchecked")
\r
77 public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
\r
78 Object parentValue = parent.getValue(graph);
\r
79 Datatype parentType = parent.getDatatype(graph);
\r
80 Binding parentBinding = Bindings.getBinding(parentType);
\r
81 if (parentType instanceof ArrayType) {
\r
82 ArrayBinding ab = (ArrayBinding)parentBinding;
\r
83 IndexReference ref = (IndexReference)reference;
\r
85 Object indexValue = ab.get(parentValue, ref.index);
\r
86 return (T) Bindings.adapt(indexValue, ab.getComponentBinding(), binding);
\r
87 } catch (IndexOutOfBoundsException e) {
\r
88 throw new RuntimeDatabaseException(e);
\r
89 } catch (BindingException e) {
\r
90 throw new RuntimeDatabaseException(e);
\r
91 } catch (AdaptException e) {
\r
92 throw new RuntimeDatabaseException(e);
\r
94 } else if (parentType instanceof RecordType) {
\r
95 RecordBinding rb = (RecordBinding)parentBinding;
\r
96 NameReference ref = (NameReference)reference;
\r
98 Object nameValue = (T)rb.getComponentObject(parentValue, ref.name);
\r
99 return (T) Bindings.adapt(nameValue, rb.getComponentBinding(ref.name), binding);
\r
100 } catch (IndexOutOfBoundsException e) {
\r
101 throw new RuntimeDatabaseException(e);
\r
102 } catch (BindingException e) {
\r
103 throw new RuntimeDatabaseException(e);
\r
104 } catch (AdaptException e) {
\r
105 throw new RuntimeDatabaseException(e);
\r
109 throw new RuntimeDatabaseException("parent variable data type " + parentType + " is not an ArrayType");
\r
113 public void setValue(WriteGraph graph, Object value, Binding binding) throws DatabaseException {
\r
114 Object parentValue = parent.getValue(graph);
\r
115 Datatype parentType = parent.getDatatype(graph);
\r
116 Binding parentBinding = Bindings.getBinding(parentType);
\r
117 if(parentType instanceof ArrayType) {
\r
118 ArrayBinding ab = (ArrayBinding)parentBinding;
\r
119 IndexReference ref = (IndexReference)reference;
\r
121 Object copy = parentBinding.clone(parentValue);
\r
122 Object indexValue = Bindings.adapt(value, binding, ab.getComponentBinding());
\r
123 ab.set(copy, ref.index, indexValue);
\r
124 parent.setValue(graph, copy, parentBinding);
\r
125 } catch (IndexOutOfBoundsException e) {
\r
126 throw new RuntimeDatabaseException(e);
\r
127 } catch (BindingException e) {
\r
128 throw new RuntimeDatabaseException(e);
\r
129 } catch (AdaptException e) {
\r
130 throw new RuntimeDatabaseException(e);
\r
132 } else if(parentType instanceof RecordType) {
\r
133 RecordBinding rb = (RecordBinding)parentBinding;
\r
134 NameReference ref = (NameReference)reference;
\r
136 Object copy = parentBinding.clone(parentValue);
\r
137 Object indexValue = Bindings.adapt(value, binding, rb.getComponentBinding(ref.name));
\r
138 rb.setComponent(copy, ref.name, indexValue);
\r
139 parent.setValue(graph, copy, parentBinding);
\r
140 } catch (IndexOutOfBoundsException e) {
\r
141 throw new RuntimeDatabaseException(e);
\r
142 } catch (BindingException e) {
\r
143 throw new RuntimeDatabaseException(e);
\r
144 } catch (AdaptException e) {
\r
145 throw new RuntimeDatabaseException(e);
\r
151 public String getName(ReadGraph graph) throws DatabaseException {
\r
152 return reference.toString(true);
\r
156 public boolean equals(Object obj) {
\r
158 if(!super.equals(obj)) return false;
\r
160 if(!(obj instanceof SubliteralPropertyVariable)) return false;
\r
162 SubliteralPropertyVariable other = (SubliteralPropertyVariable)obj;
\r
164 return reference.equals(other.reference);
\r
169 public int hashCode() {
\r
170 return super.hashCode() + 31 * reference.hashCode();
\r