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

History | View | Annotate | Download (21.7 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.Set;
34
import java.util.function.Predicate;
35
import javax.json.JsonNumber;
36
import javax.json.JsonObject;
37
import javax.json.JsonString;
38
import javax.json.JsonValue;
39
import org.gvsig.fmap.dal.DataTypeUtils;
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( !canSetValue(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.BYTEARRAY:
429
                                value = DataTypeUtils.coerce(DataTypes.BYTEARRAY, s, null);
430
                                break;
431
                            case DataTypes.STRING:
432
                            default:
433
                                value = s;
434
                                break;
435
                        }
436
                        break;
437
                    case NUMBER:
438
                        JsonNumber n =  (JsonNumber) jsonValue;
439
                        switch(attr.getType()) {
440
                            case DataTypes.DECIMAL:
441
                                value = n.bigDecimalValue();
442
                                break;
443
                            case DataTypes.BYTE:
444
                                value = n.bigDecimalValue();
445
                                break;
446
                            case DataTypes.FLOAT:
447
                            case DataTypes.DOUBLE:
448
                                value = n.doubleValue();
449
                                break;
450
                            case DataTypes.INT:
451
                                value = n.intValue();
452
                                break;
453
                            case DataTypes.LONG:
454
                                value = n.longValue();
455
                                break;
456
                            default:
457
                                value = n.toString();
458
                                break;
459
                        }
460
                        break;
461
                    case TRUE:
462
                        value = true;
463
                        break;
464
                    case FALSE:
465
                        value = false;
466
                        break;
467
                    case NULL:
468
                        value = null;
469
                        break;
470
                    default:
471
                        value = values.getString(attrname);
472
                        break;
473
                }
474
            } catch(Exception ex) {
475
                continue;
476
            }
477
            if (value == null && !attr.allowNull()) {
478
                continue;
479
            }
480
            try {
481
                set(attr.getIndex(), value);
482
            } catch(Throwable th) {
483
                // Ignore
484
            }
485
        }
486
        FeatureAttributeDescriptor attr = this.getType().getDefaultGeometryAttribute();
487
        if (attr!=null && !geometryCopied) {
488
            if( filter==null || filter.test(attr) ) {
489
                GeometryType geomType = this.getType().getDefaultGeometryAttribute().getGeomType();
490
                Set<String> keys = values.keySet();
491
                for (String key : keys) {
492
                    try {
493
                        String wkt = values.getString(key);
494
                        Geometry geometry = GeometryUtils.createFrom(wkt);
495
                        if (GeometryUtils.isSubtype(geomType.getType(), geometry.getType()) || GeometryUtils.canAggregate(geomType.getType(), geometry.getType())) {
496
                            this.setDefaultGeometry(geometry);
497
                            break;
498
                        }
499
                    } catch (Throwable tr) {
500
                        LOG.trace("Can't get geometry from field {}.", new Object[]{key});
501
                    };
502
                }
503
            }
504
        }
505
    }
506
    
507
    @Override
508
    public void copyFrom(Feature source) {
509
        this.copyFrom(source, null);
510
    }
511
    
512
    @Override
513
    public void copyFrom(Feature source, Predicate<FeatureAttributeDescriptor> filter) {
514
      // iterate over the attributes and copy one by one
515
        if( source == null ) {
516
            throw new IllegalArgumentException("'source' argument can't be null");
517
        }
518
        FeatureType sourceType = source.getType();
519
        for (FeatureAttributeDescriptor attrTarget : this.getType()) {
520
            if( !canSetValue(attrTarget, filter) ) {
521
                continue;
522
            }
523
            String attrname = attrTarget.getName();
524
            if( sourceType.getAttributeDescriptorFromAll(attrname)==null ) {
525
              continue;
526
            }
527
            FeatureAttributeDescriptor attrSource = sourceType.getAttributeDescriptor(attrname);
528
            Object value;
529
            if( attrSource.hasDataProfile() ) {
530
                value = source.getFromProfile(attrname);
531
                if( value == null ) {
532
                    value = source.get(attrname);
533
                }
534
            } else {
535
                value = source.get(attrname);
536
            }
537
            if (value == null && !attrTarget.allowNull()) {
538
                continue;
539
            }
540
            try {
541
                set(attrTarget.getIndex(), value);
542
            } catch(Throwable th) {
543
                LOG.trace("The exception thrown is: ", th);
544
            }
545
        }
546
        
547
    }
548
    
549

    
550

    
551
    @Override
552
    public void setInstant(String name, Instant value) {
553
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
554
        if ( attribute == null ) {
555
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
556
        }
557
        modified = true;
558
        this.set(attribute, value);
559
    }
560

    
561
    @Override
562
    public void setInstant(int index, Instant value) {
563
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
564
        modified = true;
565
        this.set(attribute, value);
566
    }
567

    
568
    @Override
569
    public void setInterval(String name, Interval value) {
570
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
571
        if ( attribute == null ) {
572
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
573
        }
574
        modified = true;
575
        this.set(attribute, value);
576
    }
577

    
578
    @Override
579
    public void setInterval(int index, Interval value) {
580
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
581
        modified = true;
582
        this.set(attribute, value);
583
    }
584
    
585
    @Override
586
    public Object getExtraValue(String name) {
587
        Object value;
588
        FeatureExtraColumns columns = this.getType().getExtraColumns();
589
        int index = columns.getIndexOf(name);
590
        if (index >= 0) {
591
            EditableFeatureAttributeDescriptor attrdesc = columns.get(index);
592
            value = attrdesc.getFeatureAttributeEmulator().get(this);
593
            return value;
594
        }
595
        value = this.data.getExtraValue(name);
596
        return value;
597
    }
598

    
599
    @Override
600
    public void validate(int check) throws DataException {
601
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, check);
602
    }
603
    
604
    public boolean isModified() {
605
        return modified;
606
    }
607
    
608
}