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

History | View | Annotate | Download (19.4 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.Date;
28
import java.util.Iterator;
29
import java.util.List;
30

    
31
import org.cresques.cts.IProjection;
32
import org.gvsig.fmap.dal.DataTypes;
33
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
40
import org.gvsig.fmap.dal.feature.FeatureReference;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.exception.IllegalValueException;
44
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException;
45
import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectFeatureFacade;
46
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.primitive.Envelope;
49
import org.gvsig.timesupport.Instant;
50
import org.gvsig.timesupport.Interval;
51
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.dataTypes.CoercionException;
53
import org.gvsig.tools.dataTypes.DataTypesManager;
54
import org.gvsig.tools.dynobject.DynObject;
55
import org.gvsig.tools.evaluator.Evaluator;
56
import org.gvsig.tools.evaluator.EvaluatorData;
57
import org.gvsig.tools.evaluator.EvaluatorException;
58
import org.gvsig.tools.exception.BaseException;
59
import org.gvsig.tools.exception.BaseRuntimeException;
60
import org.gvsig.tools.lang.Cloneable;
61

    
62
public class DefaultFeature implements Feature, EvaluatorData, Cloneable {
63

    
64
        private static DataTypesManager dataTypesManager = null; 
65
        protected FeatureProvider data;
66
        protected FeatureReference reference;
67
        private WeakReference storeRef;
68
        
69
        private boolean inserted = false;
70

    
71
    /*
72
         * Usar con mucha precaucion o mejor no usar. Lo precisa el
73
         * DefaultFeatureSet en la ordenacion.
74
         */
75
        public DefaultFeature(FeatureStore store) {
76
                this.storeRef = new WeakReference(store);
77
                this.reference = null;
78
        }
79

    
80
        public DefaultFeature(FeatureStore store, FeatureProvider data) {
81
                this.data = data;
82
                this.storeRef = new WeakReference(store);
83
                this.reference = null;
84
                this.inserted = !data.isNew();
85
        }
86

    
87
        DefaultFeature(DefaultFeature feature) {
88
                this.data = feature.data.getCopy();
89
                this.storeRef = feature.storeRef;
90
                this.reference = feature.reference;
91
                this.inserted = feature.isInserted();
92
        }
93

    
94
        public void setData(FeatureProvider data) {
95
                this.data = data;
96
                this.reference = null;
97
                this.inserted = true; 
98
        }
99

    
100
        public FeatureProvider getData() {
101
                return this.data;
102
        }
103

    
104
        protected DataTypesManager getDataTypesManager() {
105
                if( dataTypesManager==null ) {
106
                        dataTypesManager = ToolsLocator.getDataTypesManager();
107
                }
108
                return dataTypesManager;
109
        }
110

    
111
    void set(FeatureAttributeDescriptor attribute, Object value) {
112
        int i = attribute.getIndex();
113

    
114
        if ( attribute.isReadOnly() ) {
115
            throw new SetReadOnlyAttributeException(attribute.getName(), this.getType());
116
        }
117
        FeatureAttributeEmulator emulator = attribute.getFeatureAttributeEmulator();
118
        if( emulator!= null ) {
119
            emulator.set((EditableFeature) this,value);
120
            return;
121
        }
122
        
123
        if ( value == null ) {
124
            if ( !attribute.allowNull() ) {
125
                if ( !attribute.isAutomatic() ) {
126
                    throw new IllegalValueException(attribute, value);
127
                }
128
            }
129
            this.data.set(i, null);
130
            return;
131

    
132
        }
133

    
134
        if ( attribute.getFeatureAttributeGetter() != null ) {
135
            value = attribute.getFeatureAttributeGetter().setter(value);
136
        }
137

    
138
        Class objectClass = attribute.getObjectClass();
139
        if( objectClass!=null ) {
140
            if ( objectClass.isInstance(value) ) {
141
                this.data.set(i, value);
142
                return;
143
            }
144

    
145
            if ( !objectClass.isInstance(value) ) {
146
                try {
147
                    value
148
                            = this.getDataTypesManager().coerce(attribute.getType(),
149
                                    value);
150
                } catch (CoercionException e) {
151
                    throw new IllegalArgumentException("Can't convert to "
152
                            + this.getDataTypesManager().getTypeName(
153
                                    attribute.getType()) + " from '"
154
                            + value.getClass().getName() + "' with value '"
155
                            + value.toString() + "'.");
156
                }
157
            }
158
        }
159
        this.data.set(i, value);
160
    }
161
        
162
    private Object get(int index,Class theClass, int type) {
163
        Object value = this.get(index);
164
        if( theClass.isInstance(value) ) {
165
            return value;
166
        }
167

    
168
        
169
        try {
170
            return this.getDataTypesManager().coerce(type, value);
171
        } catch (CoercionException e) {
172
            
173
            if (value == null) {
174
                return null;
175
            }
176
            throw new IllegalArgumentException(
177
                    "Can't convert to "+theClass.getName()+
178
                    " from '"+value.getClass().getName()+
179
                    "' with value '"+value.toString()+"'.");
180
        }
181
    }
182
    
183
//  private Object getNumberByType(Number value, int type) {
184
//      if (type==DataTypes.DOUBLE){
185
//          return new Double(value.doubleValue());
186
//      }else if (type==DataTypes.FLOAT){
187
//          return new Float(value.floatValue());
188
//      }else if (type==DataTypes.LONG){
189
//          return new Long(value.longValue());
190
//      }else if (type==DataTypes.INT){
191
//          return new Integer(value.intValue());
192
//      }else if (type==DataTypes.STRING){
193
//          return value.toString();
194
//      }
195
//      return value;
196
//  }
197

    
198
    public void initializeValues() {
199
        FeatureType type = this.getType();
200
        Iterator iterator = type.iterator();
201

    
202
        while (iterator.hasNext()) {
203
            FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
204
            .next();
205
            if (attribute.isAutomatic() || attribute.isReadOnly()
206
                    || attribute.getEvaluator() != null) {
207
                continue;
208
            }
209
            if (attribute.getDefaultValue() == null && !attribute.allowNull()) {
210
                continue;
211
            }
212
            this.set(attribute, attribute.getDefaultValue());
213
        }
214
    }
215

    
216
    public void clear() {
217
        initializeValues();
218
    }
219

    
220
    public void initializeValues(Feature feature) {
221
        FeatureType myType=this.getType();
222
        FeatureType type =feature.getType();
223
        Iterator iterator = type.iterator();
224

    
225
        while (iterator.hasNext()) {
226
            FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator
227
            .next();
228
            FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName());
229
            if (myAttribute != null) {
230
                this.set(myAttribute, feature.get(attribute.getIndex()));
231
            }
232
        }
233
    }
234

    
235

    
236
    public FeatureStore getStore() {
237
        return (FeatureStore) this.storeRef.get();
238
    }
239

    
240
    public FeatureType getType() {
241
        return this.data.getType();
242
    }
243

    
244
    public EditableFeature getEditable() {
245
        return new DefaultEditableFeature(this);
246
    }
247

    
248
    public Feature getCopy() {
249
        return new DefaultFeature(this);
250
    }
251
    
252
    public Object clone() throws CloneNotSupportedException {
253
        return new DefaultFeature(this);
254
    }
255

    
256
    public FeatureReference getReference() {
257
        if (this.reference == null) {
258
            if (!isInserted()) {
259
                return null;
260
            }
261
            reference = new DefaultFeatureReference(this);
262
        }
263
        return this.reference;
264
    }
265

    
266
    class UnableToGetReferenceException extends BaseRuntimeException {
267

    
268
        /**
269
         * 
270
         */
271
        private static final long serialVersionUID = 1812805035204824163L;
272

    
273
        /**
274
         * @param exception
275
         */
276
        public UnableToGetReferenceException(BaseException exception) {
277
            super("Unable to get reference", "_UnableToGetReferenceException",
278
                serialVersionUID);
279
            this.initCause(exception);
280
            
281
        }
282
        
283
    }
284
    
285
    public void validate(int mode) throws DataException  {
286
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
287
    }
288

    
289
    public List getSRSs() {
290
        // TODO Auto-generated method stub
291
        return null;
292
    }
293

    
294
    public Envelope getDefaultEnvelope() {
295
        Envelope envelope = this.data.getDefaultEnvelope();
296
        if( envelope == null ) {
297
            Geometry geom = this.getDefaultGeometry();
298
            if( geom!=null ) {
299
                envelope = geom.getEnvelope();
300
            }
301
        }
302
        return envelope;
303
    }
304

    
305
    public Geometry getDefaultGeometry() {
306
            Geometry geom = this.data.getDefaultGeometry();
307
            if( geom!=null ) {
308
                    return geom;
309
            }
310
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
311
        Object x = this.get(i);
312
        if( x instanceof Geometry ) {
313
            return (Geometry) x;
314
        }
315
        return this.getGeometry(i);
316
    }
317

    
318
    public IProjection getDefaultSRS() {
319
        return this.data.getType().getDefaultSRS();
320
    }
321

    
322
    public List getGeometries() {
323
        // TODO Auto-generated method stub
324
        return null;
325
    }
326

    
327
    public Object get(String name) {
328
        int index = this.data.getType().getIndex(name);
329
        if( index < 0 ) {
330
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
331
        }
332
        return this.get(index);
333
    }
334

    
335

    
336
    public Object get(int index) {
337
        FeatureType type = this.data.getType();
338
        if( index <0 || index >= type.size() ) {
339
            throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
340
        }
341
        FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
342
        if (!this.data.getType().hasEvaluators()) {
343
            return get(attribute, this.data.get(index));                
344
        }                
345
        Evaluator eval = attribute.getEvaluator();
346
        if (eval == null) {
347
            return this.data.get(index);
348
        } else {
349
            Object value = this.data.get(index);
350
            if (value != null) { // FIXME: para comprobar si esta calculado usar
351
                // un array
352
                // especifico.
353
                return get(attribute, this.data.get(index));
354
            }
355
            try {
356
                value = eval.evaluate(this);
357
            } catch (EvaluatorException e) {
358
                throw new DataEvaluatorRuntimeException(e);
359
            }
360
            this.data.set(index, value);
361
            return  get(attribute, value);
362
        }                
363
    }
364

    
365
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
366
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
367
        if( emulator != null ) {
368
            return emulator.get(this);
369
        }
370
        FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
371
        if( getter != null ) {
372
            return getter.getter(value);
373
        }
374
        return value;
375
    }
