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

History | View | Annotate | Download (18.8 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.featureset.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();
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
        if ( attribute.getObjectClass().isInstance(value) ) {
139
            this.data.set(i, value);
140
            return;
141
        }
142

    
143
        if ( !attribute.getObjectClass().isInstance(value) ) {
144
            try {
145
                value
146
                        = this.getDataTypesManager().coerce(attribute.getType(),
147
                                value);
148
            } catch (CoercionException e) {
149
                throw new IllegalArgumentException("Can't convert to "
150
                        + this.getDataTypesManager().getTypeName(
151
                                attribute.getType()) + " from '"
152
                        + value.getClass().getName() + "' with value '"
153
                        + value.toString() + "'.");
154
            }
155
        }
156

    
157
        this.data.set(i, value);
158
    }
159
        
160
    private Object get(int index,Class theClass, int type) {
161
        Object value = this.get(index);
162
        if( theClass.isInstance(value) ) {
163
            return value;
164
        }
165

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

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

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

    
214
    public void clear() {
215
        initializeValues();
216
    }
217

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

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

    
233

    
234
    public FeatureStore getStore() {
235
        return (FeatureStore) this.storeRef.get();
236
    }
237

    
238
    public FeatureType getType() {
239
        return this.data.getType();
240
    }
241

    
242
    public EditableFeature getEditable() {
243
        return new DefaultEditableFeature(this);
244
    }
245

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

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

    
264
    class UnableToGetReferenceException extends BaseRuntimeException {
265

    
266
        /**
267
         * 
268
         */
269
        private static final long serialVersionUID = 1812805035204824163L;
270

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

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

    
292
    public Envelope getDefaultEnvelope() {
293
        return this.data.getDefaultEnvelope();
294
    }
295

    
296
    public Geometry getDefaultGeometry() {
297
            Geometry geom = this.data.getDefaultGeometry();
298
            if( geom!=null ) {
299
                    return geom;
300
            }
301
            int i = this.data.getType().getDefaultGeometryAttributeIndex();
302
            geom = (Geometry) this.get(i);
303
        return geom;
304
    }
305

    
306
    public IProjection getDefaultSRS() {
307
        return this.data.getType().getDefaultSRS();
308
    }
309

    
310
    public List getGeometries() {
311
        // TODO Auto-generated method stub
312
        return null;
313
    }
314

    
315
    public Object get(String name) {
316
        int index = this.data.getType().getIndex(name);
317
        if( index < 0 ) {
318
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
319
        }
320
        return this.get(index);
321
    }
322

    
323

    
324
    public Object get(int index) {
325
        FeatureType type = this.data.getType();
326
        if( index <0 || index >= type.size() ) {
327
            throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
328
        }
329
        FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
330
        if (!this.data.getType().hasEvaluators()) {
331
            return get(attribute, this.data.get(index));                
332
        }                
333
        Evaluator eval = attribute.getEvaluator();
334
        if (eval == null) {
335
            return this.data.get(index);
336
        } else {
337
            Object value = this.data.get(index);
338
            if (value != null) { // FIXME: para comprobar si esta calculado usar
339
                // un array
340
                // especifico.
341
                return get(attribute, this.data.get(index));
342
            }
343
            try {
344
                value = eval.evaluate(this);
345
            } catch (EvaluatorException e) {
346
                throw new DataEvaluatorRuntimeException(e);
347
            }
348
            this.data.set(index, value);
349
            return  get(attribute, value);
350
        }                
351
    }
352

    
353
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
354
        FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator();
355
        if( emulator != null ) {
356
            return emulator.get(this);
357
        }
358
        FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter();
359
        if( getter != null ) {
360
            return getter.getter(value);
361
        }
362
        return value;
363
    }
364

    
365
    public Object[] getArray(String name) {
366
        return this.getArray(this.data.getType().getIndex(name));
367
    }
368

    
369
    public Object[] getArray(int index) {
370
        return (Object[]) this.get(index);
371
    }
372

    
373
    public boolean getBoolean(String name) {
374
        return this.getBoolean(this.data.getType().getIndex(name));
375
    }
376

    
377
    public boolean getBoolean(int index) {
378
        Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
379
        if (value == null) {
380
            return false;
381
        }
382
        return value.booleanValue();
383
    }
384

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

    
389
    public byte getByte(int index) {
390
        Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
391
        if (value == null) {
392
            return 0;
393
        }
394
        return value.byteValue();
395
    }
396

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

    
401
    public Date getDate(int index) {
402
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
403

    
404
        return value;
405
    }
406

    
407
    public double getDouble(String name) {
408
        return this.getDouble(this.data.getType().getIndex(name));
409
    }
410

    
411
    public double getDouble(int index) {
412
        
413
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
414
        if (value == null) {
415
            return 0;
416
        }
417
        return value.doubleValue();
418
    }
419

    
420
    public Feature getFeature(String name) {
421
        return this.getFeature(this.data.getType().getIndex(name));
422
    }
423

    
424
    public Feature getFeature(int index) {
425
        return (Feature) this.get(index);
426
    }
427

    
428
    public float getFloat(String name) {
429
        return this.getFloat(this.data.getType().getIndex(name));
430
    }
431

    
432
    public float getFloat(int index) {
433
        Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
434
        if (value == null) {
435
            return 0;
436
        }
437
        return value.floatValue();
438
    }
439

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

    
444
    public Geometry getGeometry(int index) {
445
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
446
    }
447

    
448
    public int getInt(String name) {
449
        return this.getInt(this.data.getType().getIndex(name));
450
    }
451

    
452
    public int getInt(int index) {
453
        Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
454
        if (value == null) {
455
            return 0;
456
        }
457
        return ((Integer)value).intValue();
458
    }
459

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

    
464
    public long getLong(int index) {
465
        Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
466
        if (value == null) {
467
            return 0;
468
        }
469
        return value.longValue();
470
    }
471

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

    
476
    public String getString(int index) {
477
        return (String) this.get(index,String.class,DataTypes.STRING);
478
    }
479

    
480
    public Object getContextValue(String name) {
481
        name = name.toLowerCase();
482
        if (name.equals("store")) {
483
            return this.getStore();
484
        }
485

    
486
        if (name.equals("featuretype")) {
487
            return this.data.getType();
488
        }
489

    
490
        if (name.equals("feature")) {
491
            return this;
492
        }
493

    
494
        throw new IllegalArgumentException(name);
495
    }
496

    
497
    public Iterator getDataNames() {
498
        class DataNamesIterator implements Iterator {
499
            Iterator attributeIteraror;
500

    
501
            DataNamesIterator(DefaultFeature feature) {
502
                this.attributeIteraror = feature.getType().iterator();
503
            }
504

    
505
            public boolean hasNext() {
506
                return this.attributeIteraror.hasNext();
507
            }
508

    
509
            public Object next() {
510
                return ((FeatureAttributeDescriptor) this.attributeIteraror
511
                        .next()).getName();
512
            }
513

    
514
            public void remove() {
515
                throw new UnsupportedOperationException();
516
            }
517

    
518
        }
519
        return new DataNamesIterator(this);
520
    }
521

    
522
    public Object getDataValue(String name) {
523
        name = name.toLowerCase();
524
        return get(name);
525
    }
526

    
527
    public Iterator getDataValues() {
528
        class DataValuesIterator implements Iterator {
529
            DefaultFeature feature;
530
            int current = 0;
531

    
532
            DataValuesIterator(DefaultFeature feature) {
533
                this.feature = feature;
534
            }
535

    
536
            public boolean hasNext() {
537
                return current < feature.getType().size() - 1;
538
            }
539

    
540
            public Object next() {
541
                return feature.get(current++);
542
            }
543

    
544
            public void remove() {
545
                throw new UnsupportedOperationException();
546
            }
547

    
548
        }
549
        return new DataValuesIterator(this);
550
    }
551

    
552
    public boolean hasContextValue(String name) {
553
        name = name.toLowerCase();
554
        if (name.equals("store")) {
555
            return true;
556
        }
557

    
558
        if (name.equals("featuretype")) {
559
            return true;
560
        }
561

    
562
        if (name.equals("feature")) {
563
            return true;
564
        }
565
        return false;
566
    }
567

    
568
    public boolean hasDataValue(String name) {
569
        name = name.toLowerCase();
570
        return this.data.getType().getIndex(name) >= 0;
571
    }
572
    
573
    public Instant getInstant(int index) {
574
        return ((Instant) this.get(index,Date.class,DataTypes.INSTANT));
575
    }
576

    
577
    public Instant getInstant(String name) {
578
        return this.getInstant(this.data.getType().getIndex(name));
579
    }
580

    
581
    public Interval getInterval(int index) {
582
        return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL));
