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

History | View | Annotate | Download (32.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl;
25

    
26
import java.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
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
495
            if( i<0 ) {
496
                return null;
497
            }
498
            Geometry geom = this.getDefaultGeometry();
499
            if( geom!=null ) {
500
                envelope = geom.getEnvelope();
501
            }
502
        }
503
        return envelope;
504
    }
505

    
506
    @Override
507
    public Geometry getDefaultGeometry() {
508
            Geometry geom = this.data.getDefaultGeometry();
509
        if( geom == null ) {
510
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
511
            if( i<0 ) {
512
                return null;
513
            }
514
            Object x = this.get(i);
515
            if( x instanceof Geometry ) {
516
                geom = (Geometry) x;
517
            } else {
518
                geom = this.getGeometry(i);
519
            }
520
        }
521
        if( geom != null ) {
522
            if( geom.getProjection()==null ) {
523
                FeatureType type = this.getType();
524
                DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex());
525
                IProjection proj = attrdesc.getSRS(this.storeRef);
526
                geom.setProjection(proj);
527
            }
528
        }
529
        return geom;
530
    }
531

    
532
    @Override
533
    public Time getDefaultTime() {
534
            Time time = this.data.getDefaultTime();
535
        if( time == null ) {
536
            int i = this.data.getType().getDefaultTimeAttributeIndex();
537
            Object x = this.get(i);
538
            if( x instanceof Time ) {
539
                time = (Time) x;
540
            } else {
541
                time = this.getTime(i);
542
            }
543
        }
544
        return time;
545
    }
546

    
547
    @Override
548
    public IProjection getDefaultSRS() {
549
        IProjection srs = this.data.getType().getDefaultSRS();
550
        if( srs == null ) {
551
            FeatureType type = this.getType();
552
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex());
553
            srs = attrdesc.getSRS(this.storeRef);
554
        }
555
        return srs;
556
    }
557

    
558
    @Override
559
    public List getGeometries() {
560
        // TODO Auto-generated method stub
561
        return null;
562
    }
563

    
564
    @Override
565
    public Object getFromProfile(int index) {
566
        FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(index);
567
        Object value = this.get(index);
568
        String profileName = descriptor.getDataProfileName();
569
        if( StringUtils.isBlank(profileName) ) {
570
            return value;
571
        }
572
        DataProfile profile = DALLocator.getDataManager().getDataProfile(profileName);
573
        if( profile==null ) {
574
            return value;
575
        }
576
        return profile.createData(value, descriptor.getTags());
577
    }
578

    
579
    @Override
580
    public Object getFromProfile(String name) {
581
        FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(name);
582
        return this.getFromProfile(descriptor.getIndex());
583
    }
584

    
585
    @Override
586
    public Object get(String name) {
587
        int index = this.data.getType().getIndex(name);
588
        if( index < 0 ) {
589
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
590
        }
591
        return this.get(index);
592
    }
593

    
594
    public boolean has_key(String key) {
595
        Object x = this.getType().get(key);
596
        return x != null;
597
    }
598

    
599
    public List<String> keys() {
600
        List<String> ks = new ArrayList<>();
601
        for( FeatureAttributeDescriptor attr : this.getType()) {
602
            ks.add(attr.getName());
603
        }
604
        return ks;
605
    }
606

    
607
    public Iterator<String> iterkeys() {
608
        final Iterator it = this.getType().iterator();
609
        return new Iterator<String>() {
610
            @Override
611
            public boolean hasNext() {
612
                return it.hasNext();
613
            }
614

    
615
            @Override
616
            public String next() {
617
                return ((FeatureAttributeDescriptor)it.next()).getName();
618
            }
619

    
620
            @Override
621
            public void remove() {
622
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
623
            }
624
        };
625
    }