376

    
377
    public Object[] getArray(String name) {
378
        return this.getArray(this.data.getType().getIndex(name));
379
    }
380

    
381
    public Object[] getArray(int index) {
382
        return (Object[]) this.get(index);
383
    }
384

    
385
    public boolean getBoolean(String name) {
386
        return this.getBoolean(this.data.getType().getIndex(name));
387
    }
388

    
389
    public boolean getBoolean(int index) {
390
        Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
391
        if (value == null) {
392
            return false;
393
        }
394
        return value.booleanValue();
395
    }
396

    
397
    public byte getByte(String name) {
398
        return this.getByte(this.data.getType().getIndex(name));
399
    }
400

    
401
    public byte getByte(int index) {
402
        Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
403
        if (value == null) {
404
            return 0;
405
        }
406
        return value.byteValue();
407
    }
408

    
409
    public Date getDate(String name) {
410
        return this.getDate(this.data.getType().getIndex(name));
411
    }
412

    
413
    public Date getDate(int index) {
414
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
415

    
416
        return value;
417
    }
418

    
419
    public double getDouble(String name) {
420
        return this.getDouble(this.data.getType().getIndex(name));
421
    }
422

    
423
    public double getDouble(int index) {
424
        
425
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
426
        if (value == null) {
427
            return 0;
428
        }
429
        return value.doubleValue();
430
    }
