Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureAttributeDescriptor.java @ 37328

History | View | Annotate | Download (16.1 KB)

1
package org.gvsig.fmap.dal.feature.impl;
2

    
3
import java.text.DateFormat;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Map.Entry;
9

    
10
import org.cresques.cts.IProjection;
11

    
12
import org.gvsig.fmap.crs.CRSFactory;
13
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
15
import org.gvsig.fmap.geom.Geometry;
16
import org.gvsig.fmap.geom.GeometryException;
17
import org.gvsig.fmap.geom.GeometryLocator;
18
import org.gvsig.fmap.geom.type.GeometryType;
19
import org.gvsig.tools.ToolsLocator;
20
import org.gvsig.tools.dataTypes.DataType;
21
import org.gvsig.tools.dataTypes.DataTypes;
22
import org.gvsig.tools.dynobject.DynField;
23
import org.gvsig.tools.dynobject.DynObjectValueItem;
24
import org.gvsig.tools.dynobject.DynStruct;
25
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
26
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
27
import org.gvsig.tools.evaluator.Evaluator;
28
import org.gvsig.tools.persistence.Persistent;
29
import org.gvsig.tools.persistence.PersistentState;
30
import org.gvsig.tools.persistence.exception.PersistenceException;
31

    
32
public class DefaultFeatureAttributeDescriptor implements
33
    FeatureAttributeDescriptor, Persistent, DynField {
34

    
35
    protected boolean allowNull;
36
    protected DataType dataType;
37
    protected DateFormat dateFormat;
38
    protected Object defaultValue;
39
    protected int index;
40
    protected int maximumOccurrences;
41
    protected int minimumOccurrences;
42
    protected int size;
43
    protected String name;
44
    protected Class objectClass;
45
    protected int precision;
46
    protected Evaluator evaluator;
47
    protected boolean primaryKey;
48
    protected boolean readOnly;
49
    protected IProjection SRS;
50
    protected GeometryType geomType;
51
    protected int geometryType;
52
    protected int geometrySubType;
53
    protected Map additionalInfo;
54
    protected boolean isAutomatic;
55
        protected boolean isTime = false;
56
        protected FeatureAttributeGetter featureAttributeGetter = null;
57

    
58
    protected DefaultFeatureAttributeDescriptor() {
59
        this.allowNull = true;
60
        this.dataType = null;
61
        this.dateFormat = null;
62
        this.defaultValue = null;
63
        this.index = -1;
64
        this.maximumOccurrences = 0;
65
        this.minimumOccurrences = 0;
66
        this.size = 0;
67
        this.name = null;
68
        this.objectClass = null;
69
        this.precision = 0;
70
        this.evaluator = null;
71
        this.primaryKey = false;
72
        this.readOnly = false;
73
        this.SRS = null;
74
        this.geometryType = Geometry.TYPES.NULL;
75
        this.geometrySubType = Geometry.SUBTYPES.UNKNOWN;
76
        this.additionalInfo = null;
77
        this.isAutomatic = false;
78
    }
79

    
80
    protected DefaultFeatureAttributeDescriptor(
81
        DefaultFeatureAttributeDescriptor other) {
82
        this.allowNull = other.allowNull;
83
        this.dataType = other.dataType;
84
        this.dateFormat = other.dateFormat;
85
        this.defaultValue = other.defaultValue;
86
        this.index = other.index;
87
        this.maximumOccurrences = other.maximumOccurrences;
88
        this.minimumOccurrences = other.minimumOccurrences;
89
        this.size = other.size;
90
        this.name = other.name;
91
        this.objectClass = other.objectClass;
92
        this.precision = other.precision;
93
        this.evaluator = other.evaluator;
94
        this.primaryKey = other.primaryKey;
95
        this.readOnly = other.readOnly;
96
        this.SRS = other.SRS;
97
        this.geometryType = other.geometryType;
98
        this.geometrySubType = other.geometrySubType;
99
        this.geomType = other.geomType;
100
        if (other.additionalInfo != null) {
101
            Iterator iter = other.additionalInfo.entrySet().iterator();
102
            Map.Entry entry;
103
            this.additionalInfo = new HashMap();
104
            while (iter.hasNext()) {
105
                entry = (Entry) iter.next();
106
                this.additionalInfo.put(entry.getKey(), entry.getValue());
107
            }
108
        } else {
109
            this.additionalInfo = null;
110
        }
111
        this.isAutomatic = other.isAutomatic;
112
                this.isTime = other.isTime;
113
                this.featureAttributeGetter = other.featureAttributeGetter;
114
    }
115

    
116
    public String getDataTypeName() {
117
                if( this.getDataType() == null ) {
118
            return "(unknow)";
119
        }
120
                return this.getDataType().getName();
121
    }
122

    
123
    public FeatureAttributeDescriptor getCopy() {
124
        return new DefaultFeatureAttributeDescriptor(this);
125
    }
126

    
127
    public boolean allowNull() {
128
        return allowNull;
129
    }
130

    
131
    public DataType getDataType() {
132
                if (featureAttributeGetter != null){
133
                    return featureAttributeGetter.getDataType();
134
                }
135
        return this.dataType;
136
    }
137

    
138
    public DateFormat getDateFormat() {
139
        return this.dateFormat;
140
    }
141

    
142
    public Object getDefaultValue() {
143
        return this.defaultValue;
144
    }
145

    
146
    public Evaluator getEvaluator() {
147
        return this.evaluator;
148
    }
149

    
150
    public int getGeometryType() {
151
        return this.geometryType;
152
    }
153

    
154
    public int getGeometrySubType() {
155
        return this.geometrySubType;
156
    }
157

    
158
    public GeometryType getGeomType() {
159
        if (this.geomType == null) {
160
            try {
161
                this.geomType =
162
                    GeometryLocator.getGeometryManager().getGeometryType(
163
                        this.geometryType, this.geometrySubType);
164
            } catch (GeometryException e) {
165
                throw new RuntimeException(
166
                    "Error getting geometry type with type = "
167
                        + Geometry.TYPES.NULL + ", subtype = "
168
                        + Geometry.SUBTYPES.GEOM2D, e);
169
            }
170
        }
171
        return this.geomType;
172
    }
173

    
174
    public int getIndex() {
175
        return this.index;
176
    }
177

    
178
    protected FeatureAttributeDescriptor setIndex(int index) {
179
        this.index = index;
180
        return this;
181
    }
182

    
183
    public int getMaximumOccurrences() {
184
        return this.maximumOccurrences;
185
    }
186

    
187
    public int getMinimumOccurrences() {
188
        return this.minimumOccurrences;
189
    }
190

    
191
    public String getName() {
192
        return this.name;
193
    }
194

    
195
    public Class getObjectClass() {
196
                if (getDataType().getType() == DataTypes.OBJECT) {
197
            return objectClass;
198
        }
199
                return getDataType().getDefaultClass();
200
    }
201

    
202
    public int getPrecision() {
203
        return this.precision;
204
    }
205

    
206
    public IProjection getSRS() {
207
        return this.SRS;
208
    }
209

    
210
    public int getSize() {
211
        return this.size;
212
    }
213

    
214
    public boolean isPrimaryKey() {
215
        return this.primaryKey;
216
    }
217

    
218
    public boolean isReadOnly() {
219
        return this.readOnly;
220
    }
221

    
222
    public Object getAdditionalInfo(String infoName) {
223
        if (this.additionalInfo == null) {
224
            return null;
225
        }
226
        return this.additionalInfo.get(infoName);
227
    }
228

    
229
    public boolean isAutomatic() {
230
        return this.isAutomatic;
231
    }
232

    
233
    private boolean compareObject(Object a, Object b) {
234
        if (a != b) {
235
            if (a == null) {
236
                return false;
237
            }
238
            return a.equals(b);
239
        }
240
        return true;
241

    
242
    }
243

    
244
    public boolean equals(Object obj) {
245
        if (this == obj) {
246
            return true;
247
        }
248
        if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
249
            return false;
250
        }
251
        DefaultFeatureAttributeDescriptor other =
252
            (DefaultFeatureAttributeDescriptor) obj;
253

    
254
        if (this.allowNull != other.allowNull) {
255
            return false;
256
        }
257

    
258
        if (this.index != other.index) {
259
            return false;
260
        }
261

    
262
        if (!compareObject(this.name, other.name)) {
263
            return false;
264
        }
265

    
266
                if (this.getDataType() != other.getDataType()) {
267
            return false;
268
        }
269

    
270
        if (this.size != other.size) {
271
            return false;
272
        }
273

    
274
        if (!compareObject(this.defaultValue, other.defaultValue)) {
275
            return false;
276
        }
277

    
278
        if (!compareObject(this.defaultValue, other.defaultValue)) {
279
            return false;
280
        }
281

    
282
        if (this.primaryKey != other.primaryKey) {
283
            return false;
284
        }
285

    
286
        if (this.isAutomatic != other.isAutomatic) {
287
            return false;
288
        }
289

    
290
        if (this.readOnly != other.readOnly) {
291
            return false;
292
        }
293

    
294
        if (this.precision != other.precision) {
295
            return false;
296
        }
297

    
298
        if (this.maximumOccurrences != other.maximumOccurrences) {
299
            return false;
300
        }
301

    
302
        if (this.minimumOccurrences != other.minimumOccurrences) {
303
            return false;
304
        }
305

    
306
        if (this.geometryType != other.geometryType) {
307
            return false;
308
        }
309

    
310
        if (this.geometrySubType != other.geometrySubType) {
311
            return false;
312
        }
313

    
314
        if (!compareObject(this.evaluator, other.evaluator)) {
315
            return false;
316
        }
317

    
318
        if (!compareObject(this.SRS, other.SRS)) {
319
            return false;
320
        }
321

    
322
        if (!compareObject(this.dateFormat, other.dateFormat)) {
323
            return false;
324
        }
325

    
326
        if (!compareObject(this.objectClass, other.objectClass)) {
327
            return false;
328
        }
329

    
330
        return true;
331
    }
