Statistics
| Revision:

root / branches / dal_time_support / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 35115

History | View | Annotate | Download (13.4 KB)

1
package org.gvsig.fmap.dal.feature.impl;
2

    
3
import java.lang.ref.WeakReference;
4
import java.util.Date;
5
import java.util.Iterator;
6
import java.util.List;
7

    
8
import org.cresques.cts.IProjection;
9

    
10
import org.gvsig.fmap.dal.DataTypes;
11
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
12
import org.gvsig.fmap.dal.feature.EditableFeature;
13
import org.gvsig.fmap.dal.feature.Feature;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.dal.feature.FeatureReference;
16
import org.gvsig.fmap.dal.feature.FeatureStore;
17
import org.gvsig.fmap.dal.feature.FeatureType;
18
import org.gvsig.fmap.dal.feature.exception.IllegalValueException;
19
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException;
20
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
21
import org.gvsig.fmap.geom.Geometry;
22
import org.gvsig.fmap.geom.primitive.Envelope;
23
import org.gvsig.timesupport.Instant;
24
import org.gvsig.timesupport.Interval;
25
import org.gvsig.tools.ToolsLocator;
26
import org.gvsig.tools.dataTypes.CoercionException;
27
import org.gvsig.tools.dataTypes.DataTypesManager;
28
import org.gvsig.tools.evaluator.Evaluator;
29
import org.gvsig.tools.evaluator.EvaluatorData;
30
import org.gvsig.tools.evaluator.EvaluatorException;
31

    
32
public class DefaultFeature implements Feature, EvaluatorData {
33

    
34
        private static DataTypesManager dataTypesManager = null; 
35
        protected FeatureProvider data;
36
        protected FeatureReference reference;
37
        private WeakReference storeRef;
38

    
39
        
40
        /*
41
         * Usar con mucha precaucion o mejor no usar. Lo precisa el
42
         * DefaultFeatureSet en la ordenacion.
43
         */
44
        public DefaultFeature(FeatureStore store) {
45
                this.storeRef = new WeakReference(store);
46
                this.reference = null;
47
        }
48

    
49
        public DefaultFeature(FeatureStore store, FeatureProvider data) {
50
                this.data = data;
51
                this.storeRef = new WeakReference(store);
52
                this.reference = null;
53
        }
54

    
55
        DefaultFeature(DefaultFeature feature) {
56
                this.data = feature.data.getCopy();
57
                this.storeRef = feature.storeRef;
58
                this.reference = feature.reference;
59
        }
60

    
61
        public void setData(FeatureProvider data) {
62
                this.data = data;
63
                this.reference = null;
64
        }
65

    
66
        public FeatureProvider getData() {
67
                return this.data;
68
        }
69

    
70
        protected DataTypesManager getDataTypesManager() {
71
                if( dataTypesManager==null ) {
72
                        dataTypesManager = ToolsLocator.getDataTypesManager();
73
                }
74
                return dataTypesManager;
75
        }
76

    
77
        void set(FeatureAttributeDescriptor attribute, Object value) {
78
                int i = attribute.getIndex();
79

    
80
                if (attribute.isReadOnly()) {
81
                        throw new SetReadOnlyAttributeException();
82
                }
83

    
84
                if (attribute.getEvaluator() != null) {
85
                        throw new SetReadOnlyAttributeException();
86
                }
87

    
88
                if (value == null) {
89
                        if (!attribute.allowNull()) {
90
                                if (!attribute.isAutomatic()) {
91
                                        throw new IllegalValueException(attribute, value);
92
                                }
93
                        }
94
                        this.data.set(i, null);
95
                        return;
96

    
97
                }
98

    
99
                if (attribute.getObjectClass().isInstance(value)) {
100
                        this.data.set(i, value);
101
                        return;
102
                }
103

    
104
                try {
105
                        this.getDataTypesManager().coerce(attribute.getType(), value);
106
                } catch (CoercionException e) {
107
                        throw new IllegalArgumentException(
108
                                        "Can't convert to "+this.getDataTypesManager().getTypeName(attribute.getType())+" from '"+value.getClass().getName()+"' with value '"+value.toString()+"'.",
109
                                        e);
110
                }
111
                
112
//                if ((Number.class.isAssignableFrom(attribute.getObjectClass()) && Number.class.isAssignableFrom(value.getClass()))
113
//                                || (attribute.getType()==DataTypes.STRING && Number.class.isAssignableFrom(value.getClass()))) {
114
//                        Object number=getNumberByType((Number)value, attribute.getType());
115
//                        this.data.set(i, number);
116
//                        return;
117
//                }
118
//
119
//                if (!(value instanceof String)) {
120
//                        throw new IllegalValueException(attribute, value);
121
//                }
122
//                int dataType = attribute.getType();
123
//
124
//                switch (dataType) {
125
//                case DataTypes.BYTE:
126
//                        this.data.set(i, Byte.valueOf((String) value));
127
//                        break;
128
//
129
//                case DataTypes.DATE:
130
//                        try {
131
//                                this.data.set(i, attribute.getDateFormat()
132
//                                                .parse((String) value));
133
//                        } catch (ParseException e) {
134
//                                throw new IllegalValueException(attribute, value, e);
135
//                        }
136
//                        break;
137
//
138
//                case DataTypes.DOUBLE:
139
//                        this.data.set(i, Double.valueOf((String) value));
140
//                        break;
141
//
142
//                case DataTypes.FLOAT:
143
//                        this.data.set(i, Float.valueOf((String) value));
144
//                        break;
145
//
146
//                case DataTypes.INT:
147
//                        this.data.set(i, Integer.valueOf((String) value));
148
//                        break;
149
//
150
//                case DataTypes.LONG:
151
//                        this.data.set(i, Long.valueOf((String) value));
152
//                        break;
153
//
154
//                default:
155
//                        throw new IllegalValueException(attribute, value);
156
//                }
157
        }
158

    
159
        private Object get(int index,Class theClass, int type) {
160
                Object value = this.get(index);
161
                if( theClass.isInstance(value) ) {
162
                        return value;
163
                }
164
                try {
165
                        return this.getDataTypesManager().coerce(type, value);
166
                } catch (CoercionException e) {
167
                        throw new IllegalArgumentException(
168
                                        "Can't convert to "+theClass.getSimpleName()+" from '"+value.getClass().getName()+"' with value '"+value.toString()+"'.",
169
                                        e);
170
                }
171
        }
172
        
173
//        private Object getNumberByType(Number value, int type) {
174
//                if (type==DataTypes.DOUBLE){
175
//                        return new Double(value.doubleValue());
176
//                }else if (type==DataTypes.FLOAT){
177
//                        return new Float(value.floatValue());
178
//                }else if (type==DataTypes.LONG){
179
//                        return new Long(value.longValue());
180
//                }else if (type==DataTypes.INT){
181
//                        return new Integer(value.intValue());
182
//                }else if (type==DataTypes.STRING){
183
//                        return value.toString();
184
//                }
185
//                return value;
186
//        }
187

    
188
        public void initializeValues() {
189
                FeatureType type = this.getType();
190
                Iterator iterator = type.iterator();
191

    
192
                while (iterator.hasNext()) {
193
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
194
                        .next();
195
                        if (attribute.isAutomatic() || attribute.isReadOnly()
196
                                        || attribute.getEvaluator() != null) {
197
                                continue;
198
                        }
199
                        if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
200
                                continue;
201
                        }
202
                        this.set(attribute, attribute.getDefaultValue());
203
                }
204
        }
