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

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

    
485

    
486
    @Override
487
    public void setInstant(String name, Instant value) {
488
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
489
        if ( attribute == null ) {
490
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
491
        }
492
        this.set(attribute, value);
493
    }
494

    
495
    @Override
496
    public void setInstant(int index, Instant value) {
497
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
498
        this.set(attribute, value);
499
    }
500

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

    
510
    @Override
511
    public void setInterval(int index, Interval value) {
512
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
513
        this.set(attribute, value);
514
    }
515
    
516
    @Override
517
    public Object getExtraValue(String name) {
518
        Object value;
519
        FeatureExtraColumns columns = this.getType().getExtraColumns();
520
        int index = columns.getIndexOf(name);
521
        if (index >= 0) {
522
            EditableFeatureAttributeDescriptor attrdesc = columns.get(index);
523
            value = attrdesc.getFeatureAttributeEmulator().get(this);
524
            return value;
525
        }
526
        value = this.data.getExtraValue(name);
527
        return value;
528
    }
529

    
530
    @Override
531
    public void validate(int check) throws DataException {
532
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, check);
533
    }
534
    
535
}