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 / DefaultFeatureAttributeDescriptor.java @ 40435

History | View | Annotate | Download (16.5 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.CoercionException;
21
import org.gvsig.tools.dataTypes.DataType;
22
import org.gvsig.tools.dataTypes.DataTypes;
23
import org.gvsig.tools.dynobject.DynField;
24
import org.gvsig.tools.dynobject.DynObjectValueItem;
25
import org.gvsig.tools.dynobject.DynStruct;
26
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
27
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
28
import org.gvsig.tools.evaluator.Evaluator;
29
import org.gvsig.tools.persistence.Persistent;
30
import org.gvsig.tools.persistence.PersistentState;
31
import org.gvsig.tools.persistence.exception.PersistenceException;
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
243
    }
244

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
331
        return true;
332
    }
333

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
506
    public void validate(Object value) throws DynFieldValidateException {
507
        
508
        if (value == null && !this.allowNull()) {
509
            throw new DynFieldValidateException(value, this, null);
510
        }
511
        
512
        try {
513
            this.dataType.coerce(value);
514
        } catch (CoercionException e) {
515
            throw new DynFieldValidateException(value, this, e);
516
        }
517
        
518
        /*
519
         * Other checks will be needed
520
         */
521
    }
522

    
523
    public String getSubtype() {
524
            if (featureAttributeGetter != null){
525
            return featureAttributeGetter.getDataType().getSubtype();
526
        }       
527
        return this.dataType.getSubtype();
528
    }
529

    
530
    public Object coerce(Object value) {
531
        throw new UnsupportedOperationException();
532
    }
533

    
534
    public DynField setAvailableValues(List values) {
535
        throw new UnsupportedOperationException();
536
    }
537

    
538
    public String getGroup() {
539
        return null;
540
    }
541

    
542
    public int getOder() {
543
        return 0;
544
    }
545

    
546
    public DynField setGroup(String groupName) {
547
        throw new UnsupportedOperationException();
548
    }
549

    
550
    public DynField setOrder(int order) {
551
        throw new UnsupportedOperationException();
552
    }
553

    
554
    public DynField setHidden(boolean hidden) {
555
        throw new UnsupportedOperationException();
556
    }
557

    
558
    public boolean isHidden() {
559
        return false;
560
    }
561

    
562
    public DynField setReadOnly(boolean arg0) {
563
        throw new UnsupportedOperationException();
564
    }
565

    
566
    public boolean isContainer() {
567
        return false;
568
    }
569

    
570
    public Class getClassOfItems() {
571
        return null;
572
    }
573

    
574
    public DynField setDefaultFieldValue(Object defaultValue) {
575
        throw new UnsupportedOperationException();
576
    }
577

    
578
    public DynField setClassOfItems(Class theClass) {
579
        throw new UnsupportedOperationException();
580
    }
581

    
582
    public DynField setType(DataType type) {
583
        throw new UnsupportedOperationException();
584
    }
585

    
586
        public boolean isTime() {                
587
                return isTime;
588
        }
589

    
590
    public FeatureAttributeGetter getFeatureAttributeGetter() {
591
       return featureAttributeGetter;        
592
    }
593

    
594
    public void setFeatureAttributeGetter(
595
        FeatureAttributeGetter featureAttributeTransform) {
596
        this.featureAttributeGetter = featureAttributeTransform;              
597
    }        
598
}