Statistics
| Revision:

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 @ 47556

History | View | Annotate | Download (23.4 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.format.DateTimeFormatter;
31
import java.time.temporal.TemporalAccessor;
32
import java.util.Date;
33
import java.util.Objects;
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.apache.commons.lang3.StringUtils;
41
import org.gvsig.fmap.dal.DataTypeUtils;
42
import org.gvsig.fmap.dal.DataTypes;
43
import org.gvsig.fmap.dal.exception.DataException;
44

    
45
import org.gvsig.fmap.dal.feature.EditableFeature;
46
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.Feature;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
49
import org.gvsig.fmap.dal.feature.FeatureType;
50
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
51
import org.gvsig.fmap.geom.Geometry;
52
import org.gvsig.fmap.geom.GeometryUtils;
53
import org.gvsig.timesupport.Instant;
54
import org.gvsig.timesupport.Interval;
55
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.geom.type.GeometryType;
58
import org.gvsig.tools.exception.BaseException;
59
import org.gvsig.tools.util.Invocable;
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

    
63
public class DefaultEditableFeature extends DefaultFeature implements
64
        EditableFeature {
65

    
66
    private static final Logger LOG = LoggerFactory.getLogger(DefaultEditableFeature.class);
67

    
68
    private DefaultFeature source;
69
//    private boolean inserted;
70
    private boolean modified;
71

    
72
    protected DefaultEditableFeature(DefaultFeature feature) {
73
        super(feature);
74
        this.source = feature;
75
//        this.inserted = this.source!=null;
76
        this.modified = false;
77
    }
78

    
79
    protected DefaultEditableFeature(DefaultEditableFeature feature) {
80
        super(feature);
81
        this.source = (DefaultFeature) feature.getSource();
82
//        this.inserted = this.source!=null;
83
        this.modified = feature.modified;
84
    }
85

    
86
    public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
87
        // Se trata de un editable feature sobre una ya existente
88
        super(store, data);
89
        this.source = null;
90
//        this.inserted = this.source!=null;
91
        this.modified = false;
92
    }
93

    
94
    @Override
95
    public Feature getSource() {
96
        return this.source;
97
    }
98

    
99
    @Override
100
    public boolean isUpdatable() {
101
        return super.isInserted();
102
    }
103
    
104
    @Override
105
    public void setUpdatable(boolean updatable) {
106
        super.setInserted(updatable);
107
    }
108
    
109
    @Override
110
    public EditableFeature getEditable() {
111
        return this;
112
    }
113

    
114
    @Override
115
    public Feature getCopy() {
116
        return new DefaultEditableFeature(this);
117
    }
118

    
119
    @Override
120
    public Feature getNotEditableCopy() {
121
        return new DefaultFeature(this);
122
    }
123

    
124
    @Override
125
    public void setDefaultGeometry(Geometry geometry) {
126
        FeatureAttributeDescriptor attribute = this.getType()
127
                .getAttributeDescriptor(
128
                        this.getType().getDefaultGeometryAttributeIndex());
129
        modified = true;
130
        this.set(attribute, geometry);
131
    }
132

    
133
    public void __setitem__(String name, Object value) {
134
        this.set(name, value);
135
    }
136
    
137
    @Override
138
    public void set(String name, Object 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
        modified = true;
144
        this.set(attribute, value);
145
    }
146

    
147
    @Override
148
    public void set(int index, Object value) {
149
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
150
        modified = true;
151
        this.set(attribute, value);
152
    }
153

    
154
    @Override
155
    public void setArray(String name, Object[] value) {
156
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
157
        if ( attribute == null ) {
158
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
159
        }
160
        modified = true;
161
        this.set(attribute, value);
162
    }
163

    
164
    @Override
165
    public void setArray(int index, Object[] value) {
166
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
167
        modified = true;
168
        this.set(attribute, value);
169
    }
170
    
171
    @Override
172
    public void setBoolean(String name, boolean value) {
173
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
174
        if ( attribute == null ) {
175
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
176
        }
177
        modified = true;
178
        this.set(attribute, Boolean.valueOf(value));
179
    }
180

    
181
    @Override
182
    public void setBoolean(int index, boolean value) {
183
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
184
        modified = true;
185
        this.set(attribute, value);
186
    }
187

    
188
    @Override
189
    public void setByte(String name, byte value) {
190
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
191
        if ( attribute == null ) {
192
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
193
        }
194
        modified = true;
195
        this.set(attribute, value);
196
    }
197

    
198
    @Override
199
    public void setByte(int index, byte value) {
200
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
201
        modified = true;
202
        this.set(attribute, value);
203
    }
204

    
205
    @Override
206
    public void setDate(String name, Date value) {
207
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
208
        if ( attribute == null ) {
209
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
210
        }
211
        modified = true;
212
        this.set(attribute, value);
213
    }
214

    
215
    @Override
216
    public void setDate(int index, Date value) {
217
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
218
        modified = true;
219
        this.set(attribute, value);
220
    }
221

    
222
    @Override
223
    public void setDouble(String name, double value) {
224
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
225
        if ( attribute == null ) {
226
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
227
        }
228
        modified = true;
229
        this.set(attribute, value);
230
    }
231

    
232
    @Override
233
    public void setDouble(int index, double value) {
234
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
235
        modified = true;
236
        this.set(attribute, value);
237
    }
238

    
239
    @Override
240
    public void setDecimal(String name, BigDecimal 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
        modified = true;
246
        this.set(attribute, value);
247
    }
248

    
249
    @Override
250
    public void setDecimal(int index, BigDecimal value) {
251
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
252
        modified = true;
253
        this.set(attribute, value);
254
    }
255

    
256
    @Override
257
    public void setFeature(String name, Feature value) {
258
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
259
        if ( attribute == null ) {
260
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
261
        }
262
        modified = true;
263
        this.set(attribute, value);
264
    }
265

    
266
    @Override
267
    public void setFeature(int index, Feature value) {
268
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
269
        modified = true;
270
        this.set(attribute, value);
271
    }
272

    
273
    @Override
274
    public void setFloat(String name, float value) {
275
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
276
        if ( attribute == null ) {
277
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
278
        }
279
        modified = true;
280
        this.set(attribute, value);
281
    }
282

    
283
    @Override
284
    public void setFloat(int index, float value) {
285
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
286
        modified = true;
287
        this.set(attribute, value);
288
    }
289

    
290
    @Override
291
    public void setGeometry(String name, Geometry value) {
292
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
293
        if ( attribute == null ) {
294
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
295
        }
296
        modified = true;
297
        this.set(attribute, value);
298
    }
299

    
300
    @Override
301
    public void setGeometry(int index, Geometry value) {
302
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
303
        modified = true;
304
        this.set(attribute, value);
305
    }
306

    
307
    @Override
308
    public void setInt(String name, int value) {
309
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
310
        if ( attribute == null ) {
311
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
312
        }
313
        modified = true;
314
        this.set(attribute, value);
315
    }
316

    
317
    @Override
318
    public void setInt(int index, int value) {
319
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
320
        modified = true;
321
        this.set(attribute, value);
322
    }
323

    
324
    @Override
325
    public void setLong(String name, long value) {
326
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
327
        if ( attribute == null ) {
328
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
329
        }
330
        modified = true;
331
        this.set(attribute, value);
332
    }
333

    
334
    @Override
335
    public void setLong(int index, long value) {
336
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
337
        modified = true;
338
        this.set(attribute, value);
339
    }
340

    
341
    @Override
342
    public void setString(String name, String value) {
343
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
344
        if ( attribute == null ) {
345
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
346
        }
347
        modified = true;
348
        this.set(attribute, value);
349
    }
350

    
351
    @Override
352
    public void setString(int index, String value) {
353
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
354
        modified = true;
355
        this.set(attribute, value);
356
    }
357

    
358
    @Override
359
    public void copyFrom(JsonObject values) {
360
        this.copyFrom(values, null);    
361
    }
362

    
363
//    private boolean notCopyAttribute(FeatureAttributeDescriptor attr,Predicate<FeatureAttributeDescriptor> filter) {
364
//        // helper function to use in copyFrom
365
//        if (attr==null  ) {
366
//            return true;
367
//        }
368
//        if (attr.isAutomatic()  || attr.isComputed() ) {
369
//            return true;
370
//        }
371
//        if( this.isInserted() &&  attr.isReadOnly()) {
372
//            return true;
373
//        }
374
//        if( filter!=null && !filter.test(attr) ) {
375
//            return true;
376
//        }
377
//        return false;
378
//    }
379
    
380
    @Override
381
    public void copyFrom(JsonObject values, Predicate<FeatureAttributeDescriptor> filter) {
382
      // iterate over the attributes and copy one by one
383
      if( values == null ) {
384
          throw new IllegalArgumentException("'values' argument can't be null");
385
      }
386
      boolean geometryCopied = false;
387
        for (FeatureAttributeDescriptor attr : this.getType()) {
388
            if( !canSetValue(attr, filter) ) {
389
                continue;
390
            }
391
            String attrname = attr.getName();
392
            if( !values.containsKey(attrname) ) {
393
                continue;
394
            }           
395
            Object value;
396
            JsonValue jsonValue = values.get(attrname); 
397
           if(jsonValue == null){
398
                value = null;
399
            } else {
400
                try {
401
                    switch( jsonValue.getValueType() ) {
402
                        case STRING:
403
                            String s = ((JsonString)jsonValue).getString();
404
                            switch(attr.getType()) {
405
                                case DataTypes.GEOMETRY:
406
                                    value = GeometryUtils.createFrom(s);
407
                                    geometryCopied = true;
408
                                    break;
409
                                case DataTypes.TIMESTAMP:
410
                                    try {
411
                                        TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(s);
412
                                        LocalDateTime date = LocalDateTime.from(x);
413
                                        value = java.sql.Timestamp.valueOf(date);
414
                                    } catch(Exception ex) {
415
                                        value = s;
416
                                    }
417
                                    break;
418
                                case DataTypes.DATE:
419
                                    try {
420
                                        TemporalAccessor x = DateTimeFormatter.ISO_DATE.parse(s);
421
                                        LocalDate date = LocalDate.from(x);
422
                                        value = java.sql.Date.valueOf(date);
423
                                    } catch(Exception ex) {
424
                                        value = s;
425
                                    }
426
                                    break;
427
                                case DataTypes.TIME:
428
                                    try {
429
                                        TemporalAccessor x = DateTimeFormatter.ISO_TIME.parse(s);
430
                                        LocalTime date = LocalTime.from(x);
431
                                        value = java.sql.Time.valueOf(date);
432
                                    } catch(Exception ex) {
433
                                        value = s;
434
                                    }
435
                                    break;
436
                                case DataTypes.BYTEARRAY:
437
                                    value = DataTypeUtils.coerce(DataTypes.BYTEARRAY, s, null);
438
                                    break;
439
                                case DataTypes.STRING:
440
                                default:
441
                                    value = s;
442
                                    break;
443
                            }
444
                            break;
445
                        case NUMBER:
446
                            JsonNumber n =  (JsonNumber) jsonValue;
447
                            switch(attr.getType()) {
448
                                case DataTypes.DECIMAL:
449
                                    value = n.bigDecimalValue();
450
                                    break;
451
                                case DataTypes.BYTE:
452
                                    value = n.bigDecimalValue();
453
                                    break;
454
                                case DataTypes.FLOAT:
455
                                case DataTypes.DOUBLE:
456
                                    value = n.doubleValue();
457
                                    break;
458
                                case DataTypes.INT:
459
                                    value = n.intValue();
460
                                    break;
461
                                case DataTypes.LONG:
462
                                    value = n.longValue();
463
                                    break;
464
                                default:
465
                                    value = n.toString();
466
                                    break;
467
                            }
468
                            break;
469
                        case TRUE:
470
                            value = true;
471
                            break;
472
                        case FALSE:
473
                            value = false;
474
                            break;
475
                        case NULL:
476
                            value = null;
477
                            break;
478
                        default:
479
                            value = values.getString(attrname);
480
                            break;
481
                    }
482
                } catch(Exception ex) {
483
                    continue;
484
                }
485
            }
486
            if (value == null && !attr.allowNull()) {
487
                continue;
488
            }
489
            try {
490
                set(attr.getIndex(), value);
491
            } catch(Throwable th) {
492
                LOG.trace("Can't assign value "+Objects.toString(value,null)+" to "+attr.getName());// Ignore
493
            }
494
        }
495
        FeatureAttributeDescriptor attr = this.getType().getDefaultGeometryAttribute();
496
        if (attr!=null && !geometryCopied) {
497
            if( filter==null || filter.test(attr) ) {
498
                GeometryType geomType = this.getType().getDefaultGeometryAttribute().getGeomType();
499
                Set<String> keys = values.keySet();
500
                for (String key : keys) {
501
                    try {
502
                        String wkt = values.getString(key);
503
                        Geometry geometry = GeometryUtils.createFrom(wkt);
504
                        if (GeometryUtils.isSubtype(geomType.getType(), geometry.getType()) || GeometryUtils.canAggregate(geomType.getType(), geometry.getType())) {
505
                            this.setDefaultGeometry(geometry);
506
                            break;
507
                        }
508
                    } catch (Throwable tr) {
509
                        LOG.trace("Can't get geometry from field {}.", new Object[]{key});
510
                    };
511
                }
512
            }
513
        }
514
    }
515
    
516
    @Override
517
    public void copyFrom(Feature source) {
518
        this.copyFrom(source, null, null);
519
    }
520
    
521
    @Override
522
    public void copyFrom(Feature source, Predicate<FeatureAttributeDescriptor> filter) {
523
        copyFrom(source, filter, null);
524
    }
525
    
526
    @Override
527
    public void copyFrom(Feature source, Predicate<FeatureAttributeDescriptor> filter, Invocable onerror) {
528
      // iterate over the attributes and copy one by one
529
        if( source == null ) {
530
            throw new IllegalArgumentException("'source' argument can't be null");
531
        }
532
        FeatureType sourceType = source.getType();
533
        for (FeatureAttributeDescriptor attrTarget : this.getType()) {
534
            if( !canSetValue(attrTarget, filter) ) {
535
                continue;
536
            }
537
            String attrname = attrTarget.getName();
538
            FeatureAttributeDescriptor attrSource = sourceType.getAttributeDescriptorFromAll(attrname);
539
            if( attrSource==null ) {
540
              continue;
541
            }
542
            Object value;
543
            if( attrSource.hasDataProfile() ) {
544
                value = source.getFromProfile(attrname);
545
                if( value == null ) {
546
                    value = source.get(attrname);
547
                }
548
            } else {
549
                value = source.get(attrname);
550
            }
551
            if (value == null && !attrTarget.allowNull()) {
552
                if( onerror != null ) {
553
                    onerror.call("Can't assign null to field '"+attrname+"'.", null);
554
                }
555
                continue;
556
            }
557
            try {
558
                set(attrTarget.getIndex(), value);
559
            } catch(Throwable th) {
560
                if( onerror == null ) {
561
                    LOG.trace("The exception thrown is: ", th);
562
                } else {
563
                    String s = null;
564
                    try {
565
                        s = StringUtils.replace(BaseException.getMessageStack(th, 0),"\n"," ");
566
                    } catch(Throwable th2) {
567
                        LOG.trace("The exception thrown is: ", th);
568
                    }
569
                    onerror.call(s, th);
570
                }
571
            }
572
        }
573
        
574
    }
575
    
576

    
577

    
578
    @Override
579
    public void setInstant(String name, Instant value) {
580
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
581
        if ( attribute == null ) {
582
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
583
        }
584
        modified = true;
585
        this.set(attribute, value);
586
    }
587

    
588
    @Override
589
    public void setInstant(int index, Instant value) {
590
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
591
        modified = true;
592
        this.set(attribute, value);
593
    }
594

    
595
    @Override
596
    public void setInterval(String name, Interval value) {
597
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
598
        if ( attribute == null ) {
599
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
600
        }
601
        modified = true;
602
        this.set(attribute, value);
603
    }
604

    
605
    @Override
606
    public void setInterval(int index, Interval value) {
607
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
608
        modified = true;
609
        this.set(attribute, value);
610
    }
611
    
612
    @Override
613
    public Object getExtraValue(String name) {
614
        Object value;
615
        FeatureExtraColumns columns = this.getType().getExtraColumns();
616
        int index = columns.getIndexOf(name);
617
        if (index >= 0) {
618
            EditableFeatureAttributeDescriptor attrdesc = columns.get(index);
619
            value = attrdesc.getFeatureAttributeEmulator().get(this);
620
            return value;
621
        }
622
        value = this.data.getExtraValue(name);
623
        return value;
624
    }
625

    
626
    @Override
627
    public void validate(int check) throws DataException {
628
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, check);
629
    }
630
    
631
    public boolean isModified() {
632
        return modified;
633
    }
634

    
635
    @Override
636
    public Feature getOriginal() {
637
        FeatureStore store = this.getStore();
638
        if( store == null ) {
639
            return null;
640
        }
641
        Feature original = store.getOriginalFeature(this);
642
        return original;
643
    }
644

    
645
}