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

History | View | Annotate | Download (32.1 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24 40435 jjdelcerro
package org.gvsig.fmap.dal.feature.impl;
25
26
import java.lang.ref.WeakReference;
27 43550 jjdelcerro
import java.util.ArrayList;
28 40435 jjdelcerro
import java.util.Date;
29
import java.util.Iterator;
30
import java.util.List;
31 43550 jjdelcerro
import java.util.Map;
32 44128 jjdelcerro
import org.apache.commons.lang3.StringUtils;
33 40435 jjdelcerro
import org.cresques.cts.IProjection;
34 44128 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
35 40435 jjdelcerro
import org.gvsig.fmap.dal.DataTypes;
36
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
37
import org.gvsig.fmap.dal.exception.DataException;
38 44128 jjdelcerro
import org.gvsig.fmap.dal.feature.DataProfile;
39 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
42 41335 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
43
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
44 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
45
import org.gvsig.fmap.dal.feature.FeatureStore;
46
import org.gvsig.fmap.dal.feature.FeatureType;
47
import org.gvsig.fmap.dal.feature.exception.IllegalValueException;
48
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException;
49 42775 jjdelcerro
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectFeatureFacade;
50 43729 fdiaz
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
51 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
52
import org.gvsig.fmap.geom.Geometry;
53
import org.gvsig.fmap.geom.primitive.Envelope;
54
import org.gvsig.timesupport.Instant;
55
import org.gvsig.timesupport.Interval;
56 44086 jjdelcerro
import org.gvsig.timesupport.Time;
57 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
58
import org.gvsig.tools.dataTypes.CoercionException;
59
import org.gvsig.tools.dataTypes.DataTypesManager;
60
import org.gvsig.tools.dynobject.DynObject;
61
import org.gvsig.tools.evaluator.Evaluator;
62
import org.gvsig.tools.evaluator.EvaluatorData;
63
import org.gvsig.tools.evaluator.EvaluatorException;
64
import org.gvsig.tools.exception.BaseException;
65
import org.gvsig.tools.exception.BaseRuntimeException;
66 40876 jjdelcerro
import org.gvsig.tools.lang.Cloneable;
67 40435 jjdelcerro
68 40876 jjdelcerro
public class DefaultFeature implements Feature, EvaluatorData, Cloneable {
69 40435 jjdelcerro
70 43729 fdiaz
        private static DataTypesManager dataTypesManager = null;
71 40435 jjdelcerro
        protected FeatureProvider data;
72
        protected FeatureReference reference;
73
        private WeakReference storeRef;
74 43729 fdiaz
75 40435 jjdelcerro
        private boolean inserted = false;
76
77
    /*
78
         * Usar con mucha precaucion o mejor no usar. Lo precisa el
79
         * DefaultFeatureSet en la ordenacion.
80
         */
81
        public DefaultFeature(FeatureStore store) {
82
                this.storeRef = new WeakReference(store);
83
                this.reference = null;
84
        }
85
86
        public DefaultFeature(FeatureStore store, FeatureProvider data) {
87
                this.data = data;
88
                this.storeRef = new WeakReference(store);
89
                this.reference = null;
90
                this.inserted = !data.isNew();
91
        }
92
93
        DefaultFeature(DefaultFeature feature) {
94
                this.data = feature.data.getCopy();
95
                this.storeRef = feature.storeRef;
96
                this.reference = feature.reference;
97
                this.inserted = feature.isInserted();
98
        }
99
100 43729 fdiaz
    public DefaultFeature(FeatureType targetType, Feature sourceFeature) {
101
        DefaultFeature defaultFeature = (DefaultFeature)sourceFeature;
102
        this.data = new DefaultFeatureProvider(targetType, (DefaultFeatureProvider)defaultFeature.getData());
103
        this.storeRef = defaultFeature.storeRef;
104
        this.reference = defaultFeature.reference;
105
        this.inserted = defaultFeature.isInserted();
106
107
        FeatureType sourceType = sourceFeature.getType();
108
109
        for (FeatureAttributeDescriptor targetAttrDescriptor : targetType) {
110
            if ( targetAttrDescriptor.isAutomatic() ||
111
                 targetAttrDescriptor.isReadOnly() ||
112
                 targetAttrDescriptor.getEvaluator() != null) {
113
                 continue;
114
            }
115
            int sourceIndex = sourceType.getIndex(targetAttrDescriptor.getName());
116
            if (sourceIndex<0){
117
                continue;
118
            }
119
            Object value = sourceFeature.get(sourceIndex);
120
            if (value == null && !targetAttrDescriptor.allowNull()) {
121
                continue;
122
            }
123
            this.set(targetAttrDescriptor,value);
124
        }
125
    }
126
127 40435 jjdelcerro
        public void setData(FeatureProvider data) {
128
                this.data = data;
129
                this.reference = null;
130 43729 fdiaz
                this.inserted = true;
131 40435 jjdelcerro
        }
132
133
        public FeatureProvider getData() {
134
                return this.data;
135
        }
136
137
        protected DataTypesManager getDataTypesManager() {
138
                if( dataTypesManager==null ) {
139
                        dataTypesManager = ToolsLocator.getDataTypesManager();
140
                }
141
                return dataTypesManager;
142
        }
143
144 41335 jjdelcerro
    void set(FeatureAttributeDescriptor attribute, Object value) {
145
        int i = attribute.getIndex();
146 40435 jjdelcerro
147 41335 jjdelcerro
        if ( attribute.isReadOnly() ) {
148 41823 jjdelcerro
            throw new SetReadOnlyAttributeException(attribute.getName(), this.getType());
149 41335 jjdelcerro
        }
150
        FeatureAttributeEmulator emulator = attribute.getFeatureAttributeEmulator();
151
        if( emulator!= null ) {
152
            emulator.set((EditableFeature) this,value);
153
            return;
154
        }
155 43729 fdiaz
156 41335 jjdelcerro
        if ( value == null ) {
157
            if ( !attribute.allowNull() ) {
158
                if ( !attribute.isAutomatic() ) {
159
                    throw new IllegalValueException(attribute, value);
160
                }
161
            }
162
            this.data.set(i, null);
163
            return;
164 40435 jjdelcerro
165 41335 jjdelcerro
        }
166 40435 jjdelcerro
167 41335 jjdelcerro
        if ( attribute.getFeatureAttributeGetter() != null ) {
168
            value = attribute.getFeatureAttributeGetter().setter(value);
169 40435 jjdelcerro
        }
170 41335 jjdelcerro
171 42218 jjdelcerro
        Class objectClass = attribute.getObjectClass();
172
        if( objectClass!=null ) {
173
            if ( objectClass.isInstance(value) ) {
174
                this.data.set(i, value);
175
                return;
176 44448 jjdelcerro
            } else {
177
                DataProfile dataProfile = attribute.getDataProfile();
178
                if( dataProfile!=null ) {
179
                    try {
180
                        value = dataProfile.coerce(
181
                                attribute.getDataType(),
182
                                value,
183
                                attribute.getTags()
184
                        );
185
                    } catch (CoercionException e) {
186
187
                    }
188
                }
189 42218 jjdelcerro
                try {
190 44448 jjdelcerro
                    value= this.getDataTypesManager().coerce(attribute.getType(),value);
191 42218 jjdelcerro
                } catch (CoercionException e) {
192
                    throw new IllegalArgumentException("Can't convert to "
193 44448 jjdelcerro
                            + attribute.getDataType().getName()
194
                            + " from '"
195 42218 jjdelcerro
                            + value.getClass().getName() + "' with value '"
196
                            + value.toString() + "'.");
197
                }
198 40435 jjdelcerro
            }
199 41335 jjdelcerro
        }
200 40435 jjdelcerro
        this.data.set(i, value);
201
    }
202 43729 fdiaz
203 40435 jjdelcerro
    private Object get(int index,Class theClass, int type) {
204
        Object value = this.get(index);
205
        if( theClass.isInstance(value) ) {
206
            return value;
207
        }
208
        try {
209
            return this.getDataTypesManager().coerce(type, value);
210
        } catch (CoercionException e) {
211 43729 fdiaz
212 40435 jjdelcerro
            if (value == null) {
213
                return null;
214
            }
215
            throw new IllegalArgumentException(
216
                    "Can't convert to "+theClass.getName()+
217
                    " from '"+value.getClass().getName()+
218
                    "' with value '"+value.toString()+"'.");
219
        }
220
    }
221 43729 fdiaz
222 40435 jjdelcerro
    public void initializeValues() {
223
        FeatureType type = this.getType();
224 44297 jjdelcerro
        for (FeatureAttributeDescriptor attribute : type) {
225 40435 jjdelcerro
            if (attribute.isAutomatic() || attribute.isReadOnly()
226 44203 jjdelcerro
                    || attribute.isComputed() ) {
227 40435 jjdelcerro
                continue;
228
            }
229
            if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
230
                continue;
231
            }
232
            this.set(attribute, attribute.getDefaultValue());
233
        }
234
    }
235
236
    public void clear() {
237
        initializeValues();
238
    }
239
240
    public void initializeValues(Feature feature) {
241
        FeatureType myType=this.getType();
242
        FeatureType type =feature.getType();
243 44297 jjdelcerro
        for (FeatureAttributeDescriptor attribute : type) {
244 40435 jjdelcerro
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
245
            if (myAttribute != null) {
246
                this.set(myAttribute, feature.get(attribute.getIndex()));
247
            }
248
        }
249
    }
250
251
252 44297 jjdelcerro
    @Override
253 40435 jjdelcerro
    public FeatureStore getStore() {
254
        return (FeatureStore) this.storeRef.get();
255
    }
256
257 44297 jjdelcerro
    @Override
258 40435 jjdelcerro
    public FeatureType getType() {
259
        return this.data.getType();
260
    }
261
262 44297 jjdelcerro
    @Override
263 40435 jjdelcerro
    public EditableFeature getEditable() {
264
        return new DefaultEditableFeature(this);
265
    }
266
267 44297 jjdelcerro
    @Override
268 40435 jjdelcerro
    public Feature getCopy() {
269
        return new DefaultFeature(this);
270
    }
271 43729 fdiaz
272 44297 jjdelcerro
    @Override
273
    @SuppressWarnings("CloneDoesntCallSuperClone")
274 40876 jjdelcerro
    public Object clone() throws CloneNotSupportedException {
275
        return new DefaultFeature(this);
276
    }
277 40435 jjdelcerro
278 44297 jjdelcerro
    @Override
279 40435 jjdelcerro
    public FeatureReference getReference() {
280
        if (this.reference == null) {
281
            if (!isInserted()) {
282
                return null;
283
            }
284
            reference = new DefaultFeatureReference(this);
285
        }
286
        return this.reference;
287
    }
288
289 44331 jjdelcerro
    @Override
290
    public Object getOrDefault(String name, Object defaultValue) {
291
        int index = this.data.getType().getIndex(name);
292
        if( index < 0 ) {
293
            return defaultValue;
294
        }
295
        return this.get(index);
296
    }
297
298
    @Override
299
    public String getStringOrDefault(String name, String defaultValue) {
300
        int index = this.data.getType().getIndex(name);
301
        if( index < 0 ) {
302
            return defaultValue;
303
        }
304
        try {
305
            return (String) this.get(index);
306
        } catch(Throwable th) {
307
            return defaultValue;
308
        }
309
    }
310
311
    @Override
312
    public int getIntOrDefault(String name, int defaultValue) {
313
        int index = this.data.getType().getIndex(name);
314
        if( index < 0 ) {
315
            return defaultValue;
316
        }
317
        try {
318
            return (int) this.get(index);
319
        } catch(Throwable th) {
320
            return defaultValue;
321
        }
322
    }
323
324
    @Override
325
    public long getLongOrDefault(String name, long defaultValue) {
326
        int index = this.data.getType().getIndex(name);
327
        if( index < 0 ) {
328
            return defaultValue;
329
        }
330
        try {
331
            return (long) this.get(index);
332
        } catch(Throwable th) {
333
            return defaultValue;
334
        }
335
    }
336
337
    @Override
338
    public float getFloatOrDefault(String name, float defaultValue) {
339
        int index = this.data.getType().getIndex(name);
340
        if( index < 0 ) {
341
            return defaultValue;
342
        }
343
        try {
344
            return (float) this.get(index);
345
        } catch(Throwable th) {
346
            return defaultValue;
347
        }
348
    }
349
350
    @Override
351
    public double getDoubleOrDefault(String name, double defaultValue) {
352
        int index = this.data.getType().getIndex(name);
353
        if( index < 0 ) {
354
            return defaultValue;
355
        }
356
        try {
357
            return (double) this.get(index);
358
        } catch(Throwable th) {
359
            return defaultValue;
360
        }
361
    }
362
363
    @Override
364
    public Date getDateOrDefault(String name, Date defaultValue) {
365
        int index = this.data.getType().getIndex(name);
366
        if( index < 0 ) {
367
            return defaultValue;
368
        }
369
        try {
370
            return (Date) this.get(index);
371
        } catch(Throwable th) {
372
            return defaultValue;
373
        }
374
    }
375
376
    @Override
377
    public Object getOrDefault(int index, Object defaultValue) {
378
        if( index < 0 || index >= this.data.getType().size() ) {
379
            return defaultValue;
380
        }
381
        try {
382
            return this.get(index);
383
        } catch(Throwable th) {
384
            return defaultValue;
385
        }
386
    }
387
388
    @Override
389
    public String getStringOrDefault(int index, String defaultValue) {
390
        if( index < 0 || index >= this.data.getType().size() ) {
391
            return defaultValue;
392
        }
393
        try {
394
            return (String) this.get(index);
395
        } catch(Throwable th) {
396
            return defaultValue;
397
        }
398
    }
399
400
    @Override
401
    public int getIntOrDefault(int index, int defaultValue) {
402
        if( index < 0 || index >= this.data.getType().size() ) {
403
            return defaultValue;
404
        }
405
        try {
406
            return (int) this.get(index);
407
        } catch(Throwable th) {
408
            return defaultValue;
409
        }
410
    }
411
412
    @Override
413
    public long getLongOrDefault(int index, long defaultValue) {
414
        if( index < 0 || index >= this.data.getType().size() ) {
415
            return defaultValue;
416
        }
417
        try {
418
            return (long) this.get(index);
419
        } catch(Throwable th) {
420
            return defaultValue;
421
        }
422
    }
423
424
    @Override
425
    public float getFloatOrDefault(int index, float defaultValue) {
426
        if( index < 0 || index >= this.data.getType().size() ) {
427
            return defaultValue;
428
        }
429
        try {
430
            return (float) this.get(index);
431
        } catch(Throwable th) {
432
            return defaultValue;
433
        }
434
    }
435
436
    @Override
437
    public double getDoubleOrDefault(int index, double defaultValue) {
438
        if( index < 0 || index >= this.data.getType().size() ) {
439
            return defaultValue;
440
        }
441
        try {
442
            return (double) this.get(index);
443
        } catch(Throwable th) {
444
            return defaultValue;
445
        }
446
    }
447
448
    @Override
449
    public Date getDateOrDefault(int index, Date defaultValue) {
450
        if( index < 0 || index >= this.data.getType().size() ) {
451
            return defaultValue;
452
        }
453
        try {
454
            return (Date) this.get(index);
455
        } catch(Throwable th) {
456
            return defaultValue;
457
        }
458
    }
459
460 40435 jjdelcerro
    class UnableToGetReferenceException extends BaseRuntimeException {
461
462
        /**
463 43729 fdiaz
         *
464 40435 jjdelcerro
         */
465
        private static final long serialVersionUID = 1812805035204824163L;
466
467
        /**
468
         * @param exception
469
         */
470
        public UnableToGetReferenceException(BaseException exception) {
471
            super("Unable to get reference", "_UnableToGetReferenceException",
472
                serialVersionUID);
473
            this.initCause(exception);
474 43729 fdiaz
475 40435 jjdelcerro
        }
476 43729 fdiaz
477 40435 jjdelcerro
    }
478 43729 fdiaz
479 44297 jjdelcerro
    @Override
480 41251 jjdelcerro
    public void validate(int mode) throws DataException  {
481 40435 jjdelcerro
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
482
    }
483
484 44297 jjdelcerro
    @Override
485 40435 jjdelcerro
    public List getSRSs() {
486
        // TODO Auto-generated method stub
487
        return null;
488
    }
489
490 44297 jjdelcerro
    @Override
491 40435 jjdelcerro
    public Envelope getDefaultEnvelope() {
492 41342 jjdelcerro
        Envelope envelope = this.data.getDefaultEnvelope();
493
        if( envelope == null ) {
494 44618 jjdelcerro
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
495
            if( i<0 ) {
496
                return null;
497
            }
498 41342 jjdelcerro
            Geometry geom = this.getDefaultGeometry();
499 41348 jjdelcerro
            if( geom!=null ) {
500
                envelope = geom.getEnvelope();
501
            }
502 41342 jjdelcerro
        }
503
        return envelope;
504 40435 jjdelcerro
    }
505
506 44022 jjdelcerro
    @Override
507 40435 jjdelcerro
    public Geometry getDefaultGeometry() {
508 41058 jjdelcerro
            Geometry geom = this.data.getDefaultGeometry();
509 44022 jjdelcerro
        if( geom == null ) {
510
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
511 44618 jjdelcerro
            if( i<0 ) {
512
                return null;
513
            }
514 44022 jjdelcerro
            Object x = this.get(i);
515
            if( x instanceof Geometry ) {
516
                geom = (Geometry) x;
517
            } else {
518
                geom = this.getGeometry(i);
519
            }
520 43420 jjdelcerro
        }
521 44022 jjdelcerro
        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 40435 jjdelcerro
    }
531
532 44086 jjdelcerro
    @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 44297 jjdelcerro
    @Override
548 40435 jjdelcerro
    public IProjection getDefaultSRS() {
549 44022 jjdelcerro
        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 40435 jjdelcerro
    }
557
558 44297 jjdelcerro
    @Override
559 40435 jjdelcerro
    public List getGeometries() {
560
        // TODO Auto-generated method stub
561
        return null;
562
    }
563
564 44128 jjdelcerro
    @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 44253 jjdelcerro
        return profile.createData(value, descriptor.getTags());
577 44128 jjdelcerro
    }
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 44297 jjdelcerro
    @Override
