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

History | View | Annotate | Download (15.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.ZoneId;
30
import java.time.ZoneOffset;
31
import java.time.format.DateTimeFormatter;
32
import java.time.temporal.TemporalAccessor;
33
import java.util.Date;
34
import javax.json.JsonNumber;
35
import javax.json.JsonObject;
36
import org.gvsig.fmap.dal.DataTypes;
37

    
38
import org.gvsig.fmap.dal.feature.EditableFeature;
39
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.FeatureExtraColumn;
43
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.GeometryUtils;
46
import org.gvsig.timesupport.Instant;
47
import org.gvsig.timesupport.Interval;
48

    
49
public class DefaultEditableFeature extends DefaultFeature implements
50
        EditableFeature {
51

    
52
    private DefaultFeature source;
53

    
54
    protected DefaultEditableFeature(DefaultFeature feature) {
55
        super(feature);
56
        this.source = feature;
57
    }
58

    
59
    protected DefaultEditableFeature(DefaultEditableFeature feature) {
60
        super(feature);
61
        this.source = (DefaultFeature) feature.getSource();
62
    }
63

    
64
    public DefaultEditableFeature(DefaultFeatureStore store, FeatureProvider data) {
65
        // Se trata de un editable feature sobre una ya existente
66
        super(store, data);
67
        this.source = null;
68
    }
69

    
70
    public Feature getSource() {
71
        return this.source;
72
    }
73

    
74
    public EditableFeature getEditable() {
75
        return this;
76
    }
77

    
78
    public Feature getCopy() {
79
        return new DefaultEditableFeature(this);
80
    }
81

    
82
    public Feature getNotEditableCopy() {
83
        return new DefaultFeature(this);
84
    }
85

    
86
    public void setDefaultGeometry(Geometry geometry) {
87
        FeatureAttributeDescriptor attribute = this.getType()
88
                .getAttributeDescriptor(
89
                        this.getType().getDefaultGeometryAttributeIndex());
90
        this.set(attribute, geometry);
91
    }
92

    
93
    public void __setitem__(String name, Object value) {
94
        this.set(name, value);
95
    }
96
    
97
    @Override
98
    public void set(String name, Object value) {
99
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
100
        if ( attribute == null ) {
101
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
102
        }
103
        this.set(attribute, value);
104
    }
105

    
106
    public void set(int index, Object value) {
107
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
108
        this.set(attribute, value);
109
    }
110

    
111
    public void setArray(String name, Object[] value) {
112
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
113
        if ( attribute == null ) {
114
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
115
        }
116
        this.set(attribute, value);
117
    }
118

    
119
    public void setArray(int index, Object[] value) {
120
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
121
        this.set(attribute, value);
122
    }
123

    
124
    public void setBoolean(String name, boolean value) {
125
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
126
        if ( attribute == null ) {
127
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
128
        }
129
        this.set(attribute, Boolean.valueOf(value));
130
    }
131

    
132
    public void setBoolean(int index, boolean value) {
133
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
134
        this.set(attribute, Boolean.valueOf(value));
135
    }
136

    
137
    public void setByte(String name, byte value) {
138
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
139
        if ( attribute == null ) {
140
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
141
        }
142
        this.set(attribute, new Byte(value));
143
    }
144

    
145
    public void setByte(int index, byte value) {
146
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
147
        this.set(attribute, new Byte(value));
148
    }
149

    
150
    public void setDate(String name, Date 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
        this.set(attribute, value);
156
    }
157

    
158
    public void setDate(int index, Date value) {
159
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
160
        this.set(attribute, value);
161
    }
162

    
163
    public void setDouble(String name, double value) {
164
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
165
        if ( attribute == null ) {
166
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
167
        }
168
        this.set(attribute, new Double(value));
169
    }
170

    
171
    public void setDouble(int index, double value) {
172
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
173
        this.set(attribute, new Double(value));
174
    }
175

    
176
    @Override
177
    public void setDecimal(String name, BigDecimal 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, value);
183
    }
184

    
185
    public void setDecimal(int index, BigDecimal value) {
186
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
187
        this.set(attribute, value);
188
    }
189

    
190
    public void setFeature(String name, Feature value) {
191
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
192
        if ( attribute == null ) {
193
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
194
        }
195
        this.set(attribute, value);
196
    }
197

    
198
    public void setFeature(int index, Feature value) {
199
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
200
        this.set(attribute, value);
201
    }
202

    
203
    public void setFloat(String name, float 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, new Float(value));
209
    }
210

    
211
    public void setFloat(int index, float value) {
212
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
213
        this.set(attribute, new Float(value));
214
    }
215

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

    
224
    public void setGeometry(int index, Geometry value) {
225
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
226
        this.set(attribute, value);
227
    }
228

    
229
    public void setInt(String name, int value) {
230
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
231
        if ( attribute == null ) {
232
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
233
        }
234
        this.set(attribute, new Integer(value));
235
    }
236

    
237
    public void setInt(int index, int value) {
238
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
239
        this.set(attribute, new Integer(value));
240
    }