583
    }
584

    
585
    public Interval getInterval(String name) {
586
        return this.getInterval(this.data.getType().getIndex(name));
587
    }
588

    
589
    public DynObject getAsDynObject() {
590
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade();
591
        facade.setFeature(this);
592
        return facade;
593
    }
594

    
595
    public String toString() {
596
       // StringBuffer buffer = new StringBuffer("Feature with values: ");
597
            StringBuffer buffer = new StringBuffer("");
598
        FeatureAttributeDescriptor[] attributeDescriptors =
599
            getType().getAttributeDescriptors();
600
        for (int i = 0; i < attributeDescriptors.length; i++) {
601
            String name = attributeDescriptors[i].getName();
602
            //buffer.append(name).append("=").append(get(name));
603
            buffer.append(get(name));
604
            if (i < attributeDescriptors.length - 1) {
605
                buffer.append(", ");
606
            }
607
        }
608
        return buffer.toString();
609
    }
610
    
611
    
612

    
613

    
614
        /**
615
     * @return the inserted
616
     */
617
    public boolean isInserted() {
618
        return inserted;
619
    }
620

    
621
    
622
    /**
623
     * @param inserted the inserted to set
624
     */
625
    public void setInserted(boolean inserted) {
626
        this.inserted = inserted;
627
    }
628

    
629
    public EvaluatorData getEvaluatorData() {
630
        return this;
631
    }
632
}