626

    
627
    public Iterator iteritems() {
628
        final Iterator it = this.getType().iterator();
629
        return new Iterator<Map.Entry>() {
630
            @Override
631
            public boolean hasNext() {
632
                return it.hasNext();
633
            }
634

    
635
            @Override
636
            public Map.Entry next() {
637
                final String name = ((FeatureAttributeDescriptor)it.next()).getName();
638
                return new Map.Entry<String, Object>() {
639
                    @Override
640
                    public String getKey() {
641
                        return name;
642
                    }
643

    
644
                    @Override
645
                    public Object getValue() {
646
                        return get(name);
647
                    }
648

    
649
                    @Override
650
                    public Object setValue(Object value) {
651
                        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
652
                    }
653

    
654
                };
655
            }
656

    
657
            @Override
658
            public void remove() {
659
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
660
            }
661
        };
662
    }
663

    
664
    @Override
665
    public Object get(int index) {
666
        FeatureType type = this.data.getType();
667
        if( index <0 || index >= type.size() ) {
668
            throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
669
        }
670
        FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
671
        if (!this.data.getType().hasEvaluators()) {
672
            return get(attribute, this.data.get(index));
673
        }
674
        Evaluator eval = attribute.getEvaluator();
675
        if (eval == null) {
676
            return this.data.get(index);
677
        } else {
678
            Object value = this.data.get(index);
679
            if (value != null) { // FIXME: para comprobar si esta calculado usar
680
                // un array
681
                // especifico.
682
                return get(attribute, this.data.get(index));
683
            }
684
            try {
685
                value = eval.evaluate(this);
686
            } catch (EvaluatorException e) {
687
                throw new DataEvaluatorRuntimeException(e);
688
            }
689
            this.data.set(index, value);
690
            return  get(attribute, value);
691
        }
692
    }
693

    
694
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
695
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
696
        if( emulator != null ) {
697
//            int index = featureAttributeDescriptor.getIndex();
698
//            value = this.data.get(index);
699
//            if( value==null ) {
700
                value = emulator.get(this);
701
//                this.data.set(index,value);
702
//            }
703
        } else {
704
            FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
705
            if( getter != null ) {
706
                value = getter.getter(value);
707
            }
708
        }
709
        if( featureAttributeDescriptor.getType()==DataTypes.GEOMETRY ) {
710
            if( value != null ) {
711
                Geometry geom = (Geometry)value;
712
                if( geom.getProjection()==null ) {
713
                    IProjection proj = ((DefaultFeatureAttributeDescriptor)featureAttributeDescriptor).getSRS(this.storeRef);
714
                    geom.setProjection(proj);
715
                }
716
            }
717
        }
718
        return value;
719
    }
720

    
721
    @Override
722
    public byte[] getByteArray(String name) {
723
        return this.getByteArray(this.data.getType().getIndex(name));
724
    }
725

    
726
    @Override
727
    public byte[] getByteArray(int index) {
728
        return (byte[]) this.get(index);
729
    }
730

    
731
    @Override
732
    public Object[] getArray(String name) {
733
        return this.getArray(this.data.getType().getIndex(name));
734
    }
735

    
736
    @Override
737
    public Object[] getArray(int index) {
738
        return (Object[]) this.get(index);
739
    }
740

    
741
    @Override
742
    public boolean getBoolean(String name) {
743
        return this.getBoolean(this.data.getType().getIndex(name));
744
    }
745

    
746
    @Override
747
    public boolean getBoolean(int index) {
748
        Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
749
        if (value == null) {
750
            return false;
751
        }
752
        return value;
753
    }
754

    
755
    @Override
756
    public byte getByte(String name) {
757
        return this.getByte(this.data.getType().getIndex(name));
758
    }
759

    
760
    @Override
761
    public byte getByte(int index) {
762
        Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
763
        if (value == null) {
764
            return 0;
765
        }
766
        return value;
767
    }
768

    
769
    @Override
770
    public Date getDate(String name) {
771
        return this.getDate(this.data.getType().getIndex(name));
772
    }
773

    
774
    @Override
775
    public Date getDate(int index) {
776
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
777

    
778
        return value;
779
    }
780

    
781
    @Override
782
    public double getDouble(String name) {
783
        return this.getDouble(this.data.getType().getIndex(name));
784
    }
785

    
786
    @Override
787
    public double getDouble(int index) {
788

    
789
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
790
        if (value == null) {
791
            return 0;
792
        }
793
        return value;
794
    }
