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

History | View | Annotate | Download (32.2 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.isComputed() ) {
111
                 continue;
112
            }
113
            int sourceIndex = sourceType.getIndex(targetAttrDescriptor.getName());
114
            if (sourceIndex<0){
115
                continue;
116
            }
117
            Object value = sourceFeature.get(sourceIndex);
118
            if (value == null && !targetAttrDescriptor.allowNull()) {
119
                continue;
120
            }
121
            this.setforced(targetAttrDescriptor.getIndex(), targetAttrDescriptor,value);
122
        }
123
    }
124

    
125
        public void setData(FeatureProvider data) {
126
                this.data = data;
127
                this.reference = null;
128
                this.inserted = true;
129
        }
130

    
131
        public FeatureProvider getData() {
132
                return this.data;
133
        }
134

    
135
        protected DataTypesManager getDataTypesManager() {
136
                if( dataTypesManager==null ) {
137
                        dataTypesManager = ToolsLocator.getDataTypesManager();
138
                }
139
                return dataTypesManager;
140
        }
141

    
142
    protected void set(FeatureAttributeDescriptor attribute, Object value) {
143
        int i = attribute.getIndex();
144

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

    
154
        if ( value == null ) {
155
            if ( !attribute.allowNull() ) {
156
                if ( !attribute.isAutomatic() ) {
157
                    throw new IllegalValueException(attribute, value);
158
                }
159
            }
160
            this.data.set(i, null);
161
            return;
162

    
163
        }
164

    
165
        if ( attribute.getFeatureAttributeGetter() != null ) {
166
            value = attribute.getFeatureAttributeGetter().setter(value);
167
        }
168
        this.setforced(i, attribute, value);
169
    }
170

    
171
    private void setforced(int i, FeatureAttributeDescriptor attribute, Object value) {
172

    
173
        Class objectClass = attribute.getObjectClass();
174
        if( objectClass!=null ) {
175
            if ( objectClass.isInstance(value) ) {
176
                this.data.set(i, value);
177
                return;
178
            } else {
179
                DataProfile dataProfile = attribute.getDataProfile();
180
                if( dataProfile!=null ) {
181
                    try {
182
                        value = dataProfile.coerce(
183
                                attribute.getDataType(),
184
                                value, 
185
                                attribute.getTags()
186
                        );
187
                    } catch (CoercionException e) {
188
                        
189
                    }
190
                } 
191
                try {
192
                    value= this.getDataTypesManager().coerce(attribute.getType(),value);
193
                } catch (CoercionException e) {
194
                    throw new IllegalArgumentException("Can't convert to "
195
                            + attribute.getDataType().getName()
196
                            + " from '"
197
                            + value.getClass().getName() + "' with value '"
198
                            + value.toString() + "'.");
199
                }
200
            }
201
        }
202
        this.data.set(i, value);
203
    }
204

    
205
    private Object get(int index,Class theClass, int type) {
206
        Object value = this.get(index);
207
        if( theClass.isInstance(value) ) {
208
            return value;
209
        }
210
        try {
211
            return this.getDataTypesManager().coerce(type, value);
212
        } catch (CoercionException e) {
213

    
214
            if (value == null) {
215
                return null;
216
            }
217
            throw new IllegalArgumentException(
218
                    "Can't convert to "+theClass.getName()+
219
                    " from '"+value.getClass().getName()+
220
                    "' with value '"+value.toString()+"'.");
221
        }
222
    }
223

    
224
    public void initializeValues() {
225
        FeatureType type = this.getType();
226
        for (FeatureAttributeDescriptor attribute : type) {
227
            if (attribute.isAutomatic() || attribute.isReadOnly()
228
                    || attribute.isComputed() ) {
229
                continue;
230
            }
231
            if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
232
                continue;
233
            }
234
            this.set(attribute, attribute.getDefaultValue());
235
        }
236
    }
237

    
238
    public void clear() {
239
        initializeValues();
240
    }
241

    
242
    public void initializeValues(Feature feature) {
243
        FeatureType myType=this.getType();
244
        FeatureType type =feature.getType();
245
        for (FeatureAttributeDescriptor attribute : type) {
246
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
247
            if (myAttribute != null) {
248
                this.set(myAttribute, feature.get(attribute.getIndex()));
249
            }
250
        }
251
    }
252

    
253

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

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

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

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

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

    
280
    @Override
