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

History | View | Annotate | Download (31.9 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.lang.ref.WeakReference;
27
import java.util.ArrayList;
28
import java.util.Date;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32
import org.apache.commons.lang3.StringUtils;
33
import org.cresques.cts.IProjection;
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataTypes;
36
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.DataProfile;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
43
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
44
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
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectFeatureFacade;
50
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
51
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
import org.gvsig.timesupport.Time;
57
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
import org.gvsig.tools.lang.Cloneable;
67

    
68
public class DefaultFeature implements Feature, EvaluatorData, Cloneable {
69

    
70
        private static DataTypesManager dataTypesManager = null;
71
        protected FeatureProvider data;
72
        protected FeatureReference reference;
73
        private WeakReference storeRef;
74

    
75
        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
    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
        public void setData(FeatureProvider data) {
128
                this.data = data;
129
                this.reference = null;
130
                this.inserted = true;
131
        }
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
    void set(FeatureAttributeDescriptor attribute, Object value) {
145
        int i = attribute.getIndex();
146

    
147
        if ( attribute.isReadOnly() ) {
148
            throw new SetReadOnlyAttributeException(attribute.getName(), this.getType());
149
        }
150
        FeatureAttributeEmulator emulator = attribute.getFeatureAttributeEmulator();
151
        if( emulator!= null ) {
152
            emulator.set((EditableFeature) this,value);
153
            return;
154
        }
155

    
156
        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

    
165
        }
166

    
167
        if ( attribute.getFeatureAttributeGetter() != null ) {
168
            value = attribute.getFeatureAttributeGetter().setter(value);
169
        }
170

    
171
        Class objectClass = attribute.getObjectClass();
172
        if( objectClass!=null ) {
173
            if ( objectClass.isInstance(value) ) {
174
                this.data.set(i, value);
175
                return;
176
            } 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
                try {
190
                    value= this.getDataTypesManager().coerce(attribute.getType(),value);
191
                } catch (CoercionException e) {
192
                    throw new IllegalArgumentException("Can't convert to "
193
                            + attribute.getDataType().getName()
194
                            + " from '"
195
                            + value.getClass().getName() + "' with value '"
196
                            + value.toString() + "'.");
197
                }
198
            }
199
        }
200
        this.data.set(i, value);
201
    }
202

    
203
    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

    
212
            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

    
222
    public void initializeValues() {
223
        FeatureType type = this.getType();
224
        for (FeatureAttributeDescriptor attribute : type) {
225
            if (attribute.isAutomatic() || attribute.isReadOnly()
226
                    || attribute.isComputed() ) {
227
                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
        for (FeatureAttributeDescriptor attribute : type) {
244
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
245
            if (myAttribute != null) {
246
                this.set(myAttribute, feature.get(attribute.getIndex()));
247
            }
248
        }
249
    }
250

    
251

    
252
    @Override
253
    public FeatureStore getStore() {
254
        return (FeatureStore) this.storeRef.get();
255
    }
256

    
257
    @Override
258
    public FeatureType getType() {
259
        return this.data.getType();
260
    }
261

    
262
    @Override
263
    public EditableFeature getEditable() {
264
        return new DefaultEditableFeature(this);
265
    }
266

    
267
    @Override
268
    public Feature getCopy() {
269
        return new DefaultFeature(this);
270
    }
271

    
272
    @Override
273
    @SuppressWarnings("CloneDoesntCallSuperClone")
274
    public Object clone() throws CloneNotSupportedException {
275
        return new DefaultFeature(this);
276
    }
277

    
278
    @Override
279
    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
    @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
    class UnableToGetReferenceException extends BaseRuntimeException {
461

    
462
        /**
463
         *
464
         */
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

    
475
        }
476

    
477
    }
478

    
479
    @Override
480
    public void validate(int mode) throws DataException  {
481
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
482
    }
483

    
484
    @Override
485
    public List getSRSs() {
486
        // TODO Auto-generated method stub
487
        return null;
488
    }
489

    
490
    @Override
491
    public Envelope getDefaultEnvelope() {
492
        Envelope envelope = this.data.getDefaultEnvelope();
493
        if( envelope == null ) {
494
            Geometry geom = this.getDefaultGeometry();
495
            if( geom!=null ) {
496
                envelope = geom.getEnvelope();
497
            }
498
        }
499
        return envelope;
500
    }
501

    
502
    @Override
503
    public Geometry getDefaultGeometry() {
504
            Geometry geom = this.data.getDefaultGeometry();
505
        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
        }
514
        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
    }
524

    
525
    @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
    @Override
541
    public IProjection getDefaultSRS() {
542
        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
    }
