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 / DefaultFeature.java @ 44500

History | View | Annotate | Download (31.9 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
 * 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 40435 jjdelcerro
package org.gvsig.fmap.dal.feature.impl;
25
26
import java.lang.ref.WeakReference;
27 43550 jjdelcerro
import java.util.ArrayList;
28 40435 jjdelcerro
import java.util.Date;
29
import java.util.Iterator;
30
import java.util.List;
31 43550 jjdelcerro
import java.util.Map;
32 44128 jjdelcerro
import org.apache.commons.lang3.StringUtils;
33 40435 jjdelcerro
import org.cresques.cts.IProjection;
34 44128 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
35 40435 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
36
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
37
import org.gvsig.fmap.dal.exception.DataException;
38 44128 jjdelcerro
import org.gvsig.fmap.dal.feature.DataProfile;
39 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
43
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
44 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.dal.feature.FeatureType;
47
import org.gvsig.fmap.dal.feature.exception.IllegalValueException;
48
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException;
49 42775 jjdelcerro
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectFeatureFacade;
50 43729 fdiaz
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
51 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
52
import org.gvsig.fmap.geom.Geometry;
53
import org.gvsig.fmap.geom.primitive.Envelope;
54
import org.gvsig.timesupport.Instant;
55
import org.gvsig.timesupport.Interval;
56 44086 jjdelcerro
import org.gvsig.timesupport.Time;
57 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
58
import org.gvsig.tools.dataTypes.CoercionException;
59
import org.gvsig.tools.dataTypes.DataTypesManager;
60
import org.gvsig.tools.dynobject.DynObject;
61
import org.gvsig.tools.evaluator.Evaluator;
62
import org.gvsig.tools.evaluator.EvaluatorData;
63
import org.gvsig.tools.evaluator.EvaluatorException;
64
import org.gvsig.tools.exception.BaseException;
65
import org.gvsig.tools.exception.BaseRuntimeException;
66 40876 jjdelcerro
import org.gvsig.tools.lang.Cloneable;
67 40435 jjdelcerro
68 40876 jjdelcerro
public class DefaultFeature implements Feature, EvaluatorData, Cloneable {
69 40435 jjdelcerro
70 43729 fdiaz
        private static DataTypesManager dataTypesManager = null;
71 40435 jjdelcerro
        protected FeatureProvider data;
72
        protected FeatureReference reference;
73
        private WeakReference storeRef;
74 43729 fdiaz
75 40435 jjdelcerro
        private boolean inserted = false;
76
77
    /*
78
         * Usar con mucha precaucion o mejor no usar. Lo precisa el
79
         * DefaultFeatureSet en la ordenacion.
80
         */
81
        public DefaultFeature(FeatureStore store) {
82
                this.storeRef = new WeakReference(store);
83
                this.reference = null;
84
        }
85
86
        public DefaultFeature(FeatureStore store, FeatureProvider data) {
87
                this.data = data;
88
                this.storeRef = new WeakReference(store);
89
                this.reference = null;
90
                this.inserted = !data.isNew();
91
        }
92
93
        DefaultFeature(DefaultFeature feature) {
94
                this.data = feature.data.getCopy();
95
                this.storeRef = feature.storeRef;
96
                this.reference = feature.reference;
97
                this.inserted = feature.isInserted();
98
        }
99
100 43729 fdiaz
    public DefaultFeature(FeatureType targetType, Feature sourceFeature) {
101
        DefaultFeature defaultFeature = (DefaultFeature)sourceFeature;
102
        this.data = new DefaultFeatureProvider(targetType, (DefaultFeatureProvider)defaultFeature.getData());
103
        this.storeRef = defaultFeature.storeRef;
104
        this.reference = defaultFeature.reference;
105
        this.inserted = defaultFeature.isInserted();
106
107
        FeatureType sourceType = sourceFeature.getType();
108
109
        for (FeatureAttributeDescriptor targetAttrDescriptor : targetType) {
110
            if ( targetAttrDescriptor.isAutomatic() ||
111
                 targetAttrDescriptor.isReadOnly() ||
112
                 targetAttrDescriptor.getEvaluator() != null) {
113
                 continue;
114
            }
115
            int sourceIndex = sourceType.getIndex(targetAttrDescriptor.getName());
116
            if (sourceIndex<0){
117
                continue;
118
            }
119
            Object value = sourceFeature.get(sourceIndex);
120
            if (value == null && !targetAttrDescriptor.allowNull()) {
121
                continue;
122
            }
123
            this.set(targetAttrDescriptor,value);
124
        }
125
    }
126
127 40435 jjdelcerro
        public void setData(FeatureProvider data) {
128
                this.data = data;
129
                this.reference = null;
130 43729 fdiaz
                this.inserted = true;
131 40435 jjdelcerro
        }
132
133
        public FeatureProvider getData() {
134
                return this.data;
135
        }
136
137
        protected DataTypesManager getDataTypesManager() {
138
                if( dataTypesManager==null ) {
139
                        dataTypesManager = ToolsLocator.getDataTypesManager();
140
                }
141
                return dataTypesManager;
142
        }
143
144 41335 jjdelcerro
    void set(FeatureAttributeDescriptor attribute, Object value) {
145
        int i = attribute.getIndex();
146 40435 jjdelcerro
147 41335 jjdelcerro
        if ( attribute.isReadOnly() ) {
148 41823 jjdelcerro
            throw new SetReadOnlyAttributeException(attribute.getName(), this.getType());
149 41335 jjdelcerro
        }
150
        FeatureAttributeEmulator emulator = attribute.getFeatureAttributeEmulator();
151
        if( emulator!= null ) {
152
            emulator.set((EditableFeature) this,value);
153
            return;
154
        }
155 43729 fdiaz
156 41335 jjdelcerro
        if ( value == null ) {
157
            if ( !attribute.allowNull() ) {
158
                if ( !attribute.isAutomatic() ) {
159
                    throw new IllegalValueException(attribute, value);
160
                }
161
            }
162
            this.data.set(i, null);
163
            return;
164 40435 jjdelcerro
165 41335 jjdelcerro
        }
166 40435 jjdelcerro
167 41335 jjdelcerro
        if ( attribute.getFeatureAttributeGetter() != null ) {
168
            value = attribute.getFeatureAttributeGetter().setter(value);
169 40435 jjdelcerro
        }
170 41335 jjdelcerro
171 42218 jjdelcerro
        Class objectClass = attribute.getObjectClass();
172
        if( objectClass!=null ) {
173
            if ( objectClass.isInstance(value) ) {
174
                this.data.set(i, value);
175
                return;
176 44448 jjdelcerro
            } else {
177
                DataProfile dataProfile = attribute.getDataProfile();
178
                if( dataProfile!=null ) {
179
                    try {
180
                        value = dataProfile.coerce(
181
                                attribute.getDataType(),
182
                                value,
183
                                attribute.getTags()
184
                        );
185
                    } catch (CoercionException e) {
186
187
                    }
188
                }
189 42218 jjdelcerro
                try {
190 44448 jjdelcerro
                    value= this.getDataTypesManager().coerce(attribute.getType(),value);
191 42218 jjdelcerro
                } catch (CoercionException e) {
192
                    throw new IllegalArgumentException("Can't convert to "
193 44448 jjdelcerro
                            + attribute.getDataType().getName()
194
                            + " from '"
195 42218 jjdelcerro
                            + value.getClass().getName() + "' with value '"
196
                            + value.toString() + "'.");
197
                }
198 40435 jjdelcerro
            }
199 41335 jjdelcerro
        }
200 40435 jjdelcerro
        this.data.set(i, value);
201
    }
202 43729 fdiaz
203 40435 jjdelcerro
    private Object get(int index,Class theClass, int type) {
204
        Object value = this.get(index);
205
        if( theClass.isInstance(value) ) {
206
            return value;
207
        }
208
        try {
209
            return this.getDataTypesManager().coerce(type, value);
210
        } catch (CoercionException e) {
211 43729 fdiaz
212 40435 jjdelcerro
            if (value == null) {
213
                return null;
214
            }
215
            throw new IllegalArgumentException(
216
                    "Can't convert to "+theClass.getName()+
217
                    " from '"+value.getClass().getName()+
218
                    "' with value '"+value.toString()+"'.");
219
        }
220
    }
221 43729 fdiaz
222 40435 jjdelcerro
    public void initializeValues() {
223
        FeatureType type = this.getType();
224 44297 jjdelcerro
        for (FeatureAttributeDescriptor attribute : type) {
225 40435 jjdelcerro
            if (attribute.isAutomatic() || attribute.isReadOnly()
226 44203 jjdelcerro
                    || attribute.isComputed() ) {
227 40435 jjdelcerro
                continue;
228
            }
229
            if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
230
                continue;
231
            }
232
            this.set(attribute, attribute.getDefaultValue());
233
        }
234
    }
235
236
    public void clear() {
237
        initializeValues();
238
    }
239
240
    public void initializeValues(Feature feature) {
241
        FeatureType myType=this.getType();
242
        FeatureType type =feature.getType();
243 44297 jjdelcerro
        for (FeatureAttributeDescriptor attribute : type) {
244 40435 jjdelcerro
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
245
            if (myAttribute != null) {
246
                this.set(myAttribute, feature.get(attribute.getIndex()));
247
            }
248
        }
249
    }
250
251
252 44297 jjdelcerro
    @Override
253 40435 jjdelcerro
    public FeatureStore getStore() {
254
        return (FeatureStore) this.storeRef.get();
255
    }
256
257 44297 jjdelcerro
    @Override
258 40435 jjdelcerro
    public FeatureType getType() {
259
        return this.data.getType();
260
    }
261
262 44297 jjdelcerro
    @Override
263 40435 jjdelcerro
    public EditableFeature getEditable() {
264
        return new DefaultEditableFeature(this);
265
    }
266
267 44297 jjdelcerro
    @Override
268 40435 jjdelcerro
    public Feature getCopy() {
269
        return new DefaultFeature(this);
270
    }
271 43729 fdiaz
272 44297 jjdelcerro
    @Override
273
    @SuppressWarnings("CloneDoesntCallSuperClone")
274 40876 jjdelcerro
    public Object clone() throws CloneNotSupportedException {
275
        return new DefaultFeature(this);
276
    }
277 40435 jjdelcerro
278 44297 jjdelcerro
    @Override
279 40435 jjdelcerro
    public FeatureReference getReference() {
280
        if (this.reference == null) {
281
            if (!isInserted()) {
282
                return null;
283
            }
284
            reference = new DefaultFeatureReference(this);
285
        }
286
        return this.reference;
287
    }
288
289 44331 jjdelcerro
    @Override
290
    public Object getOrDefault(String name, Object defaultValue) {
291
        int index = this.data.getType().getIndex(name);
292
        if( index < 0 ) {
293
            return defaultValue;
294
        }
295
        return this.get(index);
296
    }
297
298
    @Override
299
    public String getStringOrDefault(String name, String defaultValue) {
300
        int index = this.data.getType().getIndex(name);
301
        if( index < 0 ) {
302
            return defaultValue;
303
        }
304
        try {
305
            return (String) this.get(index);
306
        } catch(Throwable th) {
307
            return defaultValue;
308
        }
309
    }
310
311
    @Override
312
    public int getIntOrDefault(String name, int defaultValue) {
313
        int index = this.data.getType().getIndex(name);
314
        if( index < 0 ) {
315
            return defaultValue;
316
        }
317
        try {
318
            return (int) this.get(index);
319
        } catch(Throwable th) {
320
            return defaultValue;
321
        }
322
    }
323
324
    @Override
325
    public long getLongOrDefault(String name, long defaultValue) {
326
        int index = this.data.getType().getIndex(name);
327
        if( index < 0 ) {
328
            return defaultValue;
329
        }
330
        try {
331
            return (long) this.get(index);
332
        } catch(Throwable th) {
333
            return defaultValue;
334
        }
335
    }
336
337
    @Override
338
    public float getFloatOrDefault(String name, float defaultValue) {
339
        int index = this.data.getType().getIndex(name);
340
        if( index < 0 ) {
341
            return defaultValue;
342
        }
343
        try {
344
            return (float) this.get(index);
345
        } catch(Throwable th) {
346
            return defaultValue;
347
        }
348
    }
349
350
    @Override
351
    public double getDoubleOrDefault(String name, double defaultValue) {
352
        int index = this.data.getType().getIndex(name);
353
        if( index < 0 ) {
354
            return defaultValue;
355
        }
356
        try {
357
            return (double) this.get(index);
358
        } catch(Throwable th) {
359
            return defaultValue;
360
        }
361
    }
362
363
    @Override
364
    public Date getDateOrDefault(String name, Date defaultValue) {
365
        int index = this.data.getType().getIndex(name);
366
        if( index < 0 ) {
367
            return defaultValue;
368
        }
369
        try {
370
            return (Date) this.get(index);
371
        } catch(Throwable th) {
372
            return defaultValue;
373
        }
374
    }
375
376
    @Override
377
    public Object getOrDefault(int index, Object defaultValue) {
378
        if( index < 0 || index >= this.data.getType().size() ) {
379
            return defaultValue;
380
        }
381
        try {
382
            return this.get(index);
383
        } catch(Throwable th) {
384
            return defaultValue;
385
        }
386
    }
387
388
    @Override
389
    public String getStringOrDefault(int index, String defaultValue) {
390
        if( index < 0 || index >= this.data.getType().size() ) {
391
            return defaultValue;
392
        }
393
        try {
394
            return (String) this.get(index);
395
        } catch(Throwable th) {
396
            return defaultValue;
397
        }
398
    }
399
400
    @Override
401
    public int getIntOrDefault(int index, int defaultValue) {
402
        if( index < 0 || index >= this.data.getType().size() ) {
403
            return defaultValue;
404
        }
405
        try {
406
            return (int) this.get(index);
407
        } catch(Throwable th) {
408
            return defaultValue;
409
        }
410
    }
411
412
    @Override
413
    public long getLongOrDefault(int index, long defaultValue) {
414
        if( index < 0 || index >= this.data.getType().size() ) {
415
            return defaultValue;
416
        }
417
        try {
418
            return (long) this.get(index);
419
        } catch(Throwable th) {
420
            return defaultValue;
421
        }
422
    }
423
424
    @Override
425
    public float getFloatOrDefault(int index, float defaultValue) {
426
        if( index < 0 || index >= this.data.getType().size() ) {
427
            return defaultValue;
428
        }
429
        try {
430
            return (float) this.get(index);
431
        } catch(Throwable th) {
432
            return defaultValue;
433
        }
434
    }
435
436
    @Override
437
    public double getDoubleOrDefault(int index, double defaultValue) {
438
        if( index < 0 || index >= this.data.getType().size() ) {
439
            return defaultValue;
440
        }
441
        try {
442
            return (double) this.get(index);
443
        } catch(Throwable th) {
444
            return defaultValue;
445
        }
446
    }
447
448
    @Override
449
    public Date getDateOrDefault(int index, Date defaultValue) {
450
        if( index < 0 || index >= this.data.getType().size() ) {
451
            return defaultValue;
452
        }
453
        try {
454
            return (Date) this.get(index);
455
        } catch(Throwable th) {
456
            return defaultValue;
457
        }
458
    }
459
460 40435 jjdelcerro
    class UnableToGetReferenceException extends BaseRuntimeException {
461
462
        /**
463 43729 fdiaz
         *
464 40435 jjdelcerro
         */
465
        private static final long serialVersionUID = 1812805035204824163L;
466
467
        /**
468
         * @param exception
469
         */
470
        public UnableToGetReferenceException(BaseException exception) {
471
            super("Unable to get reference", "_UnableToGetReferenceException",
472
                serialVersionUID);
473
            this.initCause(exception);
474 43729 fdiaz
475 40435 jjdelcerro
        }
476 43729 fdiaz
477 40435 jjdelcerro
    }
478 43729 fdiaz
479 44297 jjdelcerro
    @Override
480 41251 jjdelcerro
    public void validate(int mode) throws DataException  {
481 40435 jjdelcerro
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
482
    }
483
484 44297 jjdelcerro
    @Override
485 40435 jjdelcerro
    public List getSRSs() {
486
        // TODO Auto-generated method stub
487
        return null;
488
    }
489
490 44297 jjdelcerro
    @Override
491 40435 jjdelcerro
    public Envelope getDefaultEnvelope() {
492 41342 jjdelcerro
        Envelope envelope = this.data.getDefaultEnvelope();
493
        if( envelope == null ) {
494
            Geometry geom = this.getDefaultGeometry();
495 41348 jjdelcerro
            if( geom!=null ) {
496
                envelope = geom.getEnvelope();
497
            }
498 41342 jjdelcerro
        }
499
        return envelope;
500 40435 jjdelcerro
    }
501
502 44022 jjdelcerro
    @Override
503 40435 jjdelcerro
    public Geometry getDefaultGeometry() {
504 41058 jjdelcerro
            Geometry geom = this.data.getDefaultGeometry();
505 44022 jjdelcerro
        if( geom == null ) {
506
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
507
            Object x = this.get(i);
508
            if( x instanceof Geometry ) {
509
                geom = (Geometry) x;
510
            } else {
511
                geom = this.getGeometry(i);
512
            }
513 43420 jjdelcerro
        }
514 44022 jjdelcerro
        if( geom != null ) {
515
            if( geom.getProjection()==null ) {
516
                FeatureType type = this.getType();
517
                DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex());
518
                IProjection proj = attrdesc.getSRS(this.storeRef);
519
                geom.setProjection(proj);
520
            }
521
        }
522
        return geom;
523 40435 jjdelcerro
    }
