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

History | View | Annotate | Download (31.6 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
            }
177

    
178
            if ( !objectClass.isInstance(value) ) {
179
                try {
180
                    value
181
                            = this.getDataTypesManager().coerce(attribute.getType(),
182
                                    value);
183
                } catch (CoercionException e) {
184
                    throw new IllegalArgumentException("Can't convert to "
185
                            + this.getDataTypesManager().getTypeName(
186
                                    attribute.getType()) + " from '"
187
                            + value.getClass().getName() + "' with value '"
188
                            + value.toString() + "'.");
189
                }
190
            }
191
        }
192
        this.data.set(i, value);
193
    }
194

    
195
    private Object get(int index,Class theClass, int type) {
196
        Object value = this.get(index);
197
        if( theClass.isInstance(value) ) {
198
            return value;
199
        }
200
        try {
201
            return this.getDataTypesManager().coerce(type, value);
202
        } catch (CoercionException e) {
203

    
204
            if (value == null) {
205
                return null;
206
            }
207
            throw new IllegalArgumentException(
208
                    "Can't convert to "+theClass.getName()+
209
                    " from '"+value.getClass().getName()+
210
                    "' with value '"+value.toString()+"'.");
211
        }
212
    }
213

    
214
    public void initializeValues() {
215
        FeatureType type = this.getType();
216
        for (FeatureAttributeDescriptor attribute : type) {
217
            if (attribute.isAutomatic() || attribute.isReadOnly()
218
                    || attribute.isComputed() ) {
219
                continue;
220
            }
221
            if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
222
                continue;
223
            }
224
            this.set(attribute, attribute.getDefaultValue());
225
        }
226
    }
227

    
228
    public void clear() {
229
        initializeValues();
230
    }
231

    
232
    public void initializeValues(Feature feature) {
233
        FeatureType myType=this.getType();
234
        FeatureType type =feature.getType();
235
        for (FeatureAttributeDescriptor attribute : type) {
236
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
237
            if (myAttribute != null) {
238
                this.set(myAttribute, feature.get(attribute.getIndex()));
239
            }
240
        }
241
    }
242

    
243

    
244
    @Override
245
    public FeatureStore getStore() {
246
        return (FeatureStore) this.storeRef.get();
247
    }
248

    
249
    @Override
250
    public FeatureType getType() {
251
        return this.data.getType();
252
    }
253

    
254
    @Override
255
    public EditableFeature getEditable() {
256
        return new DefaultEditableFeature(this);
257
    }
258

    
259
    @Override
260
    public Feature getCopy() {
261
        return new DefaultFeature(this);
262
    }
263

    
264
    @Override
265
    @SuppressWarnings("CloneDoesntCallSuperClone")
266
    public Object clone() throws CloneNotSupportedException {
267
        return new DefaultFeature(this);
268
    }
269

    
270
    @Override
271
    public FeatureReference getReference() {
272
        if (this.reference == null) {
273
            if (!isInserted()) {
274
                return null;
275
            }
276
            reference = new DefaultFeatureReference(this);
277
        }
278
        return this.reference;
279
    }
280

    
281
    @Override
282
    public Object getOrDefault(String name, Object defaultValue) {
283
        int index = this.data.getType().getIndex(name);
284
        if( index < 0 ) {
285
            return defaultValue;
286
        }
287
        return this.get(index);
288
    }
289

    
290
    @Override
291
    public String getStringOrDefault(String name, String defaultValue) {
292
        int index = this.data.getType().getIndex(name);
293
        if( index < 0 ) {
294
            return defaultValue;
295
        }
296
        try {
297
            return (String) this.get(index);
298
        } catch(Throwable th) {
299
            return defaultValue;
300
        }
301
    }
302

    
303
    @Override
304
    public int getIntOrDefault(String name, int defaultValue) {
305
        int index = this.data.getType().getIndex(name);
306
        if( index < 0 ) {
307
            return defaultValue;
308
        }
309
        try {
310
            return (int) this.get(index);
311
        } catch(Throwable th) {
312
            return defaultValue;
313
        }
314
    }
315

    
316
    @Override