550

    
551
    @Override
552
    public List getGeometries() {
553
        // TODO Auto-generated method stub
554
        return null;
555
    }
556

    
557
    @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
        return profile.createData(value, descriptor.getTags());
570
    }
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
    @Override
579
    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

    
587
    public boolean has_key(String key) {
588
        Object x = this.getType().get(key);
589
        return x != null;
590
    }
591

    
592
    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

    
600
    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

    
608
            @Override
609
            public String next() {
610
                return ((FeatureAttributeDescriptor)it.next()).getName();
611
            }
612

    
613
            @Override
614
            public void remove() {
615
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
616
            }
617
        };
618
    }
619

    
620
    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

    
647
                };
648
            }
649

    
650
            @Override
651
            public void remove() {
652
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
653
            }
654
        };
655
    }
656

    
657
    @Override
658
    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
            return get(attribute, this.data.get(index));
666
        }
667
        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
        }
685
    }
686

    
687
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
688
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
689
        if( emulator != null ) {
690
//            int index = featureAttributeDescriptor.getIndex();
691
//            value = this.data.get(index);
692
//            if( value==null ) {
693
                value = emulator.get(this);
694
//                this.data.set(index,value);
695
//            }
696
        } else {
697
            FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
698
            if( getter != null ) {
699
                value = getter.getter(value);
700
            }
701
        }
702
        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
        }
711
        return value;
712
    }
713

    
714
    @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
    public Object[] getArray(String name) {
726
        return this.getArray(this.data.getType().getIndex(name));
727
    }
728

    
729
    @Override
730
    public Object[] getArray(int index) {
731
        return (Object[]) this.get(index);
732
    }
733

    
734
    @Override
735
    public boolean getBoolean(String name) {
736
        return this.getBoolean(this.data.getType().getIndex(name));
737
    }
738

    
739
    @Override
740
    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
        return value;
746
    }
747

    
748
    @Override
749
    public byte getByte(String name) {
750
        return this.getByte(this.data.getType().getIndex(name));
751
    }
752

    
753
    @Override
754
    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
        return value;
760
    }
761

    
762
    @Override
763
    public Date getDate(String name) {
764
        return this.getDate(this.data.getType().getIndex(name));
765
    }
766

    
767
    @Override
768
    public Date getDate(int index) {
769
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
770

    
771
        return value;
772
    }
773

    
774
    @Override
775
    public double getDouble(String name) {
776
        return this.getDouble(this.data.getType().getIndex(name));
777
    }
778

    
779
    @Override
780
    public double getDouble(int index) {
781

    
782
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
783
        if (value == null) {
784
            return 0;
785
        }
786
        return value;
787
    }
788

    
789
    @Override
790
    public Feature getFeature(String name) {
791
        return this.getFeature(this.data.getType().getIndex(name));
792
    }
793

    
794
    @Override
795
    public Feature getFeature(int index) {
796
        return (Feature) this.get(index);
797
    }
798

    
799
    @Override
800
    public float getFloat(String name) {
801
        return this.getFloat(this.data.getType().getIndex(name));
802
    }
803

    
804
    @Override
805
    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
        return value;
811
    }
812

    
813
    @Override
814
    public Geometry getGeometry(String name) {
815
        return this.getGeometry(this.data.getType().getIndex(name));
816
    }
817

    
818
    @Override
819
    public Geometry getGeometry(int index) {
820
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
821
    }
822

    
823
    @Override
824
    public int getInt(String name) {
825
        return this.getInt(this.data.getType().getIndex(name));
826
    }
827

    
828
    @Override
829
    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
        return value;
835
    }
836

    
837
    @Override
838
    public long getLong(String name) {
839
        return this.getLong(this.data.getType().getIndex(name));
840
    }
841

    
842
    @Override
843
    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
        return value;
849
    }
850

    
851
    @Override
852
    public String getString(String name) {
853
        return this.getString(this.data.getType().getIndex(name));
854
    }
855

    
856
    @Override
857
    public String getString(int index) {
858
        return (String) this.get(index,String.class,DataTypes.STRING);
859
    }
860

    
861
    @Override
862
    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
    @Override
880
    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
            @Override
889
            public boolean hasNext() {
890
                return this.attributeIteraror.hasNext();
891
            }
892

    
893
            @Override
894
            public Object next() {
895
                return ((FeatureAttributeDescriptor) this.attributeIteraror
896
                        .next()).getName();
897
            }
898

    
899
            @Override
900
            public void remove() {
901
                throw new UnsupportedOperationException();
902
            }
903

    
904
        }
905
        return new DataNamesIterator(this);
906
    }