524
525 44086 jjdelcerro
    @Override
526
    public Time getDefaultTime() {
527
            Time time = this.data.getDefaultTime();
528
        if( time == null ) {
529
            int i = this.data.getType().getDefaultTimeAttributeIndex();
530
            Object x = this.get(i);
531
            if( x instanceof Time ) {
532
                time = (Time) x;
533
            } else {
534
                time = this.getTime(i);
535
            }
536
        }
537
        return time;
538
    }
539
540 44297 jjdelcerro
    @Override
541 40435 jjdelcerro
    public IProjection getDefaultSRS() {
542 44022 jjdelcerro
        IProjection srs = this.data.getType().getDefaultSRS();
543
        if( srs == null ) {
544
            FeatureType type = this.getType();
545
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex());
546
            srs = attrdesc.getSRS(this.storeRef);
547
        }
548
        return srs;
549 40435 jjdelcerro
    }
550
551 44297 jjdelcerro
    @Override
552 40435 jjdelcerro
    public List getGeometries() {
553
        // TODO Auto-generated method stub
554
        return null;
555
    }
556
557 44128 jjdelcerro
    @Override
558
    public Object getFromProfile(int index) {
559
        FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(index);
560
        Object value = this.get(index);
561
        String profileName = descriptor.getDataProfileName();
562
        if( StringUtils.isBlank(profileName) ) {
563
            return value;
564
        }
565
        DataProfile profile = DALLocator.getDataManager().getDataProfile(profileName);
566
        if( profile==null ) {
567
            return value;
568
        }
569 44253 jjdelcerro
        return profile.createData(value, descriptor.getTags());
570 44128 jjdelcerro
    }