205

    
206
        public void clear() {
207
                initializeValues();
208
        }
209

    
210
        public void initializeValues(Feature feature) {
211
                FeatureType myType=this.getType();
212
                FeatureType type =feature.getType();
213
                Iterator iterator = type.iterator();
214

    
215
                while (iterator.hasNext()) {
216
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
217
                        .next();
218
                        FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
219
                        if (myAttribute != null) {
220
                                this.set(myAttribute, feature.get(attribute.getIndex()));
221
                        }
222
                }
223
        }
224

    
225
        public FeatureStore getStore() {
226
                return (FeatureStore) this.storeRef.get();
227
        }
228

    
229
        public FeatureType getType() {
230
                return this.data.getType();
231
        }
232

    
233
        public EditableFeature getEditable() {
234
                return new DefaultEditableFeature(this);
235
        }
236

    
237
        public Feature getCopy() {
238
                return new DefaultFeature(this);
239
        }
240

    
241
        public FeatureReference getReference() {
242
                if (this.reference == null) {
243
                        this.reference = new DefaultFeatureReference(this);
244
                }
245
                return this.reference;
246
        }
247

    
248
        public void validate(int mode) {
249
                ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
250
        }
251

    
252
        public List getSRSs() {
253
                // TODO Auto-generated method stub
254
                return null;
255
        }