907

    
908
    @Override
909
    public Object getDataValue(String name) {
910
        name = name.toLowerCase();
911
        try {
912
            return get(name);
913
        } catch (IllegalArgumentException ex) {
914
            if( "defaultgeometry".equalsIgnoreCase(name )) {
915
                return this.getDefaultGeometry();
916
            }
917
            throw ex;
918
        }
919
    }
920

    
921
    @Override
922
    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
            @Override
932
            public boolean hasNext() {
933
                return current < feature.getType().size() - 1;
934
            }
935

    
936
            @Override
937
            public Object next() {
938
                return feature.get(current++);
939
            }
940

    
941
            @Override
942
            public void remove() {
943
                throw new UnsupportedOperationException();
944
            }
945

    
946
        }
947
        return new DataValuesIterator(this);
948
    }
949

    
950
    @Override
951
    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
        return name.equals("feature");
962
    }
963

    
964
    @Override
965
    public boolean hasDataValue(String name) {
966
        name = name.toLowerCase();
967
        return this.data.getType().getIndex(name) >= 0;
968
    }
969

    
970
    @Override
971
    public Time getTime(int index) {
972
        return ((Time) this.get(index,Time.class,DataTypes.INSTANT));
973
    }
974

    
975
    @Override
976
    public Time getTime(String name) {
977
        return this.getInstant(this.data.getType().getIndex(name));
978
    }
979

    
980
    @Override
981
    public Instant getInstant(int index) {
982
        return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT));
983
    }
984

    
985
    @Override
986
    public Instant getInstant(String name) {
987
        return this.getInstant(this.data.getType().getIndex(name));
988
    }
989

    
990
    @Override
991
    public Interval getInterval(int index) {
992
        return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL));
993
    }
994

    
995
    @Override
996
    public Interval getInterval(String name) {
997
        return this.getInterval(this.data.getType().getIndex(name));
998
    }
999

    
1000
    @Override
1001
    public DynObject getAsDynObject() {
1002
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
1003
        return facade;
1004
    }
1005

    
1006
    @Override
1007
    public String toString() {
1008
            StringBuilder builder = new StringBuilder();
1009
        FeatureAttributeDescriptor[] attributeDescriptors =
1010
            getType().getAttributeDescriptors();
1011
        for (int i = 0; i < attributeDescriptors.length; i++) {
1012
            String name = attributeDescriptors[i].getName();
1013
            Object value = get(name);
1014
            builder.append(value);
1015
            if (i < attributeDescriptors.length - 1) {
1016
                builder.append(", ");
1017
            }
1018
        }
1019
        return builder.toString();
1020
    }
1021

    
1022

    
1023

    
1024

    
1025
        /**
1026
     * @return the inserted
1027
     */
1028
    public boolean isInserted() {
1029
        return inserted;
1030
    }
1031

    
1032

    
1033
    /**
1034
     * @param inserted the inserted to set
1035
     */
1036
    public void setInserted(boolean inserted) {
1037
        this.inserted = inserted;
1038
    }
1039

    
1040
        @Override
1041
    public EvaluatorData getEvaluatorData() {
1042
        return this;
1043
    }
1044

    
1045
    public int size() {
1046
        return this.data.getType().size();
1047
    }
1048
    
1049
    public boolean isEmpty() {
1050
        return false;
1051
    }
1052

    
1053
    public Iterator<String> iterator() {
1054
        final Iterator<FeatureAttributeDescriptor> x = this.data.getType().iterator();
1055
        return new Iterator<String>() {
1056
            @Override
1057
            public boolean hasNext() {
1058
                return x.hasNext();
1059
            }
1060

    
1061
            @Override
1062
            public String next() {
1063
                return x.next().getName();
1064
            }
1065
        };
1066
    }
1067
    
1068
    public boolean containsKey(String key) {
1069
        return this.data.getType().get(key)!=null;
1070
    }
1071

    
1072
    @Override
1073
    public String getLabelOfValue(String name) {
1074
        FeatureAttributeDescriptor attrdesc = this.data.getType().getAttributeDescriptor(name);
1075
        if( attrdesc==null ) {
1076
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
1077
        }
1078
        Object value = this.get(attrdesc.getIndex());
1079
        String label = attrdesc.getLabelOfValue(value);
1080
        return label;
1081
    }
1082

    
1083
    @Override
1084
    public Object getExtraValue(String name) {
1085
        return this.data.getExtraValue(name);
1086
    }
1087

    
1088
    @Override
1089
    public Object getExtraValue(int index) {
1090
        return this.data.getExtraValue(index);
1091
    }
1092

    
1093

    
1094
}