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

History | View | Annotate | Download (17.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

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

    
60
public class DefaultFeature implements Feature, EvaluatorData {
61

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

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

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

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

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

    
98
        public FeatureProvider getData() {
99
                return this.data;
100
        }
101

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

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

    
112
                if (attribute.isReadOnly()) {
113
                        throw new SetReadOnlyAttributeException();
114
                }
115

    
116
                if (attribute.getEvaluator() != null) {
117
                        throw new SetReadOnlyAttributeException();
118
                }
119

    
120
                if (value == null) {
121
                        if (!attribute.allowNull()) {
122
                                if (!attribute.isAutomatic()) {
123
                                        throw new IllegalValueException(attribute, value);
124
                                }
125
                        }
126
                        this.data.set(i, null);
127
                        return;
128

    
129
        }
130
                
131
        if (attribute.getFeatureAttributeGetter() != null){
132
            value = attribute.getFeatureAttributeGetter().setter(value);                    
133
        }
134
        
135
        if (attribute.getObjectClass().isInstance(value)) {
136
            this.data.set(i, value);
137
            return;
138
        }
139

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

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

    
193
    public void initializeValues() {
194
        FeatureType type = this.getType();
195
        Iterator iterator = type.iterator();
196

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

    
211
    public void clear() {
212
        initializeValues();
213
    }
214

    
215
    public void initializeValues(Feature feature) {
216
        FeatureType myType=this.getType();
217
        FeatureType type =feature.getType();
218
        Iterator iterator = type.iterator();
219

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

    
230

    
231
    public FeatureStore getStore() {
232
        return (FeatureStore) this.storeRef.get();
233
    }
234

    
235
    public FeatureType getType() {
236
        return this.data.getType();
237
    }
238

    
239
    public EditableFeature getEditable() {
240
        return new DefaultEditableFeature(this);
241
    }
242

    
243
    public Feature getCopy() {
244
        return new DefaultFeature(this);
245
    }
246

    
247
    public FeatureReference getReference() {
248
        if (this.reference == null) {
249
            if (!isInserted()) {
250
                return null;
251
            }
252
            reference = new DefaultFeatureReference(this);
253
        }
254
        return this.reference;
255
    }
256

    
257
    class UnableToGetReferenceException extends BaseRuntimeException {
258

    
259
        /**
260
         * 
261
         */
262
        private static final long serialVersionUID = 1812805035204824163L;
263

    
264
        /**
265
         * @param exception
266
         */
267
        public UnableToGetReferenceException(BaseException exception) {
268
            super("Unable to get reference", "_UnableToGetReferenceException",
269
                serialVersionUID);
270
            this.initCause(exception);
271
            
272
        }
273
        
274
    }
275
    
276
    public void validate(int mode) {
277
        ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode);
278
    }
279

    
280
    public List getSRSs() {
281
        // TODO Auto-generated method stub
282
        return null;
283
    }
284

    
285
    public Envelope getDefaultEnvelope() {
286
        return this.data.getDefaultEnvelope();
287
    }
288

    
289
    public Geometry getDefaultGeometry() {
290
        return this.data.getDefaultGeometry();
291
    }
292

    
293
    public IProjection getDefaultSRS() {
294
        return this.data.getType().getDefaultSRS();
295
    }
296

    
297
    public List getGeometries() {
298
        // TODO Auto-generated method stub
299
        return null;
300
    }
301

    
302
    public Object get(String name) {
303
        int index = this.data.getType().getIndex(name);
304
        if( index < 0 ) {
305
            throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature.");
306
        }
307
        return this.get(index);
308
    }
309

    
310

    
311
    public Object get(int index) {
312
        FeatureType type = this.data.getType();
313
        if( index <0 || index >= type.size() ) {
314
            throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+".");
315
        }
316
        FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index);
317
        if (!this.data.getType().hasEvaluators()) {
318
            return get(attribute, this.data.get(index));                
319
        }                
320
        Evaluator eval = attribute.getEvaluator();
321
        if (eval == null) {
322
            return this.data.get(index);
323
        } else {
324
            Object value = this.data.get(index);
325
            if (value != null) { // FIXME: para comprobar si esta calculado usar
326
                // un array
327
                // especifico.
328
                return get(attribute, this.data.get(index));
329
            }
330
            try {
331
                value = eval.evaluate(this);
332
            } catch (EvaluatorException e) {
333
                throw new DataEvaluatorRuntimeException(e);
334
            }
335
            this.data.set(index, value);
336
            return  get(attribute, value);
337
        }                
338
    }
339

    
340
    private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){
341
        if (featureAttributeDescriptor.getFeatureAttributeGetter() == null){
342
            return value;
343
        }else{
344
            return featureAttributeDescriptor.getFeatureAttributeGetter().getter(value);
345
        }
346
    }
