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

History | View | Annotate | Download (19.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.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.dal.feature.FeatureAttributeEmulator;
39
import org.gvsig.fmap.geom.Geometry;
40
import org.gvsig.fmap.geom.GeometryException;
41
import org.gvsig.fmap.geom.GeometryLocator;
42
import org.gvsig.fmap.geom.type.GeometryType;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dataTypes.CoercionException;
45
import org.gvsig.tools.dataTypes.DataType;
46
import org.gvsig.tools.dataTypes.DataTypes;
47
import org.gvsig.tools.dynobject.DynField;
48
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
49
import org.gvsig.tools.dynobject.DynObjectValueItem;
50
import org.gvsig.tools.dynobject.DynStruct;
51
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
52
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
53
import org.gvsig.tools.evaluator.AbstractEvaluator;
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.persistence.Persistent;
58
import org.gvsig.tools.persistence.PersistentState;
59
import org.gvsig.tools.persistence.exception.PersistenceException;
60

    
61
public class DefaultFeatureAttributeDescriptor implements
62
    FeatureAttributeDescriptor, Persistent, DynField, DynField_LabelAttribute {
63

    
64
    protected boolean allowNull;
65
    protected DataType dataType;
66
    protected DateFormat dateFormat;
67
    protected Object defaultValue;
68
    protected int index;
69
    protected int maximumOccurrences;
70
    protected int minimumOccurrences;
71
    protected int size;
72
    protected String name;
73
    protected Class objectClass;
74
    protected int precision;
75
    protected Evaluator evaluator;
76
    protected boolean primaryKey;
77
    protected boolean readOnly;
78
    protected IProjection SRS;
79
    protected GeometryType geomType;
80
    protected int geometryType;
81
    protected int geometrySubType;
82
    protected Map additionalInfo;
83
    protected boolean isAutomatic;
84
    protected boolean isTime = false;
85
    protected FeatureAttributeGetter featureAttributeGetter = null;
86
    protected FeatureAttributeEmulator featureAttributeEmulator = null;
87
    protected boolean indexed = false;
88
    protected boolean isIndexAscending = true;
89
    protected boolean allowIndexDuplicateds = true;
90

    
91
    protected DefaultFeatureAttributeDescriptor() {
92
        this.allowNull = true;
93
        this.dataType = null;
94
        this.dateFormat = null;
95
        this.defaultValue = null;
96
        this.index = -1;
97
        this.maximumOccurrences = 0;
98
        this.minimumOccurrences = 0;
99
        this.size = 0;
100
        this.name = null;
101
        this.objectClass = null;
102
        this.precision = 0;
103
        this.evaluator = null;
104
        this.primaryKey = false;
105
        this.readOnly = false;
106
        this.SRS = null;
107
        this.geometryType = Geometry.TYPES.NULL;
108
        this.geometrySubType = Geometry.SUBTYPES.UNKNOWN;
109
        this.additionalInfo = null;
110
        this.isAutomatic = false;
111
    }
112

    
113
    protected DefaultFeatureAttributeDescriptor(
114
        DefaultFeatureAttributeDescriptor other) {
115
        this.allowNull = other.allowNull;
116
        this.dataType = other.dataType;
117
        this.dateFormat = other.dateFormat;
118
        this.defaultValue = other.defaultValue;
119
        this.index = other.index;
120
        this.maximumOccurrences = other.maximumOccurrences;
121
        this.minimumOccurrences = other.minimumOccurrences;
122
        this.size = other.size;
123
        this.name = other.name;
124
        this.objectClass = other.objectClass;
125
        this.precision = other.precision;
126
        this.evaluator = other.evaluator;
127
        this.primaryKey = other.primaryKey;
128
        this.readOnly = other.readOnly;
129
        this.SRS = other.SRS;
130
        this.geometryType = other.geometryType;
131
        this.geometrySubType = other.geometrySubType;
132
        this.geomType = other.geomType;
133
        if (other.additionalInfo != null) {
134
            Iterator iter = other.additionalInfo.entrySet().iterator();
135
            Map.Entry entry;
136
            this.additionalInfo = new HashMap();
137
            while (iter.hasNext()) {
138
                entry = (Entry) iter.next();
139
                this.additionalInfo.put(entry.getKey(), entry.getValue());
140
            }
141
        } else {
142
            this.additionalInfo = null;
143
        }
144
        this.isAutomatic = other.isAutomatic;
145
        this.isTime = other.isTime;
146
        this.featureAttributeEmulator = other.featureAttributeEmulator;
147
        this.indexed = other.indexed;
148
        this.isIndexAscending = other.isIndexAscending;
149
        this.allowIndexDuplicateds = other.allowIndexDuplicateds;
150
    }
151

    
152
    public String getDataTypeName() {
153
                if( this.getDataType() == null ) {
154
            return "(unknow)";
155
        }
156
                return this.getDataType().getName();
157
    }
158

    
159
    public FeatureAttributeDescriptor getCopy() {
160
        return new DefaultFeatureAttributeDescriptor(this);
161
    }
162

    
163
    public boolean allowNull() {
164
        return allowNull;
165
    }
166

    
167
    public DataType getDataType() {
168
                if (featureAttributeGetter != null){
169
                    return featureAttributeGetter.getDataType();
170
                }
171
        return this.dataType;
172
    }
173

    
174
    public DateFormat getDateFormat() {
175
        return this.dateFormat;
176
    }
177

    
178
    public Object getDefaultValue() {
179
        return this.defaultValue;
180
    }
181

    
182
    public Evaluator getEvaluator() {
183
        return this.evaluator;
184
    }
185

    
186
    public int getGeometryType() {
187
        return this.geometryType;
188
    }
189

    
190
    public int getGeometrySubType() {
191
        return this.geometrySubType;
192
    }
193

    
194
    public GeometryType getGeomType() {
195
        if (this.geomType == null) {
196
            try {
197
                this.geomType =
198
                    GeometryLocator.getGeometryManager().getGeometryType(
199
                        this.geometryType, this.geometrySubType);
200
            } catch (GeometryException e) {
201
                throw new RuntimeException(
202
                    "Error getting geometry type with type = "
203
                        + this.geometryType + ", subtype = "
204
                        + this.geometrySubType, e);
205
            }
206
        }
207
        return this.geomType;
208
    }
209

    
210
    public int getIndex() {
211
        return this.index;
212
    }
213

    
214
    protected FeatureAttributeDescriptor setIndex(int index) {
215
        this.index = index;
216
        return this;
217
    }
218

    
219
    public int getMaximumOccurrences() {
220
        return this.maximumOccurrences;
221
    }
222

    
223
    public int getMinimumOccurrences() {
224
        return this.minimumOccurrences;
225
    }
226

    
227
    public String getName() {
228
        return this.name;
229
    }
230

    
231
    public Class getObjectClass() {
232
                if (getDataType().getType() == DataTypes.OBJECT) {
233
            return objectClass;
234
        }
235
                return getDataType().getDefaultClass();
236
    }
237

    
238
    public int getPrecision() {
239
        return this.precision;
240
    }
241

    
242
    public IProjection getSRS() {
243
        return this.SRS;
244
    }
245

    
246
    public int getSize() {
247
        return this.size;
248
    }
249

    
250
    public boolean isPrimaryKey() {
251
        return this.primaryKey;
252
    }
253

    
254
    public boolean isReadOnly() {
255
        if( this.readOnly ) {
256
            return true;
257
        }
258
        if( this.getEvaluator()!=null ) {
259
            return true;
260
        }
261
        if( this.featureAttributeEmulator!=null ) {
262
            return !this.featureAttributeEmulator.allowSetting();
263
        }
264
        return false;
265
    }
266

    
267
    public Object getAdditionalInfo(String infoName) {
268
        if (this.additionalInfo == null) {
269
            return null;
270
        }
271
        return this.additionalInfo.get(infoName);
272
    }
273

    
274
    public boolean isAutomatic() {
275
        return this.isAutomatic;
276
    }
277

    
278
    private boolean compareObject(Object a, Object b) {
279
        if (a != b) {
280
            if (a == null) {
281
                return false;
282
            }
283
            return a.equals(b);
284
        }
285
        return true;
286

    
287
    }
288

    
289
    public boolean equals(Object obj) {
290
        if (this == obj) {
291
            return true;
292
        }
293
        if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
294
            return false;
295
        }
296
        DefaultFeatureAttributeDescriptor other =
297
            (DefaultFeatureAttributeDescriptor) obj;
298

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

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

    
307
        if (!compareObject(this.name, other.name)) {
308
            return false;
309
        }
310

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

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

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

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

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

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

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

    
339
        if (this.precision != other.precision) {
340
            return false;
341
        }
342

    
343
        if (this.maximumOccurrences != other.maximumOccurrences) {
344
            return false;
345
        }
346

    
347
        if (this.minimumOccurrences != other.minimumOccurrences) {
348
            return false;
349
        }
350

    
351
        if (this.geometryType != other.geometryType) {
352
            return false;
353
        }
354

    
355
        if (this.geometrySubType != other.geometrySubType) {
356
            return false;
357
        }
358

    
359
        if (!compareObject(this.evaluator, other.evaluator)) {
360
            return false;
361
        }
362

    
363
        if (!compareObject(this.SRS, other.SRS)) {
364
            return false;
365
        }
366

    
367
        if (!compareObject(this.dateFormat, other.dateFormat)) {
368
            return false;
369
        }
370

    
371
        if (!compareObject(this.objectClass, other.objectClass)) {
372
            return false;
373
        }
374

    
375
        return true;
376
    }
377

    
378
    public void loadFromState(PersistentState state)
379
        throws PersistenceException {
380
        allowNull = state.getBoolean("allowNull");
381
        dataType =
382
            ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
383
        // FIXME how persist dateFormat ???
384
        // dateFormat;
385
        defaultValue = state.get("defaultValue");
386

    
387
        index = state.getInt("index");
388
        maximumOccurrences = state.getInt("maximumOccurrences");
389
        minimumOccurrences = state.getInt("minimumOccurrences");
390
        size = state.getInt("size");
391
        name = state.getString("name");
392
        try {
393
            objectClass = Class.forName(state.getString("objectClass"));
394
        } catch (ClassNotFoundException e) {
395
            throw new PersistenceException(e);
396
        }
397
        precision = state.getInt("precision");
398
        evaluator = (Evaluator) state.get("evaluator");
399
        primaryKey = state.getBoolean("primaryKey");
400
        readOnly = state.getBoolean("readOnly");
401
        String srsId = state.getString("srsId");
402
        if (srsId != null) {
403
            SRS = CRSFactory.getCRS(srsId);
404
        }
405
        geometryType = state.getInt("geometryType");
406
        geometrySubType = state.getInt("geometrySubType");
407
        additionalInfo = (Map) state.get("aditionalInfo");
408
        isAutomatic = state.getBoolean("isAutomatic");
409
    }
410

    
411
    public void saveToState(PersistentState state) throws PersistenceException {
412
        state.set("allowNull", allowNull);
413
        state.set("dataType", dataType);
414
        // FIXME how persist dateFormat ???
415
        // dateFormat;
416

    
417
        defaultValue = state.get("defaultValue");
418

    
419
        index = state.getInt("index");
420
        maximumOccurrences = state.getInt("maximumOccurrences");
421
        minimumOccurrences = state.getInt("minimumOccurrences");
422
        size = state.getInt("size");
423
        name = state.getString("name");
424
        try {
425
            objectClass = Class.forName(state.getString("objectClass"));
426
        } catch (ClassNotFoundException e) {
427
            throw new PersistenceException(e);
428
        }
429
        precision = state.getInt("precision");
430
        evaluator = (Evaluator) state.get("evaluator");
431
        primaryKey = state.getBoolean("primaryKey");
432
        readOnly = state.getBoolean("readOnly");
433
        String srsId = state.getString("srsId");
434
        if (srsId != null) {
435
            SRS = CRSFactory.getCRS(srsId);
436
        }
437
        geometryType = state.getInt("geometryType");
438
        geometrySubType = state.getInt("geometrySubType");
439
        additionalInfo = (Map) state.get("aditionalInfo");
440
        isAutomatic = state.getBoolean("isAutomatic");
441
    }
442

    
443
    /**
444
     * Start of DynField interface Implementation
445
     * READONLY
446
     */
447

    
448
    public DynObjectValueItem[] getAvailableValues() {
449
        return null;
450
    }
451

    
452
    public String getDescription() {
453
        return getName();
454
    }
455

    
456
    public Object getMaxValue() {
457
        return null;
458
    }
459

    
460
    public Object getMinValue() {
461
        return null;
462
    }
463

    
464
    public int getTheTypeOfAvailableValues() {
465
        return 0;
466
    }
467

    
468
    public int getType() {
469
            if (featureAttributeGetter != null){
470
            return featureAttributeGetter.getDataType().getType();
471
        }
472
        return getDataType().getType();
473
    }
474

    
475
    public boolean isMandatory() {
476
        return !allowNull() || isPrimaryKey();
477
    }
478

    
479
    public boolean isPersistent() {
480
        return false;
481
    }
482

    
483
    public DynField setAvailableValues(DynObjectValueItem[] values) {
484
        throw new UnsupportedOperationException();
485
    }
486

    
487
    public DynField setDescription(String description) {
488
        throw new UnsupportedOperationException();
489
    }
490

    
491
    public DynField setMandatory(boolean mandatory) {
492
        throw new UnsupportedOperationException();
493
    }
494

    
495
    public DynField setMaxValue(Object maxValue) {
496
        throw new UnsupportedOperationException();
497
    }
498

    
499
    public DynField setMinValue(Object minValue) {
500
        throw new UnsupportedOperationException();
501
    }
502

    
503
    public DynField setPersistent(boolean persistent) {
504
        throw new UnsupportedOperationException();
505
    }
506

    
507
    public DynField setTheTypeOfAvailableValues(int type) {
508
        throw new UnsupportedOperationException();
509
    }
510

    
511
    public DynField setType(int type) {
512
        throw new UnsupportedOperationException();
513
    }
514

    
515
    public DynField setDefaultDynValue(Object defaultValue) {
516
        throw new UnsupportedOperationException();
517
    }
518

    
519
    public DynField addElementsType() throws DynFieldIsNotAContainerException {
520
        throw new UnsupportedOperationException();
521
    }
522

    
523
    public Class getClassOfValue() {
524
        return null;
525
    }
526

    
527
    public DynField getElementsType() {
528
        return null;
529
    }
530

    
531
    public DynField setClassOfValue(Class theClass)
532
        throws DynFieldIsNotAContainerException {
533
        throw new UnsupportedOperationException();
534
    }
535

    
536
    public DynField setElementsType(DynStruct type)
537
        throws DynFieldIsNotAContainerException {
538
        throw new UnsupportedOperationException();
539
    }
540

    
541
    public DynField setElementsType(int type)
542
        throws DynFieldIsNotAContainerException {
543
        throw new UnsupportedOperationException();
544
    }
545

    
546
    public DynField setSubtype(String subtype) {
547
        throw new UnsupportedOperationException();
548
    }
549

    
550
    public void validate(Object value) throws DynFieldValidateException {
551
        
552
        if (value == null && !this.allowNull()) {
553
            throw new DynFieldValidateException(value, this, null);
554
        }
555
        
556
        try {
557
            this.dataType.coerce(value);
558
        } catch (CoercionException e) {
559
            throw new DynFieldValidateException(value, this, e);
560
        }
561
        
562
        /*
563
         * Other checks will be needed
564
         */
565
    }
566

    
567
    public String getSubtype() {
568
            if (featureAttributeGetter != null){
569
            return featureAttributeGetter.getDataType().getSubtype();
570
        }       
571
        return this.dataType.getSubtype();
572
    }
573

    
574
    public Object coerce(Object value) {
575
        throw new UnsupportedOperationException();
576
    }
577

    
578
    public DynField setAvailableValues(List values) {
579
        throw new UnsupportedOperationException();
580
    }
581

    
582
    public String getGroup() {
583
        return null;
584
    }
585

    
586
    public int getOder() {
587
        return 0;
588
    }
589

    
590
    public String getLabel() {
591
        return this.getName();
592
    }
593

    
594
    public DynField setLabel(String label) {
595
        throw new UnsupportedOperationException("Not supported yet."); 
596
    }
597

    
598
    public DynField setGroup(String groupName) {
599
        throw new UnsupportedOperationException();
600
    }
601

    
602
    public DynField setOrder(int order) {
603
        throw new UnsupportedOperationException();
604
    }
605

    
606
    public DynField setHidden(boolean hidden) {
607
        throw new UnsupportedOperationException();
608
    }
609

    
610
    public boolean isHidden() {
611
        return false;
612
    }
613

    
614
    public DynField setReadOnly(boolean arg0) {
615
        throw new UnsupportedOperationException();
616
    }
617

    
618
    public boolean isContainer() {
619
        return false;
620
    }
621

    
622
    public Class getClassOfItems() {
623
        return null;
624
    }
625

    
626
    public DynField setDefaultFieldValue(Object defaultValue) {
627
        throw new UnsupportedOperationException();
628
    }
629

    
630
    public DynField setClassOfItems(Class theClass) {
631
        throw new UnsupportedOperationException();
632
    }
633

    
634
    public DynField setType(DataType type) {
635
        throw new UnsupportedOperationException();
636
    }
637

    
638
        public boolean isTime() {                
639
                return isTime;
640
        }
641

    
642
    public FeatureAttributeGetter getFeatureAttributeGetter() {
643
       return featureAttributeGetter;        
644
    }
645

    
646
    public void setFeatureAttributeGetter(
647
        FeatureAttributeGetter featureAttributeTransform) {
648
        this.featureAttributeGetter = featureAttributeTransform;              
649
    }        
650

    
651
    public FeatureAttributeEmulator getFeatureAttributeEmulator() {
652
        return this.featureAttributeEmulator;
653
    }
654

    
655
    public boolean isIndexed() {
656
        return this.indexed;
657
    }
658

    
659
    public boolean allowIndexDuplicateds() {
660
        return this.allowIndexDuplicateds;
661
    }
662

    
663
    public boolean isIndexAscending() {
664
        return this.isIndexAscending;
665
    }
666

    
667
    private class ConstantValueEvaluator extends AbstractEvaluator {
668
        public Object evaluate(EvaluatorData data) throws EvaluatorException {
669
            return defaultValue;
670
        }
671
        public String getName() {
672
            return "Constant attribute "+name;
673
        }
674
    }
675

    
676
    public void setConstantValue(boolean isConstantValue) {
677
        if( isConstantValue ) {
678
            /* Cuando un attributo tiene asociado un evaluador, este se interpreta
679
             * como que no debe cargarse de la fuente de datos subyacente, siendo 
680
             * el evaluador el que se encarga de proporcionar su valor.
681
             * Nos limitamos a asignar un evaluador que retorna simpre el valor
682
             * por defecto para ese attributo.
683
             */
684
            this.evaluator = new ConstantValueEvaluator();
685
        } else {
686
            this.evaluator = null;
687
        }
688
    }
689
    
690
}