]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SubliteralPropertyVariable.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / SubliteralPropertyVariable.java
1 package org.simantics.db.layer0.variable;\r
2 \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
20 \r
21 public class SubliteralPropertyVariable extends StandardGraphPropertyVariable {\r
22         \r
23         private final ChildReference reference;\r
24 \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
28         }\r
29 \r
30         @Override\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
40                 }\r
41                 return null;\r
42         }\r
43 \r
44         @SuppressWarnings("unchecked")\r
45         @Override\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
53                         try {\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
59                         }\r
60                 } else if (parentType instanceof RecordType) {\r
61                         RecordBinding rb = (RecordBinding)parentBinding;\r
62                         NameReference ref = (NameReference)reference;\r
63                         try {\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
69                         }\r
70                 }\r
71 \r
72                 throw new RuntimeDatabaseException("parent variable data type " + parentType + " is not an ArrayType");\r
73         }\r
74 \r
75         @SuppressWarnings("unchecked")\r
76         @Override\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
84                         try {\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
93                         }\r
94                 } else if (parentType instanceof RecordType) {\r
95                         RecordBinding rb = (RecordBinding)parentBinding;\r
96                         NameReference ref = (NameReference)reference;\r
97                         try {\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
106                         }\r
107                 }\r
108 \r
109                 throw new RuntimeDatabaseException("parent variable data type " + parentType + " is not an ArrayType");\r
110         }\r
111 \r
112         @Override\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
120                         try {\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
131                         }\r
132                 } else if(parentType instanceof RecordType) {\r
133                         RecordBinding rb = (RecordBinding)parentBinding;\r
134                         NameReference ref = (NameReference)reference;\r
135                         try {\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
146                         }\r
147                 }\r
148         }\r
149 \r
150         @Override\r
151         public String getName(ReadGraph graph) throws DatabaseException {\r
152                 return reference.toString(true);\r
153         }\r
154 \r
155         @Override\r
156         public boolean equals(Object obj) {\r
157                 \r
158                 if(!super.equals(obj)) return false;\r
159                 \r
160                 if(!(obj instanceof SubliteralPropertyVariable)) return false;\r
161                 \r
162                 SubliteralPropertyVariable other = (SubliteralPropertyVariable)obj;\r
163                 \r
164                 return reference.equals(other.reference);\r
165                 \r
166         }\r
167         \r
168         @Override\r
169         public int hashCode() {\r
170                 return super.hashCode() + 31 * reference.hashCode();\r
171         }\r
172         \r
173 }\r