Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureAttributeDescriptor.java @ 37603

History | View | Annotate | Download (16.1 KB)

1 24496 jmvivo
package org.gvsig.fmap.dal.feature.impl;
2 23754 jjdelcerro
3
import java.text.DateFormat;
4 27439 jmvivo
import java.util.HashMap;
5
import java.util.Iterator;
6 32880 jjdelcerro
import java.util.List;
7 27439 jmvivo
import java.util.Map;
8
import java.util.Map.Entry;
9 23754 jjdelcerro
10 26717 jmvivo
import org.cresques.cts.IProjection;
11 33389 cordinyana
12 30208 jmvivo
import org.gvsig.fmap.crs.CRSFactory;
13 24496 jmvivo
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
14 37297 jpiera
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
15 23754 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
16 37328 cordinyana
import org.gvsig.fmap.geom.GeometryException;
17
import org.gvsig.fmap.geom.GeometryLocator;
18
import org.gvsig.fmap.geom.type.GeometryType;
19 32880 jjdelcerro
import org.gvsig.tools.ToolsLocator;
20 33331 jjdelcerro
import org.gvsig.tools.dataTypes.DataType;
21 33345 cordinyana
import org.gvsig.tools.dataTypes.DataTypes;
22 30187 jcarrasco
import org.gvsig.tools.dynobject.DynField;
23
import org.gvsig.tools.dynobject.DynObjectValueItem;
24 32880 jjdelcerro
import org.gvsig.tools.dynobject.DynStruct;
25
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
26
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
27 23754 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
28 30208 jmvivo
import org.gvsig.tools.persistence.Persistent;
29
import org.gvsig.tools.persistence.PersistentState;
30 32880 jjdelcerro
import org.gvsig.tools.persistence.exception.PersistenceException;
31 23754 jjdelcerro
32
public class DefaultFeatureAttributeDescriptor implements
33 37328 cordinyana
    FeatureAttributeDescriptor, Persistent, DynField {
34 23754 jjdelcerro
35 37328 cordinyana
    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 37297 jpiera
        protected boolean isTime = false;
56
        protected FeatureAttributeGetter featureAttributeGetter = null;
57 27439 jmvivo
58 37328 cordinyana
    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 27439 jmvivo
80 37328 cordinyana
    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 37297 jpiera
                this.isTime = other.isTime;
113
                this.featureAttributeGetter = other.featureAttributeGetter;
114 37328 cordinyana
    }
115 23754 jjdelcerro
116 37328 cordinyana
    public String getDataTypeName() {
117 37297 jpiera
                if( this.getDataType() == null ) {
118 37328 cordinyana
            return "(unknow)";
119
        }
120 37297 jpiera
                return this.getDataType().getName();
121 37328 cordinyana
    }
122 23754 jjdelcerro
123 37328 cordinyana
    public FeatureAttributeDescriptor getCopy() {
124
        return new DefaultFeatureAttributeDescriptor(this);
125
    }
126 23754 jjdelcerro
127 37328 cordinyana
    public boolean allowNull() {
128
        return allowNull;
129
    }
130 23754 jjdelcerro
131 37328 cordinyana
    public DataType getDataType() {
132 37297 jpiera
                if (featureAttributeGetter != null){
133
                    return featureAttributeGetter.getDataType();
134
                }
135 37328 cordinyana
        return this.dataType;
136
    }
137 23754 jjdelcerro
138 37328 cordinyana
    public DateFormat getDateFormat() {
139
        return this.dateFormat;
140
    }
141 23754 jjdelcerro
142 37328 cordinyana
    public Object getDefaultValue() {
143
        return this.defaultValue;
144
    }
145 23754 jjdelcerro
146 37328 cordinyana
    public Evaluator getEvaluator() {
147
        return this.evaluator;
148
    }
149 23754 jjdelcerro
150 37328 cordinyana
    public int getGeometryType() {
151
        return this.geometryType;
152
    }