431

    
432
    public Feature getFeature(String name) {
433
        return this.getFeature(this.data.getType().getIndex(name));
434
    }
435

    
436
    public Feature getFeature(int index) {
437
        return (Feature) this.get(index);
438
    }
439

    
440
    public float getFloat(String name) {
441
        return this.getFloat(this.data.getType().getIndex(name));
442
    }
443

    
444
    public float getFloat(int index) {
445
        Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
446
        if (value == null) {
447
            return 0;
448
        }
449
        return value.floatValue();
450
    }
451

    
452
    public Geometry getGeometry(String name) {
453
        return this.getGeometry(this.data.getType().getIndex(name));
454
    }
455

    
456
    public Geometry getGeometry(int index) {
457
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
458
    }
459

    
460
    public int getInt(String name) {
461
        return this.getInt(this.data.getType().getIndex(name));
462
    }
463

    
464
    public int getInt(int index) {
465
        Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
466
        if (value == null) {
467
            return 0;
468
        }
469
        return ((Integer)value).intValue();
470
    }
471

    
472
    public long getLong(String name) {
473
        return this.getLong(this.data.getType().getIndex(name));
474
    }
475

    
476
    public long getLong(int index) {
477
        Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
478
        if (value == null) {
479
            return 0;
480
        }
481
        return value.longValue();
482
    }
483

    
484
    public String getString(String name) {
485
        return this.getString(this.data.getType().getIndex(name));
486
    }