571
572
    @Override
573
    public Object getFromProfile(String name) {
574
        FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(name);
575
        return this.getFromProfile(descriptor.getIndex());
576
    }
577
578 44297 jjdelcerro
    @Override
579 40435 jjdelcerro
    public Object get(String name) {
580
        int index = this.data.getType().getIndex(name);
581
        if( index < 0 ) {
582
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
583
        }
584
        return this.get(index);
585
    }
586 43729 fdiaz
587 43550 jjdelcerro
    public boolean has_key(String key) {
588
        Object x = this.getType().get(key);
589
        return x != null;
590
    }
591 43729 fdiaz
592 43550 jjdelcerro
    public List<String> keys() {
593
        List<String> ks = new ArrayList<>();
594
        for( FeatureAttributeDescriptor attr : this.getType()) {
595
            ks.add(attr.getName());
596
        }
597
        return ks;
598
    }
599 43729 fdiaz
600 43550 jjdelcerro
    public Iterator<String> iterkeys() {
601
        final Iterator it = this.getType().iterator();
602
        return new Iterator<String>() {
603
            @Override
604
            public boolean hasNext() {
605
                return it.hasNext();
606
            }
607 40435 jjdelcerro
608 43550 jjdelcerro
            @Override
609
            public String next() {
610
                return ((FeatureAttributeDescriptor)it.next()).getName();
611
            }
612 43561 jjdelcerro
613
            @Override
614
            public void remove() {
615
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
616
            }
617 43550 jjdelcerro
        };
618
    }
