Statistics
| Revision:

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

History | View | Annotate | Download (12.5 KB)

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

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

    
9
import org.cresques.cts.IProjection;
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.tools.dynobject.DynClass;
24
import org.gvsig.tools.dynobject.DynObject;
25
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
26
import org.gvsig.tools.dynobject.exception.DynMethodException;
27
import org.gvsig.tools.evaluator.Evaluator;
28
import org.gvsig.tools.evaluator.EvaluatorData;
29
import org.gvsig.tools.evaluator.EvaluatorException;
30

    
31
public class DefaultFeature implements Feature, EvaluatorData {
32

    
33
        protected FeatureProvider data;
34
        protected FeatureReference reference;
35
        private WeakReference storeRef;
36

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

    
46
        public DefaultFeature(FeatureStore store, FeatureProvider data) {
47
                this.data = data;
48
                this.storeRef = new WeakReference(store);
49
                this.reference = null;
50
        }
51

    
52
        DefaultFeature(DefaultFeature feature) {
53
                this.data = feature.data.getCopy();
54
                this.storeRef = feature.storeRef;
55
                this.reference = feature.reference;
56
        }
57

    
58
        public void setData(FeatureProvider data) {
59
                this.data = data;
60
                this.reference = null;
61
        }
62

    
63
        public FeatureProvider getData() {
64
                return this.data;
65
        }
66

    
67
        void set(FeatureAttributeDescriptor attribute, Object value) {
68
                int i = attribute.getIndex();
69

    
70
                if (attribute.isReadOnly()) {
71
                        throw new SetReadOnlyAttributeException();
72
                }
73

    
74
                if (attribute.getEvaluator() != null) {
75
                        throw new SetReadOnlyAttributeException();
76
                }
77

    
78
                if (value == null) {
79
                        if (!attribute.allowNull()) {
80
                                if (!attribute.isAutomatic()) {
81
                                        throw new IllegalValueException(attribute, value);
82
                                }
83
                        }
84
                        this.data.set(i, null);
85
                        return;
86

    
87
                }
88

    
89
                if (attribute.getObjectClass().isInstance(value)) {
90
                        this.data.set(i, value);
91
                        return;
92
                }
93

    
94
                if ((Number.class.isAssignableFrom(attribute.getObjectClass()) && Number.class.isAssignableFrom(value.getClass()))
95
                                || (attribute.getType()==DataTypes.STRING && Number.class.isAssignableFrom(value.getClass()))) {
96
                        Object number=getNumberByType((Number)value, attribute.getType());
97
                        this.data.set(i, number);
98
                        return;
99
                }
100

    
101
                if (!(value instanceof String)) {
102
                        throw new IllegalValueException(attribute, value);
103
                }
104
                int dataType = attribute.getType();
105

    
106
                switch (dataType) {
107
                case DataTypes.BYTE:
108
                        this.data.set(i, Byte.valueOf((String) value));
109
                        break;
110

    
111
                case DataTypes.DATE:
112
                        try {
113
                                this.data.set(i, attribute.getDateFormat()
114
                                                .parse((String) value));
115
                        } catch (ParseException e) {
116
                                throw new IllegalValueException(attribute, value, e);
117
                        }
118
                        break;
119

    
120
                case DataTypes.DOUBLE:
121
                        this.data.set(i, Double.valueOf((String) value));
122
                        break;
123

    
124
                case DataTypes.FLOAT:
125
                        this.data.set(i, Float.valueOf((String) value));
126
                        break;
127

    
128
                case DataTypes.INT:
129
                        this.data.set(i, Integer.valueOf((String) value));
130
                        break;
131

    
132
                case DataTypes.LONG:
133
                        this.data.set(i, Long.valueOf((String) value));
134
                        break;
135

    
136
                default:
137
                        throw new IllegalValueException(attribute, value);
138
                }
139
        }
140

    
141
        private Object getNumberByType(Number value, int type) {
142
                if (type==DataTypes.DOUBLE){
143
                        return new Double(value.doubleValue());
144
                }else if (type==DataTypes.FLOAT){
145
                        return new Float(value.floatValue());
146
                }else if (type==DataTypes.LONG){
147
                        return new Long(value.longValue());
148
                }else if (type==DataTypes.INT){
149
                        return new Integer(value.intValue());
150
                }else if (type==DataTypes.STRING){
151
                        return value.toString();
152
                }
153
                return value;
154
        }
155

    
156
        public void initializeValues() {
157
                FeatureType type = this.getType();
158
                Iterator iterator = type.iterator();
159

    
160
                while (iterator.hasNext()) {
161
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
162
                        .next();
163
                        if (attribute.isAutomatic() || attribute.isReadOnly()
164
                                        || attribute.getEvaluator() != null) {
165
                                continue;
166
                        }
167
                        if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
168
                                continue;
169
                        }
170
                        this.set(attribute, attribute.getDefaultValue());
171
                }
172
        }