332

    
333
    public void loadFromState(PersistentState state)
334
        throws PersistenceException {
335
        allowNull = state.getBoolean("allowNull");
336
        dataType =
337
            ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
338
        // FIXME how persist dateFormat ???
339
        // dateFormat;
340
        defaultValue = state.get("defaultValue");
341

    
342
        index = state.getInt("index");
343
        maximumOccurrences = state.getInt("maximumOccurrences");
344
        minimumOccurrences = state.getInt("minimumOccurrences");
345
        size = state.getInt("size");
346
        name = state.getString("name");
347
        try {
348
            objectClass = Class.forName(state.getString("objectClass"));
349
        } catch (ClassNotFoundException e) {
350
            throw new PersistenceException(e);
351
        }
352
        precision = state.getInt("precision");
353
        evaluator = (Evaluator) state.get("evaluator");
354
        primaryKey = state.getBoolean("primaryKey");
355
        readOnly = state.getBoolean("readOnly");
356
        String srsId = state.getString("srsId");
357
        if (srsId != null) {
358
            SRS = CRSFactory.getCRS(srsId);
359
        }
360
        geometryType = state.getInt("geometryType");
361
        geometrySubType = state.getInt("geometrySubType");
362
        additionalInfo = (Map) state.get("aditionalInfo");
363
        isAutomatic = state.getBoolean("isAutomatic");
364
    }