153 23754 jjdelcerro
154 37328 cordinyana
    public int getGeometrySubType() {
155
        return this.geometrySubType;
156
    }
157 26911 jpiera
158 37328 cordinyana
    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 23754 jjdelcerro
174 37328 cordinyana
    public int getIndex() {
175
        return this.index;
176
    }
177 25917 jmvivo
178 37328 cordinyana
    protected FeatureAttributeDescriptor setIndex(int index) {
179
        this.index = index;
180
        return this;
181
    }
182 23754 jjdelcerro
183 37328 cordinyana
    public int getMaximumOccurrences() {
184
        return this.maximumOccurrences;
185
    }
186 23754 jjdelcerro
187 37328 cordinyana
    public int getMinimumOccurrences() {
188
        return this.minimumOccurrences;
189
    }
190 23754 jjdelcerro
191 37328 cordinyana
    public String getName() {
192
        return this.name;
193
    }
194
195
    public Class getObjectClass() {
196 37297 jpiera
                if (getDataType().getType() == DataTypes.OBJECT) {
197 37328 cordinyana
            return objectClass;
198
        }
199 37297 jpiera
                return getDataType().getDefaultClass();
200 37328 cordinyana
    }
201 23754 jjdelcerro
202 37328 cordinyana
    public int getPrecision() {
203
        return this.precision;
204
    }
205 23754 jjdelcerro
206 37328 cordinyana
    public IProjection getSRS() {
207
        return this.SRS;
208
    }
209 23754 jjdelcerro
210 37328 cordinyana
    public int getSize() {
211
        return this.size;
212
    }
213 23754 jjdelcerro
214 37328 cordinyana
    public boolean isPrimaryKey() {
215
        return this.primaryKey;
216
    }
217 23754 jjdelcerro
218 37328 cordinyana
    public boolean isReadOnly() {
219
        return this.readOnly;
220
    }
221 23754 jjdelcerro
222 37328 cordinyana
    public Object getAdditionalInfo(String infoName) {
223
        if (this.additionalInfo == null) {
224
            return null;
225
        }
226
        return this.additionalInfo.get(infoName);
227
    }
228 27439 jmvivo
229 37328 cordinyana
    public boolean isAutomatic() {
230
        return this.isAutomatic;
231
    }
232 27439 jmvivo
233 37328 cordinyana
    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 30187 jcarrasco
242 37328 cordinyana
    }
243 29033 jmvivo
244 37328 cordinyana
    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 29033 jmvivo
254 37328 cordinyana
        if (this.allowNull != other.allowNull) {
255
            return false;
256
        }
257 29033 jmvivo
258 37328 cordinyana
        if (this.index != other.index) {
259
            return false;
260
        }
261 29033 jmvivo
262 37328 cordinyana
        if (!compareObject(this.name, other.name)) {
263
            return false;
264
        }
265 29033 jmvivo
266 37297 jpiera
                if (this.getDataType() != other.getDataType()) {
267 37328 cordinyana
            return false;
268
        }
269 29033 jmvivo
270 37328 cordinyana
        if (this.size != other.size) {
271
            return false;
272
        }
273 29033 jmvivo
274 37328 cordinyana
        if (!compareObject(this.defaultValue, other.defaultValue)) {
275
            return false;
276
        }
277 29033 jmvivo
278 37328 cordinyana
        if (!compareObject(this.defaultValue, other.defaultValue)) {
279
            return false;
280
        }
281 29033 jmvivo
282 37328 cordinyana
        if (this.primaryKey != other.primaryKey) {
283
            return false;
284
        }
285 29033 jmvivo
286 37328 cordinyana
        if (this.isAutomatic != other.isAutomatic) {
287
            return false;
288
        }
289 29033 jmvivo
290 37328 cordinyana
        if (this.readOnly != other.readOnly) {
291
            return false;
292
        }
293 29033 jmvivo
294 37328 cordinyana
        if (this.precision != other.precision) {
295
            return false;
296
        }