173

    
174
        public void clear() {
175
                initializeValues();
176
        }
177

    
178
        public void initializeValues(Feature feature) {
179
                FeatureType myType=this.getType();
180
                FeatureType type =feature.getType();
181
                Iterator iterator = type.iterator();
182

    
183
                while (iterator.hasNext()) {
184
                        FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
185
                        .next();
186
                        FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
187
                        if (myAttribute != null) {
188
                                this.set(myAttribute, feature.get(attribute.getIndex()));
189
                        }
190
                }
191
        }
192

    
193
        public FeatureStore getStore() {
194
                return (FeatureStore) this.storeRef.get();
195
        }
196

    
197
        public FeatureType getType() {
198
                return this.data.getType();
199
        }
200

    
201
        public EditableFeature getEditable() {
202
                return new DefaultEditableFeature(this);
203
        }
204

    
205
        public Feature getCopy() {
206
                return new DefaultFeature(this);
207
        }
208

    
209
        public FeatureReference getReference() {
210
                if (this.reference == null) {
211
                        this.reference = new DefaultFeatureReference(this);
212
                }
213
                return this.reference;
214
        }
215

    
216
        public void validate(int mode) {
217
                ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
218
        }
219

    
220
        public List getSRSs() {
221
                // TODO Auto-generated method stub
222
                return null;
223
        }
224

    
225
        public Envelope getDefaultEnvelope() {
226
                return this.data.getDefaultEnvelope();
227
        }
228

    
229
        public Geometry getDefaultGeometry() {
230
                return this.data.getDefaultGeometry();
231
        }
232

    
233
        public IProjection getDefaultSRS() {
234
                return this.data.getType().getDefaultSRS();
235
        }
236

    
237
        public List getGeometries() {
238
                // TODO Auto-generated method stub
239
                return null;
240
        }
241

    
242
        public Object get(String name) {
243
                int index = this.data.getType().getIndex(name);
244
                if( index < 0 ) {
245
                        throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
246
                }
247
                return this.get(index);
248
        }
249

    
250
        public Object get(int index) {
251
                FeatureType type = this.data.getType();
252
                if( index <0 || index >= type.size() ) {
253
                        throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
254
                }
255
                if (!this.data.getType().hasEvaluators()) {
256
                        return this.data.get(index);
257
                }
258
                FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
259
                Evaluator eval = attribute.getEvaluator();
260
                if (eval == null) {
261
                        return this.data.get(index);
262
                } else {
263
                        Object value = this.data.get(index);
264
                        if (value != null) { // FIXME: para comprobar si esta calculado usar
265
                                                                        // un array
266
                                                                        // especifico.
267
                                return this.data.get(index);
268
                        }
269
                        try {
270
                                value = eval.evaluate(this);
271
                        } catch (EvaluatorException e) {
272
                                throw new DataEvaluatorRuntimeException(e);
273
                        }
274
                        this.data.set(index, value);
275
                        return value;
276
                }
277
        }
278

    
279
        public Object[] getArray(String name) {
280
                return this.getArray(this.data.getType().getIndex(name));
281
        }
282

    
283
        public Object[] getArray(int index) {
284
                return (Object[]) this.get(index);
285
        }
286

    
287
        public boolean getBoolean(String name) {
288
                return this.getBoolean(this.data.getType().getIndex(name));
289
        }
290

    
291
        public boolean getBoolean(int index) {
292
                Boolean value = (Boolean) this.get(index);
293
                if (value == null) {
294
                        return false;
295
                }
296
                return value.booleanValue();
297
        }
298

    
299
        public byte getByte(String name) {
300
                return this.getByte(this.data.getType().getIndex(name));
301
        }
302

    
303
        public byte getByte(int index) {
304
                Byte value = (Byte) this.get(index);
305
                if (value == null) {
306
                        return 0;
307
                }
308
                return value.byteValue();
309
        }
310

    
311
        public Date getDate(String name) {
312
                return this.getDate(this.data.getType().getIndex(name));
313
        }
314

    
315
        public Date getDate(int index) {
316
                return (Date) this.get(index);
317
        }
318

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

    
323
        public double getDouble(int index) {
324
                Double value = (Double) this.get(index);
325
                if (value == null) {
326
                        return 0;
327
                }
328
                return value.doubleValue();
329
        }
330

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

    
335
        public Feature getFeature(int index) {
336
                return (Feature) this.get(index);
337
        }
338

    
339
        public float getFloat(String name) {
340
                return this.getFloat(this.data.getType().getIndex(name));
341
        }
342

    
343
        public float getFloat(int index) {
344
                Float value = (Float) this.get(index);
345
                if (value == null) {
346
                        return 0;
347
                }
348
                return value.floatValue();
349
        }
