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 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 44641 jjdelcerro
            if ( targetAttrDescriptor.isComputed() ) {
111 43729 fdiaz
                 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 44641 jjdelcerro
            this.setforced(targetAttrDescriptor.getIndex(), targetAttrDescriptor,value);
122 43729 fdiaz
        }
123
    }
124
125 40435 jjdelcerro
        public void setData(FeatureProvider data) {
126
                this.data = data;
127
                this.reference = null;
128 43729 fdiaz
                this.inserted = true;
129 40435 jjdelcerro
        }
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 44641 jjdelcerro
    protected void set(FeatureAttributeDescriptor attribute, Object value) {
143 41335 jjdelcerro
        int i = attribute.getIndex();
144 40435 jjdelcerro
145 41335 jjdelcerro
        if ( attribute.isReadOnly() ) {
146 41823 jjdelcerro
            throw new SetReadOnlyAttributeException(attribute.getName(), this.getType());
147 41335 jjdelcerro
        }
148
        FeatureAttributeEmulator emulator = attribute.getFeatureAttributeEmulator();
149
        if( emulator!= null ) {
150
            emulator.set((EditableFeature) this,value);
151
            return;
152
        }
153 43729 fdiaz
154 41335 jjdelcerro
        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 40435 jjdelcerro
163 41335 jjdelcerro
        }
164 40435 jjdelcerro
165 41335 jjdelcerro
        if ( attribute.getFeatureAttributeGetter() != null ) {
166
            value = attribute.getFeatureAttributeGetter().setter(value);
167 40435 jjdelcerro
        }
168 44641 jjdelcerro
        this.setforced(i, attribute, value);
169
    }
170 41335 jjdelcerro
171 44641 jjdelcerro
    private void setforced(int i, FeatureAttributeDescriptor attribute, Object value) {
172
173 42218 jjdelcerro
        Class objectClass = attribute.getObjectClass();
174
        if( objectClass!=null ) {
175
            if ( objectClass.isInstance(value) ) {
176
                this.data.set(i, value);
177
                return;
178 44448 jjdelcerro
            } 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 42218 jjdelcerro
                try {
192 44448 jjdelcerro
                    value= this.getDataTypesManager().coerce(attribute.getType(),value);
193 42218 jjdelcerro
                } catch (CoercionException e) {
194
                    throw new IllegalArgumentException("Can't convert to "
195 44448 jjdelcerro
                            + attribute.getDataType().getName()
196
                            + " from '"
197 42218 jjdelcerro
                            + value.getClass().getName() + "' with value '"
198
                            + value.toString() + "'.");
199
                }
200 40435 jjdelcerro
            }
201 41335 jjdelcerro
        }
202 40435 jjdelcerro
        this.data.set(i, value);
203
    }
204 43729 fdiaz
205 40435 jjdelcerro
    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 43729 fdiaz
214 40435 jjdelcerro
            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 43729 fdiaz