586 40435 jjdelcerro
    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 43729 fdiaz
594 43550 jjdelcerro
    public boolean has_key(String key) {
595
        Object x = this.getType().get(key);
596
        return x != null;
597
    }
598 43729 fdiaz
599 43550 jjdelcerro
    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 43729 fdiaz
607 43550 jjdelcerro
    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 40435 jjdelcerro
615 43550 jjdelcerro
            @Override
616
            public String next() {
617
                return ((FeatureAttributeDescriptor)it.next()).getName();
618
            }
619 43561 jjdelcerro
620
            @Override
621
            public void remove() {
622
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
623
            }
624 43550 jjdelcerro
        };
625
    }
626 40435 jjdelcerro
627 43550 jjdelcerro
    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 43729 fdiaz
654 43550 jjdelcerro
                };
655
            }
656 43561 jjdelcerro
657
            @Override
658
            public void remove() {
659
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
660
            }
661 43729 fdiaz
        };
662 43550 jjdelcerro
    }
663
664
    @Override
665 40435 jjdelcerro
    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 43729 fdiaz
            return get(attribute, this.data.get(index));
673
        }
674 40435 jjdelcerro
        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 43729 fdiaz
        }
692 40435 jjdelcerro
    }
693
694
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
695 41335 jjdelcerro
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
696
        if( emulator != null ) {
697 44226 jjdelcerro
//            int index = featureAttributeDescriptor.getIndex();
698
//            value = this.data.get(index);
699
//            if( value==null ) {
700 44203 jjdelcerro
                value = emulator.get(this);
701 44226 jjdelcerro
//                this.data.set(index,value);
702
//            }
703 44022 jjdelcerro
        } else {
704
            FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
705
            if( getter != null ) {
706
                value = getter.getter(value);
707
            }
708 40435 jjdelcerro
        }
