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

History | View | Annotate | Download (17.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.text.DateFormat;
27
import java.util.HashMap;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.Map;
31
import java.util.Map.Entry;
32

    
33
import org.cresques.cts.IProjection;
34

    
35
import org.gvsig.fmap.crs.CRSFactory;
36
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryException;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.type.GeometryType;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dataTypes.CoercionException;
44
import org.gvsig.tools.dataTypes.DataType;
45
import org.gvsig.tools.dataTypes.DataTypes;
46
import org.gvsig.tools.dynobject.DynField;
47
import org.gvsig.tools.dynobject.DynObjectValueItem;
48
import org.gvsig.tools.dynobject.DynStruct;
49
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
50
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
51
import org.gvsig.tools.evaluator.Evaluator;
52
import org.gvsig.tools.persistence.Persistent;
53
import org.gvsig.tools.persistence.PersistentState;
54
import org.gvsig.tools.persistence.exception.PersistenceException;
55

    
56
public class DefaultFeatureAttributeDescriptor implements
57
    FeatureAttributeDescriptor, Persistent, DynField {
58

    
59
    protected boolean allowNull;
60
    protected DataType dataType;
61
    protected DateFormat dateFormat;
62
    protected Object defaultValue;
63
    protected int index;
64
    protected int maximumOccurrences;
65
    protected int minimumOccurrences;
66
    protected int size;
67
    protected String name;
68
    protected Class objectClass;
69
    protected int precision;
70
    protected Evaluator evaluator;
71
    protected boolean primaryKey;
72
    protected boolean readOnly;
73
    protected IProjection SRS;
74
    protected GeometryType geomType;
75
    protected int geometryType;
76
    protected int geometrySubType;
77
    protected Map additionalInfo;
78
    protected boolean isAutomatic;
79
        protected boolean isTime = false;
80
        protected FeatureAttributeGetter featureAttributeGetter = null;
81

    
82
    protected DefaultFeatureAttributeDescriptor() {
83
        this.allowNull = true;
84
        this.dataType = null;
85
        this.dateFormat = null;
86
        this.defaultValue = null;
87
        this.index = -1;
88
        this.maximumOccurrences = 0;
89
        this.minimumOccurrences = 0;
90
        this.size = 0;
91
        this.name = null;
92
        this.objectClass = null;
93
        this.precision = 0;
94
        this.evaluator = null;
95
        this.primaryKey = false;
96
        this.readOnly = false;
97
        this.SRS = null;
98
        this.geometryType = Geometry.TYPES.NULL;
99
        this.geometrySubType = Geometry.SUBTYPES.UNKNOWN;
100
        this.additionalInfo = null;
101
        this.isAutomatic = false;
102
    }
103

    
104
    protected DefaultFeatureAttributeDescriptor(
105
        DefaultFeatureAttributeDescriptor other) {
106
        this.allowNull = other.allowNull;
107
        this.dataType = other.dataType;
108
        this.dateFormat = other.dateFormat;
109
        this.defaultValue = other.defaultValue;
110
        this.index = other.index;
111
        this.maximumOccurrences = other.maximumOccurrences;
112
        this.minimumOccurrences = other.minimumOccurrences;
113
        this.size = other.size;
114
        this.name = other.name;
115
        this.objectClass = other.objectClass;
116
        this.precision = other.precision;
117
        this.evaluator = other.evaluator;
118
        this.primaryKey = other.primaryKey;
119
        this.readOnly = other.readOnly;
120
        this.SRS = other.SRS;
121
        this.geometryType = other.geometryType;
122
        this.geometrySubType = other.geometrySubType;
123
        this.geomType = other.geomType;
124
        if (other.additionalInfo != null) {
125
            Iterator iter = other.additionalInfo.entrySet().iterator();
126
            Map.Entry entry;
127
            this.additionalInfo = new HashMap();
128
            while (iter.hasNext()) {
129
                entry = (Entry) iter.next();
130
                this.additionalInfo.put(entry.getKey(), entry.getValue());
131
            }
132
        } else {
133
            this.additionalInfo = null;
134
        }
135
        this.isAutomatic = other.isAutomatic;
136
                this.isTime = other.isTime;
137
                this.featureAttributeGetter = other.featureAttributeGetter;
138
    }
139

    
140
    public String getDataTypeName() {
141
                if( this.getDataType() == null ) {
142
            return "(unknow)";
143
        }
144
                return this.getDataType().getName();
145
    }
146

    
147
    public FeatureAttributeDescriptor getCopy() {
148
        return new DefaultFeatureAttributeDescriptor(this);
149
    }
150

    
151
    public boolean allowNull() {
152
        return allowNull;
153
    }
154

    
155
    public DataType getDataType() {
156
                if (featureAttributeGetter != null){
157
                    return featureAttributeGetter.getDataType();
158
                }
159
        return this.dataType;
160
    }
161

    
162
    public DateFormat getDateFormat() {
163
        return this.dateFormat;
164
    }
165

    
166
    public Object getDefaultValue() {
167
        return this.defaultValue;
168
    }
169

    
170
    public Evaluator getEvaluator() {
171
        return this.evaluator;
172
    }
173

    
174
    public int getGeometryType() {
175
        return this.geometryType;
176
    }
177

    
178
    public int getGeometrySubType() {
179
        return this.geometrySubType;
180
    }
181

    
182
    public GeometryType getGeomType() {
183
        if (this.geomType == null) {
184
            try {
185
                this.geomType =
186
                    GeometryLocator.getGeometryManager().getGeometryType(
187
                        this.geometryType, this.geometrySubType);
188
            } catch (GeometryException e) {
189
                throw new RuntimeException(
190
                    "Error getting geometry type with type = "
191
                        + this.geometryType + ", subtype = "
192
                        + this.geometrySubType, e);
193
            }
194
        }
195
        return this.geomType;
196
    }
197

    
198
    public int getIndex() {
199
        return this.index;
200
    }
201

    
202
    protected FeatureAttributeDescriptor setIndex(int index) {
203
        this.index = index;
204
        return this;
205
    }
206

    
207
    public int getMaximumOccurrences() {
208
        return this.maximumOccurrences;
209
    }
210

    
211
    public int getMinimumOccurrences() {
212
        return this.minimumOccurrences;
213
    }
214

    
215
    public String getName() {
216
        return this.name;
217
    }
218

    
219
    public Class getObjectClass() {
220
                if (getDataType().getType() == DataTypes.OBJECT) {
221
            return objectClass;
222
        }
223
                return getDataType().getDefaultClass();
224
    }
225

    
226
    public int getPrecision() {
227
        return this.precision;
228
    }
229

    
230
    public IProjection getSRS() {
231
        return this.SRS;
232
    }
233

    
234
    public int getSize() {
235
        return this.size;
236
    }
237

    
238
    public boolean isPrimaryKey() {
239
        return this.primaryKey;
240
    }
241

    
242
    public boolean isReadOnly() {
243
        return this.readOnly;
244
    }
245

    
246
    public Object getAdditionalInfo(String infoName) {
247
        if (this.additionalInfo == null) {
248
            return null;
249
        }
250
        return this.additionalInfo.get(infoName);
251
    }
252

    
253
    public boolean isAutomatic() {
254
        return this.isAutomatic;
255
    }
256

    
257
    private boolean compareObject(Object a, Object b) {
258
        if (a != b) {
259
            if (a == null) {
260
                return false;
261
            }
262
            return a.equals(b);
263
        }
264
        return true;
265

    
266
    }
267

    
268
    public boolean equals(Object obj) {
269
        if (this == obj) {
270
            return true;
271
        }
272
        if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
273
            return false;
274
        }
275
        DefaultFeatureAttributeDescriptor other =
276
            (DefaultFeatureAttributeDescriptor) obj;
277

    
278
        if (this.allowNull != other.allowNull) {
279
            return false;
280
        }
281

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

    
286
        if (!compareObject(this.name, other.name)) {
287
            return false;
288
        }
289

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

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

    
298
        if (!compareObject(this.defaultValue, other.defaultValue)) {
299
            return false;
300
        }
301

    
302
        if (!compareObject(this.defaultValue, other.defaultValue)) {
303
            return false;
304
        }
305

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

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

    
314
        if (this.readOnly != other.readOnly) {
315
            return false;
316
        }
317

    
318
        if (this.precision != other.precision) {
319
            return false;
320
        }
321

    
322
        if (this.maximumOccurrences != other.maximumOccurrences) {
323
            return false;
324
        }
325

    
326
        if (this.minimumOccurrences != other.minimumOccurrences) {
327
            return false;
328
        }
329

    
330
        if (this.geometryType != other.geometryType) {
331
            return false;
332
        }
333

    
334
        if (this.geometrySubType != other.geometrySubType) {
335
            return false;
336
        }
337

    
338
        if (!compareObject(this.evaluator, other.evaluator)) {
339
            return false;
340
        }
341

    
342
        if (!compareObject(this.SRS, other.SRS)) {
343
            return false;
344
        }
345

    
346
        if (!compareObject(this.dateFormat, other.dateFormat)) {
347
            return false;
348
        }
349

    
350
        if (!compareObject(this.objectClass, other.objectClass)) {
351
            return false;
352
        }
353

    
354
        return true;
355
    }
356

    
357
    public void loadFromState(PersistentState state)
358
        throws PersistenceException {
359
        allowNull = state.getBoolean("allowNull");
360
        dataType =
361
            ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
362
        // FIXME how persist dateFormat ???
363
        // dateFormat;
364
        defaultValue = state.get("defaultValue");
365

    
366
        index = state.getInt("index");
367
        maximumOccurrences = state.getInt("maximumOccurrences");
368
        minimumOccurrences = state.getInt("minimumOccurrences");
369
        size = state.getInt("size");
370
        name = state.getString("name");
371
        try {
372
            objectClass = Class.forName(state.getString("objectClass"));
373
        } catch (ClassNotFoundException e) {
374
            throw new PersistenceException(e);
375
        }
376
        precision = state.getInt("precision");
377
        evaluator = (Evaluator) state.get("evaluator");
378
        primaryKey = state.getBoolean("primaryKey");
379
        readOnly = state.getBoolean("readOnly");
380
        String srsId = state.getString("srsId");
381
        if (srsId != null) {
382
            SRS = CRSFactory.getCRS(srsId);
383
        }
384
        geometryType = state.getInt("geometryType");
385
        geometrySubType = state.getInt("geometrySubType");
386
        additionalInfo = (Map) state.get("aditionalInfo");
387
        isAutomatic = state.getBoolean("isAutomatic");
388
    }
389

    
390
    public void saveToState(PersistentState state) throws PersistenceException {
391
        state.set("allowNull", allowNull);
392
        state.set("dataType", dataType);
393
        // FIXME how persist dateFormat ???
394
        // dateFormat;
395

    
396
        defaultValue = state.get("defaultValue");
397

    
398
        index = state.getInt("index");
399
        maximumOccurrences = state.getInt("maximumOccurrences");
400
        minimumOccurrences = state.getInt("minimumOccurrences");
401
        size = state.getInt("size");
402
        name = state.getString("name");
403
        try {
404
            objectClass = Class.forName(state.getString("objectClass"));
405
        } catch (ClassNotFoundException e) {
406
            throw new PersistenceException(e);
407
        }
408
        precision = state.getInt("precision");
409
        evaluator = (Evaluator) state.get("evaluator");
410
        primaryKey = state.getBoolean("primaryKey");
411
        readOnly = state.getBoolean("readOnly");
412
        String srsId = state.getString("srsId");
413
        if (srsId != null) {
414
            SRS = CRSFactory.getCRS(srsId);
415
        }
416
        geometryType = state.getInt("geometryType");
417
        geometrySubType = state.getInt("geometrySubType");
418
        additionalInfo = (Map) state.get("aditionalInfo");
419
        isAutomatic = state.getBoolean("isAutomatic");
420
    }
421

    
422
    /**
423
     * Start of DynField interface Implementation
424
     * READONLY
425
     */
426

    
427
    public DynObjectValueItem[] getAvailableValues() {
428
        return null;
429
    }
430

    
431
    public String getDescription() {
432
        return getName();
433
    }
434

    
435
    public Object getMaxValue() {
436
        return null;
437
    }
438

    
439
    public Object getMinValue() {
440
        return null;
441
    }
442

    
443
    public int getTheTypeOfAvailableValues() {
444
        return 0;
445
    }
446

    
447
    public int getType() {
448
            if (featureAttributeGetter != null){
449
            return featureAttributeGetter.getDataType().getType();
450
        }
451
        return getDataType().getType();
452
    }
453

    
454
    public boolean isMandatory() {
455
        return !allowNull() || isPrimaryKey();
456
    }
457

    
458
    public boolean isPersistent() {
459
        return false;
460
    }
461

    
462
    public DynField setAvailableValues(DynObjectValueItem[] values) {
463
        throw new UnsupportedOperationException();
464
    }
465

    
466
    public DynField setDescription(String description) {
467
        throw new UnsupportedOperationException();
468
    }
469

    
470
    public DynField setMandatory(boolean mandatory) {
471
        throw new UnsupportedOperationException();
472
    }
473

    
474
    public DynField setMaxValue(Object maxValue) {
475
        throw new UnsupportedOperationException();
476
    }
477

    
478
    public DynField setMinValue(Object minValue) {
479
        throw new UnsupportedOperationException();
480
    }
481

    
482
    public DynField setPersistent(boolean persistent) {
483
        throw new UnsupportedOperationException();
484
    }
485

    
486
    public DynField setTheTypeOfAvailableValues(int type) {
487
        throw new UnsupportedOperationException();
488
    }
489

    
490
    public DynField setType(int type) {
491
        throw new UnsupportedOperationException();
492
    }
493

    
494
    public DynField setDefaultDynValue(Object defaultValue) {
495
        throw new UnsupportedOperationException();
496
    }
497

    
498
    public DynField addElementsType() throws DynFieldIsNotAContainerException {
499
        throw new UnsupportedOperationException();
500
    }
501

    
502
    public Class getClassOfValue() {
503
        return null;
504
    }
505

    
506
    public DynField getElementsType() {
507
        return null;
508
    }
509

    
510
    public DynField setClassOfValue(Class theClass)
511
        throws DynFieldIsNotAContainerException {
512
        throw new UnsupportedOperationException();
513
    }
514

    
515
    public DynField setElementsType(DynStruct type)
516
        throws DynFieldIsNotAContainerException {
517
        throw new UnsupportedOperationException();
518
    }
519

    
520
    public DynField setElementsType(int type)
521
        throws DynFieldIsNotAContainerException {
522
        throw new UnsupportedOperationException();
523
    }
524

    
525
    public DynField setSubtype(String subtype) {
526
        throw new UnsupportedOperationException();
527
    }
528

    
529
    public void validate(Object value) throws DynFieldValidateException {
530
        
531
        if (value == null && !this.allowNull()) {
532
            throw new DynFieldValidateException(value, this, null);
533
        }
534
        
535
        try {
536
            this.dataType.coerce(value);
537
        } catch (CoercionException e) {
538
            throw new DynFieldValidateException(value, this, e);
539
        }
540
        
541
        /*
542
         * Other checks will be needed
543
         */
544
    }
545

    
546
    public String getSubtype() {
547
            if (featureAttributeGetter != null){
548
            return featureAttributeGetter.getDataType().getSubtype();
549
        }       
550
        return this.dataType.getSubtype();
551
    }
552

    
553
    public Object coerce(Object value) {
554
        throw new UnsupportedOperationException();
555
    }
556

    
557
    public DynField setAvailableValues(List values) {
558
        throw new UnsupportedOperationException();
559
    }
560

    
561
    public String getGroup() {
562
        return null;
563
    }
564

    
565
    public int getOder() {
566
        return 0;
567
    }
568

    
569
    public DynField setGroup(String groupName) {
570
        throw new UnsupportedOperationException();
571
    }
572

    
573
    public DynField setOrder(int order) {
574
        throw new UnsupportedOperationException();
575
    }
576

    
577
    public DynField setHidden(boolean hidden) {
578
        throw new UnsupportedOperationException();
579
    }
580

    
581
    public boolean isHidden() {
582
        return false;
583
    }
584

    
585
    public DynField setReadOnly(boolean arg0) {
586
        throw new UnsupportedOperationException();
587
    }
588

    
589
    public boolean isContainer() {
590
        return false;
591
    }
592

    
593
    public Class getClassOfItems() {
594
        return null;
595
    }
596

    
597
    public DynField setDefaultFieldValue(Object defaultValue) {
598
        throw new UnsupportedOperationException();
599
    }
600

    
601
    public DynField setClassOfItems(Class theClass) {
602
        throw new UnsupportedOperationException();
603
    }
604

    
605
    public DynField setType(DataType type) {
606
        throw new UnsupportedOperationException();
607
    }
608

    
609
        public boolean isTime() {                
610
                return isTime;
611
        }
612

    
613
    public FeatureAttributeGetter getFeatureAttributeGetter() {
614
       return featureAttributeGetter;        
615
    }
616

    
617
    public void setFeatureAttributeGetter(
618
        FeatureAttributeGetter featureAttributeTransform) {
619
        this.featureAttributeGetter = featureAttributeTransform;              
620
    }        
621
}