241

    
242
    public void setLong(String name, long value) {
243
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
244
        if ( attribute == null ) {
245
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
246
        }
247
        this.set(attribute, new Long(value));
248
    }
249

    
250
    public void setLong(int index, long value) {
251
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
252
        this.set(attribute, new Long(value));
253
    }
254

    
255
    public void setString(String name, String value) {
256
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
257
        if ( attribute == null ) {
258
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
259
        }
260
        this.set(attribute, value);
261
    }
262

    
263
    public void setString(int index, String value) {
264
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
265
        this.set(attribute, value);
266
    }
267

    
268
    public void copyFrom(JsonObject values) {
269
        // iterate over the attributes and copy one by one
270
        for (FeatureAttributeDescriptor attr : this.getType()) {
271
            if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
272
                continue;
273
            }
274
            String attrname = attr.getName();
275
            if( !values.containsKey(attrname) ) {
276
                continue;
277
            }           
278
            Object value;
279
            try {
280
                switch( values.getValueType() ) {
281
                    case STRING:
282
                        switch(attr.getType()) {
283
                            case DataTypes.GEOMETRY:
284
                                value = GeometryUtils.createFrom(values.getString(attrname));
285
                                break;
286
                            case DataTypes.DATE:
287
                                try {
288
                                    TemporalAccessor x = DateTimeFormatter.ISO_DATE_TIME.parse(values.getString(attrname));
289
                                    LocalDateTime date = LocalDateTime.from(x);
290
//                                    value = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
291
                                    value = Date.from(date.toInstant(ZoneOffset.UTC));
292
                                } catch(Exception ex) {
293
                                    value = values.getString(attrname);
294
                                }
295
                                break;
296
                            case DataTypes.STRING:
297
                            default:
298
                                value = values.getString(attrname);
299
                                break;
300
                        }
301
                        break;
302
                    case NUMBER:
303
                        JsonNumber n = values.getJsonNumber(attrname);
304
                        switch(attr.getType()) {
305
                            case DataTypes.BIGDECIMAL:
306
                                value = n.bigDecimalValue();
307
                                break;
308
                            case DataTypes.BYTE:
309
                                value = n.bigDecimalValue();
310
                                break;
311
                            case DataTypes.FLOAT:
312
                            case DataTypes.DOUBLE:
313
                                value = n.doubleValue();
314
                                break;
315
                            case DataTypes.INT:
316
                                value = n.intValue();
317
                                break;
318
                            case DataTypes.LONG:
319
                                value = n.longValue();
320
                                break;
321
                            default:
322
                                value = n.toString();
323
                                break;
324
                        }
325
                        break;
326
                    case TRUE:
327
                        value = true;
328
                        break;
329
                    case FALSE:
330
                        value = false;
331
                        break;
332
                    case NULL:
333
                        value = null;
334
                        break;
335
                    default:
336
                        value = values.getString(attrname);
337
                        break;
338
                }
339
            } catch(Exception ex) {
340
                continue;
341
            }
342
            if (value == null && !attr.allowNull()) {
343
                continue;
344
            }
345
            try {
346
                set(attr.getIndex(), value);
347
            } catch(Throwable th) {
348
                // Ignore
349
            }
350
        }
351
    }
352
    
353
    public void copyFrom(Feature source) {
354
        // iterate over the attributes and copy one by one
355
        for (FeatureAttributeDescriptor attr : this.getType()) {
356
            if (attr==null || attr.isAutomatic() || attr.isReadOnly() || attr.isComputed() ) {
357
                continue;
358
            }
359
            Object value = source.get(attr.getName());
360
            if (value == null && !attr.allowNull()) {
361
                continue;
362
            }
363
            try {
364
                set(attr.getIndex(), value);
365
            } catch(Throwable th) {
366
                value = null;
367
                // Ignore
368
            }
369
        }
370
    }
371

    
372
    public void setInstant(String name, Instant value) {
373
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
374
        if ( attribute == null ) {
375
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
376
        }
377
        this.set(attribute, value);
378
    }
379

    
380
    public void setInstant(int index, Instant value) {
381
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
382
        this.set(attribute, value);
383
    }
384

    
385
    public void setInterval(String name, Interval value) {
386
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
387
        if ( attribute == null ) {
388
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
389
        }
390
        this.set(attribute, value);
391
    }
392

    
393
    public void setInterval(int index, Interval value) {
394
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
395
        this.set(attribute, value);
396
    }
397
    
398
    @Override
399
    public Object getExtraValue(String name) {
400
        Object value;
401
        FeatureExtraColumn columns = this.getType().getExtraColumn();
402
        int index = columns.getIndexOf(name);
403
        if (index >= 0) {
404
            EditableFeatureAttributeDescriptor attrdesc = columns.get(index);
405
            value = attrdesc.getFeatureAttributeEmulator().get(this);
406
            return value;
407
        }
408
        value = this.data.getExtraValue(name);
409
        return value;
410
    }
411
}