709 44022 jjdelcerro
        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 41335 jjdelcerro
        }
718
        return value;
719 40435 jjdelcerro
    }
720
721 44297 jjdelcerro
    @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 40435 jjdelcerro
    public Object[] getArray(String name) {
733
        return this.getArray(this.data.getType().getIndex(name));
734
    }
735
736 44297 jjdelcerro
    @Override
737 40435 jjdelcerro
    public Object[] getArray(int index) {
738
        return (Object[]) this.get(index);
739
    }
740
741 44297 jjdelcerro
    @Override
742 40435 jjdelcerro
    public boolean getBoolean(String name) {
743
        return this.getBoolean(this.data.getType().getIndex(name));
744
    }
745
746 44297 jjdelcerro
    @Override
747 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
753 40435 jjdelcerro
    }
754
755 44297 jjdelcerro
    @Override
756 40435 jjdelcerro
    public byte getByte(String name) {
757
        return this.getByte(this.data.getType().getIndex(name));
758
    }
759
760 44297 jjdelcerro
    @Override
761 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
767 40435 jjdelcerro
    }
768
769 44297 jjdelcerro
    @Override
770 40435 jjdelcerro
    public Date getDate(String name) {
771
        return this.getDate(this.data.getType().getIndex(name));
772
    }
773
774 44297 jjdelcerro
    @Override