619 40435 jjdelcerro
620 43550 jjdelcerro
    public Iterator iteritems() {
621
        final Iterator it = this.getType().iterator();
622
        return new Iterator<Map.Entry>() {
623
            @Override
624
            public boolean hasNext() {
625
                return it.hasNext();
626
            }
627
628
            @Override
629
            public Map.Entry next() {
630
                final String name = ((FeatureAttributeDescriptor)it.next()).getName();
631
                return new Map.Entry<String, Object>() {
632
                    @Override
633
                    public String getKey() {
634
                        return name;
635
                    }
636
637
                    @Override
638
                    public Object getValue() {
639
                        return get(name);
640
                    }
641
642
                    @Override
643
                    public Object setValue(Object value) {
644
                        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
645
                    }
646 43729 fdiaz
647 43550 jjdelcerro
                };
648
            }
649 43561 jjdelcerro
650
            @Override
651
            public void remove() {
652
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
653
            }
654 43729 fdiaz
        };
655 43550 jjdelcerro
    }
656
657
    @Override
658 40435 jjdelcerro
    public Object get(int index) {
659
        FeatureType type = this.data.getType();
660
        if( index <0 || index >= type.size() ) {
661
            throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
662
        }
663
        FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
664
        if (!this.data.getType().hasEvaluators()) {
665 43729 fdiaz
            return get(attribute, this.data.get(index));
666
        }
667 40435 jjdelcerro
        Evaluator eval = attribute.getEvaluator();
668
        if (eval == null) {
669
            return this.data.get(index);
670
        } else {
671
            Object value = this.data.get(index);
672
            if (value != null) { // FIXME: para comprobar si esta calculado usar
673
                // un array
674
                // especifico.
675
                return get(attribute, this.data.get(index));
676
            }
677
            try {
678
                value = eval.evaluate(this);
679
            } catch (EvaluatorException e) {
680
                throw new DataEvaluatorRuntimeException(e);
681
            }
682
            this.data.set(index, value);
683
            return  get(attribute, value);
684 43729 fdiaz
        }
685 40435 jjdelcerro
    }