350

    
351
        public Geometry getGeometry(String name) {
352
                return this.getGeometry(this.data.getType().getIndex(name));
353
        }
354

    
355
        public Geometry getGeometry(int index) {
356
                return (Geometry) this.get(index);
357
        }
358

    
359
        public int getInt(String name) {
360
                return this.getInt(this.data.getType().getIndex(name));
361
        }
362

    
363
        public int getInt(int index) {
364
                Integer value = (Integer) this.get(index);
365
                if (value == null) {
366
                        return 0;
367
                }
368
                return value.intValue();
369
        }
370

    
371
        public long getLong(String name) {
372
                return this.getLong(this.data.getType().getIndex(name));
373
        }
374

    
375
        public long getLong(int index) {
376
                Long value = (Long) this.get(index);
377
                if (value == null) {
378
                        return 0;
379
                }
380
                return value.longValue();
381
        }
382

    
383
        public String getString(String name) {
384
                return this.getString(this.data.getType().getIndex(name));
385
        }
386

    
387
        public String getString(int index) {
388
                return (String) this.get(index);
389
        }
390

    
391
        public Object getContextValue(String name) {
392
                name = name.toLowerCase();
393
                if (name.equals("store")) {
394
                        return this.getStore();
395
                }
396

    
397
                if (name.equals("featuretype")) {
398
                        return this.data.getType();
399
                }
400

    
401
                if (name.equals("feature")) {
402
                        return this;
403
                }
404

    
405
                throw new IllegalArgumentException(name);
406
        }
407

    
408
        public Iterator getDataNames() {
409
                class DataNamesIterator implements Iterator {
410
                        Iterator attributeIteraror;
411

    
412
                        DataNamesIterator(DefaultFeature feature) {
413
                                this.attributeIteraror = feature.getType().iterator();
414
                        }
415

    
416
                        public boolean hasNext() {
417
                                return this.attributeIteraror.hasNext();
418
                        }
419

    
420
                        public Object next() {
421
                                return ((FeatureAttributeDescriptor) this.attributeIteraror
422
                                                .next()).getName();
423
                        }
424

    
425
                        public void remove() {
426
                                throw new UnsupportedOperationException();
427
                        }
428

    
429
                }
430
                return new DataNamesIterator(this);
431
        }
432

    
433
        public Object getDataValue(String name) {
434
                name = name.toLowerCase();
435
                return get(name);
436
        }
437

    
438
        public Iterator getDataValues() {
439
                class DataValuesIterator implements Iterator {
440
                        DefaultFeature feature;
441
                        int current = 0;
442

    
443
                        DataValuesIterator(DefaultFeature feature) {
444
                                this.feature = feature;
445
                        }
446

    
447
                        public boolean hasNext() {
448
                                return current < feature.getType().size() - 1;
449
                        }
450

    
451
                        public Object next() {
452
                                return feature.get(current++);
453
                        }
454

    
455
                        public void remove() {
456
                                throw new UnsupportedOperationException();
457
                        }
458

    
459
                }
460
                return new DataValuesIterator(this);
461
        }
462

    
463
        public boolean hasContextValue(String name) {
464
                name = name.toLowerCase();
465
                if (name.equals("store")) {
466
                        return true;
467
                }
468

    
469
                if (name.equals("featuretype")) {
470
                        return true;
471
                }
472

    
473
                if (name.equals("feature")) {
474
                        return true;
475
                }
476
                return false;
477
        }
478

    
479
        public boolean hasDataValue(String name) {
480
                name = name.toLowerCase();
481
                return this.data.getType().getIndex(name) >= 0;
482
        }
483

    
484
        /**
485
         * Start of DynField interface Implementation
486
         * READONLY
487
         */
488

    
489
        public void delegate(DynObject dynObject) {
490
                throw new UnsupportedOperationException();
491
        }
492

    
493
        public DynClass getDynClass() {
494
                return (DynClass) getType();
495
        }
496

    
497
        public Object getDynValue(String name) throws DynFieldNotFoundException {
498
                return get(name);
499
        }
500

    
501
        public boolean hasDynValue(String name) {
502
                if (data.getType().getIndex(name)<0) {
503
                        return false;
504
                }
505
                return true;
506
        }
507

    
508
        public void implement(DynClass dynClass) {
509
                throw new UnsupportedOperationException();
510

    
511
        }
512

    
513
        public Object invokeDynMethod(String name, DynObject context)
514
                        throws DynMethodException {
515
                throw new UnsupportedOperationException();
516
        }
517

    
518
        public Object invokeDynMethod(int code, DynObject context)
519
                        throws DynMethodException {
520
                throw new UnsupportedOperationException();
521
        }
522

    
523
        public void setDynValue(String name, Object value)
524
                        throws DynFieldNotFoundException {
525
                throw new UnsupportedOperationException();
526

    
527
        }
528

    
529
}