775 40435 jjdelcerro
    public Date getDate(int index) {
776
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
777
778
        return value;
779
    }
780
781 44297 jjdelcerro
    @Override
782 40435 jjdelcerro
    public double getDouble(String name) {
783
        return this.getDouble(this.data.getType().getIndex(name));
784
    }
785
786 44297 jjdelcerro
    @Override
787 40435 jjdelcerro
    public double getDouble(int index) {
788 43729 fdiaz
789 40435 jjdelcerro
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
790
        if (value == null) {
791
            return 0;
792
        }
793 44297 jjdelcerro
        return value;
794 40435 jjdelcerro
    }
795
796 44297 jjdelcerro
    @Override
797 40435 jjdelcerro
    public Feature getFeature(String name) {
798
        return this.getFeature(this.data.getType().getIndex(name));
799
    }
800
801 44297 jjdelcerro
    @Override
802 40435 jjdelcerro
    public Feature getFeature(int index) {
803
        return (Feature) this.get(index);
804
    }
805
806 44297 jjdelcerro
    @Override
807 40435 jjdelcerro
    public float getFloat(String name) {
808
        return this.getFloat(this.data.getType().getIndex(name));
809
    }
810
811 44297 jjdelcerro
    @Override
812 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
818 40435 jjdelcerro
    }