686
687
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
688 41335 jjdelcerro
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
689
        if( emulator != null ) {
690 44226 jjdelcerro
//            int index = featureAttributeDescriptor.getIndex();
691
//            value = this.data.get(index);
692
//            if( value==null ) {
693 44203 jjdelcerro
                value = emulator.get(this);
694 44226 jjdelcerro
//                this.data.set(index,value);
695
//            }
696 44022 jjdelcerro
        } else {
697
            FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
698
            if( getter != null ) {
699
                value = getter.getter(value);
700
            }
701 40435 jjdelcerro
        }
702 44022 jjdelcerro
        if( featureAttributeDescriptor.getType()==DataTypes.GEOMETRY ) {
703
            if( value != null ) {
704
                Geometry geom = (Geometry)value;
705
                if( geom.getProjection()==null ) {
706
                    IProjection proj = ((DefaultFeatureAttributeDescriptor)featureAttributeDescriptor).getSRS(this.storeRef);
707
                    geom.setProjection(proj);
708
                }
709
            }
710 41335 jjdelcerro
        }
711
        return value;
712 40435 jjdelcerro
    }
713
714 44297 jjdelcerro
    @Override
715
    public byte[] getByteArray(String name) {
716
        return this.getByteArray(this.data.getType().getIndex(name));
717
    }