347

    
348
    public Object[] getArray(String name) {
349
        return this.getArray(this.data.getType().getIndex(name));
350
    }
351

    
352
    public Object[] getArray(int index) {
353
        return (Object[]) this.get(index);
354
    }
355

    
356
    public boolean getBoolean(String name) {
357
        return this.getBoolean(this.data.getType().getIndex(name));
358
    }
359

    
360
    public boolean getBoolean(int index) {
361
        Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN));
362
        if (value == null) {
363
            return false;
364
        }
365
        return value.booleanValue();
366
    }
367

    
368
    public byte getByte(String name) {
369
        return this.getByte(this.data.getType().getIndex(name));
370
    }
371

    
372
    public byte getByte(int index) {
373
        Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE));
374
        if (value == null) {
375
            return 0;
376
        }
377
        return value.byteValue();
378
    }
379

    
380
    public Date getDate(String name) {
381
        return this.getDate(this.data.getType().getIndex(name));
382
    }
383

    
384
    public Date getDate(int index) {
385
        Date value = ((Date) this.get(index,Date.class,DataTypes.DATE));
386

    
387
        return value;
388
    }
389

    
390
    public double getDouble(String name) {
391
        return this.getDouble(this.data.getType().getIndex(name));
392
    }
393

    
394
    public double getDouble(int index) {
395
        
396
        Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE));
397
        if (value == null) {
398
            return 0;
399
        }
400
        return value.doubleValue();
401
    }
402

    
403
    public Feature getFeature(String name) {
404
        return this.getFeature(this.data.getType().getIndex(name));
405
    }
406

    
407
    public Feature getFeature(int index) {
408
        return (Feature) this.get(index);
409
    }
410

    
411
    public float getFloat(String name) {
412
        return this.getFloat(this.data.getType().getIndex(name));
413
    }
414

    
415
    public float getFloat(int index) {
416
        Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT));
417
        if (value == null) {
418
            return 0;
419
        }
420
        return value.floatValue();
421
    }
422

    
423
    public Geometry getGeometry(String name) {
424
        return this.getGeometry(this.data.getType().getIndex(name));
425
    }
426

    
427
    public Geometry getGeometry(int index) {
428
        return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY);
429
    }
430

    
431
    public int getInt(String name) {
432
        return this.getInt(this.data.getType().getIndex(name));
433
    }
434

    
435
    public int getInt(int index) {
436
        Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT));
437
        if (value == null) {
438
            return 0;
439
        }
440
        return ((Integer)value).intValue();
441
    }
442

    
443
    public long getLong(String name) {
444
        return this.getLong(this.data.getType().getIndex(name));
445
    }
446

    
447
    public long getLong(int index) {
448
        Long value = ((Long) this.get(index,Long.class,DataTypes.LONG));
449
        if (value == null) {
450
            return 0;
451
        }
452
        return value.longValue();
453
    }
454

    
455
    public String getString(String name) {
456
        return this.getString(this.data.getType().getIndex(name));
457
    }
458

    
459
    public String getString(int index) {
460
        return (String) this.get(index,String.class,DataTypes.STRING);
461
    }
