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

History | View | Annotate | Download (18.5 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.AbstractEvaluator;
52
import org.gvsig.tools.evaluator.Evaluator;
53
import org.gvsig.tools.evaluator.EvaluatorData;
54
import org.gvsig.tools.evaluator.EvaluatorException;
55
import org.gvsig.tools.persistence.Persistent;
56
import org.gvsig.tools.persistence.PersistentState;
57
import org.gvsig.tools.persistence.exception.PersistenceException;
58

    
59
public class DefaultFeatureAttributeDescriptor implements
60
    FeatureAttributeDescriptor, Persistent, DynField {
61

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

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

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

    
143
    public String getDataTypeName() {
144
                if( this.getDataType() == null ) {
145
            return "(unknow)";
146
        }
147
                return this.getDataType().getName();
148
    }
149

    
150
    public FeatureAttributeDescriptor getCopy() {
151
        return new DefaultFeatureAttributeDescriptor(this);
152
    }
153

    
154
    public boolean allowNull() {
155
        return allowNull;
156
    }
157

    
158
    public DataType getDataType() {
159
                if (featureAttributeGetter != null){
160
                    return featureAttributeGetter.getDataType();
161
                }
162
        return this.dataType;
163
    }
164

    
165
    public DateFormat getDateFormat() {
166
        return this.dateFormat;
167
    }
168

    
169
    public Object getDefaultValue() {
170
        return this.defaultValue;
171
    }
172

    
173
    public Evaluator getEvaluator() {
174
        return this.evaluator;
175
    }
176

    
177
    public int getGeometryType() {
178
        return this.geometryType;
179
    }
180

    
181
    public int getGeometrySubType() {
182
        return this.geometrySubType;
183
    }
184

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

    
201
    public int getIndex() {
202
        return this.index;
203
    }
204

    
205
    protected FeatureAttributeDescriptor setIndex(int index) {
206
        this.index = index;
207
        return this;
208
    }
209

    
210
    public int getMaximumOccurrences() {
211
        return this.maximumOccurrences;
212
    }
213

    
214
    public int getMinimumOccurrences() {
215
        return this.minimumOccurrences;
216
    }
217

    
218
    public String getName() {
219
        return this.name;
220
    }
221

    
222
    public Class getObjectClass() {
223
                if (getDataType().getType() == DataTypes.OBJECT) {
224
            return objectClass;
225
        }
226
                return getDataType().getDefaultClass();
227
    }
228

    
229
    public int getPrecision() {
230
        return this.precision;
231
    }
232

    
233
    public IProjection getSRS() {
234
        return this.SRS;
235
    }
236

    
237
    public int getSize() {
238
        return this.size;
239
    }
240

    
241
    public boolean isPrimaryKey() {
242
        return this.primaryKey;
243
    }
244

    
245
    public boolean isReadOnly() {
246
        return this.readOnly;
247
    }
248

    
249
    public Object getAdditionalInfo(String infoName) {
250
        if (this.additionalInfo == null) {
251
            return null;
252
        }
253
        return this.additionalInfo.get(infoName);
254
    }
255

    
256
    public boolean isAutomatic() {
257
        return this.isAutomatic;
258
    }
259

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

    
269
    }
270

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

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

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

    
289
        if (!compareObject(this.name, other.name)) {
290
            return false;
291
        }
292

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

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

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

    
305
        if (!compareObject(this.defaultValue, other.defaultValue)) {
306
            return false;
307
        }
308

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

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

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

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

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

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

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

    
337
        if (this.geometrySubType != other.geometrySubType) {
338
            return false;
339
        }
340

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

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

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

    
353
        if (!compareObject(this.objectClass, other.objectClass)) {
354
            return false;
355
        }
356

    
357
        return true;
358
    }
359

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

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

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

    
399
        defaultValue = state.get("defaultValue");
400

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

    
425
    /**
426
     * Start of DynField interface Implementation
427
     * READONLY
428
     */
429

    
430
    public DynObjectValueItem[] getAvailableValues() {
431
        return null;
432
    }
433

    
434
    public String getDescription() {
435
        return getName();
436
    }
437

    
438
    public Object getMaxValue() {
439
        return null;
440
    }
441

    
442
    public Object getMinValue() {
443
        return null;
444
    }
445

    
446
    public int getTheTypeOfAvailableValues() {
447
        return 0;
448
    }
449

    
450
    public int getType() {
451
            if (featureAttributeGetter != null){
452
            return featureAttributeGetter.getDataType().getType();
453
        }
454
        return getDataType().getType();
455
    }
456

    
457
    public boolean isMandatory() {
458
        return !allowNull() || isPrimaryKey();
459
    }
460

    
461
    public boolean isPersistent() {
462
        return false;
463
    }
464

    
465
    public DynField setAvailableValues(DynObjectValueItem[] values) {
466
        throw new UnsupportedOperationException();
467
    }