224 40435 jjdelcerro
    public void initializeValues() {
225
        FeatureType type = this.getType();
226 44297 jjdelcerro
        for (FeatureAttributeDescriptor attribute : type) {
227 40435 jjdelcerro
            if (attribute.isAutomatic() || attribute.isReadOnly()
228 44203 jjdelcerro
                    || attribute.isComputed() ) {
229 40435 jjdelcerro
                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 44297 jjdelcerro
        for (FeatureAttributeDescriptor attribute : type) {
246 40435 jjdelcerro
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
247
            if (myAttribute != null) {
248
                this.set(myAttribute, feature.get(attribute.getIndex()));
249
            }
250
        }
251
    }
252
253
254 44297 jjdelcerro
    @Override
255 40435 jjdelcerro
    public FeatureStore getStore() {
256
        return (FeatureStore) this.storeRef.get();
257
    }
258
259 44297 jjdelcerro
    @Override
260 40435 jjdelcerro
    public FeatureType getType() {
261
        return this.data.getType();
262
    }
263
264 44297 jjdelcerro
    @Override
265 40435 jjdelcerro
    public EditableFeature getEditable() {
266
        return new DefaultEditableFeature(this);
267
    }
268
269 44297 jjdelcerro
    @Override
270 40435 jjdelcerro
    public Feature getCopy() {
271
        return new DefaultFeature(this);
272
    }
273 43729 fdiaz
274 44297 jjdelcerro
    @Override
275
    @SuppressWarnings("CloneDoesntCallSuperClone")
276 40876 jjdelcerro
    public Object clone() throws CloneNotSupportedException {
277
        return new DefaultFeature(this);
278
    }
279 40435 jjdelcerro
280 44297 jjdelcerro
    @Override
281 40435 jjdelcerro
    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 44331 jjdelcerro
    @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 40435 jjdelcerro
    class UnableToGetReferenceException extends BaseRuntimeException {
463
464
        /**
465 43729 fdiaz
         *
466 40435 jjdelcerro
         */
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 43729 fdiaz
477 40435 jjdelcerro
        }
478 43729 fdiaz
479 40435 jjdelcerro
    }
480 43729 fdiaz
481 44297 jjdelcerro
    @Override
482 41251 jjdelcerro
    public void validate(int mode) throws DataException  {
483 40435 jjdelcerro
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
484
    }
485
486 44297 jjdelcerro
    @Override
487 40435 jjdelcerro
    public List getSRSs() {
488
        // TODO Auto-generated method stub
489
        return null;
490
    }
491
492 44297 jjdelcerro
    @Override
493 40435 jjdelcerro
    public Envelope getDefaultEnvelope() {
494 41342 jjdelcerro
        Envelope envelope = this.data.getDefaultEnvelope();
495
        if( envelope == null ) {
496 44618 jjdelcerro
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
497
            if( i<0 ) {
498
                return null;
499
            }
500 41342 jjdelcerro
            Geometry geom = this.getDefaultGeometry();
501 41348 jjdelcerro
            if( geom!=null ) {
502
                envelope = geom.getEnvelope();
503
            }
504 41342 jjdelcerro
        }
505
        return envelope;
506 40435 jjdelcerro
    }
507
508 44022 jjdelcerro
    @Override
509 40435 jjdelcerro
    public Geometry getDefaultGeometry() {
510 41058 jjdelcerro
            Geometry geom = this.data.getDefaultGeometry();
511 44022 jjdelcerro
        if( geom == null ) {
512
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
513 44618 jjdelcerro
            if( i<0 ) {
514
                return null;
515
            }
516 44022 jjdelcerro
            Object x = this.get(i);
517
            if( x instanceof Geometry ) {
518
                geom = (Geometry) x;
519
            } else {
520
                geom = this.getGeometry(i);
521
            }
522 43420 jjdelcerro
        }
523 44022 jjdelcerro
        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 40435 jjdelcerro
    }
533
534 44086 jjdelcerro
    @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 44297 jjdelcerro
    @Override
550 40435 jjdelcerro
    public IProjection getDefaultSRS() {
551 44022 jjdelcerro
        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 40435 jjdelcerro
    }
559
560 44297 jjdelcerro
    @Override
561 40435 jjdelcerro
    public List getGeometries() {
562
        // TODO Auto-generated method stub
563
        return null;
564
    }
565
566 44128 jjdelcerro
    @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 44253 jjdelcerro
        return profile.createData(value, descriptor.getTags());
579 44128 jjdelcerro
    }
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 44297 jjdelcerro
    @Override
588 40435 jjdelcerro
    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 43729 fdiaz
596 43550 jjdelcerro
    public boolean has_key(String key) {
597
        Object x = this.getType().get(key);
598
        return x != null;
599
    }
600 43729 fdiaz
601 43550 jjdelcerro
    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 43729 fdiaz
609 43550 jjdelcerro
    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 40435 jjdelcerro
617 43550 jjdelcerro
            @Override
618
            public String next() {
619
                return ((FeatureAttributeDescriptor)it.next()).getName();
620
            }
621 43561 jjdelcerro
622
            @Override
623
            public void remove() {
624
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
625
            }
626 43550 jjdelcerro
        };
627
    }
628 40435 jjdelcerro
629 43550 jjdelcerro
    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 43729 fdiaz
656 43550 jjdelcerro
                };
657
            }
658 43561 jjdelcerro
659
            @Override
660
            public void remove() {
661
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
662
            }
663 43729 fdiaz
        };
664 43550 jjdelcerro
    }
665
666
    @Override
667 40435 jjdelcerro
    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 43729 fdiaz
            return get(attribute, this.data.get(index));
675
        }
676 40435 jjdelcerro
        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 43729 fdiaz
        }
694 40435 jjdelcerro
    }
695
696
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
697 41335 jjdelcerro
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
698
        if( emulator != null ) {
699 44226 jjdelcerro
//            int index = featureAttributeDescriptor.getIndex();
700
//            value = this.data.get(index);
701
//            if( value==null ) {
702 44203 jjdelcerro
                value = emulator.get(this);
703 44226 jjdelcerro
//                this.data.set(index,value);
704
//            }
705 44022 jjdelcerro
        } else {
706
            FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
707
            if( getter != null ) {
708
                value = getter.getter(value);
709
            }
710 40435 jjdelcerro
        }
711 44022 jjdelcerro
        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 41335 jjdelcerro
        }
720
        return value;
721 40435 jjdelcerro
    }