317
    public long getLongOrDefault(String name, long defaultValue) {
318
        int index = this.data.getType().getIndex(name);
319
        if( index < 0 ) {
320
            return defaultValue;
321
        }
322
        try {
323
            return (long) this.get(index);
324
        } catch(Throwable th) {
325
            return defaultValue;
326
        }
327
    }
328

    
329
    @Override
330
    public float getFloatOrDefault(String name, float defaultValue) {
331
        int index = this.data.getType().getIndex(name);
332
        if( index < 0 ) {
333
            return defaultValue;
334
        }
335
        try {
336
            return (float) this.get(index);
337
        } catch(Throwable th) {
338
            return defaultValue;
339
        }
340
    }
341

    
342
    @Override
343
    public double getDoubleOrDefault(String name, double defaultValue) {
344
        int index = this.data.getType().getIndex(name);
345
        if( index < 0 ) {
346
            return defaultValue;
347
        }
348
        try {
349
            return (double) this.get(index);
350
        } catch(Throwable th) {
351
            return defaultValue;
352
        }
353
    }
354

    
355
    @Override
356
    public Date getDateOrDefault(String name, Date defaultValue) {
357
        int index = this.data.getType().getIndex(name);
358
        if( index < 0 ) {
359
            return defaultValue;
360
        }
361
        try {
362
            return (Date) this.get(index);
363
        } catch(Throwable th) {
364
            return defaultValue;
365
        }
366
    }
367

    
368
    @Override
369
    public Object getOrDefault(int index, Object defaultValue) {
370
        if( index < 0 || index >= this.data.getType().size() ) {
371
            return defaultValue;
372
        }
373
        try {
374
            return this.get(index);
375
        } catch(Throwable th) {
376
            return defaultValue;
377
        }
378
    }
379

    
380
    @Override
381
    public String getStringOrDefault(int index, String defaultValue) {
382
        if( index < 0 || index >= this.data.getType().size() ) {
383
            return defaultValue;
384
        }
385
        try {
386
            return (String) this.get(index);
387
        } catch(Throwable th) {
388
            return defaultValue;
389
        }
390
    }
391

    
392
    @Override
393
    public int getIntOrDefault(int index, int defaultValue) {
394
        if( index < 0 || index >= this.data.getType().size() ) {
395
            return defaultValue;
396
        }
397
        try {
398
            return (int) this.get(index);
399
        } catch(Throwable th) {
400
            return defaultValue;
401
        }
402
    }
403

    
404
    @Override
405
    public long getLongOrDefault(int index, long defaultValue) {
406
        if( index < 0 || index >= this.data.getType().size() ) {
407
            return defaultValue;
408
        }
409
        try {
410
            return (long) this.get(index);
411
        } catch(Throwable th) {
412
            return defaultValue;
413
        }
414
    }
415

    
416
    @Override
417
    public float getFloatOrDefault(int index, float defaultValue) {
418
        if( index < 0 || index >= this.data.getType().size() ) {
419
            return defaultValue;
420
        }
421
        try {
422
            return (float) this.get(index);
423
        } catch(Throwable th) {
424
            return defaultValue;
425
        }
426
    }
427

    
428
    @Override
429
    public double getDoubleOrDefault(int index, double defaultValue) {
430
        if( index < 0 || index >= this.data.getType().size() ) {
431
            return defaultValue;
432
        }
433
        try {
434
            return (double) this.get(index);
435
        } catch(Throwable th) {
436
            return defaultValue;
437
        }
438
    }
439

    
440
    @Override
441
    public Date getDateOrDefault(int index, Date defaultValue) {
442
        if( index < 0 || index >= this.data.getType().size() ) {
443
            return defaultValue;
444
        }
445
        try {
446
            return (Date) this.get(index);
447
        } catch(Throwable th) {
448
            return defaultValue;
449
        }
450
    }
451

    
452
    class UnableToGetReferenceException extends BaseRuntimeException {
453

    
454
        /**
455
         *
456
         */
457
        private static final long serialVersionUID = 1812805035204824163L;
458

    
459
        /**
460
         * @param exception
461
         */
462
        public UnableToGetReferenceException(BaseException exception) {
463
            super("Unable to get reference", "_UnableToGetReferenceException",
464
                serialVersionUID);
465
            this.initCause(exception);
466

    
467
        }
468

    
469
    }