487

    
488
    public String getString(int index) {
489
        return (String) this.get(index,String.class,DataTypes.STRING);
490
    }
491

    
492
    public Object getContextValue(String name) {
493
        name = name.toLowerCase();
494
        if (name.equals("store")) {
495
            return this.getStore();
496
        }
497

    
498
        if (name.equals("featuretype")) {
499
            return this.data.getType();
500
        }
501

    
502
        if (name.equals("feature")) {
503
            return this;
504
        }
505

    
506
        throw new IllegalArgumentException(name);
507
    }
508

    
509
    public Iterator getDataNames() {
510
        class DataNamesIterator implements Iterator {
511
            Iterator attributeIteraror;
512

    
513
            DataNamesIterator(DefaultFeature feature) {
514
                this.attributeIteraror = feature.getType().iterator();
515
            }
516

    
517
            public boolean hasNext() {
518
                return this.attributeIteraror.hasNext();
519
            }
520

    
521
            public Object next() {
522
                return ((FeatureAttributeDescriptor) this.attributeIteraror
523
                        .next()).getName();
524
            }
525

    
526
            public void remove() {
527
                throw new UnsupportedOperationException();
528
            }
529

    
530
        }
531
        return new DataNamesIterator(this);
532
    }
533

    
534
    public Object getDataValue(String name) {
535
        name = name.toLowerCase();
536
        try {
537
            return get(name);
538
        } catch (IllegalArgumentException ex) {
539
            if( "defaultgeometry".equalsIgnoreCase(name )) {
540
                return this.getDefaultGeometry();
541
            }
542
            throw ex;
543
        }
544
    }
545

    
546
    public Iterator getDataValues() {
547
        class DataValuesIterator implements Iterator {
548
            DefaultFeature feature;
549
            int current = 0;
550

    
551
            DataValuesIterator(DefaultFeature feature) {
552
                this.feature = feature;
553
            }
554

    
555
            public boolean hasNext() {
556
                return current < feature.getType().size() - 1;
557
            }
558

    
559
            public Object next() {
560
                return feature.get(current++);
561
            }
562

    
563
            public void remove() {
564
                throw new UnsupportedOperationException();
565
            }
566

    
567
        }
568
        return new DataValuesIterator(this);
569
    }
570

    
571
    public boolean hasContextValue(String name) {
572
        name = name.toLowerCase();
573
        if (name.equals("store")) {
574
            return true;
575
        }
576

    
577
        if (name.equals("featuretype")) {
578
            return true;
579
        }
580

    
581
        if (name.equals("feature")) {
582
            return true;
583
        }
584
        return false;
585
    }
586

    
587
    public boolean hasDataValue(String name) {
588
        name = name.toLowerCase();
589
        return this.data.getType().getIndex(name) >= 0;
590
    }
591
    
592
    public Instant getInstant(int index) {
593
        return ((Instant) this.get(index,Date.class,DataTypes.INSTANT));
594
    }
595

    
596
    public Instant getInstant(String name) {
597
        return this.getInstant(this.data.getType().getIndex(name));
598
    }
599

    
600
    public Interval getInterval(int index) {
601
        return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL));
602
    }
603

    
604
    public Interval getInterval(String name) {
605
        return this.getInterval(this.data.getType().getIndex(name));
606
    }
607

    
608
    @Override
609
    public DynObject getAsDynObject() {
610
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
611
        return facade;
612
    }
613

    
614
    public String toString() {
615
       // StringBuffer buffer = new StringBuffer("Feature with values: ");
616
            StringBuffer buffer = new StringBuffer("");
617
        FeatureAttributeDescriptor[] attributeDescriptors =
618
            getType().getAttributeDescriptors();
619
        for (int i = 0; i < attributeDescriptors.length; i++) {
620
            String name = attributeDescriptors[i].getName();
621
            //buffer.append(name).append("=").append(get(name));
622
            buffer.append(get(name));
623
            if (i < attributeDescriptors.length - 1) {
624
                buffer.append(", ");
625
            }
626
        }
627
        return buffer.toString();
628
    }
629
    
630
    
631

    
632

    
633
        /**
634
     * @return the inserted
635
     */
636
    public boolean isInserted() {
637
        return inserted;
638
    }
639

    
640
    
641
    /**
642
     * @param inserted the inserted to set
643
     */
644
    public void setInserted(boolean inserted) {
645
        this.inserted = inserted;
646
    }
647

    
648
    public EvaluatorData getEvaluatorData() {
649
        return this;
650
    }
651
}