Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-cvsgis1 / 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 @ 45275

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

    
42
import org.gvsig.fmap.dal.feature.EditableFeature;
43
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.FeatureType;
47
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.GeometryUtils;
50
import org.gvsig.timesupport.Instant;
51
import org.gvsig.timesupport.Interval;
52
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
53
import org.gvsig.fmap.geom.type.GeometryType;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
public class DefaultEditableFeature extends DefaultFeature implements
58
        EditableFeature {
59

    
60
    private static final Logger LOG = LoggerFactory.getLogger(DefaultEditableFeature.class);
61

    
62
    private DefaultFeature source;
63
    private boolean updatable;
64

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
171
    @Override
172
    public void setByte(String name, byte 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
        this.set(attribute, value);
178
    }
179

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

    
186
    @Override
187
    public void setDate(String name, Date value) {
188
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
189
        if ( attribute == null ) {
190
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
191
        }
192
        this.set(attribute, value);
193
    }
194

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

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

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

    
216
    @Override
217
    public void setDecimal(String name, BigDecimal value) {
218
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
219
        if ( attribute == null ) {
220
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
221
        }
222
        this.set(attribute, value);
223
    }
224

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
484

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

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

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

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