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

History | View | Annotate | Download (15.1 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.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.GeometryUtils;
44
import org.gvsig.timesupport.Instant;
45
import org.gvsig.timesupport.Interval;
46

    
47
public class DefaultEditableFeature extends DefaultFeature implements
48
        EditableFeature {
49

    
50
    private DefaultFeature source;
51

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

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

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

    
68
    public Feature getSource() {
69
        return this.source;
70
    }
71

    
72
    public EditableFeature getEditable() {
73
        return this;
74
    }
75

    
76
    public Feature getCopy() {
77
        return new DefaultEditableFeature(this);
78
    }
79

    
80
    public Feature getNotEditableCopy() {
81
        return new DefaultFeature(this);
82
    }
83

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

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

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

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

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

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

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

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

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

    
148
    public void setDate(String name, Date value) {
149
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(name);
150
        if ( attribute == null ) {
151
            throw new IllegalArgumentException("Attribute '" + name + "' does not exits in the feature.");
152
        }
153
        this.set(attribute, value);
154
    }
155

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

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

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

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

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

    
188
    public void setFeature(String name, Feature 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
    public void setFeature(int index, Feature value) {
197
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
198
        this.set(attribute, value);
199
    }
200

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
391
    public void setInterval(int index, Interval value) {
392
        FeatureAttributeDescriptor attribute = this.getType().getAttributeDescriptor(index);
393
        this.set(attribute, value);
394
    }
395
}