718
719
    @Override
720
    public byte[] getByteArray(int index) {
721
        return (byte[]) this.get(index);
722
    }
723
724
    @Override
725 40435 jjdelcerro
    public Object[] getArray(String name) {
726
        return this.getArray(this.data.getType().getIndex(name));
727
    }
728
729 44297 jjdelcerro
    @Override
730 40435 jjdelcerro
    public Object[] getArray(int index) {
731
        return (Object[]) this.get(index);
732
    }
733
734 44297 jjdelcerro
    @Override
735 40435 jjdelcerro
    public boolean getBoolean(String name) {
736
        return this.getBoolean(this.data.getType().getIndex(name));
737
    }
738
739 44297 jjdelcerro
    @Override
740 40435 jjdelcerro
    public boolean getBoolean(int index) {
741
        Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
742
        if (value == null) {
743
            return false;
744
        }
745 44297 jjdelcerro
        return value;
746 40435 jjdelcerro
    }
747
748 44297 jjdelcerro
    @Override
749 40435 jjdelcerro
    public byte getByte(String name) {
750
        return this.getByte(this.data.getType().getIndex(name));
751
    }
752
753 44297 jjdelcerro
    @Override
754 40435 jjdelcerro
    public byte getByte(int index) {
755
        Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
756
        if (value == null) {
757
            return 0;
758
        }
759 44297 jjdelcerro
        return value;
760 40435 jjdelcerro
    }
761
762 44297 jjdelcerro
    @Override
763 40435 jjdelcerro
    public Date getDate(String name) {
764
        return this.getDate(this.data.getType().getIndex(name));
765
    }
766
767 44297 jjdelcerro
    @Override
768 40435 jjdelcerro
    public Date getDate(int index) {
769
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
770
771
        return value;
772
    }
773
774 44297 jjdelcerro
    @Override
775 40435 jjdelcerro
    public double getDouble(String name) {
776
        return this.getDouble(this.data.getType().getIndex(name));
777
    }
778
779 44297 jjdelcerro
    @Override
780 40435 jjdelcerro
    public double getDouble(int index) {
781 43729 fdiaz
782 40435 jjdelcerro
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
783
        if (value == null) {
784
            return 0;
785
        }
786 44297 jjdelcerro
        return value;
787 40435 jjdelcerro
    }
788
789 44297 jjdelcerro
    @Override
790 40435 jjdelcerro
    public Feature getFeature(String name) {
791
        return this.getFeature(this.data.getType().getIndex(name));
792
    }
793
794 44297 jjdelcerro
    @Override
795 40435 jjdelcerro
    public Feature getFeature(int index) {
796
        return (Feature) this.get(index);
797
    }
798
799 44297 jjdelcerro
    @Override
800 40435 jjdelcerro
    public float getFloat(String name) {
801
        return this.getFloat(this.data.getType().getIndex(name));
802
    }
803
804 44297 jjdelcerro
    @Override
805 40435 jjdelcerro
    public float getFloat(int index) {
806
        Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
807
        if (value == null) {
808
            return 0;
809
        }
810 44297 jjdelcerro
        return value;
811 40435 jjdelcerro
    }
812
813 44297 jjdelcerro
    @Override
814 40435 jjdelcerro
    public Geometry getGeometry(String name) {
815
        return this.getGeometry(this.data.getType().getIndex(name));
816
    }
817
818 44297 jjdelcerro
    @Override
819 40435 jjdelcerro
    public Geometry getGeometry(int index) {
820
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
821
    }