722
723 44297 jjdelcerro
    @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 40435 jjdelcerro
    public Object[] getArray(String name) {
735
        return this.getArray(this.data.getType().getIndex(name));
736
    }
737
738 44297 jjdelcerro
    @Override
739 40435 jjdelcerro
    public Object[] getArray(int index) {
740
        return (Object[]) this.get(index);
741
    }
742
743 44297 jjdelcerro
    @Override
744 40435 jjdelcerro
    public boolean getBoolean(String name) {
745
        return this.getBoolean(this.data.getType().getIndex(name));
746
    }
747
748 44297 jjdelcerro
    @Override
749 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
755 40435 jjdelcerro
    }
756
757 44297 jjdelcerro
    @Override
758 40435 jjdelcerro
    public byte getByte(String name) {
759
        return this.getByte(this.data.getType().getIndex(name));
760
    }
761
762 44297 jjdelcerro
    @Override
763 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
769 40435 jjdelcerro
    }
770
771 44297 jjdelcerro
    @Override
772 40435 jjdelcerro
    public Date getDate(String name) {
773
        return this.getDate(this.data.getType().getIndex(name));
774
    }
775
776 44297 jjdelcerro
    @Override
777 40435 jjdelcerro
    public Date getDate(int index) {
778
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
779
780
        return value;
781
    }
782
783 44297 jjdelcerro
    @Override
784 40435 jjdelcerro
    public double getDouble(String name) {
785
        return this.getDouble(this.data.getType().getIndex(name));
786
    }
787
788 44297 jjdelcerro
    @Override
789 40435 jjdelcerro
    public double getDouble(int index) {
790 43729 fdiaz
791 40435 jjdelcerro
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
792
        if (value == null) {
793
            return 0;
794
        }
795 44297 jjdelcerro
        return value;
796 40435 jjdelcerro
    }
797
798 44297 jjdelcerro
    @Override
799 40435 jjdelcerro
    public Feature getFeature(String name) {
800
        return this.getFeature(this.data.getType().getIndex(name));
801
    }
802
803 44297 jjdelcerro
    @Override
804 40435 jjdelcerro
    public Feature getFeature(int index) {
805
        return (Feature) this.get(index);
806
    }
807
808 44297 jjdelcerro
    @Override
809 40435 jjdelcerro
    public float getFloat(String name) {
810
        return this.getFloat(this.data.getType().getIndex(name));
811
    }
812
813 44297 jjdelcerro
    @Override
814 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
820 40435 jjdelcerro
    }
821
822 44297 jjdelcerro
    @Override
823 40435 jjdelcerro
    public Geometry getGeometry(String name) {
824
        return this.getGeometry(this.data.getType().getIndex(name));
825
    }
826
827 44297 jjdelcerro
    @Override
828 40435 jjdelcerro
    public Geometry getGeometry(int index) {
829
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
830
    }
831
832 44297 jjdelcerro
    @Override
833 40435 jjdelcerro
    public int getInt(String name) {
834
        return this.getInt(this.data.getType().getIndex(name));
835
    }
836
837 44297 jjdelcerro
    @Override
838 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
844 40435 jjdelcerro
    }
845
846 44297 jjdelcerro
    @Override
847 40435 jjdelcerro
    public long getLong(String name) {
848
        return this.getLong(this.data.getType().getIndex(name));
849
    }
850
851 44297 jjdelcerro
    @Override
852 40435 jjdelcerro
    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 44297 jjdelcerro
        return value;
858 40435 jjdelcerro
    }
859
860 44297 jjdelcerro
    @Override
861 40435 jjdelcerro
    public String getString(String name) {
862
        return this.getString(this.data.getType().getIndex(name));
863
    }
864
865 44297 jjdelcerro
    @Override
866 40435 jjdelcerro
    public String getString(int index) {
867
        return (String) this.get(index,String.class,DataTypes.STRING);
868
    }
869
870 44297 jjdelcerro
    @Override
871 40435 jjdelcerro
    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 44297 jjdelcerro
    @Override
889 40435 jjdelcerro
    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 44297 jjdelcerro
            @Override
898 40435 jjdelcerro
            public boolean hasNext() {
899
                return this.attributeIteraror.hasNext();
900
            }
901
902 44297 jjdelcerro
            @Override
903 40435 jjdelcerro
            public Object next() {
904
                return ((FeatureAttributeDescriptor) this.attributeIteraror
905
                        .next()).getName();
906
            }
907
908 44297 jjdelcerro
            @Override
909 40435 jjdelcerro
            public void remove() {
910
                throw new UnsupportedOperationException();
911
            }
912
913
        }
914
        return new DataNamesIterator(this);
915
    }
916
917 44297 jjdelcerro
    @Override