365

    
366
    public void saveToState(PersistentState state) throws PersistenceException {
367
        state.set("allowNull", allowNull);
368
        state.set("dataType", dataType);
369
        // FIXME how persist dateFormat ???
370
        // dateFormat;
371

    
372
        defaultValue = state.get("defaultValue");
373

    
374
        index = state.getInt("index");
375
        maximumOccurrences = state.getInt("maximumOccurrences");
376
        minimumOccurrences = state.getInt("minimumOccurrences");
377
        size = state.getInt("size");
378
        name = state.getString("name");
379
        try {
380
            objectClass = Class.forName(state.getString("objectClass"));
381
        } catch (ClassNotFoundException e) {
382
            throw new PersistenceException(e);
383
        }
384
        precision = state.getInt("precision");
385
        evaluator = (Evaluator) state.get("evaluator");
386
        primaryKey = state.getBoolean("primaryKey");
387
        readOnly = state.getBoolean("readOnly");
388
        String srsId = state.getString("srsId");
389
        if (srsId != null) {
390
            SRS = CRSFactory.getCRS(srsId);
391
        }
392
        geometryType = state.getInt("geometryType");
393
        geometrySubType = state.getInt("geometrySubType");
394
        additionalInfo = (Map) state.get("aditionalInfo");
395
        isAutomatic = state.getBoolean("isAutomatic");
396
    }
397

    
398
    /**
399
     * Start of DynField interface Implementation
400
     * READONLY
401
     */
402

    
403
    public DynObjectValueItem[] getAvailableValues() {
404
        return null;
405
    }
406

    
407
    public String getDescription() {
408
        return getName();
409
    }
410

    
411
    public Object getMaxValue() {
412
        return null;
413
    }
414

    
415
    public Object getMinValue() {
416
        return null;
417
    }
418

    
419
    public int getTheTypeOfAvailableValues() {
420
        return 0;
421
    }
422

    
423
    public int getType() {
424
            if (featureAttributeGetter != null){
425
            return featureAttributeGetter.getDataType().getType();
426
        }
427
        return getDataType().getType();
428
    }