822
823 44297 jjdelcerro
    @Override
824 40435 jjdelcerro
    public int getInt(String name) {
825
        return this.getInt(this.data.getType().getIndex(name));
826
    }
827
828 44297 jjdelcerro
    @Override
829 40435 jjdelcerro
    public int getInt(int index) {
830
        Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
831
        if (value == null) {
832
            return 0;
833
        }
834 44297 jjdelcerro
        return value;
835 40435 jjdelcerro
    }
836
837 44297 jjdelcerro
    @Override
838 40435 jjdelcerro
    public long getLong(String name) {
839
        return this.getLong(this.data.getType().getIndex(name));
840
    }
841
842 44297 jjdelcerro
    @Override
843 40435 jjdelcerro
    public long getLong(int index) {
844
        Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
845
        if (value == null) {
846
            return 0;
847
        }
848 44297 jjdelcerro
        return value;
849 40435 jjdelcerro
    }
850
851 44297 jjdelcerro
    @Override
852 40435 jjdelcerro
    public String getString(String name) {
853
        return this.getString(this.data.getType().getIndex(name));
854
    }
855
856 44297 jjdelcerro
    @Override
857 40435 jjdelcerro
    public String getString(int index) {
858
        return (String) this.get(index,String.class,DataTypes.STRING);
859
    }
860
861 44297 jjdelcerro
    @Override
862 40435 jjdelcerro
    public Object getContextValue(String name) {
863
        name = name.toLowerCase();
864
        if (name.equals("store")) {
865
            return this.getStore();
866
        }
867
868
        if (name.equals("featuretype")) {
869
            return this.data.getType();
870
        }
871
872
        if (name.equals("feature")) {
873
            return this;
874
        }
875
876
        throw new IllegalArgumentException(name);
877
    }
878
879 44297 jjdelcerro
    @Override
880 40435 jjdelcerro
    public Iterator getDataNames() {
881
        class DataNamesIterator implements Iterator {
882
            Iterator attributeIteraror;
883
884
            DataNamesIterator(DefaultFeature feature) {
885
                this.attributeIteraror = feature.getType().iterator();
886
            }
887
888 44297 jjdelcerro
            @Override
889 40435 jjdelcerro
            public boolean hasNext() {
890
                return this.attributeIteraror.hasNext();
891
            }
892
893 44297 jjdelcerro
            @Override
894 40435 jjdelcerro
            public Object next() {
895
                return ((FeatureAttributeDescriptor) this.attributeIteraror
896
                        .next()).getName();
897
            }
898
899 44297 jjdelcerro
            @Override
900 40435 jjdelcerro
            public void remove() {
901
                throw new UnsupportedOperationException();
902
            }
903
904
        }
905
        return new DataNamesIterator(this);
906
    }
907
908 44297 jjdelcerro
    @Override
909 40435 jjdelcerro
    public Object getDataValue(String name) {
910
        name = name.toLowerCase();
911 41542 jjdelcerro
        try {
912
            return get(name);
913
        } catch (IllegalArgumentException ex) {
914
            if( "defaultgeometry".equalsIgnoreCase(name )) {
915
                return this.getDefaultGeometry();
916
            }
917
            throw ex;
918
        }
919 40435 jjdelcerro
    }
920
921 44297 jjdelcerro
    @Override
922 40435 jjdelcerro
    public Iterator getDataValues() {
923
        class DataValuesIterator implements Iterator {
924
            DefaultFeature feature;
925
            int current = 0;
926
927
            DataValuesIterator(DefaultFeature feature) {
928
                this.feature = feature;
929
            }
930
931 44297 jjdelcerro
            @Override
932 40435 jjdelcerro
            public boolean hasNext() {
933
                return current < feature.getType().size() - 1;
934
            }
935
936 44297 jjdelcerro
            @Override
937 40435 jjdelcerro
            public Object next() {
938
                return feature.get(current++);
939
            }
940
941 44297 jjdelcerro
            @Override
942 40435 jjdelcerro
            public void remove() {
943
                throw new UnsupportedOperationException();
944
            }
945
946
        }
947
        return new DataValuesIterator(this);
948
    }
949
950 44297 jjdelcerro
    @Override
951 40435 jjdelcerro
    public boolean hasContextValue(String name) {
952
        name = name.toLowerCase();
953
        if (name.equals("store")) {
954
            return true;
955
        }
956
957
        if (name.equals("featuretype")) {
958
            return true;
959
        }
960
961 44297 jjdelcerro
        return name.equals("feature");
962 40435 jjdelcerro
    }
963
964 44297 jjdelcerro
    @Override
