svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultEditableFeature.java @ 44670
History | View | Annotate | Download (15.1 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | 41805 | jjdelcerro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 40559 | jjdelcerro | * GNU General Public License for more details.
|
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | 41805 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl; |
25 | |||
26 | 44669 | jjdelcerro | import java.math.BigDecimal; |
27 | 44655 | jjdelcerro | import java.time.LocalDate; |
28 | import java.time.LocalDateTime; |
||
29 | import java.time.ZoneId; |
||
30 | import java.time.ZoneOffset; |
||
31 | import java.time.format.DateTimeFormatter; |
||
32 | import java.time.temporal.TemporalAccessor; |
||
33 | 40435 | jjdelcerro | import java.util.Date; |
34 | 44655 | jjdelcerro | import javax.json.JsonNumber; |
35 | import javax.json.JsonObject; |
||
36 | import org.gvsig.fmap.dal.DataTypes; |
||
37 | 40435 | jjdelcerro | |
38 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
39 | import org.gvsig.fmap.dal.feature.Feature; |
||
40 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
41 | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
||
42 | import org.gvsig.fmap.geom.Geometry; |
||
43 | 44655 | jjdelcerro | import org.gvsig.fmap.geom.GeometryUtils; |
44 | 40435 | jjdelcerro | import org.gvsig.timesupport.Instant; |
45 | import org.gvsig.timesupport.Interval; |
||
46 | |||
47 | public class DefaultEditableFeature extends DefaultFeature implements |
||
48 | 41805 | jjdelcerro | EditableFeature { |
49 | 40435 | jjdelcerro | |
50 | 41805 | jjdelcerro | private DefaultFeature source;
|
51 | 40435 | jjdelcerro | |
52 | 41805 | jjdelcerro | protected DefaultEditableFeature(DefaultFeature feature) {
|
53 | super(feature);
|
||
54 | this.source = feature;
|
||
55 | } |
||
56 | 40435 | jjdelcerro | |
57 | 41805 | jjdelcerro | protected DefaultEditableFeature(DefaultEditableFeature feature) {
|
58 | super(feature);
|
||
59 | this.source = (DefaultFeature) feature.getSource();
|
||
60 | } |
||
61 | 40435 | jjdelcerro | |
62 | 41805 | jjdelcerro | public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
|
63 | // Se trata de un editable feature sobre una ya existente
|
||
64 | super(store, data);
|
||
65 | this.source = null; |
||
66 | } |
||
67 | 40435 | jjdelcerro | |
68 | 41805 | jjdelcerro | public Feature getSource() {
|
69 | return this.source; |
||
70 | } |
||
71 | 40435 | jjdelcerro | |
72 | 41805 | jjdelcerro | public EditableFeature getEditable() {
|
73 | return this; |
||
74 | } |
||
75 | 40435 | jjdelcerro | |
76 | 41805 | jjdelcerro | public Feature getCopy() {
|
77 | return new DefaultEditableFeature(this); |
||
78 | } |
||
79 | 40435 | jjdelcerro | |
80 | 41805 | jjdelcerro | public Feature getNotEditableCopy() {
|
81 | return new DefaultFeature(this); |
||
82 | } |
||
83 | 40435 | jjdelcerro | |
84 | 41805 | jjdelcerro | public void setDefaultGeometry(Geometry geometry) { |
85 | FeatureAttributeDescriptor attribute = this.getType()
|
||
86 | .getAttributeDescriptor( |
||
87 | this.getType().getDefaultGeometryAttributeIndex());
|
||
88 | this.set(attribute, geometry);
|
||
89 | } |
||
90 | 40435 | jjdelcerro | |
91 | 44390 | jjdelcerro | public void __setitem__(String name, Object value) { |
92 | this.set(name, value);
|
||
93 | } |
||
94 | |||
95 | @Override
|
||
96 | 41805 | jjdelcerro | public void set(String name, Object value) { |
97 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
98 | if ( attribute == null ) { |
||
99 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
100 | } |
||
101 | this.set(attribute, value);
|
||
102 | } |
||
103 | 40435 | jjdelcerro | |
104 | 41805 | jjdelcerro | public void set(int index, Object value) { |
105 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
106 | this.set(attribute, value);
|
||
107 | } |
||
108 | 40435 | jjdelcerro | |
109 | 41805 | jjdelcerro | public void setArray(String name, Object[] value) { |
110 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
111 | if ( attribute == null ) { |
||
112 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
113 | } |
||
114 | this.set(attribute, value);
|
||
115 | } |
||
116 | 40435 | jjdelcerro | |
117 | 41805 | jjdelcerro | public void setArray(int index, Object[] value) { |
118 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
119 | this.set(attribute, value);
|
||
120 | } |
||
121 | 40435 | jjdelcerro | |
122 | 41805 | jjdelcerro | public void setBoolean(String name, boolean value) { |
123 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
124 | if ( attribute == null ) { |
||
125 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
126 | } |
||
127 | this.set(attribute, Boolean.valueOf(value)); |
||
128 | } |
||
129 | 40435 | jjdelcerro | |
130 | 41805 | jjdelcerro | public void setBoolean(int index, boolean value) { |
131 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
132 | this.set(attribute, Boolean.valueOf(value)); |
||
133 | } |
||
134 | 40435 | jjdelcerro | |
135 | 41805 | jjdelcerro | public void setByte(String name, byte value) { |
136 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
137 | if ( attribute == null ) { |
||
138 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
139 | } |
||
140 | this.set(attribute, new Byte(value)); |
||
141 | } |
||
142 | 40435 | jjdelcerro | |
143 | 41805 | jjdelcerro | public void setByte(int index, byte value) { |
144 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
145 | this.set(attribute, new Byte(value)); |
||
146 | } |
||
147 | 40435 | jjdelcerro | |
148 | 41805 | jjdelcerro | public void setDate(String name, Date value) { |
149 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
150 | if ( attribute == null ) { |
||
151 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
152 | } |
||
153 | this.set(attribute, value);
|
||
154 | } |
||
155 | 40435 | jjdelcerro | |
156 | 41805 | jjdelcerro | public void setDate(int index, Date value) { |
157 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
158 | this.set(attribute, value);
|
||
159 | } |
||
160 | 40435 | jjdelcerro | |
161 | 41805 | jjdelcerro | public void setDouble(String name, double value) { |
162 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
163 | if ( attribute == null ) { |
||
164 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
165 | } |
||
166 | this.set(attribute, new Double(value)); |
||
167 | } |
||
168 | 40435 | jjdelcerro | |
169 | 41805 | jjdelcerro | public void setDouble(int index, double value) { |
170 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
171 | this.set(attribute, new Double(value)); |
||
172 | } |
||
173 | 40435 | jjdelcerro | |
174 | 44669 | jjdelcerro | @Override
|
175 | public void setDecimal(String name, BigDecimal value) { |
||
176 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
177 | if ( attribute == null ) { |
||
178 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
179 | } |
||
180 | this.set(attribute, value);
|
||
181 | } |
||
182 | |||
183 | public void setDecimal(int index, BigDecimal value) { |
||
184 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
185 | this.set(attribute, value);
|
||
186 | } |
||
187 | |||
188 | 41805 | jjdelcerro | public void setFeature(String name, Feature value) { |
189 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
190 | if ( attribute == null ) { |
||
191 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
192 | } |
||
193 | this.set(attribute, value);
|
||
194 | } |
||
195 | 40435 | jjdelcerro | |
196 | 41805 | jjdelcerro | public void setFeature(int index, Feature value) { |
197 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
198 | this.set(attribute, value);
|
||
199 | } |
||
200 | 40435 | jjdelcerro | |
201 | 41805 | jjdelcerro | public void setFloat(String name, float value) { |
202 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
203 | if ( attribute == null ) { |
||
204 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
205 | } |
||
206 | this.set(attribute, new Float(value)); |
||
207 | } |
||
208 | 40435 | jjdelcerro | |
209 | 41805 | jjdelcerro | public void setFloat(int index, float value) { |
210 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
211 | this.set(attribute, new Float(value)); |
||
212 | } |
||
213 | 40435 | jjdelcerro | |
214 | 41805 | jjdelcerro | public void setGeometry(String name, Geometry value) { |
215 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
216 | if ( attribute == null ) { |
||
217 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
218 | } |
||
219 | this.set(attribute, value);
|
||
220 | } |
||
221 | 40435 | jjdelcerro | |
222 | 41805 | jjdelcerro | public void setGeometry(int index, Geometry value) { |
223 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
224 | this.set(attribute, value);
|
||
225 | } |
||
226 | 40435 | jjdelcerro | |
227 | 41805 | jjdelcerro | public void setInt(String name, int value) { |
228 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
229 | if ( attribute == null ) { |
||
230 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
231 | } |
||
232 | this.set(attribute, new Integer(value)); |
||
233 | } |
||
234 | 40435 | jjdelcerro | |
235 | 41805 | jjdelcerro | public void setInt(int index, int value) { |
236 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
237 | this.set(attribute, new Integer(value)); |
||
238 | } |
||
239 | 40435 | jjdelcerro | |
240 | 41805 | jjdelcerro | public void setLong(String name, long value) { |
241 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
242 | if ( attribute == null ) { |
||
243 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
244 | } |
||
245 | this.set(attribute, new Long(value)); |
||
246 | } |
||
247 | 40435 | jjdelcerro | |
248 | 41805 | jjdelcerro | public void setLong(int index, long value) { |
249 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
250 | this.set(attribute, new Long(value)); |
||
251 | } |
||
252 | 40435 | jjdelcerro | |
253 | 41805 | jjdelcerro | public void setString(String name, String value) { |
254 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
255 | if ( attribute == null ) { |
||
256 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
257 | } |
||
258 | this.set(attribute, value);
|
||
259 | } |
||
260 | 40435 | jjdelcerro | |
261 | 41805 | jjdelcerro | public void setString(int index, String value) { |
262 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
263 | this.set(attribute, value);
|
||
264 | } |
||
265 | 40435 | jjdelcerro | |
266 | 44655 | jjdelcerro | public void copyFrom(JsonObject values) { |
267 | // iterate over the attributes and copy one by one
|
||
268 | for (FeatureAttributeDescriptor attr : this.getType()) { |
||
269 | if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) { |
||
270 | continue;
|
||
271 | } |
||
272 | String attrname = attr.getName();
|
||
273 | if( !values.containsKey(attrname) ) {
|
||
274 | continue;
|
||
275 | } |
||
276 | Object value;
|
||
277 | try {
|
||
278 | switch( values.getValueType() ) {
|
||
279 | case STRING:
|
||
280 | switch(attr.getType()) {
|
||
281 | case DataTypes.GEOMETRY:
|
||
282 | value = GeometryUtils.createFrom(values.getString(attrname)); |
||
283 | break;
|
||
284 | case DataTypes.DATE:
|
||
285 | try {
|
||
286 | TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(values.getString(attrname)); |
||
287 | LocalDateTime date = LocalDateTime.from(x); |
||
288 | // value = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
|
||
289 | value = Date.from(date.toInstant(ZoneOffset.UTC));
|
||
290 | } catch(Exception ex) { |
||
291 | value = values.getString(attrname); |
||
292 | } |
||
293 | break;
|
||
294 | case DataTypes.STRING:
|
||
295 | default:
|
||
296 | value = values.getString(attrname); |
||
297 | break;
|
||
298 | } |
||
299 | break;
|
||
300 | case NUMBER:
|
||
301 | JsonNumber n = values.getJsonNumber(attrname); |
||
302 | switch(attr.getType()) {
|
||
303 | case DataTypes.BIGDECIMAL:
|
||
304 | value = n.bigDecimalValue(); |
||
305 | break;
|
||
306 | case DataTypes.BYTE:
|
||
307 | value = n.bigDecimalValue(); |
||
308 | break;
|
||
309 | case DataTypes.FLOAT:
|
||
310 | case DataTypes.DOUBLE:
|
||
311 | value = n.doubleValue(); |
||
312 | break;
|
||
313 | case DataTypes.INT:
|
||
314 | value = n.intValue(); |
||
315 | break;
|
||
316 | case DataTypes.LONG:
|
||
317 | value = n.longValue(); |
||
318 | break;
|
||
319 | default:
|
||
320 | value = n.toString(); |
||
321 | break;
|
||
322 | } |
||
323 | break;
|
||
324 | case TRUE:
|
||
325 | value = true;
|
||
326 | break;
|
||
327 | case FALSE:
|
||
328 | value = false;
|
||
329 | break;
|
||
330 | case NULL:
|
||
331 | value = null;
|
||
332 | break;
|
||
333 | default:
|
||
334 | value = values.getString(attrname); |
||
335 | break;
|
||
336 | } |
||
337 | } catch(Exception ex) { |
||
338 | continue;
|
||
339 | } |
||
340 | if (value == null && !attr.allowNull()) { |
||
341 | continue;
|
||
342 | } |
||
343 | try {
|
||
344 | set(attr.getIndex(), value); |
||
345 | } catch(Throwable th) { |
||
346 | // Ignore
|
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | |||
351 | 41805 | jjdelcerro | public void copyFrom(Feature source) { |
352 | // iterate over the attributes and copy one by one
|
||
353 | 44610 | jjdelcerro | for (FeatureAttributeDescriptor attr : this.getType()) { |
354 | if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) { |
||
355 | continue;
|
||
356 | 43562 | jjdelcerro | } |
357 | 44610 | jjdelcerro | Object value = source.get(attr.getName());
|
358 | if (value == null && !attr.allowNull()) { |
||
359 | continue;
|
||
360 | } |
||
361 | try {
|
||
362 | set(attr.getIndex(), value); |
||
363 | } catch(Throwable th) { |
||
364 | 44669 | jjdelcerro | value = null;
|
365 | 44610 | jjdelcerro | // Ignore
|
366 | } |
||
367 | 41805 | jjdelcerro | } |
368 | } |
||
369 | 40435 | jjdelcerro | |
370 | 41805 | jjdelcerro | public void setInstant(String name, Instant value) { |
371 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
372 | if ( attribute == null ) { |
||
373 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
374 | } |
||
375 | this.set(attribute, value);
|
||
376 | } |
||
377 | 40435 | jjdelcerro | |
378 | 41805 | jjdelcerro | public void setInstant(int index, Instant value) { |
379 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
380 | this.set(attribute, value);
|
||
381 | } |
||
382 | 40435 | jjdelcerro | |
383 | 41805 | jjdelcerro | public void setInterval(String name, Interval value) { |
384 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
||
385 | if ( attribute == null ) { |
||
386 | throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
||
387 | } |
||
388 | this.set(attribute, value);
|
||
389 | } |
||
390 | 40435 | jjdelcerro | |
391 | 41805 | jjdelcerro | public void setInterval(int index, Interval value) { |
392 | FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
||
393 | this.set(attribute, value);
|
||
394 | } |
||
395 | 40435 | jjdelcerro | } |