819
820 44297 jjdelcerro
    @Override
821 40435 jjdelcerro
    public Geometry getGeometry(String name) {
822
        return this.getGeometry(this.data.getType().getIndex(name));
823
    }
824
825 44297 jjdelcerro
    @Override
826 40435 jjdelcerro
    public Geometry getGeometry(int index) {
827
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
828
    }
829
830 44297 jjdelcerro
    @Override
831 40435 jjdelcerro
    public int getInt(String name) {
832
        return this.getInt(this.data.getType().getIndex(name));
833
    }
834
835 44297 jjdelcerro
    @Override
836 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
842 40435 jjdelcerro
    }
843
844 44297 jjdelcerro
    @Override
845 40435 jjdelcerro
    public long getLong(String name) {
846
        return this.getLong(this.data.getType().getIndex(name));
847
    }
848
849 44297 jjdelcerro
    @Override
850 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
856 40435 jjdelcerro
    }
857
858 44297 jjdelcerro
    @Override
859 40435 jjdelcerro
    public String getString(String name) {
860
        return this.getString(this.data.getType().getIndex(name));
861
    }
862
863 44297 jjdelcerro
    @Override
864 40435 jjdelcerro
    public String getString(int index) {
865
        return (String) this.get(index,String.class,DataTypes.STRING);
866
    }