470

    
471
    @Override
472
    public void validate(int mode) throws DataException  {
473
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
474
    }
475

    
476
    @Override
477
    public List getSRSs() {
478
        // TODO Auto-generated method stub
479
        return null;
480
    }
481

    
482
    @Override
483
    public Envelope getDefaultEnvelope() {
484
        Envelope envelope = this.data.getDefaultEnvelope();
485
        if( envelope == null ) {
486
            Geometry geom = this.getDefaultGeometry();
487
            if( geom!=null ) {
488
                envelope = geom.getEnvelope();
489
            }
490
        }
491
        return envelope;
492
    }
493

    
494
    @Override
495
    public Geometry getDefaultGeometry() {
496
            Geometry geom = this.data.getDefaultGeometry();
497
        if( geom == null ) {
498
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
499
            Object x = this.get(i);
500
            if( x instanceof Geometry ) {
501
                geom = (Geometry) x;
502
            } else {
503
                geom = this.getGeometry(i);
504
            }
505
        }
506
        if( geom != null ) {
507
            if( geom.getProjection()==null ) {
508
                FeatureType type = this.getType();
509
                DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex());
510
                IProjection proj = attrdesc.getSRS(this.storeRef);
511
                geom.setProjection(proj);
512
            }
513
        }
514
        return geom;
515
    }
516

    
517
    @Override
518
    public Time getDefaultTime() {
519
            Time time = this.data.getDefaultTime();
520
        if( time == null ) {
521
            int i = this.data.getType().getDefaultTimeAttributeIndex();
522
            Object x = this.get(i);
523
            if( x instanceof Time ) {
524
                time = (Time) x;
525
            } else {
526
                time = this.getTime(i);
527
            }
528
        }
529
        return time;
530
    }
531

    
532
    @Override
533
    public IProjection getDefaultSRS() {
534
        IProjection srs = this.data.getType().getDefaultSRS();
535
        if( srs == null ) {
536
            FeatureType type = this.getType();
537
            DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex());
538
            srs = attrdesc.getSRS(this.storeRef);
539
        }
540
        return srs;
541
    }
542

    
543
    @Override
544
    public List getGeometries() {
545
        // TODO Auto-generated method stub
546
        return null;
547
    }
548

    
549
    @Override
550
    public Object getFromProfile(int index) {
551
        FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(index);
552
        Object value = this.get(index);
553
        String profileName = descriptor.getDataProfileName();
554
        if( StringUtils.isBlank(profileName) ) {
555
            return value;
556
        }
557
        DataProfile profile = DALLocator.getDataManager().getDataProfile(profileName);
558
        if( profile==null ) {
559
            return value;
560
        }
561
        return profile.createData(value, descriptor.getTags());
562
    }
563

    
564
    @Override
565
    public Object getFromProfile(String name) {
566
        FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(name);
567
        return this.getFromProfile(descriptor.getIndex());
568
    }
569

    
570
    @Override
571
    public Object get(String name) {
572
        int index = this.data.getType().getIndex(name);
573
        if( index < 0 ) {
574
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
575
        }
576
        return this.get(index);
577
    }
578

    
579
    public boolean has_key(String key) {
580
        Object x = this.getType().get(key);
581
        return x != null;
582
    }
583

    
584
    public List<String> keys() {
585
        List<String> ks = new ArrayList<>();
586
        for( FeatureAttributeDescriptor attr : this.getType()) {
587
            ks.add(attr.getName());
588
        }
589
        return ks;
590
    }
591

    
592
    public Iterator<String> iterkeys() {
593
        final Iterator it = this.getType().iterator();
594
        return new Iterator<String>() {
595
            @Override
596
            public boolean hasNext() {
597
                return it.hasNext();
598
            }
599

    
600
            @Override
601
            public String next() {
602
                return ((FeatureAttributeDescriptor)it.next()).getName();
603
            }
604

    
605
            @Override
606
            public void remove() {
607
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
608
            }
609
        };
610
    }