468

    
469
    public DynField setDescription(String description) {
470
        throw new UnsupportedOperationException();
471
    }
472

    
473
    public DynField setMandatory(boolean mandatory) {
474
        throw new UnsupportedOperationException();
475
    }
476

    
477
    public DynField setMaxValue(Object maxValue) {
478
        throw new UnsupportedOperationException();
479
    }
480

    
481
    public DynField setMinValue(Object minValue) {
482
        throw new UnsupportedOperationException();
483
    }
484

    
485
    public DynField setPersistent(boolean persistent) {
486
        throw new UnsupportedOperationException();
487
    }
488

    
489
    public DynField setTheTypeOfAvailableValues(int type) {
490
        throw new UnsupportedOperationException();
491
    }
492

    
493
    public DynField setType(int type) {
494
        throw new UnsupportedOperationException();
495
    }
496

    
497
    public DynField setDefaultDynValue(Object defaultValue) {
498
        throw new UnsupportedOperationException();
499
    }
500

    
501
    public DynField addElementsType() throws DynFieldIsNotAContainerException {
502
        throw new UnsupportedOperationException();
503
    }
504

    
505
    public Class getClassOfValue() {
506
        return null;
507
    }
508

    
509
    public DynField getElementsType() {
510
        return null;
511
    }
512

    
513
    public DynField setClassOfValue(Class theClass)
514
        throws DynFieldIsNotAContainerException {
515
        throw new UnsupportedOperationException();
516
    }
517

    
518
    public DynField setElementsType(DynStruct type)
519
        throws DynFieldIsNotAContainerException {
520
        throw new UnsupportedOperationException();
521
    }
522

    
523
    public DynField setElementsType(int type)
524
        throws DynFieldIsNotAContainerException {
525
        throw new UnsupportedOperationException();
526
    }
527

    
528
    public DynField setSubtype(String subtype) {
529
        throw new UnsupportedOperationException();
530
    }
531

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

    
549
    public String getSubtype() {
550
            if (featureAttributeGetter != null){
551
            return featureAttributeGetter.getDataType().getSubtype();
552
        }       
553
        return this.dataType.getSubtype();
554
    }
555

    
556
    public Object coerce(Object value) {
557
        throw new UnsupportedOperationException();
558
    }
559

    
560
    public DynField setAvailableValues(List values) {
561
        throw new UnsupportedOperationException();
562
    }
563

    
564
    public String getGroup() {
565
        return null;
566
    }
567

    
568
    public int getOder() {
569
        return 0;
570
    }
571

    
572
    public DynField setGroup(String groupName) {
573
        throw new UnsupportedOperationException();
574
    }
575

    
576
    public DynField setOrder(int order) {
577
        throw new UnsupportedOperationException();
578
    }
579

    
580
    public DynField setHidden(boolean hidden) {
581
        throw new UnsupportedOperationException();
582
    }
583

    
584
    public boolean isHidden() {
585
        return false;
586
    }
587

    
588
    public DynField setReadOnly(boolean arg0) {
589
        throw new UnsupportedOperationException();
590
    }
591

    
592
    public boolean isContainer() {
593
        return false;
594
    }
595

    
596
    public Class getClassOfItems() {
597
        return null;
598
    }
599

    
600
    public DynField setDefaultFieldValue(Object defaultValue) {
601
        throw new UnsupportedOperationException();
602
    }
603

    
604
    public DynField setClassOfItems(Class theClass) {
605
        throw new UnsupportedOperationException();
606
    }
607

    
608
    public DynField setType(DataType type) {
609
        throw new UnsupportedOperationException();
610
    }
611

    
612
        public boolean isTime() {                
613
                return isTime;
614
        }
615

    
616
    public FeatureAttributeGetter getFeatureAttributeGetter() {
617
       return featureAttributeGetter;        
618
    }
619

    
620
    public void setFeatureAttributeGetter(
621
        FeatureAttributeGetter featureAttributeTransform) {
622
        this.featureAttributeGetter = featureAttributeTransform;              
623
    }        
624
        
625
    private class ConstantValueEvaluator extends AbstractEvaluator {
626
        public Object evaluate(EvaluatorData data) throws EvaluatorException {
627
            return defaultValue;
628
        }
629
        public String getName() {
630
            return "Constant attribute "+name;
631
        }
632
    }
633
    public void setConstantValue(boolean isConstantValue) {
634
        if( isConstantValue ) {
635
            /* Cuando un attributo tiene asociado un evaluador, este se interpreta
636
             * como que no debe cargarse de la fuente de datos subyacente, siendo 
637
             * el evaluador el que se encarga de proporcionar su valor.
638
             * Nos limitamos a asignar un evaluador que retorna simpre el valor
639
             * por defecto para ese attributo.
640
             */
641
            this.evaluator = new ConstantValueEvaluator();
642
        } else {
643
            this.evaluator = null;
644
        }
645
    }
646
    
647
}