867
868 44297 jjdelcerro
    @Override
869 40435 jjdelcerro
    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 44297 jjdelcerro
    @Override
887 40435 jjdelcerro
    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 44297 jjdelcerro
            @Override
896 40435 jjdelcerro
            public boolean hasNext() {
897
                return this.attributeIteraror.hasNext();
898
            }
899
900 44297 jjdelcerro
            @Override
901 40435 jjdelcerro
            public Object next() {
902
                return ((FeatureAttributeDescriptor) this.attributeIteraror
903
                        .next()).getName();
904
            }
905
906 44297 jjdelcerro
            @Override
907 40435 jjdelcerro
            public void remove() {
908
                throw new UnsupportedOperationException();
909
            }
910
911
        }
912
        return new DataNamesIterator(this);
913
    }
914
915 44297 jjdelcerro
    @Override
916 40435 jjdelcerro
    public Object getDataValue(String name) {
917
        name = name.toLowerCase();
918 41542 jjdelcerro
        try {
919
            return get(name);
920
        } catch (IllegalArgumentException ex) {
921
            if( "defaultgeometry".equalsIgnoreCase(name )) {
922
                return this.getDefaultGeometry();
923
            }
924
            throw ex;
925
        }
926 40435 jjdelcerro
    }
927
928 44297 jjdelcerro
    @Override