611

    
612
    public Iterator iteritems() {
613
        final Iterator it = this.getType().iterator();
614
        return new Iterator<Map.Entry>() {
615
            @Override
616
            public boolean hasNext() {
617
                return it.hasNext();
618
            }
619

    
620
            @Override
621
            public Map.Entry next() {
622
                final String name = ((FeatureAttributeDescriptor)it.next()).getName();
623
                return new Map.Entry<String, Object>() {
624
                    @Override
625
                    public String getKey() {
626
                        return name;
627
                    }
628

    
629
                    @Override
630
                    public Object getValue() {
631
                        return get(name);
632
                    }
633

    
634
                    @Override
635
                    public Object setValue(Object value) {
636
                        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
637
                    }
638

    
639
                };
640
            }
641

    
642
            @Override
643
            public void remove() {
644
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
645
            }
646
        };
647
    }
648

    
649
    @Override
650
    public Object get(int index) {
651
        FeatureType type = this.data.getType();
652
        if( index <0 || index >= type.size() ) {
653
            throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
654
        }
655
        FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
656
        if (!this.data.getType().hasEvaluators()) {
657
            return get(attribute, this.data.get(index));
658
        }
659
        Evaluator eval = attribute.getEvaluator();
660
        if (eval == null) {
661
            return this.data.get(index);
662
        } else {
663
            Object value = this.data.get(index);
664
            if (value != null) { // FIXME: para comprobar si esta calculado usar
665
                // un array
666
                // especifico.
667
                return get(attribute, this.data.get(index));
668
            }
669
            try {
670
                value = eval.evaluate(this);
671
            } catch (EvaluatorException e) {
672
                throw new DataEvaluatorRuntimeException(e);
673
            }
674
            this.data.set(index, value);
675
            return  get(attribute, value);
676
        }
677
    }
678

    
679
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
680
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
681
        if( emulator != null ) {
682
//            int index = featureAttributeDescriptor.getIndex();
683
//            value = this.data.get(index);
684
//            if( value==null ) {
685
                value = emulator.get(this);
686
//                this.data.set(index,value);
687
//            }
688
        } else {
689
            FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
690
            if( getter != null ) {
691
                value = getter.getter(value);
692
            }
693
        }
694
        if( featureAttributeDescriptor.getType()==DataTypes.GEOMETRY ) {
695
            if( value != null ) {
696
                Geometry geom = (Geometry)value;
697
                if( geom.getProjection()==null ) {
698
                    IProjection proj = ((DefaultFeatureAttributeDescriptor)featureAttributeDescriptor).getSRS(this.storeRef);
699
                    geom.setProjection(proj);
700
                }
701
            }
702
        }
703
        return value;
704
    }
705

    
706
    @Override
707
    public byte[] getByteArray(String name) {
708
        return this.getByteArray(this.data.getType().getIndex(name));
709
    }
710

    
711
    @Override
712
    public byte[] getByteArray(int index) {
713
        return (byte[]) this.get(index);
714
    }
715

    
716
    @Override
717
    public Object[] getArray(String name) {
718
        return this.getArray(this.data.getType().getIndex(name));
719
    }
720

    
721
    @Override
722
    public Object[] getArray(int index) {
723
        return (Object[]) this.get(index);
724
    }
725

    
726
    @Override
727
    public boolean getBoolean(String name) {
728
        return this.getBoolean(this.data.getType().getIndex(name));
729
    }
730

    
731
    @Override
732
    public boolean getBoolean(int index) {
733
        Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
734
        if (value == null) {
735
            return false;
736
        }
737
        return value;
738
    }
739

    
740
    @Override
741
    public byte getByte(String name) {
742
        return this.getByte(this.data.getType().getIndex(name));
743
    }
744

    
745
    @Override
746
    public byte getByte(int index) {
747
        Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
748
        if (value == null) {
749
            return 0;
750
        }
751
        return value;
752
    }
753

    
754
    @Override
755
    public Date getDate(String name) {
756
        return this.getDate(this.data.getType().getIndex(name));
757
    }