918 40435 jjdelcerro
    public Object getDataValue(String name) {
919
        name = name.toLowerCase();
920 41542 jjdelcerro
        try {
921
            return get(name);
922
        } catch (IllegalArgumentException ex) {
923
            if( "defaultgeometry".equalsIgnoreCase(name )) {
924
                return this.getDefaultGeometry();
925
            }
926
            throw ex;
927
        }
928 40435 jjdelcerro
    }
929
930 44297 jjdelcerro
    @Override
931 40435 jjdelcerro
    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 44297 jjdelcerro
            @Override
941 40435 jjdelcerro
            public boolean hasNext() {
942
                return current < feature.getType().size() - 1;
943
            }
944
945 44297 jjdelcerro
            @Override
946 40435 jjdelcerro
            public Object next() {
947
                return feature.get(current++);
948
            }
949
950 44297 jjdelcerro
            @Override
951 40435 jjdelcerro
            public void remove() {
952
                throw new UnsupportedOperationException();
953
            }
954
955
        }
956
        return new DataValuesIterator(this);
957
    }
958
959 44297 jjdelcerro
    @Override
960 40435 jjdelcerro
    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 44297 jjdelcerro
        return name.equals("feature");
971 40435 jjdelcerro
    }
972
973 44297 jjdelcerro
    @Override
974 40435 jjdelcerro
    public boolean hasDataValue(String name) {
975
        name = name.toLowerCase();
976
        return this.data.getType().getIndex(name) >= 0;
977
    }
978 43729 fdiaz
979 44297 jjdelcerro
    @Override
980 44086 jjdelcerro
    public Time getTime(int index) {
981
        return ((Time) this.get(index,Time.class,DataTypes.INSTANT));
982
    }
983
984 44297 jjdelcerro
    @Override
985 44086 jjdelcerro
    public Time getTime(String name) {
986
        return this.getInstant(this.data.getType().getIndex(name));
987
    }
988
989 44297 jjdelcerro
    @Override
990 40435 jjdelcerro
    public Instant getInstant(int index) {
991 44086 jjdelcerro
        return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT));
992 40435 jjdelcerro
    }
993
994 44297 jjdelcerro
    @Override
995 40435 jjdelcerro
    public Instant getInstant(String name) {
996
        return this.getInstant(this.data.getType().getIndex(name));
997
    }
998
999 44297 jjdelcerro
    @Override
1000 40435 jjdelcerro
    public Interval getInterval(int index) {
1001 44086 jjdelcerro
        return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL));
1002 40435 jjdelcerro
    }
1003
1004 44297 jjdelcerro
    @Override
1005 40435 jjdelcerro
    public Interval getInterval(String name) {
1006
        return this.getInterval(this.data.getType().getIndex(name));
1007
    }
1008
1009 42775 jjdelcerro
    @Override
1010 40435 jjdelcerro
    public DynObject getAsDynObject() {
1011 42775 jjdelcerro
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
1012 40435 jjdelcerro
        return facade;
1013
    }
1014
1015 44297 jjdelcerro
    @Override
1016 40435 jjdelcerro
    public String toString() {
1017 44297 jjdelcerro
            StringBuilder builder = new StringBuilder();
1018 40435 jjdelcerro
        FeatureAttributeDescriptor[] attributeDescriptors =
1019
            getType().getAttributeDescriptors();
1020
        for (int i = 0; i < attributeDescriptors.length; i++) {
1021
            String name = attributeDescriptors[i].getName();
1022 44607 jjdelcerro
            Object value = get(name);
1023
            builder.append(value);
1024 40435 jjdelcerro
            if (i < attributeDescriptors.length - 1) {
1025 44297 jjdelcerro
                builder.append(", ");
1026 40435 jjdelcerro
            }
1027
        }
1028 44297 jjdelcerro
        return builder.toString();
1029 40435 jjdelcerro
    }
1030 40867 jbadia
1031
1032 43729 fdiaz
1033
1034 40867 jbadia
        /**
1035 40435 jjdelcerro
     * @return the inserted
1036
     */
1037
    public boolean isInserted() {
1038
        return inserted;
1039
    }
1040
1041 43729 fdiaz
1042 40435 jjdelcerro
    /**
1043
     * @param inserted the inserted to set
1044
     */
1045
    public void setInserted(boolean inserted) {
1046
        this.inserted = inserted;
1047
    }
1048
1049 43983 jjdelcerro
        @Override
1050 40435 jjdelcerro
    public EvaluatorData getEvaluatorData() {
1051
        return this;
1052
    }
1053 43983 jjdelcerro
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 44346 jjdelcerro
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 44376 jjdelcerro
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 40435 jjdelcerro
}