929 40435 jjdelcerro
    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 44297 jjdelcerro
            @Override
939 40435 jjdelcerro
            public boolean hasNext() {
940
                return current < feature.getType().size() - 1;
941
            }
942
943 44297 jjdelcerro
            @Override
944 40435 jjdelcerro
            public Object next() {
945
                return feature.get(current++);
946
            }
947
948 44297 jjdelcerro
            @Override
949 40435 jjdelcerro
            public void remove() {
950
                throw new UnsupportedOperationException();
951
            }
952
953
        }
954
        return new DataValuesIterator(this);
955
    }
956
957 44297 jjdelcerro
    @Override
958 40435 jjdelcerro
    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 44297 jjdelcerro
        return name.equals("feature");
969 40435 jjdelcerro
    }
970
971 44297 jjdelcerro
    @Override
972 40435 jjdelcerro
    public boolean hasDataValue(String name) {
973
        name = name.toLowerCase();
974
        return this.data.getType().getIndex(name) >= 0;
975
    }
976 43729 fdiaz
977 44297 jjdelcerro
    @Override
978 44086 jjdelcerro
    public Time getTime(int index) {
979
        return ((Time) this.get(index,Time.class,DataTypes.INSTANT));
980
    }
981
982 44297 jjdelcerro
    @Override
983 44086 jjdelcerro
    public Time getTime(String name) {
984
        return this.getInstant(this.data.getType().getIndex(name));
985
    }
986
987 44297 jjdelcerro
    @Override
988 40435 jjdelcerro
    public Instant getInstant(int index) {
989 44086 jjdelcerro
        return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT));
990 40435 jjdelcerro
    }
991
992 44297 jjdelcerro
    @Override
993 40435 jjdelcerro
    public Instant getInstant(String name) {
994
        return this.getInstant(this.data.getType().getIndex(name));
995
    }
996
997 44297 jjdelcerro
    @Override
998 40435 jjdelcerro
    public Interval getInterval(int index) {
999 44086 jjdelcerro
        return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL));
1000 40435 jjdelcerro
    }
1001
1002 44297 jjdelcerro
    @Override
1003 40435 jjdelcerro
    public Interval getInterval(String name) {
1004
        return this.getInterval(this.data.getType().getIndex(name));
1005
    }
1006
1007 42775 jjdelcerro
    @Override
1008 40435 jjdelcerro
    public DynObject getAsDynObject() {
1009 42775 jjdelcerro
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
1010 40435 jjdelcerro
        return facade;
1011
    }
1012
1013 44297 jjdelcerro
    @Override
1014 40435 jjdelcerro
    public String toString() {
1015 44297 jjdelcerro
            StringBuilder builder = new StringBuilder();
1016 40435 jjdelcerro
        FeatureAttributeDescriptor[] attributeDescriptors =
1017
            getType().getAttributeDescriptors();
1018
        for (int i = 0; i < attributeDescriptors.length; i++) {
1019
            String name = attributeDescriptors[i].getName();
1020 44607 jjdelcerro
            Object value = get(name);
1021
            builder.append(value);
1022 40435 jjdelcerro
            if (i < attributeDescriptors.length - 1) {
1023 44297 jjdelcerro
                builder.append(", ");
1024 40435 jjdelcerro
            }
1025
        }
1026 44297 jjdelcerro
        return builder.toString();
1027 40435 jjdelcerro
    }
1028 40867 jbadia
1029
1030 43729 fdiaz
1031
1032 40867 jbadia
        /**
1033 40435 jjdelcerro
     * @return the inserted
1034
     */
1035
    public boolean isInserted() {
1036
        return inserted;
1037
    }
1038
1039 43729 fdiaz
1040 40435 jjdelcerro
    /**
1041
     * @param inserted the inserted to set
1042
     */
1043
    public void setInserted(boolean inserted) {
1044
        this.inserted = inserted;
1045
    }
1046
1047 43983 jjdelcerro
        @Override
1048 40435 jjdelcerro
    public EvaluatorData getEvaluatorData() {
1049
        return this;
1050
    }
1051 43983 jjdelcerro
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 44346 jjdelcerro
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 44376 jjdelcerro
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 40435 jjdelcerro
}