256

    
257
        public Envelope getDefaultEnvelope() {
258
                return this.data.getDefaultEnvelope();
259
        }
260

    
261
        public Geometry getDefaultGeometry() {
262
                return this.data.getDefaultGeometry();
263
        }
264

    
265
        public IProjection getDefaultSRS() {
266
                return this.data.getType().getDefaultSRS();
267
        }
268

    
269
        public List getGeometries() {
270
                // TODO Auto-generated method stub
271
                return null;
272
        }
273

    
274
        public Object get(String name) {
275
                int index = this.data.getType().getIndex(name);
276
                if( index < 0 ) {
277
                        throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
278
                }
279
                return this.get(index);
280
        }
281

    
282
        public Object get(int index) {
283
                FeatureType type = this.data.getType();
284
                if( index <0 || index >= type.size() ) {
285
                        throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
286
                }
287
                if (!this.data.getType().hasEvaluators()) {
288
                        return this.data.get(index);
289
                }
290
                FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
291
                Evaluator eval = attribute.getEvaluator();
292
                if (eval == null) {
293
                        return this.data.get(index);
294
                } else {
295
                        Object value = this.data.get(index);
296
                        if (value != null) { // FIXME: para comprobar si esta calculado usar
297
                                                                        // un array
298
                                                                        // especifico.
299
                                return this.data.get(index);
300
                        }
301
                        try {
302
                                value = eval.evaluate(this);
303
                        } catch (EvaluatorException e) {
304
                                throw new DataEvaluatorRuntimeException(e);
305
                        }
306
                        this.data.set(index, value);
307
                        return value;
308
                }
309
        }
310

    
311
        public Object[] getArray(String name) {
312
                return this.getArray(this.data.getType().getIndex(name));
313
        }
314

    
315
        public Object[] getArray(int index) {
316
                return (Object[]) this.get(index);
317
        }
318

    
319
        public boolean getBoolean(String name) {
320
                return this.getBoolean(this.data.getType().getIndex(name));
321
        }
322

    
323
        public boolean getBoolean(int index) {
324
                Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
325
                if (value == null) {
326
                        return false;
327
                }
328
                return value.booleanValue();
329
        }
330

    
331
        public byte getByte(String name) {
332
                return this.getByte(this.data.getType().getIndex(name));
333
        }
334

    
335
        public byte getByte(int index) {
336
                Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
337
                if (value == null) {
338
                        return 0;
339
                }
340
                return value.byteValue();
341
        }
342

    
343
        public Date getDate(String name) {
344
                return this.getDate(this.data.getType().getIndex(name));
345
        }
346

    
347
        public Date getDate(int index) {
348
                Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
349

    
350
                return value;
351
        }
352

    
353
        public double getDouble(String name) {
354
                return this.getDouble(this.data.getType().getIndex(name));
355
        }
356

    
357
        public double getDouble(int index) {
358
                
359
                Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
360
                if (value == null) {
361
                        return 0;
362
                }
363
                return value.doubleValue();
364
        }
365

    
366
        public Feature getFeature(String name) {
367
                return this.getFeature(this.data.getType().getIndex(name));
368
        }
369

    
370
        public Feature getFeature(int index) {
371
                return (Feature) this.get(index);
372
        }
373

    
374
        public float getFloat(String name) {
375
                return this.getFloat(this.data.getType().getIndex(name));
376
        }
377

    
378
        public float getFloat(int index) {
379
                Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
380
                if (value == null) {
381
                        return 0;
382
                }
383
                return value.floatValue();
384
        }
