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

History | View | Annotate | Download (19.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.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

    
66
    protected DefaultEditableFeature(DefaultFeature feature) {
67
        super(feature);
68
        this.source = feature;
69
//        this.inserted = this.source!=null;
70
    }
71

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

    
78
    public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
79
        // Se trata de un editable feature sobre una ya existente
80
        super(store, data);
81
        this.source = null;
82
//        this.inserted = this.source!=null;
83
    }
84

    
85
    @Override
86
    public Feature getSource() {
87
        return this.source;
88
    }
89

    
90
    @Override
91
    public boolean isUpdatable() {
92
        return super.isInserted();
93
    }
94
    
95
    @Override
96
    public void setUpdatable(boolean updatable) {
97
        super.setInserted(updatable);
98
    }
99
    
100
    @Override
101
    public EditableFeature getEditable() {
102
        return this;
103
    }
104

    
105
    @Override
106
    public Feature getCopy() {
107
        return new DefaultEditableFeature(this);
108
    }
109

    
110
    @Override
111
    public Feature getNotEditableCopy() {
112
        return new DefaultFeature(this);
113
    }
114

    
115
    @Override
116
    public void setDefaultGeometry(Geometry geometry) {
117
        FeatureAttributeDescriptor attribute = this.getType()
118
                .getAttributeDescriptor(
119
                        this.getType().getDefaultGeometryAttributeIndex());
120
        this.set(attribute, geometry);
121
    }
122

    
123
    public void __setitem__(String name, Object value) {
124
        this.set(name, value);
125
    }
126
    
127
    @Override
128
    public void set(String name, Object value) {
129
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
130
        if ( attribute == null ) {
131
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
132
        }
133
        this.set(attribute, value);
134
    }
135

    
136
    @Override
137
    public void set(int index, Object value) {
138
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
139
        this.set(attribute, value);
140
    }
141

    
142
    @Override
143
    public void setArray(String name, Object[] value) {
144
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
145
        if ( attribute == null ) {
146
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
147
        }
148
        this.set(attribute, value);
149
    }
150

    
151
    @Override
152
    public void setArray(int index, Object[] value) {
153
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
154
        this.set(attribute, value);
155
    }
156

    
157
    @Override
158
    public void setBoolean(String name, boolean value) {
159
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
160
        if ( attribute == null ) {
161
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
162
        }
163
        this.set(attribute, Boolean.valueOf(value));
164
    }
165

    
166
    @Override
167
    public void setBoolean(int index, boolean value) {
168
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
169
        this.set(attribute, value);
170
    }
171

    
172
    @Override
173
    public void setByte(String name, byte value) {
174
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
175
        if ( attribute == null ) {
176
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
177
        }
178
        this.set(attribute, value);
179
    }
180

    
181
    @Override
182
    public void setByte(int index, byte value) {
183
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
184
        this.set(attribute, value);
185
    }
186

    
187
    @Override