795

    
796
    @Override
797
    public Feature getFeature(String name) {
798
        return this.getFeature(this.data.getType().getIndex(name));
799
    }
800

    
801
    @Override
802
    public Feature getFeature(int index) {
803
        return (Feature) this.get(index);
804
    }
805

    
806
    @Override
807
    public float getFloat(String name) {
808
        return this.getFloat(this.data.getType().getIndex(name));
809
    }
810

    
811
    @Override
812
    public float getFloat(int index) {
813
        Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
814
        if (value == null) {
815
            return 0;
816
        }
817
        return value;
818
    }
819

    
820
    @Override
821
    public Geometry getGeometry(String name) {
822
        return this.getGeometry(this.data.getType().getIndex(name));
823
    }
824

    
825
    @Override
826
    public Geometry getGeometry(int index) {
827
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
828
    }
829

    
830
    @Override
831
    public int getInt(String name) {
832
        return this.getInt(this.data.getType().getIndex(name));
833
    }
834

    
835
    @Override
836
    public int getInt(int index) {
837
        Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
838
        if (value == null) {
839
            return 0;
840
        }
841
        return value;
842
    }
843

    
844
    @Override
845
    public long getLong(String name) {
846
        return this.getLong(this.data.getType().getIndex(name));
847
    }
848

    
849
    @Override
850
    public long getLong(int index) {
851
        Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
852
        if (value == null) {
853
            return 0;
854
        }
855
        return value;
856
    }
857

    
858
    @Override
859
    public String getString(String name) {
860
        return this.getString(this.data.getType().getIndex(name));
861
    }
862

    
863
    @Override
864
    public String getString(int index) {
865
        return (String) this.get(index,String.class,DataTypes.STRING);
866
    }
867

    
868
    @Override
869
    public Object getContextValue(String name) {
870
        name = name.toLowerCase();
871
        if (name.equals("store")) {
872
            return this.getStore();
873
        }
874

    
875
        if (name.equals("featuretype")) {
876
            return this.data.getType();
877
        }
878

    
879
        if (name.equals("feature")) {
880
            return this;
881
        }
882

    
883
        throw new IllegalArgumentException(name);
884
    }
885

    
886
    @Override
887
    public Iterator getDataNames() {
888
        class DataNamesIterator implements Iterator {
889
            Iterator attributeIteraror;
890

    
891
            DataNamesIterator(DefaultFeature feature) {
892
                this.attributeIteraror = feature.getType().iterator();
893
            }
894

    
895
            @Override
896
            public boolean hasNext() {
897
                return this.attributeIteraror.hasNext();
898
            }
899

    
900
            @Override
901
            public Object next() {
902
                return ((FeatureAttributeDescriptor) this.attributeIteraror
903
                        .next()).getName();
904
            }
905

    
906
            @Override
907
            public void remove() {
908
                throw new UnsupportedOperationException();
909
            }
910

    
911
        }
912
        return new DataNamesIterator(this);
913
    }
914

    
915
    @Override
916
    public Object getDataValue(String name) {
917
        name = name.toLowerCase();
918
        try {
919
            return get(name);
920
        } catch (IllegalArgumentException ex) {
921
            if( "defaultgeometry".equalsIgnoreCase(name )) {
922
                return this.getDefaultGeometry();
923
            }
924
            throw ex;
925
        }
926
    }
927

    
928
    @Override
929
    public Iterator getDataValues() {
930
        class DataValuesIterator implements Iterator {
931
            DefaultFeature feature;
932
            int current = 0;
933

    
934
            DataValuesIterator(DefaultFeature feature) {
935
                this.feature = feature;
936
            }
937

    
938
            @Override
939
            public boolean hasNext() {
940
                return current < feature.getType().size() - 1;
941
            }
942

    
943
            @Override
944
            public Object next() {
945
                return feature.get(current++);
946
            }
947

    
948
            @Override
949
            public void remove() {
950
                throw new UnsupportedOperationException();
951
            }
952

    
953
        }
954
        return new DataValuesIterator(this);
955
    }