385

    
386
        public Geometry getGeometry(String name) {
387
                return this.getGeometry(this.data.getType().getIndex(name));
388
        }
389

    
390
        public Geometry getGeometry(int index) {
391
                return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
392
        }
393

    
394
        public int getInt(String name) {
395
                return this.getInt(this.data.getType().getIndex(name));
396
        }
397

    
398
        public int getInt(int index) {
399
                Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
400
                if (value == null) {
401
                        return 0;
402
                }
403
                return ((Integer)value).intValue();
404
        }
405

    
406
        public long getLong(String name) {
407
                return this.getLong(this.data.getType().getIndex(name));
408
        }
409

    
410
        public long getLong(int index) {
411
                Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
412
                if (value == null) {
413
                        return 0;
414
                }
415
                return value.longValue();
416
        }
417

    
418
        public String getString(String name) {
419
                return this.getString(this.data.getType().getIndex(name));
420
        }
421

    
422
        public String getString(int index) {
423
                return (String) this.get(index,String.class,DataTypes.STRING);
424
        }
425

    
426
        public Object getContextValue(String name) {
427
                name = name.toLowerCase();
428
                if (name.equals("store")) {
429
                        return this.getStore();
430
                }
431

    
432
                if (name.equals("featuretype")) {
433
                        return this.data.getType();
434
                }
435

    
436
                if (name.equals("feature")) {
437
                        return this;
438
                }
439

    
440
                throw new IllegalArgumentException(name);
441
        }
442

    
443
        public Iterator getDataNames() {
444
                class DataNamesIterator implements Iterator {
445
                        Iterator attributeIteraror;
446

    
447
                        DataNamesIterator(DefaultFeature feature) {
448
                                this.attributeIteraror = feature.getType().iterator();
449
                        }
450

    
451
                        public boolean hasNext() {
452
                                return this.attributeIteraror.hasNext();
453
                        }
454

    
455
                        public Object next() {
456
                                return ((FeatureAttributeDescriptor) this.attributeIteraror
457
                                                .next()).getName();
458
                        }
459

    
460
                        public void remove() {
461
                                throw new UnsupportedOperationException();
462
                        }
463

    
464
                }
465
                return new DataNamesIterator(this);
466
        }
467

    
468
        public Object getDataValue(String name) {
469
                name = name.toLowerCase();
470
                return get(name);
471
        }
472

    
473
        public Iterator getDataValues() {
474
                class DataValuesIterator implements Iterator {
475
                        DefaultFeature feature;
476
                        int current = 0;
477

    
478
                        DataValuesIterator(DefaultFeature feature) {
479
                                this.feature = feature;
480
                        }
481

    
482
                        public boolean hasNext() {
483
                                return current < feature.getType().size() - 1;
484
                        }
485

    
486
                        public Object next() {
487
                                return feature.get(current++);
488
                        }
489

    
490
                        public void remove() {
491
                                throw new UnsupportedOperationException();
492
                        }
493

    
494
                }
495
                return new DataValuesIterator(this);
496
        }
497

    
498
        public boolean hasContextValue(String name) {
499
                name = name.toLowerCase();
500
                if (name.equals("store")) {
501
                        return true;
502
                }
503

    
504
                if (name.equals("featuretype")) {
505
                        return true;
506
                }
507

    
508
                if (name.equals("feature")) {
509
                        return true;
510
                }
511
                return false;
512
        }
513

    
514
        public boolean hasDataValue(String name) {
515
                name = name.toLowerCase();
516
                return this.data.getType().getIndex(name) >= 0;
517
        }
518

    
519
    public Instant getInstant(int index) {
520
        return ((Instant) this.get(index,Date.class,DataTypes.INSTANT));
521
    }
522

    
523
    public Instant getInstant(String name) {
524
        return this.getInstant(this.data.getType().getIndex(name));
525
    }
526

    
527
    public Interval getInterval(int index) {
528
        return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL));
529
    }
530

    
531
    public Interval getInterval(String name) {
532
        return this.getInterval(this.data.getType().getIndex(name));
533
    }
534
}