758

    
759
    @Override
760
    public Date getDate(int index) {
761
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
762

    
763
        return value;
764
    }
765

    
766
    @Override
767
    public double getDouble(String name) {
768
        return this.getDouble(this.data.getType().getIndex(name));
769
    }
770

    
771
    @Override
772
    public double getDouble(int index) {
773

    
774
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
775
        if (value == null) {
776
            return 0;
777
        }
778
        return value;
779
    }
780

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

    
786
    @Override
787
    public Feature getFeature(int index) {
788
        return (Feature) this.get(index);
789
    }
790

    
791
    @Override
792
    public float getFloat(String name) {
793
        return this.getFloat(this.data.getType().getIndex(name));
794
    }
795

    
796
    @Override
797
    public float getFloat(int index) {
798
        Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
799
        if (value == null) {
800
            return 0;
801
        }
802
        return value;
803
    }
804

    
805
    @Override
806
    public Geometry getGeometry(String name) {
807
        return this.getGeometry(this.data.getType().getIndex(name));
808
    }
809

    
810
    @Override
811
    public Geometry getGeometry(int index) {
812
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
813
    }
814

    
815
    @Override
816
    public int getInt(String name) {
817
        return this.getInt(this.data.getType().getIndex(name));
818
    }
819

    
820
    @Override
821
    public int getInt(int index) {
822
        Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
823
        if (value == null) {
824
            return 0;
825
        }
826
        return value;
827
    }
828

    
829
    @Override
830
    public long getLong(String name) {
831
        return this.getLong(this.data.getType().getIndex(name));
832
    }
833

    
834
    @Override
835
    public long getLong(int index) {
836
        Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
837
        if (value == null) {
838
            return 0;
839
        }
840
        return value;
841
    }
842

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

    
848
    @Override
849
    public String getString(int index) {
850
        return (String) this.get(index,String.class,DataTypes.STRING);
851
    }
852

    
853
    @Override
854
    public Object getContextValue(String name) {
855
        name = name.toLowerCase();
856
        if (name.equals("store")) {
857
            return this.getStore();
858
        }
859

    
860
        if (name.equals("featuretype")) {
861
            return this.data.getType();
862
        }
863

    
864
        if (name.equals("feature")) {
865
            return this;
866
        }
867

    
868
        throw new IllegalArgumentException(name);
869
    }
870

    
871
    @Override
872
    public Iterator getDataNames() {
873
        class DataNamesIterator implements Iterator {
874
            Iterator attributeIteraror;
875

    
876
            DataNamesIterator(DefaultFeature feature) {
877
                this.attributeIteraror = feature.getType().iterator();
878
            }
879

    
880
            @Override
881
            public boolean hasNext() {
882
                return this.attributeIteraror.hasNext();
883
            }
884

    
885
            @Override
886
            public Object next() {
887
                return ((FeatureAttributeDescriptor) this.attributeIteraror
888
                        .next()).getName();
889
            }
890

    
891
            @Override
892
            public void remove() {
893
                throw new UnsupportedOperationException();
894
            }
895

    
896
        }
897
        return new DataNamesIterator(this);
898
    }
899

    
900
    @Override
901
    public Object getDataValue(String name) {
902
        name = name.toLowerCase();
903
        try {
904
            return get(name);
905
        } catch (IllegalArgumentException ex) {
906
            if( "defaultgeometry".equalsIgnoreCase(name )) {
907
                return this.getDefaultGeometry();
908
            }
909
            throw ex;
910
        }
911
    }
912

    
913
    @Override
914
    public Iterator getDataValues() {
915
        class DataValuesIterator implements Iterator {
916
            DefaultFeature feature;
917
            int current = 0;
918

    
919
            DataValuesIterator(DefaultFeature feature) {
920
                this.feature = feature;
921
            }
922

    
923
            @Override
924
            public boolean hasNext() {
925
                return current < feature.getType().size() - 1;
926
            }
927

    
928
            @Override
929
            public Object next() {
930
                return feature.get(current++);
931
            }
932

    
933
            @Override
934
            public void remove() {
935
                throw new UnsupportedOperationException();
936
            }
937

    
938
        }
939
        return new DataValuesIterator(this);
940
    }