281
    public FeatureReference getReference() {
282
        if (this.reference == null) {
283
            if (!isInserted()) {
284
                return null;
285
            }
286
            reference = new DefaultFeatureReference(this);
287
        }
288
        return this.reference;
289
    }
290

    
291
    @Override
292
    public Object getOrDefault(String name, Object defaultValue) {
293
        int index = this.data.getType().getIndex(name);
294
        if( index < 0 ) {
295
            return defaultValue;
296
        }
297
        return this.get(index);
298
    }
299

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

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

    
326
    @Override
327
    public long getLongOrDefault(String name, long defaultValue) {
328
        int index = this.data.getType().getIndex(name);
329
        if( index < 0 ) {
330
            return defaultValue;
331
        }
332
        try {
333
            return (long) this.get(index);
334
        } catch(Throwable th) {
335
            return defaultValue;
336
        }
337
    }
338

    
339
    @Override
340
    public float getFloatOrDefault(String name, float defaultValue) {
341
        int index = this.data.getType().getIndex(name);
342
        if( index < 0 ) {
343
            return defaultValue;
344
        }
345
        try {
346
            return (float) this.get(index);
347
        } catch(Throwable th) {
348
            return defaultValue;
349
        }
350
    }
351

    
352
    @Override
353
    public double getDoubleOrDefault(String name, double defaultValue) {
354
        int index = this.data.getType().getIndex(name);
355
        if( index < 0 ) {
356
            return defaultValue;
357
        }
358
        try {
359
            return (double) this.get(index);
360
        } catch(Throwable th) {
361
            return defaultValue;
362
        }
363
    }
364

    
365
    @Override
366
    public Date getDateOrDefault(String name, Date defaultValue) {
367
        int index = this.data.getType().getIndex(name);
368
        if( index < 0 ) {
369
            return defaultValue;
370
        }
371
        try {
372
            return (Date) this.get(index);
373
        } catch(Throwable th) {
374
            return defaultValue;
375
        }
376
    }
377

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

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

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

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

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

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

    
450
    @Override
451
    public Date getDateOrDefault(int index, Date defaultValue) {
452
        if( index < 0 || index >= this.data.getType().size() ) {
453
            return defaultValue;
454
        }
455
        try {
456
            return (Date) this.get(index);
457
        } catch(Throwable th) {
458
            return defaultValue;
459
        }
460
    }
461

    
462
    class UnableToGetReferenceException extends BaseRuntimeException {
463

    
464
        /**
465
         *
466
         */
467
        private static final long serialVersionUID = 1812805035204824163L;
468

    
469
        /**
470
         * @param exception
471
         */
472
        public UnableToGetReferenceException(BaseException exception) {
473
            super("Unable to get reference", "_UnableToGetReferenceException",
474
                serialVersionUID);
475
            this.initCause(exception);
476

    
477
        }
478

    
479
    }
480

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

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

    
492
    @Override
493
    public Envelope getDefaultEnvelope() {
494
        Envelope envelope = this.data.getDefaultEnvelope();
495
        if( envelope == null ) {
496
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
497
            if( i<0 ) {
498
                return null;
499
            }
500
            Geometry geom = this.getDefaultGeometry();
501
            if( geom!=null ) {
502
                envelope = geom.getEnvelope();
503
            }
504
        }
505
        return envelope;
506
    }
507

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
656
                };
657
            }
658

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

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

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

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

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

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

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

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

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

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

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

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

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

    
780
        return value;
781
    }
782

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

    
788
    @Override
789
    public double getDouble(int index) {
790

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
881
        if (name.equals("feature")) {
882
            return this;
883
        }
884

    
885
        throw new IllegalArgumentException(name);
886
    }
887

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

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

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

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

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

    
913
        }
914
        return new DataNamesIterator(this);
915
    }
916

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

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

    
936
            DataValuesIterator(DefaultFeature feature) {
937
                this.feature = feature;
938
            }
939

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

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

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

    
955
        }
956
        return new DataValuesIterator(this);
957
    }
958

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

    
966
        if (name.equals("featuretype")) {
967
            return true;
968
        }
969

    
970
        return name.equals("feature");
971
    }
972

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

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

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

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

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

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

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

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

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

    
1031

    
1032

    
1033

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

    
1041

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

    
1049
        @Override
1050
    public EvaluatorData getEvaluatorData() {
1051
        return this;
1052
    }
1053

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

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

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

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

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

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

    
1102

    
1103
}