188
    public void setDate(String name, Date 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

    
196
    @Override
197
    public void setDate(int index, Date value) {
198
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
199
        this.set(attribute, value);
200
    }
201

    
202
    @Override
203
    public void setDouble(String name, double value) {
204
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
205
        if ( attribute == null ) {
206
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
207
        }
208
        this.set(attribute, value);
209
    }
210

    
211
    @Override
212
    public void setDouble(int index, double value) {
213
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
214
        this.set(attribute, value);
215
    }
216

    
217
    @Override
218
    public void setDecimal(String name, BigDecimal 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
        this.set(attribute, value);
224
    }
225

    
226
    @Override
227
    public void setDecimal(int index, BigDecimal value) {
228
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
229
        this.set(attribute, value);
230
    }
231

    
232
    @Override
233
    public void setFeature(String name, Feature value) {
234
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
235
        if ( attribute == null ) {
236
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
237
        }
238
        this.set(attribute, value);
239
    }
240

    
241
    @Override
242
    public void setFeature(int index, Feature value) {
243
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
244
        this.set(attribute, value);
245
    }
246

    
247
    @Override
248
    public void setFloat(String name, float value) {
249
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
250
        if ( attribute == null ) {
251
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
252
        }
253
        this.set(attribute, value);
254
    }
255

    
256
    @Override
257
    public void setFloat(int index, float value) {
258
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
259
        this.set(attribute, value);
260
    }
261

    
262
    @Override
263
    public void setGeometry(String name, Geometry value) {
264
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
265
        if ( attribute == null ) {
266
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
267
        }
268
        this.set(attribute, value);
269
    }
270

    
271
    @Override
272
    public void setGeometry(int index, Geometry value) {
273
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
274
        this.set(attribute, value);
275
    }
276

    
277
    @Override
278
    public void setInt(String name, int value) {
279
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
280
        if ( attribute == null ) {
281
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
282
        }
283
        this.set(attribute, value);
284
    }
285

    
286
    @Override
287
    public void setInt(int index, int value) {
288
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
289
        this.set(attribute, value);
290
    }
291

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

    
301
    @Override
302
    public void setLong(int index, long value) {
303
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
304
        this.set(attribute, value);
305
    }
306

    
307
    @Override
308
    public void setString(String name, String 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
        this.set(attribute, value);
314
    }
315

    
316
    @Override
317
    public void setString(int index, String value) {
318
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
319
        this.set(attribute, value);
320
    }
321

    
322
    @Override
323
    public void copyFrom(JsonObject values) {
324
        this.copyFrom(values, null);    
325
    }
326

    
327
    @Override
328
    public void copyFrom(JsonObject values, Predicate<FeatureAttributeDescriptor> filter) {
329
      // iterate over the attributes and copy one by one
330
      boolean geometryCopied = false;
331
        for (FeatureAttributeDescriptor attr : this.getType()) {
332
            if (attr==null  ) {
333
                continue;
334
            }
335
            if( filter!=null && !filter.test(attr) ) {
336
                continue;
337
            }
338
            if (attr.isAutomatic()  || attr.isComputed() ) {
339
                continue;
340
            }
341
            if( this.isInserted() &&  attr.isReadOnly()) {
342
                continue;
343
            }
344
            String attrname = attr.getName();
345
            if( !values.containsKey(attrname) ) {
346
                continue;
347
            }           
348
            Object value;
349
            JsonValue jsonValue = values.get(attrname);
350
            try {
351
                switch( jsonValue.getValueType() ) {
352
                    case STRING:
353
                        String s = ((JsonString)jsonValue).getString();
354
                        switch(attr.getType()) {
355
                            case DataTypes.GEOMETRY:
356
                                value = GeometryUtils.createFrom(s);
357
                                geometryCopied = true;
358
                                break;
359
                            case DataTypes.TIMESTAMP:
360
                                try {
361
                                    TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(s);
362
                                    LocalDateTime date = LocalDateTime.from(x);
363
                                    value = java.sql.Timestamp.valueOf(date);
364
                                } catch(Exception ex) {
365
                                    value = s;
366
                                }
367
                                break;
368
                            case DataTypes.DATE:
369
                                try {
370
                                    TemporalAccessor x = DateTimeFormatter.ISO_DATE.parse(s);
371
                                    LocalDate date = LocalDate.from(x);
372
                                    value = java.sql.Date.valueOf(date);
373
                                } catch(Exception ex) {
374
                                    value = s;
375
                                }
376
                                break;
377
                            case DataTypes.TIME:
378
                                try {
379
                                    TemporalAccessor x = DateTimeFormatter.ISO_TIME.parse(s);
380
                                    LocalTime date = LocalTime.from(x);
381
                                    value = java.sql.Time.valueOf(date);
382
                                } catch(Exception ex) {
383
                                    value = s;
384
                                }
385
                                break;
386
                            case DataTypes.STRING:
387
                            default:
388
                                value = s;
389
                                break;
390
                        }
391
                        break;
392
                    case NUMBER:
393
                        JsonNumber n =  (JsonNumber) jsonValue;
394
                        switch(attr.getType()) {
395
                            case DataTypes.DECIMAL:
396
                                value = n.bigDecimalValue();
397
                                break;
398
                            case DataTypes.BYTE:
399
                                value = n.bigDecimalValue();
400
                                break;
401
                            case DataTypes.FLOAT:
402
                            case DataTypes.DOUBLE:
403
                                value = n.doubleValue();
404
                                break;
405
                            case DataTypes.INT:
406
                                value = n.intValue();
407
                                break;
408
                            case DataTypes.LONG:
409
                                value = n.longValue();
410
                                break;
411
                            default:
412
                                value = n.toString();
413
                                break;
414
                        }
415
                        break;
416
                    case TRUE:
417
                        value = true;
418
                        break;
419
                    case FALSE:
420
                        value = false;
421
                        break;
422
                    case NULL:
423
                        value = null;
424
                        break;
425
                    default:
426
                        value = values.getString(attrname);
427
                        break;
428
                }
429
            } catch(Exception ex) {
430
                continue;
431
            }
432
            if (value == null && !attr.allowNull()) {
433
                continue;
434
            }
435
            try {
436
                set(attr.getIndex(), value);
437
            } catch(Throwable th) {
438
                // Ignore
439
            }
440
        }
441
        FeatureAttributeDescriptor attr = this.getType().getDefaultGeometryAttribute();
442
        if (attr!=null && !geometryCopied) {
443
            if( filter==null || filter.test(attr) ) {
444
                GeometryType geomType = this.getType().getDefaultGeometryAttribute().getGeomType();
445
                Set<String> keys = values.keySet();
446
                for (String key : keys) {
447
                    try {
448
                        String wkt = values.getString(key);
449
                        Geometry geometry = GeometryUtils.createFrom(wkt);
450
                        if (GeometryUtils.isSubtype(geomType.getType(), geometry.getType()) || GeometryUtils.canAggregate(geomType.getType(), geometry.getType())) {
451
                            this.setDefaultGeometry(geometry);
452
                            break;
453
                        }
454
                    } catch (Throwable tr) {
455
                        LOG.trace("Can't get geometry from field {}.", new Object[]{key});
456
                    };
457
                }
458
            }
459
        }
460
    }
461
    
462
    @Override
463
    public void copyFrom(Feature source) {
464
        this.copyFrom(source, null);
465
    }
466
    
467
    @Override
468
    public void copyFrom(Feature source, Predicate<FeatureAttributeDescriptor> filter) {
469
      // iterate over the attributes and copy one by one
470
        FeatureType sourceType = source.getType();
471
        for (FeatureAttributeDescriptor attr : this.getType()) {
472
            if (attr==null  ) {
473
                continue;
474
            }
475
            if( filter!=null && !filter.test(attr) ) {
476
                continue;
477
            }
478
            if (attr.isAutomatic()  || attr.isComputed() ) {
479
                continue;
480
            }
481
            if( this.isInserted() &&  attr.isReadOnly()) {
482
                continue;
483
            }
484
            String attrname = attr.getName();
485
            if( sourceType.getAttributeDescriptor(attrname)==null ) {
486
              continue;
487
            }
488
            Object value = source.get(attrname);
489
            if (value == null && !attr.allowNull()) {
490
                continue;
491
            }
492
            try {
493
                set(attr.getIndex(), value);
494
            } catch(Throwable th) {
495
                LOG.trace("The exception thrown is: ", th);
496
            }
497
        }
498
        
499
    }
500
    
501

    
502

    
503
    @Override
504
    public void setInstant(String name, Instant value) {
505
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
506
        if ( attribute == null ) {
507
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
508
        }
509
        this.set(attribute, value);
510
    }
511

    
512
    @Override
513
    public void setInstant(int index, Instant value) {
514
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
515
        this.set(attribute, value);
516
    }
517

    
518
    @Override
519
    public void setInterval(String name, Interval value) {
520
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
521
        if ( attribute == null ) {
522
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
523
        }
524
        this.set(attribute, value);
525
    }
526

    
527
    @Override
528
    public void setInterval(int index, Interval value) {
529
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
530
        this.set(attribute, value);
531
    }
532
    
533
    @Override
534
    public Object getExtraValue(String name) {
535
        Object value;
536
        FeatureExtraColumns columns = this.getType().getExtraColumns();
537
        int index = columns.getIndexOf(name);
538
        if (index >= 0) {
539
            EditableFeatureAttributeDescriptor attrdesc = columns.get(index);
540
            value = attrdesc.getFeatureAttributeEmulator().get(this);
541
            return value;
542
        }
543
        value = this.data.getExtraValue(name);
544
        return value;
545
    }
546

    
547
    @Override
548
    public void validate(int check) throws DataException {
549
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, check);
550
    }
551
    
552
}