429

    
430
    public boolean isMandatory() {
431
        return !allowNull() || isPrimaryKey();
432
    }
433

    
434
    public boolean isPersistent() {
435
        return false;
436
    }
437

    
438
    public DynField setAvailableValues(DynObjectValueItem[] values) {
439
        throw new UnsupportedOperationException();
440
    }
441

    
442
    public DynField setDescription(String description) {
443
        throw new UnsupportedOperationException();
444
    }
445

    
446
    public DynField setMandatory(boolean mandatory) {
447
        throw new UnsupportedOperationException();
448
    }
449

    
450
    public DynField setMaxValue(Object maxValue) {
451
        throw new UnsupportedOperationException();
452
    }
453

    
454
    public DynField setMinValue(Object minValue) {
455
        throw new UnsupportedOperationException();
456
    }
457

    
458
    public DynField setPersistent(boolean persistent) {
459
        throw new UnsupportedOperationException();
460
    }
461

    
462
    public DynField setTheTypeOfAvailableValues(int type) {
463
        throw new UnsupportedOperationException();
464
    }
465

    
466
    public DynField setType(int type) {
467
        throw new UnsupportedOperationException();
468
    }
469

    
470
    public DynField setDefaultDynValue(Object defaultValue) {
471
        throw new UnsupportedOperationException();
472
    }
473

    
474
    public DynField addElementsType() throws DynFieldIsNotAContainerException {
475
        throw new UnsupportedOperationException();
476
    }
477

    
478
    public Class getClassOfValue() {
479
        return null;
480
    }
481

    
482
    public DynField getElementsType() {
483
        return null;
484
    }
485

    
486
    public DynField setClassOfValue(Class theClass)
487
        throws DynFieldIsNotAContainerException {
488
        throw new UnsupportedOperationException();
489
    }
490

    
491
    public DynField setElementsType(DynStruct type)
492
        throws DynFieldIsNotAContainerException {
493
        throw new UnsupportedOperationException();
494
    }
495

    
496
    public DynField setElementsType(int type)
497
        throws DynFieldIsNotAContainerException {
498
        throw new UnsupportedOperationException();
499
    }
500

    
501
    public DynField setSubtype(String subtype) {
502
        throw new UnsupportedOperationException();
503
    }
504

    
505
    public void validate(Object value) throws DynFieldValidateException {
506
        // Do nothing
507
    }
508

    
509
    public String getSubtype() {
510
            if (featureAttributeGetter != null){
511
            return featureAttributeGetter.getDataType().getSubtype();
512
        }       
513
        return this.dataType.getSubtype();
514
    }
515

    
516
    public Object coerce(Object value) {
517
        throw new UnsupportedOperationException();
518
    }
519

    
520
    public DynField setAvailableValues(List values) {
521
        throw new UnsupportedOperationException();
522
    }
523

    
524
    public String getGroup() {
525
        return null;
526
    }
527

    
528
    public int getOder() {
529
        return 0;
530
    }
531

    
532
    public DynField setGroup(String groupName) {
533
        throw new UnsupportedOperationException();
534
    }
535

    
536
    public DynField setOrder(int order) {
537
        throw new UnsupportedOperationException();
538
    }
539

    
540
    public DynField setHidden(boolean hidden) {
541
        throw new UnsupportedOperationException();
542
    }
543

    
544
    public boolean isHidden() {
545
        return false;
546
    }
547

    
548
    public DynField setReadOnly(boolean arg0) {
549
        throw new UnsupportedOperationException();
550
    }
551

    
552
    public boolean isContainer() {
553
        return false;
554
    }
555

    
556
    public Class getClassOfItems() {
557
        return null;
558
    }
559

    
560
    public DynField setDefaultFieldValue(Object defaultValue) {
561
        throw new UnsupportedOperationException();
562
    }
563

    
564
    public DynField setClassOfItems(Class theClass) {
565
        throw new UnsupportedOperationException();
566
    }
567

    
568
    public DynField setType(DataType type) {
569
        throw new UnsupportedOperationException();
570
    }
571

    
572
        public boolean isTime() {                
573
                return isTime;
574
        }
575

    
576
    public FeatureAttributeGetter getFeatureAttributeGetter() {
577
       return featureAttributeGetter;        
578
    }
579

    
580
    public void setFeatureAttributeGetter(
581
        FeatureAttributeGetter featureAttributeTransform) {
582
        this.featureAttributeGetter = featureAttributeTransform;              
583
    }        
584
}