462

    
463
    public Object getContextValue(String name) {
464
        name = name.toLowerCase();
465
        if (name.equals("store")) {
466
            return this.getStore();
467
        }
468

    
469
        if (name.equals("featuretype")) {
470
            return this.data.getType();
471
        }
472

    
473
        if (name.equals("feature")) {
474
            return this;
475
        }
476

    
477
        throw new IllegalArgumentException(name);
478
    }
479

    
480
    public Iterator getDataNames() {
481
        class DataNamesIterator implements Iterator {
482
            Iterator attributeIteraror;
483

    
484
            DataNamesIterator(DefaultFeature feature) {
485
                this.attributeIteraror = feature.getType().iterator();
486
            }
487

    
488
            public boolean hasNext() {
489
                return this.attributeIteraror.hasNext();
490
            }
491

    
492
            public Object next() {
493
                return ((FeatureAttributeDescriptor) this.attributeIteraror
494
                        .next()).getName();
495
            }
496

    
497
            public void remove() {
498
                throw new UnsupportedOperationException();
499
            }
500

    
501
        }
502
        return new DataNamesIterator(this);
503
    }
504

    
505
    public Object getDataValue(String name) {
506
        name = name.toLowerCase();
507
        return get(name);
508
    }
509

    
510
    public Iterator getDataValues() {
511
        class DataValuesIterator implements Iterator {
512
            DefaultFeature feature;
513
            int current = 0;
514

    
515
            DataValuesIterator(DefaultFeature feature) {
516
                this.feature = feature;
517
            }
518

    
519
            public boolean hasNext() {
520
                return current < feature.getType().size() - 1;
521
            }
522

    
523
            public Object next() {
524
                return feature.get(current++);
525
            }
526

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

    
531
        }
532
        return new DataValuesIterator(this);
533
    }
534

    
535
    public boolean hasContextValue(String name) {
536
        name = name.toLowerCase();
537
        if (name.equals("store")) {
538
            return true;
539
        }
540

    
541
        if (name.equals("featuretype")) {
542
            return true;
543
        }
544

    
545
        if (name.equals("feature")) {
546
            return true;
547
        }
548
        return false;
549
    }
550

    
551
    public boolean hasDataValue(String name) {
552
        name = name.toLowerCase();
553
        return this.data.getType().getIndex(name) >= 0;
554
    }
555
    
556
    public Instant getInstant(int index) {
557
        return ((Instant) this.get(index,Date.class,DataTypes.INSTANT));
558
    }
559

    
560
    public Instant getInstant(String name) {
561
        return this.getInstant(this.data.getType().getIndex(name));
562
    }
563

    
564
    public Interval getInterval(int index) {
565
        return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL));
566
    }
567

    
568
    public Interval getInterval(String name) {
569
        return this.getInterval(this.data.getType().getIndex(name));
570
    }
571

    
572
    public DynObject getAsDynObject() {
573
        DynObjectFeatureFacade facade = new DynObjectFeatureFacade();
574
        facade.setFeature(this);
575
        return facade;
576
    }
577

    
578
    public String toString() {
579
        StringBuffer buffer = new StringBuffer("Feature with values: ");
580
        FeatureAttributeDescriptor[] attributeDescriptors =
581
            getType().getAttributeDescriptors();
582
        for (int i = 0; i < attributeDescriptors.length; i++) {
583
            String name = attributeDescriptors[i].getName();
584
            buffer.append(name).append("=").append(get(name));
585
            if (i < attributeDescriptors.length - 1) {
586
                buffer.append(", ");
587
            }
588
        }
589
        return super.toString();
590
    }
591
    
592
    
593
    /**
594
     * @return the inserted
595
     */
596
    public boolean isInserted() {
597
        return inserted;
598
    }
599

    
600
    
601
    /**
602
     * @param inserted the inserted to set
603
     */
604
    public void setInserted(boolean inserted) {
605
        this.inserted = inserted;
606
    }
607

    
608
    public EvaluatorData getEvaluatorData() {
609
        return this;
610
    }
611
}