956

    
957
    @Override
958
    public boolean hasContextValue(String name) {
959
        name = name.toLowerCase();
960
        if (name.equals("store")) {
961
            return true;
962
        }
963

    
964
        if (name.equals("featuretype")) {
965
            return true;
966
        }
967

    
968
        return name.equals("feature");
969
    }
970

    
971
    @Override
972
    public boolean hasDataValue(String name) {
973
        name = name.toLowerCase();
974
        return this.data.getType().getIndex(name) >= 0;
975
    }
976

    
977
    @Override
978
    public Time getTime(int index) {
979
        return ((Time) this.get(index,Time.class,DataTypes.INSTANT));
980
    }
981

    
982
    @Override
983
    public Time getTime(String name) {
984
        return this.getInstant(this.data.getType().getIndex(name));
985
    }
986

    
987
    @Override
988
    public Instant getInstant(int index) {
989
        return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT));
990
    }
991

    
992
    @Override
993
    public Instant getInstant(String name) {
994
        return this.getInstant(this.data.getType().getIndex(name));
995
    }
996

    
997
    @Override
998
    public Interval getInterval(int index) {
999
        return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL));
1000
    }
1001

    
1002
    @Override
1003
    public Interval getInterval(String name) {
1004
        return this.getInterval(this.data.getType().getIndex(name));
1005
    }
1006

    
1007
    @Override
1008
    public DynObject getAsDynObject() {
1009
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
1010
        return facade;
1011
    }
1012

    
1013
    @Override
1014
    public String toString() {
1015
            StringBuilder builder = new StringBuilder();
1016
        FeatureAttributeDescriptor[] attributeDescriptors =
1017
            getType().getAttributeDescriptors();
1018
        for (int i = 0; i < attributeDescriptors.length; i++) {
1019
            String name = attributeDescriptors[i].getName();
1020
            Object value = get(name);
1021
            builder.append(value);
1022
            if (i < attributeDescriptors.length - 1) {
1023
                builder.append(", ");
1024
            }
1025
        }
1026
        return builder.toString();
1027
    }
1028

    
1029

    
1030

    
1031

    
1032
        /**
1033
     * @return the inserted
1034
     */
1035
    public boolean isInserted() {
1036
        return inserted;
1037
    }
1038

    
1039

    
1040
    /**
1041
     * @param inserted the inserted to set
1042
     */
1043
    public void setInserted(boolean inserted) {
1044
        this.inserted = inserted;
1045
    }
1046

    
1047
        @Override
1048
    public EvaluatorData getEvaluatorData() {
1049
        return this;
1050
    }
1051

    
1052
    public int size() {
1053
        return this.data.getType().size();
1054
    }
1055
    
1056
    public boolean isEmpty() {
1057
        return false;
1058
    }
1059

    
1060
    public Iterator<String> iterator() {
1061
        final Iterator<FeatureAttributeDescriptor> x = this.data.getType().iterator();
1062
        return new Iterator<String>() {
1063
            @Override
1064
            public boolean hasNext() {
1065
                return x.hasNext();
1066
            }
1067

    
1068
            @Override
1069
            public String next() {
1070
                return x.next().getName();
1071
            }
1072
        };
1073
    }
1074
    
1075
    public boolean containsKey(String key) {
1076
        return this.data.getType().get(key)!=null;
1077
    }
1078

    
1079
    @Override
1080
    public String getLabelOfValue(String name) {
1081
        FeatureAttributeDescriptor attrdesc = this.data.getType().getAttributeDescriptor(name);
1082
        if( attrdesc==null ) {
1083
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
1084
        }
1085
        Object value = this.get(attrdesc.getIndex());
1086
        String label = attrdesc.getLabelOfValue(value);
1087
        return label;
1088
    }
1089

    
1090
    @Override
1091
    public Object getExtraValue(String name) {
1092
        return this.data.getExtraValue(name);
1093
    }
1094

    
1095
    @Override
1096
    public Object getExtraValue(int index) {
1097
        return this.data.getExtraValue(index);
1098
    }
1099

    
1100

    
1101
}