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 @ 45989
History | View | Annotate | Download (21.1 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.LocalTime; |
30 |
import java.time.ZoneOffset; |
31 |
import java.time.format.DateTimeFormatter; |
32 |
import java.time.temporal.TemporalAccessor; |
33 |
import java.util.Date; |
34 |
import java.util.Set; |
35 |
import java.util.function.Predicate; |
36 |
import javax.json.JsonNumber; |
37 |
import javax.json.JsonObject; |
38 |
import javax.json.JsonString; |
39 |
import javax.json.JsonValue; |
40 |
import org.gvsig.fmap.dal.DataTypes; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
|
43 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
45 |
import org.gvsig.fmap.dal.feature.Feature; |
46 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
47 |
import org.gvsig.fmap.dal.feature.FeatureType; |
48 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.GeometryUtils; |
51 |
import org.gvsig.timesupport.Instant; |
52 |
import org.gvsig.timesupport.Interval; |
53 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
54 |
import org.gvsig.fmap.geom.type.GeometryType; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
public class DefaultEditableFeature extends DefaultFeature implements |
59 |
EditableFeature { |
60 |
|
61 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultEditableFeature.class); |
62 |
|
63 |
private DefaultFeature source;
|
64 |
// private boolean inserted;
|
65 |
private boolean modified; |
66 |
|
67 |
protected DefaultEditableFeature(DefaultFeature feature) {
|
68 |
super(feature);
|
69 |
this.source = feature;
|
70 |
// this.inserted = this.source!=null;
|
71 |
this.modified = false; |
72 |
} |
73 |
|
74 |
protected DefaultEditableFeature(DefaultEditableFeature feature) {
|
75 |
super(feature);
|
76 |
this.source = (DefaultFeature) feature.getSource();
|
77 |
// this.inserted = this.source!=null;
|
78 |
this.modified = feature.modified;
|
79 |
} |
80 |
|
81 |
public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
|
82 |
// Se trata de un editable feature sobre una ya existente
|
83 |
super(store, data);
|
84 |
this.source = null; |
85 |
// this.inserted = this.source!=null;
|
86 |
this.modified = false; |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public Feature getSource() {
|
91 |
return this.source; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public boolean isUpdatable() { |
96 |
return super.isInserted(); |
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void setUpdatable(boolean updatable) { |
101 |
super.setInserted(updatable);
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public EditableFeature getEditable() {
|
106 |
return this; |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public Feature getCopy() {
|
111 |
return new DefaultEditableFeature(this); |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public Feature getNotEditableCopy() {
|
116 |
return new DefaultFeature(this); |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void setDefaultGeometry(Geometry geometry) { |
121 |
FeatureAttributeDescriptor attribute = this.getType()
|
122 |
.getAttributeDescriptor( |
123 |
this.getType().getDefaultGeometryAttributeIndex());
|
124 |
modified = true;
|
125 |
this.set(attribute, geometry);
|
126 |
} |
127 |
|
128 |
public void __setitem__(String name, Object value) { |
129 |
this.set(name, value);
|
130 |
} |
131 |
|
132 |
@Override
|
133 |
public void set(String name, Object value) { |
134 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
135 |
if ( attribute == null ) { |
136 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
137 |
} |
138 |
modified = true;
|
139 |
this.set(attribute, value);
|
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void set(int index, Object value) { |
144 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
145 |
modified = true;
|
146 |
this.set(attribute, value);
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void setArray(String name, Object[] value) { |
151 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
152 |
if ( attribute == null ) { |
153 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
154 |
} |
155 |
modified = true;
|
156 |
this.set(attribute, value);
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
public void setArray(int index, Object[] value) { |
161 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
162 |
modified = true;
|
163 |
this.set(attribute, value);
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
public void setBoolean(String name, boolean value) { |
168 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
169 |
if ( attribute == null ) { |
170 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
171 |
} |
172 |
modified = true;
|
173 |
this.set(attribute, Boolean.valueOf(value)); |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public void setBoolean(int index, boolean value) { |
178 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
179 |
modified = true;
|
180 |
this.set(attribute, value);
|
181 |
} |
182 |
|
183 |
@Override
|
184 |
public void setByte(String name, byte value) { |
185 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
186 |
if ( attribute == null ) { |
187 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
188 |
} |
189 |
modified = true;
|
190 |
this.set(attribute, value);
|
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void setByte(int index, byte value) { |
195 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
196 |
modified = true;
|
197 |
this.set(attribute, value);
|
198 |
} |
199 |
|
200 |
@Override
|
201 |
public void setDate(String name, Date 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 |
modified = true;
|
207 |
this.set(attribute, value);
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void setDate(int index, Date value) { |
212 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
213 |
modified = true;
|
214 |
this.set(attribute, value);
|
215 |
} |
216 |
|
217 |
@Override
|
218 |
public void setDouble(String name, double value) { |
219 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
220 |
if ( attribute == null ) { |
221 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
222 |
} |
223 |
modified = true;
|
224 |
this.set(attribute, value);
|
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void setDouble(int index, double value) { |
229 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
230 |
modified = true;
|
231 |
this.set(attribute, value);
|
232 |
} |
233 |
|
234 |
@Override
|
235 |
public void setDecimal(String name, BigDecimal value) { |
236 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
237 |
if ( attribute == null ) { |
238 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
239 |
} |
240 |
modified = true;
|
241 |
this.set(attribute, value);
|
242 |
} |
243 |
|
244 |
@Override
|
245 |
public void setDecimal(int index, BigDecimal value) { |
246 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
247 |
modified = true;
|
248 |
this.set(attribute, value);
|
249 |
} |
250 |
|
251 |
@Override
|
252 |
public void setFeature(String name, Feature value) { |
253 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
254 |
if ( attribute == null ) { |
255 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
256 |
} |
257 |
modified = true;
|
258 |
this.set(attribute, value);
|
259 |
} |
260 |
|
261 |
@Override
|
262 |
public void setFeature(int index, Feature value) { |
263 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
264 |
modified = true;
|
265 |
this.set(attribute, value);
|
266 |
} |
267 |
|
268 |
@Override
|
269 |
public void setFloat(String name, float value) { |
270 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
271 |
if ( attribute == null ) { |
272 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
273 |
} |
274 |
modified = true;
|
275 |
this.set(attribute, value);
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public void setFloat(int index, float value) { |
280 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
281 |
modified = true;
|
282 |
this.set(attribute, value);
|
283 |
} |
284 |
|
285 |
@Override
|
286 |
public void setGeometry(String name, Geometry value) { |
287 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
288 |
if ( attribute == null ) { |
289 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
290 |
} |
291 |
modified = true;
|
292 |
this.set(attribute, value);
|
293 |
} |
294 |
|
295 |
@Override
|
296 |
public void setGeometry(int index, Geometry value) { |
297 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
298 |
modified = true;
|
299 |
this.set(attribute, value);
|
300 |
} |
301 |
|
302 |
@Override
|
303 |
public void setInt(String name, int value) { |
304 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
305 |
if ( attribute == null ) { |
306 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
307 |
} |
308 |
modified = true;
|
309 |
this.set(attribute, value);
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
public void setInt(int index, int value) { |
314 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
315 |
modified = true;
|
316 |
this.set(attribute, value);
|
317 |
} |
318 |
|
319 |
@Override
|
320 |
public void setLong(String name, long value) { |
321 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
322 |
if ( attribute == null ) { |
323 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
324 |
} |
325 |
modified = true;
|
326 |
this.set(attribute, value);
|
327 |
} |
328 |
|
329 |
@Override
|
330 |
public void setLong(int index, long value) { |
331 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
332 |
modified = true;
|
333 |
this.set(attribute, value);
|
334 |
} |
335 |
|
336 |
@Override
|
337 |
public void setString(String name, String value) { |
338 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
339 |
if ( attribute == null ) { |
340 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
341 |
} |
342 |
modified = true;
|
343 |
this.set(attribute, value);
|
344 |
} |
345 |
|
346 |
@Override
|
347 |
public void setString(int index, String value) { |
348 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
349 |
modified = true;
|
350 |
this.set(attribute, value);
|
351 |
} |
352 |
|
353 |
@Override
|
354 |
public void copyFrom(JsonObject values) { |
355 |
this.copyFrom(values, null); |
356 |
} |
357 |
|
358 |
private boolean notCopyAttribute(FeatureAttributeDescriptor attr,Predicate<FeatureAttributeDescriptor> filter) { |
359 |
// helper function to use in copyFrom
|
360 |
if (attr==null ) { |
361 |
return true; |
362 |
} |
363 |
if (attr.isAutomatic() || attr.isComputed() ) {
|
364 |
return true; |
365 |
} |
366 |
if( this.isInserted() && attr.isReadOnly()) { |
367 |
return true; |
368 |
} |
369 |
if( filter!=null && !filter.test(attr) ) { |
370 |
return true; |
371 |
} |
372 |
return false; |
373 |
} |
374 |
|
375 |
@Override
|
376 |
public void copyFrom(JsonObject values, Predicate<FeatureAttributeDescriptor> filter) { |
377 |
// iterate over the attributes and copy one by one
|
378 |
if( values == null ) { |
379 |
throw new IllegalArgumentException("'values' argument can't be null"); |
380 |
} |
381 |
boolean geometryCopied = false; |
382 |
for (FeatureAttributeDescriptor attr : this.getType()) { |
383 |
if( notCopyAttribute(attr, filter) ) {
|
384 |
continue;
|
385 |
} |
386 |
String attrname = attr.getName();
|
387 |
if( !values.containsKey(attrname) ) {
|
388 |
continue;
|
389 |
} |
390 |
Object value;
|
391 |
JsonValue jsonValue = values.get(attrname); |
392 |
try {
|
393 |
switch( jsonValue.getValueType() ) {
|
394 |
case STRING:
|
395 |
String s = ((JsonString)jsonValue).getString();
|
396 |
switch(attr.getType()) {
|
397 |
case DataTypes.GEOMETRY:
|
398 |
value = GeometryUtils.createFrom(s); |
399 |
geometryCopied = true;
|
400 |
break;
|
401 |
case DataTypes.TIMESTAMP:
|
402 |
try {
|
403 |
TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(s); |
404 |
LocalDateTime date = LocalDateTime.from(x); |
405 |
value = java.sql.Timestamp.valueOf(date); |
406 |
} catch(Exception ex) { |
407 |
value = s; |
408 |
} |
409 |
break;
|
410 |
case DataTypes.DATE:
|
411 |
try {
|
412 |
TemporalAccessor x = DateTimeFormatter.ISO_DATE.parse(s); |
413 |
LocalDate date = LocalDate.from(x); |
414 |
value = java.sql.Date.valueOf(date); |
415 |
} catch(Exception ex) { |
416 |
value = s; |
417 |
} |
418 |
break;
|
419 |
case DataTypes.TIME:
|
420 |
try {
|
421 |
TemporalAccessor x = DateTimeFormatter.ISO_TIME.parse(s); |
422 |
LocalTime date = LocalTime.from(x); |
423 |
value = java.sql.Time.valueOf(date); |
424 |
} catch(Exception ex) { |
425 |
value = s; |
426 |
} |
427 |
break;
|
428 |
case DataTypes.STRING:
|
429 |
default:
|
430 |
value = s; |
431 |
break;
|
432 |
} |
433 |
break;
|
434 |
case NUMBER:
|
435 |
JsonNumber n = (JsonNumber) jsonValue; |
436 |
switch(attr.getType()) {
|
437 |
case DataTypes.DECIMAL:
|
438 |
value = n.bigDecimalValue(); |
439 |
break;
|
440 |
case DataTypes.BYTE:
|
441 |
value = n.bigDecimalValue(); |
442 |
break;
|
443 |
case DataTypes.FLOAT:
|
444 |
case DataTypes.DOUBLE:
|
445 |
value = n.doubleValue(); |
446 |
break;
|
447 |
case DataTypes.INT:
|
448 |
value = n.intValue(); |
449 |
break;
|
450 |
case DataTypes.LONG:
|
451 |
value = n.longValue(); |
452 |
break;
|
453 |
default:
|
454 |
value = n.toString(); |
455 |
break;
|
456 |
} |
457 |
break;
|
458 |
case TRUE:
|
459 |
value = true;
|
460 |
break;
|
461 |
case FALSE:
|
462 |
value = false;
|
463 |
break;
|
464 |
case NULL:
|
465 |
value = null;
|
466 |
break;
|
467 |
default:
|
468 |
value = values.getString(attrname); |
469 |
break;
|
470 |
} |
471 |
} catch(Exception ex) { |
472 |
continue;
|
473 |
} |
474 |
if (value == null && !attr.allowNull()) { |
475 |
continue;
|
476 |
} |
477 |
try {
|
478 |
set(attr.getIndex(), value); |
479 |
} catch(Throwable th) { |
480 |
// Ignore
|
481 |
} |
482 |
} |
483 |
FeatureAttributeDescriptor attr = this.getType().getDefaultGeometryAttribute();
|
484 |
if (attr!=null && !geometryCopied) { |
485 |
if( filter==null || filter.test(attr) ) { |
486 |
GeometryType geomType = this.getType().getDefaultGeometryAttribute().getGeomType();
|
487 |
Set<String> keys = values.keySet(); |
488 |
for (String key : keys) { |
489 |
try {
|
490 |
String wkt = values.getString(key);
|
491 |
Geometry geometry = GeometryUtils.createFrom(wkt); |
492 |
if (GeometryUtils.isSubtype(geomType.getType(), geometry.getType()) || GeometryUtils.canAggregate(geomType.getType(), geometry.getType())) {
|
493 |
this.setDefaultGeometry(geometry);
|
494 |
break;
|
495 |
} |
496 |
} catch (Throwable tr) { |
497 |
LOG.trace("Can't get geometry from field {}.", new Object[]{key}); |
498 |
}; |
499 |
} |
500 |
} |
501 |
} |
502 |
} |
503 |
|
504 |
@Override
|
505 |
public void copyFrom(Feature source) { |
506 |
this.copyFrom(source, null); |
507 |
} |
508 |
|
509 |
@Override
|
510 |
public void copyFrom(Feature source, Predicate<FeatureAttributeDescriptor> filter) { |
511 |
// iterate over the attributes and copy one by one
|
512 |
if( source == null ) { |
513 |
throw new IllegalArgumentException("'source' argument can't be null"); |
514 |
} |
515 |
FeatureType sourceType = source.getType(); |
516 |
for (FeatureAttributeDescriptor attr : this.getType()) { |
517 |
if( notCopyAttribute(attr, filter) ) {
|
518 |
continue;
|
519 |
} |
520 |
String attrname = attr.getName();
|
521 |
if( sourceType.getAttributeDescriptor(attrname)==null ) { |
522 |
continue;
|
523 |
} |
524 |
Object value = source.get(attrname);
|
525 |
if (value == null && !attr.allowNull()) { |
526 |
continue;
|
527 |
} |
528 |
try {
|
529 |
set(attr.getIndex(), value); |
530 |
} catch(Throwable th) { |
531 |
LOG.trace("The exception thrown is: ", th);
|
532 |
} |
533 |
} |
534 |
|
535 |
} |
536 |
|
537 |
|
538 |
|
539 |
@Override
|
540 |
public void setInstant(String name, Instant value) { |
541 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
542 |
if ( attribute == null ) { |
543 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
544 |
} |
545 |
modified = true;
|
546 |
this.set(attribute, value);
|
547 |
} |
548 |
|
549 |
@Override
|
550 |
public void setInstant(int index, Instant value) { |
551 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
552 |
modified = true;
|
553 |
this.set(attribute, value);
|
554 |
} |
555 |
|
556 |
@Override
|
557 |
public void setInterval(String name, Interval value) { |
558 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
|
559 |
if ( attribute == null ) { |
560 |
throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature."); |
561 |
} |
562 |
modified = true;
|
563 |
this.set(attribute, value);
|
564 |
} |
565 |
|
566 |
@Override
|
567 |
public void setInterval(int index, Interval value) { |
568 |
FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
|
569 |
modified = true;
|
570 |
this.set(attribute, value);
|
571 |
} |
572 |
|
573 |
@Override
|
574 |
public Object getExtraValue(String name) { |
575 |
Object value;
|
576 |
FeatureExtraColumns columns = this.getType().getExtraColumns();
|
577 |
int index = columns.getIndexOf(name);
|
578 |
if (index >= 0) { |
579 |
EditableFeatureAttributeDescriptor attrdesc = columns.get(index); |
580 |
value = attrdesc.getFeatureAttributeEmulator().get(this);
|
581 |
return value;
|
582 |
} |
583 |
value = this.data.getExtraValue(name);
|
584 |
return value;
|
585 |
} |
586 |
|
587 |
@Override
|
588 |
public void validate(int check) throws DataException { |
589 |
((DefaultFeatureType) this.data.getType()).validateFeature(this, check); |
590 |
} |
591 |
|
592 |
public boolean isModified() { |
593 |
return modified;
|
594 |
} |
595 |
|
596 |
} |