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

History | View | Annotate | Download (16.4 KB)

1 40559 jjdelcerro
/**
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 41805 jjdelcerro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 40559 jjdelcerro
 * 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 41805 jjdelcerro
 * MA 02110-1301, USA.
20 40559 jjdelcerro
 *
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 40435 jjdelcerro
package org.gvsig.fmap.dal.feature.impl;
25
26 44669 jjdelcerro
import java.math.BigDecimal;
27 44655 jjdelcerro
import java.time.LocalDateTime;
28
import java.time.ZoneOffset;
29
import java.time.format.DateTimeFormatter;
30
import java.time.temporal.TemporalAccessor;
31 40435 jjdelcerro
import java.util.Date;
32 44655 jjdelcerro
import javax.json.JsonNumber;
33
import javax.json.JsonObject;
34
import org.gvsig.fmap.dal.DataTypes;
35 40435 jjdelcerro
36
import org.gvsig.fmap.dal.feature.EditableFeature;
37 44779 omartinez
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
38 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.Feature;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40 44815 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
41 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
42
import org.gvsig.fmap.geom.Geometry;
43 44655 jjdelcerro
import org.gvsig.fmap.geom.GeometryUtils;
44 40435 jjdelcerro
import org.gvsig.timesupport.Instant;
45
import org.gvsig.timesupport.Interval;
46 44829 omartinez
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
47 40435 jjdelcerro
48
public class DefaultEditableFeature extends DefaultFeature implements
49 41805 jjdelcerro
        EditableFeature {
50 40435 jjdelcerro
51 41805 jjdelcerro
    private DefaultFeature source;
52 40435 jjdelcerro
53 41805 jjdelcerro
    protected DefaultEditableFeature(DefaultFeature feature) {
54
        super(feature);
55
        this.source = feature;
56
    }
57 40435 jjdelcerro
58 41805 jjdelcerro
    protected DefaultEditableFeature(DefaultEditableFeature feature) {
59
        super(feature);
60
        this.source = (DefaultFeature) feature.getSource();
61
    }
62 40435 jjdelcerro
63 41805 jjdelcerro
    public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
64
        // Se trata de un editable feature sobre una ya existente
65
        super(store, data);
66
        this.source = null;
67
    }
68 40435 jjdelcerro
69 44829 omartinez
    @Override
70 41805 jjdelcerro
    public Feature getSource() {
71
        return this.source;
72
    }
73 40435 jjdelcerro
74 44829 omartinez
    @Override
75 41805 jjdelcerro
    public EditableFeature getEditable() {
76
        return this;
77
    }
78 40435 jjdelcerro
79 44829 omartinez
    @Override
80 41805 jjdelcerro
    public Feature getCopy() {
81
        return new DefaultEditableFeature(this);
82
    }
83 40435 jjdelcerro
84 44829 omartinez
    @Override
85 41805 jjdelcerro
    public Feature getNotEditableCopy() {
86
        return new DefaultFeature(this);
87
    }
88 40435 jjdelcerro
89 44829 omartinez
    @Override
90 41805 jjdelcerro
    public void setDefaultGeometry(Geometry geometry) {
91
        FeatureAttributeDescriptor attribute = this.getType()
92
                .getAttributeDescriptor(
93
                        this.getType().getDefaultGeometryAttributeIndex());
94
        this.set(attribute, geometry);
95
    }
96 40435 jjdelcerro
97 44390 jjdelcerro
    public void __setitem__(String name, Object value) {
98
        this.set(name, value);
99
    }
100
101
    @Override
102 41805 jjdelcerro
    public void set(String name, Object value) {
103
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
104
        if ( attribute == null ) {
105
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
106
        }
107
        this.set(attribute, value);
108
    }
109 40435 jjdelcerro
110 44829 omartinez
    @Override
111 41805 jjdelcerro
    public void set(int index, Object value) {
112
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
113
        this.set(attribute, value);
114
    }
115 40435 jjdelcerro
116 44829 omartinez
    @Override
117 41805 jjdelcerro
    public void setArray(String name, Object[] value) {
118
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
119
        if ( attribute == null ) {
120
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
121
        }
122
        this.set(attribute, value);
123
    }
124 40435 jjdelcerro
125 44829 omartinez
    @Override
126 41805 jjdelcerro
    public void setArray(int index, Object[] value) {
127
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
128
        this.set(attribute, value);
129
    }
130 40435 jjdelcerro
131 44829 omartinez
    @Override
132 41805 jjdelcerro
    public void setBoolean(String name, boolean value) {
133
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
134
        if ( attribute == null ) {
135
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
136
        }
137
        this.set(attribute, Boolean.valueOf(value));
138
    }
139 40435 jjdelcerro
140 44829 omartinez
    @Override
141 41805 jjdelcerro
    public void setBoolean(int index, boolean value) {
142
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
143
        this.set(attribute, Boolean.valueOf(value));
144
    }
145 40435 jjdelcerro
146 44829 omartinez
    @Override
147 41805 jjdelcerro
    public void setByte(String name, byte value) {
148
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
149
        if ( attribute == null ) {
150
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
151
        }
152
        this.set(attribute, new Byte(value));
153
    }
154 40435 jjdelcerro
155 44829 omartinez
    @Override
156 41805 jjdelcerro
    public void setByte(int index, byte value) {
157
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
158
        this.set(attribute, new Byte(value));
159
    }
160 40435 jjdelcerro
161 44829 omartinez
    @Override
162 41805 jjdelcerro
    public void setDate(String name, Date value) {
163
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
164
        if ( attribute == null ) {
165
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
166
        }
167
        this.set(attribute, value);
168
    }
169 40435 jjdelcerro
170 44829 omartinez
    @Override
171 41805 jjdelcerro
    public void setDate(int index, Date value) {
172
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
173
        this.set(attribute, value);
174
    }
175 40435 jjdelcerro
176 44829 omartinez
    @Override
177 41805 jjdelcerro
    public void setDouble(String name, double value) {
178
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
179
        if ( attribute == null ) {
180
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
181
        }
182
        this.set(attribute, new Double(value));
183
    }
184 40435 jjdelcerro
185 44829 omartinez
    @Override
186 41805 jjdelcerro
    public void setDouble(int index, double value) {
187
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
188
        this.set(attribute, new Double(value));
189
    }
190 40435 jjdelcerro
191 44669 jjdelcerro
    @Override
192
    public void setDecimal(String name, BigDecimal value) {
193
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
194
        if ( attribute == null ) {
195
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
196
        }
197
        this.set(attribute, value);
198
    }
199
200 44829 omartinez
    @Override
201 44669 jjdelcerro
    public void setDecimal(int index, BigDecimal value) {
202
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
203
        this.set(attribute, value);
204
    }
205
206 44829 omartinez
    @Override
207 41805 jjdelcerro
    public void setFeature(String name, Feature value) {
208
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
209
        if ( attribute == null ) {
210
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
211
        }
212
        this.set(attribute, value);
213
    }
214 40435 jjdelcerro
215 44829 omartinez
    @Override
216 41805 jjdelcerro
    public void setFeature(int index, Feature value) {
217
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
218
        this.set(attribute, value);
219
    }
220 40435 jjdelcerro
221 44829 omartinez
    @Override
222 41805 jjdelcerro
    public void setFloat(String name, float value) {
223
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
224
        if ( attribute == null ) {
225
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
226
        }
227
        this.set(attribute, new Float(value));
228
    }
229 40435 jjdelcerro
230 44829 omartinez
    @Override
231 41805 jjdelcerro
    public void setFloat(int index, float value) {
232
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
233
        this.set(attribute, new Float(value));
234
    }
235 40435 jjdelcerro
236 44829 omartinez
    @Override
237 41805 jjdelcerro
    public void setGeometry(String name, Geometry value) {
238
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
239
        if ( attribute == null ) {
240
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
241
        }
242
        this.set(attribute, value);
243
    }
244 40435 jjdelcerro
245 44829 omartinez
    @Override
246 41805 jjdelcerro
    public void setGeometry(int index, Geometry value) {
247
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
248
        this.set(attribute, value);
249
    }
250 40435 jjdelcerro
251 44829 omartinez
    @Override
252 41805 jjdelcerro
    public void setInt(String name, int 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
        this.set(attribute, new Integer(value));
258
    }
259 40435 jjdelcerro
260 44829 omartinez
    @Override
261 41805 jjdelcerro
    public void setInt(int index, int value) {
262
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
263
        this.set(attribute, new Integer(value));
264
    }
265 40435 jjdelcerro
266 44829 omartinez
    @Override
267 41805 jjdelcerro
    public void setLong(String name, long value) {
268
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
269
        if ( attribute == null ) {
270
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
271
        }
272
        this.set(attribute, new Long(value));
273
    }
274 40435 jjdelcerro
275 44829 omartinez
    @Override
276 41805 jjdelcerro
    public void setLong(int index, long value) {
277
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
278
        this.set(attribute, new Long(value));
279
    }
280 40435 jjdelcerro
281 44829 omartinez
    @Override
282 41805 jjdelcerro
    public void setString(String name, String value) {
283
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
284
        if ( attribute == null ) {
285
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
286
        }
287
        this.set(attribute, value);
288
    }
289 40435 jjdelcerro
290 44829 omartinez
    @Override
291 41805 jjdelcerro
    public void setString(int index, String value) {
292
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
293
        this.set(attribute, value);
294
    }
295 40435 jjdelcerro
296 44829 omartinez
    @Override
297 44655 jjdelcerro
    public void copyFrom(JsonObject values) {
298
        // iterate over the attributes and copy one by one
299
        for (FeatureAttributeDescriptor attr : this.getType()) {
300
            if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
301
                continue;
302
            }
303
            String attrname = attr.getName();
304
            if( !values.containsKey(attrname) ) {
305
                continue;
306
            }
307
            Object value;
308
            try {
309
                switch( values.getValueType() ) {
310
                    case STRING:
311
                        switch(attr.getType()) {
312
                            case DataTypes.GEOMETRY:
313
                                value = GeometryUtils.createFrom(values.getString(attrname));
314
                                break;
315
                            case DataTypes.DATE:
316
                                try {
317
                                    TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(values.getString(attrname));
318
                                    LocalDateTime date = LocalDateTime.from(x);
319
//                                    value = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
320
                                    value = Date.from(date.toInstant(ZoneOffset.UTC));
321
                                } catch(Exception ex) {
322
                                    value = values.getString(attrname);
323
                                }
324
                                break;
325
                            case DataTypes.STRING:
326
                            default:
327
                                value = values.getString(attrname);
328
                                break;
329
                        }
330
                        break;
331
                    case NUMBER:
332
                        JsonNumber n = values.getJsonNumber(attrname);
333
                        switch(attr.getType()) {
334
                            case DataTypes.BIGDECIMAL:
335
                                value = n.bigDecimalValue();
336
                                break;
337
                            case DataTypes.BYTE:
338
                                value = n.bigDecimalValue();
339
                                break;
340
                            case DataTypes.FLOAT:
341
                            case DataTypes.DOUBLE:
342
                                value = n.doubleValue();
343
                                break;
344
                            case DataTypes.INT:
345
                                value = n.intValue();
346
                                break;
347
                            case DataTypes.LONG:
348
                                value = n.longValue();
349
                                break;
350
                            default:
351
                                value = n.toString();
352
                                break;
353
                        }
354
                        break;
355
                    case TRUE:
356
                        value = true;
357
                        break;
358
                    case FALSE:
359
                        value = false;
360
                        break;
361
                    case NULL:
362
                        value = null;
363
                        break;
364
                    default:
365
                        value = values.getString(attrname);
366
                        break;
367
                }
368
            } catch(Exception ex) {
369
                continue;
370
            }
371
            if (value == null && !attr.allowNull()) {
372
                continue;
373
            }
374
            try {
375
                set(attr.getIndex(), value);
376
            } catch(Throwable th) {
377
                // Ignore
378
            }
379
        }
380
    }
381
382 44829 omartinez
    @Override
383 41805 jjdelcerro
    public void copyFrom(Feature source) {
384 44815 jjdelcerro
      // iterate over the attributes and copy one by one
385
        FeatureType sourceType = source.getType();
386 44610 jjdelcerro
        for (FeatureAttributeDescriptor attr : this.getType()) {
387
            if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
388
                continue;
389 43562 jjdelcerro
            }
390 44815 jjdelcerro
            String attrname = attr.getName();
391
            if( sourceType.getAttributeDescriptor(attrname)==null ) {
392
              continue;
393
            }
394
            Object value = source.get(attrname);
395 44610 jjdelcerro
            if (value == null && !attr.allowNull()) {
396
                continue;
397
            }
398
            try {
399
                set(attr.getIndex(), value);
400
            } catch(Throwable th) {
401 44669 jjdelcerro
                value = null;
402 44610 jjdelcerro
                // Ignore
403
            }
404 41805 jjdelcerro
        }
405
    }
406 40435 jjdelcerro
407 44829 omartinez
    @Override
408 41805 jjdelcerro
    public void setInstant(String name, Instant value) {
409
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
410
        if ( attribute == null ) {
411
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
412
        }
413
        this.set(attribute, value);
414
    }
415 40435 jjdelcerro
416 44829 omartinez
    @Override
417 41805 jjdelcerro
    public void setInstant(int index, Instant value) {
418
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
419
        this.set(attribute, value);
420
    }
421 40435 jjdelcerro
422 44829 omartinez
    @Override
423 41805 jjdelcerro
    public void setInterval(String name, Interval value) {
424
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
425
        if ( attribute == null ) {
426
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
427
        }
428
        this.set(attribute, value);
429
    }
430 40435 jjdelcerro
431 44829 omartinez
    @Override
432 41805 jjdelcerro
    public void setInterval(int index, Interval value) {
433
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
434
        this.set(attribute, value);
435
    }
436 44779 omartinez
437
    @Override
438
    public Object getExtraValue(String name) {
439
        Object value;
440 44829 omartinez
        FeatureExtraColumns columns = this.getType().getExtraColumns();
441 44779 omartinez
        int index = columns.getIndexOf(name);
442
        if (index >= 0) {
443
            EditableFeatureAttributeDescriptor attrdesc = columns.get(index);
444
            value = attrdesc.getFeatureAttributeEmulator().get(this);
445
            return value;
446
        }
447
        value = this.data.getExtraValue(name);
448
        return value;
449
    }
450 40435 jjdelcerro
}