965 40435 jjdelcerro
    public boolean hasDataValue(String name) {
966
        name = name.toLowerCase();
967
        return this.data.getType().getIndex(name) >= 0;
968
    }
969 43729 fdiaz
970 44297 jjdelcerro
    @Override
971 44086 jjdelcerro
    public Time getTime(int index) {
972
        return ((Time) this.get(index,Time.class,DataTypes.INSTANT));
973
    }
974
975 44297 jjdelcerro
    @Override
976 44086 jjdelcerro
    public Time getTime(String name) {
977
        return this.getInstant(this.data.getType().getIndex(name));
978
    }
979
980 44297 jjdelcerro
    @Override
981 40435 jjdelcerro
    public Instant getInstant(int index) {
982 44086 jjdelcerro
        return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT));
983 40435 jjdelcerro
    }
984
985 44297 jjdelcerro
    @Override
986 40435 jjdelcerro
    public Instant getInstant(String name) {
987
        return this.getInstant(this.data.getType().getIndex(name));
988
    }
989
990 44297 jjdelcerro
    @Override
991 40435 jjdelcerro
    public Interval getInterval(int index) {
992 44086 jjdelcerro
        return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL));
993 40435 jjdelcerro
    }
994
995 44297 jjdelcerro
    @Override
996 40435 jjdelcerro
    public Interval getInterval(String name) {
997
        return this.getInterval(this.data.getType().getIndex(name));
998
    }
999
1000 42775 jjdelcerro
    @Override
1001 40435 jjdelcerro
    public DynObject getAsDynObject() {
1002 42775 jjdelcerro
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
1003 40435 jjdelcerro
        return facade;
1004
    }
1005
1006 44297 jjdelcerro
    @Override
1007 40435 jjdelcerro
    public String toString() {
1008 44297 jjdelcerro
            StringBuilder builder = new StringBuilder();
1009 40435 jjdelcerro
        FeatureAttributeDescriptor[] attributeDescriptors =
1010
            getType().getAttributeDescriptors();
1011
        for (int i = 0; i < attributeDescriptors.length; i++) {
1012
            String name = attributeDescriptors[i].getName();
1013 44297 jjdelcerro
            builder.append(get(name));
1014 40435 jjdelcerro
            if (i < attributeDescriptors.length - 1) {
1015 44297 jjdelcerro
                builder.append(", ");
1016 40435 jjdelcerro
            }
1017
        }
1018 44297 jjdelcerro
        return builder.toString();
1019 40435 jjdelcerro
    }
1020 40867 jbadia
1021
1022 43729 fdiaz
1023
1024 40867 jbadia
        /**
1025 40435 jjdelcerro
     * @return the inserted
1026
     */
1027
    public boolean isInserted() {
1028
        return inserted;
1029
    }
1030
1031 43729 fdiaz
1032 40435 jjdelcerro
    /**
1033
     * @param inserted the inserted to set
1034
     */
1035
    public void setInserted(boolean inserted) {
1036
        this.inserted = inserted;
1037
    }
1038
1039 43983 jjdelcerro
        @Override
1040 40435 jjdelcerro
    public EvaluatorData getEvaluatorData() {
1041
        return this;
1042
    }
1043 43983 jjdelcerro
1044
    public int size() {
1045
        return this.data.getType().size();
1046
    }
1047
1048
    public boolean isEmpty() {
1049
        return false;
1050
    }
1051
1052
    public Iterator<String> iterator() {
1053
        final Iterator<FeatureAttributeDescriptor> x = this.data.getType().iterator();
1054
        return new Iterator<String>() {
1055
            @Override
1056
            public boolean hasNext() {
1057
                return x.hasNext();
1058
            }
1059
1060
            @Override
1061
            public String next() {
1062
                return x.next().getName();
1063
            }
1064
        };
1065
    }
1066
1067
    public boolean containsKey(String key) {
1068
        return this.data.getType().get(key)!=null;
1069
    }
1070 44346 jjdelcerro
1071
    @Override
1072
    public String getLabelOfValue(String name) {
1073
        FeatureAttributeDescriptor attrdesc = this.data.getType().getAttributeDescriptor(name);
1074
        if( attrdesc==null ) {
1075
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
1076
        }
1077
        Object value = this.get(attrdesc.getIndex());
1078
        String label = attrdesc.getLabelOfValue(value);
1079
        return label;
1080
    }
1081 44376 jjdelcerro
1082
    @Override
1083
    public Object getExtraValue(String name) {
1084
        return this.data.getExtraValue(name);
1085
    }
1086
1087
    @Override
1088
    public Object getExtraValue(int index) {
1089
        return this.data.getExtraValue(index);
1090
    }
1091
1092
1093 40435 jjdelcerro
}