297 29033 jmvivo
298 37328 cordinyana
        if (this.maximumOccurrences != other.maximumOccurrences) {
299
            return false;
300
        }
301 29033 jmvivo
302 37328 cordinyana
        if (this.minimumOccurrences != other.minimumOccurrences) {
303
            return false;
304
        }
305 29033 jmvivo
306 37328 cordinyana
        if (this.geometryType != other.geometryType) {
307
            return false;
308
        }
309 29033 jmvivo
310 37328 cordinyana
        if (this.geometrySubType != other.geometrySubType) {
311
            return false;
312
        }
313 29033 jmvivo
314 37328 cordinyana
        if (!compareObject(this.evaluator, other.evaluator)) {
315
            return false;
316
        }
317 29033 jmvivo
318 37328 cordinyana
        if (!compareObject(this.SRS, other.SRS)) {
319
            return false;
320
        }
321 29033 jmvivo
322 37328 cordinyana
        if (!compareObject(this.dateFormat, other.dateFormat)) {
323
            return false;
324
        }
325 29033 jmvivo
326 37328 cordinyana
        if (!compareObject(this.objectClass, other.objectClass)) {
327
            return false;
328
        }
329 30187 jcarrasco
330 37328 cordinyana
        return true;
331
    }
332 30208 jmvivo
333 37328 cordinyana
    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 30208 jmvivo
342 37328 cordinyana
        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 30208 jmvivo
366 37328 cordinyana
    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 30208 jmvivo
372 37328 cordinyana
        defaultValue = state.get("defaultValue");
373 30208 jmvivo
374 37328 cordinyana
        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 30550 jmvivo
398 37328 cordinyana
    /**
399
     * Start of DynField interface Implementation
400
     * READONLY
401
     */
402 30550 jmvivo
403 37328 cordinyana
    public DynObjectValueItem[] getAvailableValues() {
404
        return null;
405
    }
406 30187 jcarrasco
407 37328 cordinyana
    public String getDescription() {
408 33389 cordinyana
        return getName();
409 37328 cordinyana
    }
410 30187 jcarrasco
411 37328 cordinyana
    public Object getMaxValue() {
412
        return null;
413
    }
414 30187 jcarrasco
415 37328 cordinyana
    public Object getMinValue() {
416
        return null;
417
    }
418 30187 jcarrasco
419 37328 cordinyana
    public int getTheTypeOfAvailableValues() {
420
        return 0;
421
    }
422 30187 jcarrasco
423 37328 cordinyana
    public int getType() {
424 37297 jpiera
            if (featureAttributeGetter != null){
425
            return featureAttributeGetter.getDataType().getType();
426
        }
427 37328 cordinyana
        return getDataType().getType();
428
    }
429 30187 jcarrasco
430 37328 cordinyana
    public boolean isMandatory() {
431
        return !allowNull() || isPrimaryKey();
432
    }
433 30187 jcarrasco
434 37328 cordinyana
    public boolean isPersistent() {
435
        return false;
436
    }
437 30187 jcarrasco
438 37328 cordinyana
    public DynField setAvailableValues(DynObjectValueItem[] values) {
439
        throw new UnsupportedOperationException();
440
    }
441 30187 jcarrasco
442 37328 cordinyana
    public DynField setDescription(String description) {
443
        throw new UnsupportedOperationException();
444
    }
445 30187 jcarrasco
446 37328 cordinyana
    public DynField setMandatory(boolean mandatory) {
447
        throw new UnsupportedOperationException();
448
    }
449 30187 jcarrasco
450 37328 cordinyana
    public DynField setMaxValue(Object maxValue) {
451
        throw new UnsupportedOperationException();
452
    }
453 30187 jcarrasco
454 37328 cordinyana
    public DynField setMinValue(Object minValue) {
455
        throw new UnsupportedOperationException();
456
    }
