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 @ 44815
History | View | Annotate | Download (15.9 KB)
1 |
/**
|
---|---|
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 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* 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 |
* MA 02110-1301, USA.
|
20 |
*
|
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 |
package org.gvsig.fmap.dal.feature.impl; |
25 |
|
26 |
import java.math.BigDecimal; |
27 |
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 |
import java.util.Date; |
34 |
import javax.json.JsonNumber; |
35 |
import javax.json.JsonObject; |
36 |
import org.gvsig.fmap.dal.DataTypes; |
37 |
|
38 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumn; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
45 |
import org.gvsig.fmap.geom.Geometry; |
46 |
import org.gvsig.fmap.geom.GeometryUtils; |
47 |
import org.gvsig.timesupport.Instant; |
48 |
import org.gvsig.timesupport.Interval; |
49 |
|
50 |
public class DefaultEditableFeature extends DefaultFeature implements |
51 |
EditableFeature { |
52 |
|
53 |
private DefaultFeature source;
|
54 |
|
55 |
protected DefaultEditableFeature(DefaultFeature feature) {
|
56 |
super(feature);
|
57 |
this.source = feature;
|
58 |
} |
59 |
|
60 |
protected DefaultEditableFeature(DefaultEditableFeature feature) {
|
61 |
super(feature);
|
62 |
this.source = (DefaultFeature) feature.getSource();
|
63 |
} |
64 |
|
65 |
public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
|
66 |
// Se trata de un editable feature sobre una ya existente
|
67 |
super(store, data);
|
68 |
this.source = null; |
69 |
} |
70 |
|
71 |
public Feature getSource() {
|
72 |
return this.source; |
73 |
} |
74 |
|
75 |
public EditableFeature getEditable() {
|
76 |
return this; |
77 |
} |
78 |
|
79 |
public Feature getCopy() {
|
80 |
return new DefaultEditableFeature(this); |
81 |
} |
82 |
|
83 |
public Feature getNotEditableCopy() {
|
84 |
return new DefaultFeature(this); |
85 |
} |
86 |
|
87 |
public void setDefaultGeometry(Geometry geometry) { |
88 |
FeatureAttributeDescriptor attribute = this.getType()
|
89 |
.getAttributeDescriptor( |
90 |
this.getType().getDefaultGeometryAttributeIndex());
|
91 |
this.set(attribute, geometry);
|
92 |
} |
93 |
|
94 |
public void __setitem__(String name, Object value) { |
95 |
this.set(name, value);
|
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void set(String name, Object value) { |
100 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
101 |
if ( attribute == null ) { |
102 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
103 |
} |
104 |
this.set(attribute, value);
|
105 |
} |
106 |
|
107 |
public void set(int index, Object value) { |
108 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
109 |
this.set(attribute, value);
|
110 |
} |
111 |
|
112 |
public void setArray(String name, Object[] value) { |
113 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
114 |
if ( attribute == null ) { |
115 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
116 |
} |
117 |
this.set(attribute, value);
|
118 |
} |
119 |
|
120 |
public void setArray(int index, Object[] value) { |
121 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
122 |
this.set(attribute, value);
|
123 |
} |
124 |
|
125 |
public void setBoolean(String name, boolean value) { |
126 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
127 |
if ( attribute == null ) { |
128 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
129 |
} |
130 |
this.set(attribute, Boolean.valueOf(value)); |
131 |
} |
132 |
|
133 |
public void setBoolean(int index, boolean value) { |
134 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
135 |
this.set(attribute, Boolean.valueOf(value)); |
136 |
} |
137 |
|
138 |
public void setByte(String name, byte value) { |
139 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
140 |
if ( attribute == null ) { |
141 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
142 |
} |
143 |
this.set(attribute, new Byte(value)); |
144 |
} |
145 |
|
146 |
public void setByte(int index, byte value) { |
147 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
148 |
this.set(attribute, new Byte(value)); |
149 |
} |
150 |
|
151 |
public void setDate(String name, Date value) { |
152 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
153 |
if ( attribute == null ) { |
154 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
155 |
} |
156 |
this.set(attribute, value);
|
157 |
} |
158 |
|
159 |
public void setDate(int index, Date value) { |
160 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
161 |
this.set(attribute, value);
|
162 |
} |
163 |
|
164 |
public void setDouble(String name, double value) { |
165 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
166 |
if ( attribute == null ) { |
167 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
168 |
} |
169 |
this.set(attribute, new Double(value)); |
170 |
} |
171 |
|
172 |
public void setDouble(int index, double value) { |
173 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
174 |
this.set(attribute, new Double(value)); |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public void setDecimal(String name, BigDecimal value) { |
179 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
180 |
if ( attribute == null ) { |
181 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
182 |
} |
183 |
this.set(attribute, value);
|
184 |
} |
185 |
|
186 |
public void setDecimal(int index, BigDecimal value) { |
187 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
188 |
this.set(attribute, value);
|
189 |
} |
190 |
|
191 |
public void setFeature(String name, Feature value) { |
192 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
193 |
if ( attribute == null ) { |
194 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
195 |
} |
196 |
this.set(attribute, value);
|
197 |
} |
198 |
|
199 |
public void setFeature(int index, Feature value) { |
200 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
201 |
this.set(attribute, value);
|
202 |
} |
203 |
|
204 |
public void setFloat(String name, float value) { |
205 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
206 |
if ( attribute == null ) { |
207 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
208 |
} |
209 |
this.set(attribute, new Float(value)); |
210 |
} |
211 |
|
212 |
public void setFloat(int index, float value) { |
213 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
214 |
this.set(attribute, new Float(value)); |
215 |
} |
216 |
|
217 |
public void setGeometry(String name, Geometry value) { |
218 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
219 |
if ( attribute == null ) { |
220 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
221 |
} |
222 |
this.set(attribute, value);
|
223 |
} |
224 |
|
225 |
public void setGeometry(int index, Geometry value) { |
226 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
227 |
this.set(attribute, value);
|
228 |
} |
229 |
|
230 |
public void setInt(String name, int value) { |
231 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
232 |
if ( attribute == null ) { |
233 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
234 |
} |
235 |
this.set(attribute, new Integer(value)); |
236 |
} |
237 |
|
238 |
public void setInt(int index, int value) { |
239 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
240 |
this.set(attribute, new Integer(value)); |
241 |
} |
242 |
|
243 |
public void setLong(String name, long value) { |
244 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
245 |
if ( attribute == null ) { |
246 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
247 |
} |
248 |
this.set(attribute, new Long(value)); |
249 |
} |
250 |
|
251 |
public void setLong(int index, long value) { |
252 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
253 |
this.set(attribute, new Long(value)); |
254 |
} |
255 |
|
256 |
public void setString(String name, String value) { |
257 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
258 |
if ( attribute == null ) { |
259 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
260 |
} |
261 |
this.set(attribute, value);
|
262 |
} |
263 |
|
264 |
public void setString(int index, String value) { |
265 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
266 |
this.set(attribute, value);
|
267 |
} |
268 |
|
269 |
public void copyFrom(JsonObject values) { |
270 |
// iterate over the attributes and copy one by one
|
271 |
for (FeatureAttributeDescriptor attr : this.getType()) { |
272 |
if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) { |
273 |
continue;
|
274 |
} |
275 |
String attrname = attr.getName();
|
276 |
if( !values.containsKey(attrname) ) {
|
277 |
continue;
|
278 |
} |
279 |
Object value;
|
280 |
try {
|
281 |
switch( values.getValueType() ) {
|
282 |
case STRING:
|
283 |
switch(attr.getType()) {
|
284 |
case DataTypes.GEOMETRY:
|
285 |
value = GeometryUtils.createFrom(values.getString(attrname)); |
286 |
break;
|
287 |
case DataTypes.DATE:
|
288 |
try {
|
289 |
TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(values.getString(attrname)); |
290 |
LocalDateTime date = LocalDateTime.from(x); |
291 |
// value = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
|
292 |
value = Date.from(date.toInstant(ZoneOffset.UTC));
|
293 |
} catch(Exception ex) { |
294 |
value = values.getString(attrname); |
295 |
} |
296 |
break;
|
297 |
case DataTypes.STRING:
|
298 |
default:
|
299 |
value = values.getString(attrname); |
300 |
break;
|
301 |
} |
302 |
break;
|
303 |
case NUMBER:
|
304 |
JsonNumber n = values.getJsonNumber(attrname); |
305 |
switch(attr.getType()) {
|
306 |
case DataTypes.BIGDECIMAL:
|
307 |
value = n.bigDecimalValue(); |
308 |
break;
|
309 |
case DataTypes.BYTE:
|
310 |
value = n.bigDecimalValue(); |
311 |
break;
|
312 |
case DataTypes.FLOAT:
|
313 |
case DataTypes.DOUBLE:
|
314 |
value = n.doubleValue(); |
315 |
break;
|
316 |
case DataTypes.INT:
|
317 |
value = n.intValue(); |
318 |
break;
|
319 |
case DataTypes.LONG:
|
320 |
value = n.longValue(); |
321 |
break;
|
322 |
default:
|
323 |
value = n.toString(); |
324 |
break;
|
325 |
} |
326 |
break;
|
327 |
case TRUE:
|
328 |
value = true;
|
329 |
break;
|
330 |
case FALSE:
|
331 |
value = false;
|
332 |
break;
|
333 |
case NULL:
|
334 |
value = null;
|
335 |
break;
|
336 |
default:
|
337 |
value = values.getString(attrname); |
338 |
break;
|
339 |
} |
340 |
} catch(Exception ex) { |
341 |
continue;
|
342 |
} |
343 |
if (value == null && !attr.allowNull()) { |
344 |
continue;
|
345 |
} |
346 |
try {
|
347 |
set(attr.getIndex(), value); |
348 |
} catch(Throwable th) { |
349 |
// Ignore
|
350 |
} |
351 |
} |
352 |
} |
353 |
|
354 |
public void copyFrom(Feature source) { |
355 |
// iterate over the attributes and copy one by one
|
356 |
FeatureType sourceType = source.getType(); |
357 |
for (FeatureAttributeDescriptor attr : this.getType()) { |
358 |
if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) { |
359 |
continue;
|
360 |
} |
361 |
String attrname = attr.getName();
|
362 |
if( sourceType.getAttributeDescriptor(attrname)==null ) { |
363 |
continue;
|
364 |
} |
365 |
Object value = source.get(attrname);
|
366 |
if (value == null && !attr.allowNull()) { |
367 |
continue;
|
368 |
} |
369 |
try {
|
370 |
set(attr.getIndex(), value); |
371 |
} catch(Throwable th) { |
372 |
value = null;
|
373 |
// Ignore
|
374 |
} |
375 |
} |
376 |
} |
377 |
|
378 |
public void setInstant(String name, Instant value) { |
379 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
380 |
if ( attribute == null ) { |
381 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
382 |
} |
383 |
this.set(attribute, value);
|
384 |
} |
385 |
|
386 |
public void setInstant(int index, Instant value) { |
387 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
388 |
this.set(attribute, value);
|
389 |
} |
390 |
|
391 |
public void setInterval(String name, Interval value) { |
392 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
393 |
if ( attribute == null ) { |
394 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
395 |
} |
396 |
this.set(attribute, value);
|
397 |
} |
398 |
|
399 |
public void setInterval(int index, Interval value) { |
400 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
401 |
this.set(attribute, value);
|
402 |
} |
403 |
|
404 |
@Override
|
405 |
public Object getExtraValue(String name) { |
406 |
Object value;
|
407 |
FeatureExtraColumn columns = this.getType().getExtraColumn();
|
408 |
int index = columns.getIndexOf(name);
|
409 |
if (index >= 0) { |
410 |
EditableFeatureAttributeDescriptor attrdesc = columns.get(index); |
411 |
value = attrdesc.getFeatureAttributeEmulator().get(this);
|
412 |
return value;
|
413 |
} |
414 |
value = this.data.getExtraValue(name);
|
415 |
return value;
|
416 |
} |
417 |
} |