941

    
942
    @Override
943
    public boolean hasContextValue(String name) {
944
        name = name.toLowerCase();
945
        if (name.equals("store")) {
946
            return true;
947
        }
948

    
949
        if (name.equals("featuretype")) {
950
            return true;
951
        }
952

    
953
        return name.equals("feature");
954
    }
955

    
956
    @Override
957
    public boolean hasDataValue(String name) {
958
        name = name.toLowerCase();
959
        return this.data.getType().getIndex(name) >= 0;
960
    }
961

    
962
    @Override
963
    public Time getTime(int index) {
964
        return ((Time) this.get(index,Time.class,DataTypes.INSTANT));
965
    }
966

    
967
    @Override
968
    public Time getTime(String name) {
969
        return this.getInstant(this.data.getType().getIndex(name));
970
    }
971

    
972
    @Override
973
    public Instant getInstant(int index) {
974
        return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT));
975
    }
976

    
977
    @Override
978
    public Instant getInstant(String name) {
979
        return this.getInstant(this.data.getType().getIndex(name));
980
    }
981

    
982
    @Override
983
    public Interval getInterval(int index) {
984
        return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL));
985
    }
986

    
987
    @Override
988
    public Interval getInterval(String name) {
989
        return this.getInterval(this.data.getType().getIndex(name));
990
    }
991

    
992
    @Override
993
    public DynObject getAsDynObject() {
994
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
995
        return facade;
996
    }
997

    
998
    @Override
999
    public String toString() {
1000
            StringBuilder builder = new StringBuilder();
1001
        FeatureAttributeDescriptor[] attributeDescriptors =
1002
            getType().getAttributeDescriptors();
1003
        for (int i = 0; i < attributeDescriptors.length; i++) {
1004
            String name = attributeDescriptors[i].getName();
1005
            builder.append(get(name));
1006
            if (i < attributeDescriptors.length - 1) {
1007
                builder.append(", ");
1008
            }
1009
        }
1010
        return builder.toString();
1011
    }
1012

    
1013

    
1014

    
1015

    
1016
        /**
1017
     * @return the inserted
1018
     */
1019
    public boolean isInserted() {
1020
        return inserted;
1021
    }
1022

    
1023

    
1024
    /**
1025
     * @param inserted the inserted to set
1026
     */
1027
    public void setInserted(boolean inserted) {
1028
        this.inserted = inserted;
1029
    }
1030

    
1031
        @Override
1032
    public EvaluatorData getEvaluatorData() {
1033
        return this;
1034
    }
1035

    
1036
    public int size() {
1037
        return this.data.getType().size();
1038
    }
1039
    
1040
    public boolean isEmpty() {
1041
        return false;
1042
    }
1043

    
1044
    public Iterator<String> iterator() {
1045
        final Iterator<FeatureAttributeDescriptor> x = this.data.getType().iterator();
1046
        return new Iterator<String>() {
1047
            @Override
1048
            public boolean hasNext() {
1049
                return x.hasNext();
1050
            }
1051

    
1052
            @Override
1053
            public String next() {
1054
                return x.next().getName();
1055
            }
1056
        };
1057
    }
1058
    
1059
    public boolean containsKey(String key) {
1060
        return this.data.getType().get(key)!=null;
1061
    }
1062

    
1063
    @Override
1064
    public String getLabelOfValue(String name) {
1065
        FeatureAttributeDescriptor attrdesc = this.data.getType().getAttributeDescriptor(name);
1066
        if( attrdesc==null ) {
1067
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
1068
        }
1069
        Object value = this.get(attrdesc.getIndex());
1070
        String label = attrdesc.getLabelOfValue(value);
1071
        return label;
1072
    }
1073

    
1074
    @Override
1075
    public Object getExtraValue(String name) {
1076
        return this.data.getExtraValue(name);
1077
    }
1078

    
1079
    @Override
1080
    public Object getExtraValue(int index) {
1081
        return this.data.getExtraValue(index);
1082
    }
1083

    
1084

    
1085
}