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

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
            geom = (Geometry) this.get(i);
312
        return geom;
313
    }
314

    
315
    public IProjection getDefaultSRS() {
316
        return this.data.getType().getDefaultSRS();
317
    }
318

    
319
    public List getGeometries() {
320
        // TODO Auto-generated method stub
321
        return null;
322
    }
323

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

    
332

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

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

    
374
    public Object[] getArray(String name) {
375
        return this.getArray(this.data.getType().getIndex(name));
376
    }
377

    
378
    public Object[] getArray(int index) {
379
        return (Object[]) this.get(index);
380
    }
381

    
382
    public boolean getBoolean(String name) {
383
        return this.getBoolean(this.data.getType().getIndex(name));
384
    }
385

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

    
394
    public byte getByte(String name) {
395
        return this.getByte(this.data.getType().getIndex(name));
396
    }
397

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

    
406
    public Date getDate(String name) {
407
        return this.getDate(this.data.getType().getIndex(name));
408
    }
409

    
410
    public Date getDate(int index) {
411
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
412

    
413
        return value;
414
    }
415

    
416
    public double getDouble(String name) {
417
        return this.getDouble(this.data.getType().getIndex(name));
418
    }
419

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

    
429
    public Feature getFeature(String name) {
430
        return this.getFeature(this.data.getType().getIndex(name));
431
    }
432

    
433
    public Feature getFeature(int index) {
434
        return (Feature) this.get(index);
435
    }
436

    
437
    public float getFloat(String name) {
438
        return this.getFloat(this.data.getType().getIndex(name));
439
    }
440

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

    
449
    public Geometry getGeometry(String name) {
450
        return this.getGeometry(this.data.getType().getIndex(name));
451
    }
452

    
453
    public Geometry getGeometry(int index) {
454
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
455
    }
456

    
457
    public int getInt(String name) {
458
        return this.getInt(this.data.getType().getIndex(name));
459
    }
460

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

    
469
    public long getLong(String name) {
470
        return this.getLong(this.data.getType().getIndex(name));
471
    }
472

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

    
481
    public String getString(String name) {
482
        return this.getString(this.data.getType().getIndex(name));
483
    }
484

    
485
    public String getString(int index) {
486
        return (String) this.get(index,String.class,DataTypes.STRING);
487
    }
488

    
489
    public Object getContextValue(String name) {
490
        name = name.toLowerCase();
491
        if (name.equals("store")) {
492
            return this.getStore();
493
        }
494

    
495
        if (name.equals("featuretype")) {
496
            return this.data.getType();
497
        }
498

    
499
        if (name.equals("feature")) {
500
            return this;
501
        }
502

    
503
        throw new IllegalArgumentException(name);
504
    }
505

    
506
    public Iterator getDataNames() {
507
        class DataNamesIterator implements Iterator {
508
            Iterator attributeIteraror;
509

    
510
            DataNamesIterator(DefaultFeature feature) {
511
                this.attributeIteraror = feature.getType().iterator();
512
            }
513

    
514
            public boolean hasNext() {
515
                return this.attributeIteraror.hasNext();
516
            }
517

    
518
            public Object next() {
519
                return ((FeatureAttributeDescriptor) this.attributeIteraror
520
                        .next()).getName();
521
            }
522

    
523
            public void remove() {
524
                throw new UnsupportedOperationException();
525
            }
526

    
527
        }
528
        return new DataNamesIterator(this);
529
    }
530

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

    
543
    public Iterator getDataValues() {
544
        class DataValuesIterator implements Iterator {
545
            DefaultFeature feature;
546
            int current = 0;
547

    
548
            DataValuesIterator(DefaultFeature feature) {
549
                this.feature = feature;
550
            }
551

    
552
            public boolean hasNext() {
553
                return current < feature.getType().size() - 1;
554
            }
555

    
556
            public Object next() {
557
                return feature.get(current++);
558
            }
559

    
560
            public void remove() {
561
                throw new UnsupportedOperationException();
562
            }
563

    
564
        }
565
        return new DataValuesIterator(this);
566
    }
567

    
568
    public boolean hasContextValue(String name) {
569
        name = name.toLowerCase();
570
        if (name.equals("store")) {
571
            return true;
572
        }
573

    
574
        if (name.equals("featuretype")) {
575
            return true;
576
        }
577

    
578
        if (name.equals("feature")) {
579
            return true;
580
        }
581
        return false;
582
    }
583

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

    
593
    public Instant getInstant(String name) {
594
        return this.getInstant(this.data.getType().getIndex(name));
595
    }
596

    
597
    public Interval getInterval(int index) {
598
        return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL));
599
    }
600

    
601
    public Interval getInterval(String name) {
602
        return this.getInterval(this.data.getType().getIndex(name));
603
    }
604

    
605
    @Override
606
    public DynObject getAsDynObject() {
607
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this);
608
        return facade;
609
    }
610

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

    
629

    
630
        /**
631
     * @return the inserted
632
     */
633
    public boolean isInserted() {
634
        return inserted;
635
    }
636

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

    
645
    public EvaluatorData getEvaluatorData() {
646
        return this;
647
    }
648
}