457 30187 jcarrasco
458 37328 cordinyana
    public DynField setPersistent(boolean persistent) {
459
        throw new UnsupportedOperationException();
460
    }
461 30187 jcarrasco
462 37328 cordinyana
    public DynField setTheTypeOfAvailableValues(int type) {
463
        throw new UnsupportedOperationException();
464
    }
465 30187 jcarrasco
466 37328 cordinyana
    public DynField setType(int type) {
467
        throw new UnsupportedOperationException();
468
    }
469 30187 jcarrasco
470 37328 cordinyana
    public DynField setDefaultDynValue(Object defaultValue) {
471
        throw new UnsupportedOperationException();
472
    }
473 30187 jcarrasco
474 37328 cordinyana
    public DynField addElementsType() throws DynFieldIsNotAContainerException {
475
        throw new UnsupportedOperationException();
476
    }
477 32880 jjdelcerro
478 37328 cordinyana
    public Class getClassOfValue() {
479
        return null;
480
    }
481 32880 jjdelcerro
482 37328 cordinyana
    public DynField getElementsType() {
483
        return null;
484
    }
485 32880 jjdelcerro
486 37328 cordinyana
    public DynField setClassOfValue(Class theClass)
487
        throws DynFieldIsNotAContainerException {
488
        throw new UnsupportedOperationException();
489
    }
490 32880 jjdelcerro
491 37328 cordinyana
    public DynField setElementsType(DynStruct type)
492
        throws DynFieldIsNotAContainerException {
493
        throw new UnsupportedOperationException();
494
    }
495 32880 jjdelcerro
496 37328 cordinyana
    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 37297 jpiera
            if (featureAttributeGetter != null){
511
            return featureAttributeGetter.getDataType().getSubtype();
512
        }
513 37328 cordinyana
        return this.dataType.getSubtype();
514
    }
515 32880 jjdelcerro
516 37328 cordinyana
    public Object coerce(Object value) {
517
        throw new UnsupportedOperationException();
518
    }
519 32880 jjdelcerro
520 37328 cordinyana
    public DynField setAvailableValues(List values) {
521
        throw new UnsupportedOperationException();
522
    }
523 32880 jjdelcerro
524 37328 cordinyana
    public String getGroup() {
525
        return null;
526
    }
527 32880 jjdelcerro
528 37328 cordinyana
    public int getOder() {
529
        return 0;
530
    }
531 32880 jjdelcerro
532 37328 cordinyana
    public DynField setGroup(String groupName) {
533
        throw new UnsupportedOperationException();
534
    }
535 32880 jjdelcerro
536 37328 cordinyana
    public DynField setOrder(int order) {
537
        throw new UnsupportedOperationException();
538
    }
539 32880 jjdelcerro
540 37328 cordinyana
    public DynField setHidden(boolean hidden) {
541
        throw new UnsupportedOperationException();
542
    }
543 32880 jjdelcerro
544 37328 cordinyana
    public boolean isHidden() {
545
        return false;
546
    }
547 32880 jjdelcerro
548 37328 cordinyana
    public DynField setReadOnly(boolean arg0) {
549
        throw new UnsupportedOperationException();
550
    }
551 33281 jjdelcerro
552 37328 cordinyana
    public boolean isContainer() {
553
        return false;
554
    }
555 33281 jjdelcerro
556 37328 cordinyana
    public Class getClassOfItems() {
557
        return null;
558
    }
559 33281 jjdelcerro
560 37328 cordinyana
    public DynField setDefaultFieldValue(Object defaultValue) {
561
        throw new UnsupportedOperationException();
562
    }
563 33281 jjdelcerro
564 37328 cordinyana
    public DynField setClassOfItems(Class theClass) {
565
        throw new UnsupportedOperationException();
566
    }
567 33331 jjdelcerro
568 37328 cordinyana
    public DynField setType(DataType type) {
569
        throw new UnsupportedOperationException();
570
    }
571 37297 jpiera
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 23754 jjdelcerro
}