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

History | View | Annotate | Download (40.2 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.feature.impl;
24

    
25
import java.lang.ref.WeakReference;
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
import java.util.Objects;
33
import org.apache.commons.lang3.ArrayUtils;
34
import org.apache.commons.lang3.StringUtils;
35
import org.cresques.cts.IProjection;
36
import org.gvsig.fmap.dal.DataStore;
37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.ForeingKey;
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.GeometryException;
46
import org.gvsig.fmap.geom.GeometryLocator;
47
import org.gvsig.fmap.geom.GeometryUtils;
48
import org.gvsig.fmap.geom.type.GeometryType;
49
import org.gvsig.timesupport.Interval;
50
import org.gvsig.timesupport.RelativeInterval;
51
import org.gvsig.timesupport.TimeSupportLocator;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.dataTypes.CoercionException;
54
import org.gvsig.tools.dataTypes.DataType;
55
import org.gvsig.tools.dataTypes.DataTypeUtils;
56
import org.gvsig.tools.dynobject.DynField;
57
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
58
import org.gvsig.tools.dynobject.DynField_v2;
59
import org.gvsig.tools.dynobject.DynMethod;
60
import org.gvsig.tools.dynobject.DynObject;
61
import org.gvsig.tools.dynobject.DynObjectValueItem;
62
import org.gvsig.tools.dynobject.DynStruct;
63
import org.gvsig.tools.dynobject.Tags;
64
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
65
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
66
import org.gvsig.tools.dynobject.exception.DynMethodException;
67
import org.gvsig.tools.dynobject.impl.DefaultTags;
68
import org.gvsig.tools.evaluator.AbstractEvaluator;
69
import org.gvsig.tools.evaluator.Evaluator;
70
import org.gvsig.tools.evaluator.EvaluatorData;
71
import org.gvsig.tools.evaluator.EvaluatorException;
72
import org.gvsig.tools.persistence.PersistenceManager;
73
import org.gvsig.tools.persistence.Persistent;
74
import org.gvsig.tools.persistence.PersistentState;
75
import org.gvsig.tools.persistence.exception.PersistenceException;
76
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
78

    
79
@SuppressWarnings("UseSpecificCatch")
80
public class DefaultFeatureAttributeDescriptor implements
81
        FeatureAttributeDescriptor, Persistent, DynField_v2, DynField_LabelAttribute {
82

    
83
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeDescriptor.class);
84
    
85
    protected boolean allowNull;
86
    protected DataType dataType;
87
    protected String dataProfile; 
88
    protected DateFormat dateFormat;
89
    protected Object defaultValue;
90
    protected int index;
91
    protected int maximumOccurrences;
92
    protected int minimumOccurrences;
93
    protected int size;
94
    protected String name;
95
    protected Class objectClass;
96
    protected int precision;
97
    protected Evaluator evaluator;
98
    protected boolean primaryKey;
99
    protected boolean readOnly;
100
    protected IProjection SRS;
101
    protected GeometryType geomType;
102
    protected int geometryType;
103
    protected int geometrySubType;
104
    protected Map additionalInfo;
105
    protected boolean isAutomatic;
106
    protected boolean isTime = false;
107
    protected Interval interval;
108
    protected FeatureAttributeGetter featureAttributeGetter = null;
109
    protected FeatureAttributeEmulator featureAttributeEmulator = null;
110
    protected boolean indexed = false;
111
    protected boolean isIndexAscending = true;
112
    protected boolean allowIndexDuplicateds = true;
113

    
114
    protected DynObjectValueItem[] availableValues;
115
    protected DynObjectValueItem[] availableValuesFromForeingKey;
116
    protected String description;
117
    protected Object minValue;
118
    protected Object maxValue;
119
    protected String label;
120
    protected String shortLabel;
121
    protected int order;
122
    protected boolean hidden;
123
    protected String groupName;
124
    protected Tags tags = new DefaultTags();
125
    private DynMethod availableValuesMethod;
126
    private DynMethod calculateMethod;
127
    private WeakReference typeRef;
128
    protected DefaultForeingKey foreingKey = null;
129
    
130
    private int relationType = RELATION_TYPE_NONE;
131

    
132
    public DefaultFeatureAttributeDescriptor() {
133
        // Usada en la persistencia
134
    }
135

    
136
    protected DefaultFeatureAttributeDescriptor(FeatureType type) {
137
        this();
138
//        LOGGER.info(String.format("Created FeatureAttributeDescriptor [%08x].", this.hashCode()));
139
        setFeatureType(type);
140
        this.allowNull = true;
141
        this.dataType = null;
142
        this.dateFormat = null;
143
        this.defaultValue = null;
144
        this.index = -1;
145
        this.maximumOccurrences = 0;
146
        this.minimumOccurrences = 0;
147
        this.size = 0;
148
        this.name = null;
149
        this.objectClass = null;
150
        this.precision = 0;
151
        this.evaluator = null;
152
        this.primaryKey = false;
153
        this.readOnly = false;
154
        this.SRS = null;
155
        this.geometryType = Geometry.TYPES.NULL;
156
        this.geometrySubType = Geometry.SUBTYPES.UNKNOWN;
157
        this.additionalInfo = null;
158
        this.isAutomatic = false;
159
        this.hidden = false;
160
        this.relationType = RELATION_TYPE_NONE;
161
    }
162

    
163
    protected DefaultFeatureAttributeDescriptor(
164
            DefaultFeatureAttributeDescriptor other
165
        ) {
166
        this();
167
        copyFrom(other);
168
//        LOGGER.info(String.format("Created FeatureAttributeDescriptor [%08x] [%s] (copy).", this.hashCode(), this.name));
169
    }
170
    
171
    @Override
172
    public void copyFrom(DynField other1) {
173
        if( !(other1 instanceof DefaultFeatureAttributeDescriptor) ) {
174
            throw new IllegalArgumentException("Can't copy from a non DefaultFeatureAttributeDescriptor");
175
        }
176
        DefaultFeatureAttributeDescriptor other = (DefaultFeatureAttributeDescriptor) other1;
177
        this.typeRef = other.typeRef;
178
        this.allowNull = other.allowNull;
179
        this.dataType = other.dataType;
180
        this.dateFormat = other.dateFormat;
181
        this.defaultValue = other.defaultValue;
182
        this.index = other.index;
183
        this.maximumOccurrences = other.maximumOccurrences;
184
        this.minimumOccurrences = other.minimumOccurrences;
185
        this.size = other.size;
186
        this.name = other.name;
187
        this.objectClass = other.objectClass;
188
        this.precision = other.precision;
189
        this.evaluator = other.evaluator;
190
        this.primaryKey = other.primaryKey;
191
        this.readOnly = other.readOnly;
192
        this.SRS = other.SRS;
193
        this.geometryType = other.geometryType;
194
        this.geometrySubType = other.geometrySubType;
195
        this.geomType = other.geomType;
196
        if (other.additionalInfo != null) {
197
            Iterator iter = other.additionalInfo.entrySet().iterator();
198
            Map.Entry entry;
199
            this.additionalInfo = new HashMap();
200
            while (iter.hasNext()) {
201
                entry = (Entry) iter.next();
202
                this.additionalInfo.put(entry.getKey(), entry.getValue());
203
            }
204
        } else {
205
            this.additionalInfo = null;
206
        }
207
        this.isAutomatic = other.isAutomatic;
208
        this.isTime = other.isTime;
209
        this.featureAttributeEmulator = other.featureAttributeEmulator;
210
        this.indexed = other.indexed;
211
        this.isIndexAscending = other.isIndexAscending;
212
        this.allowIndexDuplicateds = other.allowIndexDuplicateds;
213
        this.hidden = other.hidden;
214
        this.dataProfile = other.dataProfile;
215
        
216
        this.availableValues = other.availableValues;
217
        this.description = other.description;
218
        this.minValue = other.minValue;
219
        this.maxValue = other.maxValue;
220
        this.label = other.label;
221
        this.order = other.order;
222
        this.groupName = other.groupName;
223
        if( other.tags==null ) {
224
            this.tags = null;
225
        } else {
226
            try {
227
                this.tags = (Tags) other.tags.clone();
228
            } catch (Exception ex) {
229
            }
230
        }
231
        this.foreingKey = null;
232
        if( other.foreingKey!=null ) {
233
            try {
234
                this.foreingKey = (DefaultForeingKey) other.foreingKey.clone();
235
            } catch (CloneNotSupportedException ex) {
236
            }
237
        }
238
        if( this.foreingKey!=null ) {
239
            this.foreingKey.setDescriptor(this);
240
        }
241
        
242
        // TODO: ? Habria que clonarlos ?
243
        this.availableValuesMethod = other.availableValuesMethod;
244
        this.calculateMethod = other.calculateMethod;
245
        this.relationType = other.relationType;
246
    }
247
    
248
    public void setFeatureType(FeatureType type) {
249
        // Usada en la persistencia
250
        if( type == null ) {
251
            this.typeRef = null;
252
        } else {
253
            this.typeRef = new WeakReference(type);
254
//            LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] set FeatureType [%08x], ref [%08x].", this.hashCode(), type.hashCode(), typeRef.hashCode()));
255
        }
256
    }
257
    
258
    @Override
259
    public String getDataTypeName() {
260
        if (this.getDataType() == null) {
261
            return "(unknow)";
262
        }
263
        return this.getDataType().getName();
264
    }
265

    
266
    @Override
267
    public DefaultFeatureAttributeDescriptor getCopy() {
268
        return new DefaultFeatureAttributeDescriptor(this);
269
    }
270

    
271
    @Override
272
    public Object clone() throws CloneNotSupportedException {
273
        return new DefaultFeatureAttributeDescriptor(this);
274
    }
275
    
276
    @Override
277
    public boolean allowNull() {
278
        return allowNull;
279
    }
280

    
281
    @Override
282
    public DataType getDataType() {
283
        if (featureAttributeGetter != null) {
284
            return featureAttributeGetter.getDataType();
285
        }
286
        return this.dataType;
287
    }
288

    
289
    public FeatureAttributeDescriptor setDataType(int type) {
290
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
291
        return this;
292
    }
293

    
294
    @Override
295
    public DateFormat getDateFormat() {
296
        return this.dateFormat;
297
    }
298

    
299
    @Override
300
    public Object getDefaultValue() {
301
        return this.defaultValue;
302
    }
303

    
304
    @Override
305
    public Evaluator getEvaluator() {
306
        return this.evaluator;
307
    }
308

    
309
    @Override
310
    public int getGeometryType() {
311
        if( this.dataType.getType()!=DataTypes.GEOMETRY ) {
312
            return Geometry.TYPES.UNKNOWN;
313
        }
314
        return this.geometryType;
315
    }
316

    
317
    @Override
318
    public int getGeometrySubType() {
319
        if( this.dataType.getType()!=DataTypes.GEOMETRY ) {
320
            return Geometry.SUBTYPES.UNKNOWN;
321
        }
322
        return this.geometrySubType;
323
    }
324

    
325
    @Override
326
    public GeometryType getGeomType() {
327
        if( this.dataType.getType()!=DataTypes.GEOMETRY ) {
328
            return null;
329
        }
330
        if (this.geomType == null) {
331
            try {
332
                this.geomType
333
                        = GeometryLocator.getGeometryManager().getGeometryType(
334
                                this.geometryType, this.geometrySubType);
335
            } catch (GeometryException e) {
336
                throw new RuntimeException(
337
                        "Error getting geometry type with type = "
338
                        + this.geometryType + ", subtype = "
339
                        + this.geometrySubType, e);
340
            }
341
        }
342
        return this.geomType;
343
    }
344

    
345
    @Override
346
    public int getIndex() {
347
        return this.index;
348
    }
349

    
350
    protected FeatureAttributeDescriptor setIndex(int index) {
351
        this.index = index;
352
        return this;
353
    }
354

    
355
    @Override
356
    public int getMaximumOccurrences() {
357
        return this.maximumOccurrences;
358
    }
359

    
360
    @Override
361
    public int getMinimumOccurrences() {
362
        return this.minimumOccurrences;
363
    }
364

    
365
    @Override
366
    public String getName() {
367
        return this.name;
368
    }
369
    
370
    public FeatureAttributeDescriptor setName(String name) {
371
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] set name [%s].", this.hashCode(), name));
372
        this.name = name;
373
        return this;
374
    }
375
    
376
    @Override
377
    public Class getObjectClass() {
378
        if (getDataType().getType() == DataTypes.OBJECT) {
379
            return objectClass;
380
        }
381
        return getDataType().getDefaultClass();
382
    }
383

    
384
    @Override
385
    public int getPrecision() {
386
        return this.precision;
387
    }
388

    
389
    @Override
390
    public IProjection getSRS() {
391
        return this.SRS;
392
    }
393

    
394
    @Override
395
    public Interval getInterval() {
396
        return this.interval;
397
    }
398

    
399
    public IProjection getSRS(WeakReference storeRef) {
400
        if( this.SRS==null ) {
401
            FeatureStore store = (FeatureStore) storeRef.get();
402
            this.SRS = (IProjection) store.getDynValue(DataStore.METADATA_CRS);
403
        }
404
        return this.SRS;
405
    }
406

    
407

    
408
    @Override
409
    public int getSize() {
410
        return this.size;
411
    }
412

    
413
    @Override
414
    public boolean isPrimaryKey() {
415
        return this.primaryKey;
416
    }
417

    
418
    @Override
419
    public boolean isReadOnly() {
420
        if (this.readOnly) {
421
            return true;
422
        }
423
        if (this.getEvaluator() != null) {
424
            return true;
425
        }
426
        if (this.featureAttributeEmulator != null) {
427
            return !this.featureAttributeEmulator.allowSetting();
428
        }
429
        return false;
430
    }
431

    
432
    @Override
433
    public Object getAdditionalInfo(String infoName) {
434
        if (this.additionalInfo == null) {
435
            return null;
436
        }
437
        return this.additionalInfo.get(infoName);
438
    }
439

    
440
    @Override
441
    public boolean isAutomatic() {
442
        return this.isAutomatic;
443
    }
444

    
445
    @Override
446
    public boolean equals(Object obj) {
447
        if (this == obj) {
448
            return true;
449
        }
450
        if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
451
            return false;
452
        }
453
        DefaultFeatureAttributeDescriptor other
454
                = (DefaultFeatureAttributeDescriptor) obj;
455

    
456
        if (this.allowNull != other.allowNull) {
457
            return false;
458
        }
459

    
460
        if (this.index != other.index) {
461
            return false;
462
        }
463

    
464
        if (!Objects.equals(this.name, other.name)) {
465
            return false;
466
        }
467

    
468
        if (this.getDataType() != other.getDataType()) {
469
            return false;
470
        }
471

    
472
        if (this.size != other.size) {
473
            return false;
474
        }
475

    
476
        if (!Objects.equals(this.defaultValue, other.defaultValue)) {
477
            return false;
478
        }
479

    
480
        if (this.primaryKey != other.primaryKey) {
481
            return false;
482
        }
483

    
484
        if (this.isAutomatic != other.isAutomatic) {
485
            return false;
486
        }
487

    
488
        if (this.readOnly != other.readOnly) {
489
            return false;
490
        }
491

    
492
        if (this.precision != other.precision) {
493
            return false;
494
        }
495

    
496
        if (this.maximumOccurrences != other.maximumOccurrences) {
497
            return false;
498
        }
499

    
500
        if (this.minimumOccurrences != other.minimumOccurrences) {
501
            return false;
502
        }
503

    
504
        if (this.geometryType != other.geometryType) {
505
            return false;
506
        }
507

    
508
        if (this.geometrySubType != other.geometrySubType) {
509
            return false;
510
        }
511

    
512
        if (!Objects.equals(this.evaluator, other.evaluator)) {
513
            return false;
514
        }
515

    
516
        if (!Objects.equals(this.featureAttributeEmulator, other.featureAttributeEmulator)) {
517
            return false;
518
        }
519

    
520
        if (!Objects.equals(this.SRS, other.SRS)) {
521
            return false;
522
        }
523

    
524
        if (!Objects.equals(this.dateFormat, other.dateFormat)) {
525
            return false;
526
        }
527

    
528
        if (!Objects.equals(this.objectClass, other.objectClass)) {
529
            return false;
530
        }
531

    
532
        if (!Objects.equals(this.dataProfile, other.dataProfile)) {
533
            return false;
534
        }
535

    
536
        return true;
537
    }
538

    
539
    @Override
540
    public void loadFromState(PersistentState state)
541
            throws PersistenceException {
542
        allowNull = state.getBoolean("allowNull");
543
        dataType = ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
544
        dataProfile = state.getString("dataProfile");
545
        
546
//        FIXME: dateFormat;
547
        try {
548
            defaultValue = dataType.coerce(state.get("defaultValue"));
549
        } catch (CoercionException ex) {
550
        }
551

    
552
        index = state.getInt("index");
553
        maximumOccurrences = state.getInt("maximumOccurrences");
554
        minimumOccurrences = state.getInt("minimumOccurrences");
555
        size = state.getInt("size");
556
        name = state.getString("name");
557
        try {
558
            String objectClassName = state.getString("objectClass"); 
559
            if( !StringUtils.isBlank(objectClassName) ) { 
560
                objectClass = Class.forName(objectClassName); 
561
            }
562
        } catch (Throwable e) {
563
            LOGGER.warn("Can't restore the objectClass of the FeatureAttributreDescriptor", e);
564
        }
565
        precision = state.getInt("precision");
566
        evaluator = (Evaluator) state.get("evaluator");
567
        primaryKey = state.getBoolean("primaryKey");
568
        readOnly = state.getBoolean("readOnly");
569
        SRS = (IProjection) state.get("SRS");
570
        geometryType = state.getInt("geometryType");
571
        geometrySubType = state.getInt("geometrySubType");
572
        if( geometryType!=Geometry.TYPES.UNKNOWN && 
573
                geometrySubType!=Geometry.SUBTYPES.UNKNOWN ) {
574
            geomType = GeometryUtils.getGeometryType(
575
                    geometryType, 
576
                    geometrySubType
577
            );
578
        }
579
//        additionalInfo = (Map) state.get("aditionalInfo");
580
        isAutomatic = state.getBoolean("isAutomatic");
581
        isTime = state.getBoolean("isTime");
582
        if( state.hasValue("intervalStart") ) {
583
            long intervalStart = state.getLong("interval_start");
584
            long intervalEnd = state.getLong("interval_end");
585
            interval = TimeSupportLocator.getManager().createRelativeInterval(intervalStart, intervalEnd);
586
        } else {
587
            interval = null;
588
        }
589
        featureAttributeEmulator = (FeatureAttributeEmulator) state.get("featureAttributeEmulator");
590
        indexed = state.getBoolean("indexed");
591
        isIndexAscending = state.getBoolean("isIndexAscending");
592
        allowIndexDuplicateds = state.getBoolean("allowIndexDuplicateds");
593

    
594
        Map<String,Object> values = state.getMap("availableValues");
595
        if( values == null || values.isEmpty()  ) {
596
            this.availableValues = null;
597
        } else {
598
            this.availableValues = new DynObjectValueItem[values.size()];
599
            int n = 0;
600
            for (Entry<String, Object> entry : values.entrySet()) {
601
                this.availableValues[n++] = new DynObjectValueItem(entry.getValue(), entry.getKey());
602
            }
603
        }
604
        
605
        description = state.getString("description");
606
        minValue = state.get("minValue");
607
        maxValue = state.get("maxValue");
608
        label = state.getString("label");
609
        order = state.getInt("order");
610
        hidden = state.getBoolean("hidden");
611
        groupName = state.getString("groupName");
612
        relationType = state.getInt("relationType");
613
        
614
        foreingKey = (DefaultForeingKey) state.get("foreingKey");
615
        if( foreingKey!=null ) {
616
            this.foreingKey.setDescriptor(this);
617
        }
618
        tags = (Tags) state.get("tags");
619
        if( tags == null ) {
620
            this.tags = new DefaultTags();
621
        }
622
    }
623

    
624
    @Override
625
    public void saveToState(PersistentState state) throws PersistenceException {
626
        state.set("allowNull", allowNull);
627
        state.set("dataType", dataType.getType());
628
        state.set("dataProfile", dataProfile);
629
        
630
//        FIXME: dateFormat;
631

    
632
        state.set("defaultValue", Objects.toString(defaultValue));
633

    
634
        state.set("index", index);
635
        state.set("maximumOccurrences", maximumOccurrences);
636
        state.set("minimumOccurrences", minimumOccurrences);
637
        state.set("size", size);
638
        state.set("name", name);
639
        state.set("objectClass", objectClass==null? null:objectClass.getName());
640
        state.set("precision", precision);
641
        state.set("evaluator", evaluator);
642
        
643
        state.set("primaryKey", primaryKey);
644
        state.set("readOnly", readOnly);
645
        state.set("SRS", SRS);
646
        GeometryType theGeomType = this.getGeomType();
647
        if( theGeomType==null ) {
648
            state.set("geometryType", Geometry.TYPES.UNKNOWN);
649
            state.set("geometrySubType", Geometry.SUBTYPES.UNKNOWN);
650
        } else {
651
            state.set("geometryType", theGeomType.getType());
652
            state.set("geometrySubType", theGeomType.getSubType());
653
        }
654

    
655
//      FIXME: additionalInfo
656

    
657
        state.set("isAutomatic", isAutomatic);
658
        state.set("isTime", isTime);
659
        if( this.interval==null ) {
660
            state.setNull("interval_start");
661
            state.setNull("interval_end");
662
        } else {
663
            state.set("interval_start", ((RelativeInterval)interval).getStart().toMillis());
664
            state.set("interval_end", ((RelativeInterval)interval).getEnd().toMillis());
665
        }
666
        state.set("SRS", SRS);
667

    
668
//      FIXME: featureAttributeGetter
669

    
670
        if( featureAttributeEmulator instanceof Persistent ) {
671
            state.set("featureAttributeEmulator", featureAttributeEmulator);
672
        } else {
673
            state.setNull("featureAttributeEmulator");
674
        }
675

    
676
        state.set("indexed", indexed);
677
        state.set("isIndexAscending", isIndexAscending);
678
        state.set("allowIndexDuplicateds", allowIndexDuplicateds);
679
        
680
        if( this.availableValues==null ) {
681
            state.setNull("availableValues");
682
        } else {
683
            Map<String,Object> values = new HashMap<>();
684
            for (DynObjectValueItem value : availableValues) {
685
                values.put(value.getLabel(),value.getValue());
686
            }
687
            state.set("availableValues", values);
688
        }
689
        state.set("description", description);
690
        state.set("minValue", minValue);
691
        state.set("maxValue", maxValue);
692
        state.set("label", label);
693
        state.set("order", order);
694
        state.set("hidden", hidden);
695
        state.set("groupName", groupName);
696
        state.set("relationType",relationType);
697
        
698
        state.set("foreingKey" ,this.foreingKey);
699
        state.set("tags" ,this.tags);
700

    
701
    }
702
    
703
    private static final String FEATATTRDESC_PERSISTENCE_DEFINITION_NAME = "FeatureAttributeDescriptor";
704

    
705
    public static void registerPersistenceDefinition() {
706
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
707
        
708

    
709
        if (manager.getDefinition(FEATATTRDESC_PERSISTENCE_DEFINITION_NAME)
710
                == null) {
711
            DynStruct definition = manager.addDefinition(DefaultFeatureAttributeDescriptor.class,
712
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME,
713
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME
714
                        + " persistent definition",
715
                    null,
716
                    null
717
            );
718
            definition.addDynFieldBoolean("allowNull");
719
            definition.addDynFieldInt("dataType");
720
            definition.addDynFieldString("dataProfile");
721
//            definition.addDynFieldString("dateFormat");
722
            definition.addDynFieldString("defaultValue");
723
            definition.addDynFieldInt("index");
724
            definition.addDynFieldInt("maximumOccurrences");
725
            definition.addDynFieldInt("minimumOccurrences");
726
            definition.addDynFieldInt("size");
727
            definition.addDynFieldString("name");
728
            definition.addDynFieldString("objectClass");
729
            definition.addDynFieldInt("precision");
730
            definition.addDynFieldObject("evaluator")
731
                    .setClassOfValue(Evaluator.class);
732
            definition.addDynFieldBoolean("primaryKey");
733
            definition.addDynFieldBoolean("readOnly");
734
            definition.addDynFieldObject("SRS")
735
                    .setClassOfValue(IProjection.class);
736
            definition.addDynFieldInt("geometryType");
737
            definition.addDynFieldInt("geometrySubType");
738
//            definition.addDynFieldMap("additionalInfo");
739
            definition.addDynFieldBoolean("isAutomatic");
740
            definition.addDynFieldBoolean("isTime");
741
            definition.addDynFieldLong("interval_start");
742
            definition.addDynFieldLong("interval_end");
743
            definition.addDynFieldObject("featureAttributeEmulator")
744
                    .setClassOfValue(FeatureAttributeEmulator.class);
745
            definition.addDynFieldBoolean("indexed");
746
            definition.addDynFieldBoolean("isIndexAscending");
747
            definition.addDynFieldBoolean("allowIndexDuplicateds");
748
            definition.addDynFieldMap("availableValues");
749
            definition.addDynFieldString("description");
750
            definition.addDynFieldObject("minValue");
751
            definition.addDynFieldObject("maxValue");
752
            definition.addDynFieldString("label");
753
            definition.addDynFieldInt("order");
754
            definition.addDynFieldBoolean("hidden");
755
            definition.addDynFieldString("groupName");
756
            definition.addDynFieldInt("relationType");
757
            
758
            definition.addDynFieldObject("foreingKey")
759
                    .setClassOfValue(DefaultForeingKey.class);
760
            
761
            definition.addDynFieldObject("tags")
762
                    .setClassOfValue(Tags.class);
763
            
764
        }
765
    }
766

    
767
    
768
    /*
769
     * Start of DynField interface Implementation
770
     *
771
     */
772

    
773
    @Override
774
    public Tags getTags() {
775
        return tags;
776
    }
777

    
778
    @Override
779
    public DynObjectValueItem[] getAvailableValues() {
780
        if( this.availableValues == null ) {
781
            if( this.availableValuesFromForeingKey==null ) {
782
                if( this.isForeingKey() && this.foreingKey.isSelectable() ) {
783
                    this.availableValuesFromForeingKey = this.foreingKey.getAvailableValues(null);
784
                }
785
                return this.availableValuesFromForeingKey;
786
            }
787
        }
788
        return this.availableValues;
789
    }
790

    
791
    @Override
792
    public String getDescription() {
793
        if( this.description == null ) {
794
            return getName();
795
        }
796
        return this.description;
797
    }
798

    
799
    @Override
800
    public Object getMaxValue() {
801
        return this.maxValue;
802
    }
803

    
804
    @Override
805
    public Object getMinValue() {
806
        return this.minValue;
807
    }
808

    
809
    @Override
810
    public int getTheTypeOfAvailableValues() {
811
        return 1;
812
    }
813

    
814
    @Override
815
    public int getType() {
816
        if (featureAttributeGetter != null) {
817
            return featureAttributeGetter.getDataType().getType();
818
        }
819
        return getDataType().getType();
820
    }
821

    
822
    @Override
823
    public boolean isMandatory() {
824
        return !allowNull() || isPrimaryKey();
825
    }
826

    
827
    @Override
828
    public boolean isPersistent() {
829
        return false;
830
    }
831

    
832
    @Override
833
    public DynField setAvailableValues(DynObjectValueItem[] values) {
834
        if ( ArrayUtils.isEmpty(values) ) {
835
            this.availableValues = null;
836
        } else {
837
            this.availableValues = values;
838
        }
839
        return this;
840
    }
841

    
842
    @Override
843
    public DynField setDescription(String description) {
844
        this.description = description;
845
        return this;
846
    }
847

    
848
    @Override
849
    public DynField setMandatory(boolean mandatory) {
850
        throw new UnsupportedOperationException();
851
    }
852

    
853
    @Override
854
    public DynField setMaxValue(Object maxValue) {
855
        try {
856
            this.maxValue = this.coerce(maxValue);
857
        } catch (CoercionException e) {
858
            throw new IllegalArgumentException(e);
859
        }
860
        return this;
861
    }
862

    
863
    @Override
864
    public DynField setMinValue(Object minValue) {
865
        try {
866
            this.maxValue = this.coerce(minValue);
867
        } catch (CoercionException e) {
868
            throw new IllegalArgumentException(e);
869
        }
870
        return this;
871
    }
872

    
873
    @Override
874
    public DynField setPersistent(boolean persistent) {
875
        throw new UnsupportedOperationException();
876
    }
877

    
878
    @Override
879
    public DynField setTheTypeOfAvailableValues(int type) {
880
        throw new UnsupportedOperationException();
881
    }
882

    
883
    @Override
884
    public DynField setType(int type) {
885
        throw new UnsupportedOperationException();
886
    }
887

    
888
    @Override
889
    public DynField setDefaultDynValue(Object defaultValue) {
890
        throw new UnsupportedOperationException();
891
    }
892

    
893
    @Override
894
    public Class getClassOfValue() {
895
        return null;
896
    }
897

    
898
    @Override
899
    public DynField getElementsType() {
900
        return null;
901
    }
902

    
903
    @Override
904
    public DynField setClassOfValue(Class theClass)
905
            throws DynFieldIsNotAContainerException {
906
        throw new UnsupportedOperationException();
907
    }
908

    
909
    @Override
910
    public DynField setElementsType(DynStruct type)
911
            throws DynFieldIsNotAContainerException {
912
        throw new UnsupportedOperationException();
913
    }
914

    
915
    @Override
916
    public DynField setElementsType(int type)
917
            throws DynFieldIsNotAContainerException {
918
        throw new UnsupportedOperationException();
919
    }
920

    
921
    public FeatureAttributeDescriptor setDataProfileName(String dataProfile) {
922
        this.dataProfile = dataProfile;
923
        return this;
924
    }
925

    
926
    @Override
927
    public String getDataProfileName() {
928
        return dataProfile;
929
    }
930

    
931
    @Override
932
    public void validate(Object value) throws DynFieldValidateException {
933

    
934
        if (value == null && !this.allowNull()) {
935
            throw new DynFieldValidateException(value, this, null);
936
        }
937

    
938
        try {
939
            this.dataType.coerce(value);
940
        } catch (CoercionException e) {
941
            throw new DynFieldValidateException(value, this, e);
942
        }
943

    
944
        /*
945
         * Other checks will be needed
946
         */
947
    }
948

    
949
    @Override
950
    public String getSubtype() {
951
        if (featureAttributeGetter != null) {
952
            return featureAttributeGetter.getDataType().getSubtype();
953
        }
954
        return this.dataType.getSubtype();
955
    }
956

    
957
    @Override
958
    public Object coerce(Object value) throws CoercionException {
959
        if ( value == null ) {
960
            return value; // O debe devolver this.defaultValue
961
        }
962
        try {
963
            return this.getDataType().coerce(value);
964
        } catch(Exception ex){
965
            throw new RuntimeException(ex);
966
        }
967
    }
968

    
969
    @Override
970
    public DynField setAvailableValues(List values) {
971
        if (  values == null || values.isEmpty() ) {
972
            this.availableValues = null;
973
        } else {
974
            this.availableValues = (DynObjectValueItem[]) values.toArray(
975
                new DynObjectValueItem[values.size()]
976
            );
977
        }
978
        return this;
979
    }
980

    
981
    @Override
982
    public String getGroup() {
983
        return this.groupName;
984
    }
985

    
986
    @Override
987
    public int getOder() {
988
        return this.order;
989
    }
990

    
991
    @Override
992
    public String getLabel() {
993
        if( this.label == null ) {
994
            return this.getName();
995
        }
996
        return this.label;
997
    }
998

    
999
    @Override
1000
    public DynField setLabel(String label) {
1001
        this.label = label;
1002
        return this;
1003
    }
1004

    
1005
    @Override
1006
    public DynField setShortLabel(String shortLabel) {
1007
        this.shortLabel = shortLabel;
1008
        return this;
1009
    }
1010

    
1011
    @Override
1012
    public String getShortLabel() {
1013
        return StringUtils.isBlank(shortLabel) ? getLabel() : shortLabel;
1014
    }
1015

    
1016
    @Override
1017
    public DynField setGroup(String groupName) {
1018
        this.groupName = groupName;
1019
        return this;
1020
    }
1021

    
1022
    @Override
1023
    public DynField setOrder(int order) {
1024
        this.order = order;
1025
        return this;
1026
    }
1027

    
1028
    @Override
1029
    public DynField setHidden(boolean hidden) {
1030
        this.hidden = hidden;
1031
        return this;
1032
    }
1033

    
1034
    @Override
1035
    public boolean isHidden() {
1036
        return this.hidden;
1037
    }
1038

    
1039
    @Override
1040
    public DynField setReadOnly(boolean readOnly) {
1041
        this.readOnly = readOnly;
1042
        return this;
1043
    }
1044

    
1045
    @Override
1046
    public boolean isContainer() {
1047
        return false;
1048
    }
1049

    
1050
    @Override
1051
    public Class getClassOfItems() {
1052
        return null;
1053
    }
1054

    
1055
    @Override
1056
    public DynField setDefaultFieldValue(Object defaultValue) {
1057
        throw new UnsupportedOperationException();
1058
    }
1059

    
1060
    @Override
1061
    public DynField setClassOfItems(Class theClass) {
1062
        throw new UnsupportedOperationException();
1063
    }
1064

    
1065
    @Override
1066
    public DynField setType(DataType type) {
1067
        throw new UnsupportedOperationException();
1068
    }
1069

    
1070
    @Override
1071
    public DynField setSubtype(String subtype) {
1072
        throw new UnsupportedOperationException();
1073
    }
1074

    
1075
    @Override
1076
    public boolean isTime() {
1077
        return isTime;
1078
    }
1079

    
1080
    @Override
1081
    public FeatureAttributeGetter getFeatureAttributeGetter() {
1082
        return featureAttributeGetter;
1083
    }
1084

    
1085
    @Override
1086
    public void setFeatureAttributeGetter(
1087
            FeatureAttributeGetter featureAttributeTransform) {
1088
        this.featureAttributeGetter = featureAttributeTransform;
1089
    }
1090

    
1091
    @Override
1092
    public FeatureAttributeEmulator getFeatureAttributeEmulator() {
1093
        return this.featureAttributeEmulator;
1094
    }
1095

    
1096
    public FeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
1097
        this.featureAttributeEmulator = featureAttributeEmulator;
1098
        return this;
1099
    }
1100
        
1101
    @Override
1102
    public boolean isIndexed() {
1103
        return this.indexed;
1104
    }
1105
    
1106
    public boolean isForeingKey() {
1107
        return this.foreingKey!=null && this.foreingKey.isForeingKey();
1108
    }
1109
    
1110
    public ForeingKey getForeingKey() {
1111
        return this.foreingKey;
1112
    }
1113

    
1114
    @Override
1115
    public boolean allowIndexDuplicateds() {
1116
        return this.allowIndexDuplicateds;
1117
    }
1118

    
1119
    @Override
1120
    public boolean isIndexAscending() {
1121
        return this.isIndexAscending;
1122
    }
1123

    
1124
    @Override
1125
    public DynField setClassOfValue(DynStruct dynStrct) {
1126
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1127
    }
1128

    
1129
    @Override
1130
    public DynField setClassOfValue(String theClassNameOfValue) {
1131
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1132
    }
1133

    
1134
    @Override
1135
    public String getClassNameOfValue() {
1136
        return null;
1137
    }
1138

    
1139
    @Override
1140
    public DynStruct getDynClassOfValue() {
1141
        return null;
1142
    }
1143

    
1144
    @Override
1145
    public DynField setTypeOfItems(int type) {
1146
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1147
    }
1148

    
1149
    @Override
1150
    public int getTypeOfItems() {
1151
        return DataTypes.INVALID;
1152
    }
1153

    
1154
    @Override
1155
    public DynField setClassOfItems(DynStruct dynStrct) {
1156
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1157
    }
1158

    
1159
    @Override
1160
    public DynField setClassOfItems(String theClassNameOfValue) {
1161
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1162
    }
1163

    
1164
    @Override
1165
    public String getClassNameOfItems() {
1166
        return null;
1167
    }
1168

    
1169
    @Override
1170
    public DynStruct getDynClassOfItems() {
1171
        return null;
1172
    }
1173

    
1174
    @Override
1175
    public DynField setRelationType(int relationType) {
1176
        this.relationType = relationType;
1177
        return this;
1178
    }
1179

    
1180
    @Override
1181
    public int getRelationType() {
1182
        return this.relationType;
1183
    }
1184

    
1185
    @Override
1186
    public DynField setAvailableValues(DynMethod availableValuesMethod) {
1187
        this.availableValuesMethod = availableValuesMethod;
1188
        return this;
1189
    }
1190

    
1191
    @Override
1192
    public DynObjectValueItem[] getAvailableValues(DynObject self) {
1193
        if( this.availableValuesMethod != null ) {
1194
            DynObjectValueItem[] values;
1195
            try {
1196
                values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(self,new Object[] {this});
1197
            } catch (DynMethodException ex) {
1198
                return this.availableValues;
1199
            }
1200
            if( values != null ) {
1201
                return values;
1202
            }
1203
        }
1204
        return this.availableValues;
1205
    }
1206

    
1207
    @Override
1208
    public DynMethod getAvailableValuesMethod() {
1209
        return this.availableValuesMethod;
1210
    }
1211

    
1212
    @Override
1213
    public boolean isAvailableValuesCalculated() {
1214
        return this.availableValuesMethod!=null;
1215
    }
1216

    
1217
    @Override
1218
    public DynMethod getCalculateMethod() {
1219
        return this.calculateMethod;
1220
    }
1221

    
1222
    @Override
1223
    public DynField setCalculateMethod(DynMethod method) {
1224
        this.calculateMethod = method;
1225
        return this;
1226
    }
1227
    
1228
    @Override
1229
    public boolean isCalculated() {
1230
        return this.calculateMethod != null;
1231
    }
1232
    
1233
    @Override
1234
    public Object getCalculatedValue(DynObject self) {
1235
        try {
1236
            return this.calculateMethod.invoke(self, new Object[] { this });
1237
        } catch (DynMethodException ex) {
1238
            throw new RuntimeException(ex);
1239
        }
1240
    }
1241

    
1242
    @Override
1243
    public DynField setValidateElements(boolean validate) {
1244
        return this;
1245
    }
1246

    
1247
    @Override
1248
    public boolean getValidateElements() {
1249
        return false;
1250
    }
1251

    
1252
    private class ConstantValueEvaluator extends AbstractEvaluator {
1253

    
1254
        @Override
1255
        public Object evaluate(EvaluatorData data) throws EvaluatorException {
1256
            return defaultValue;
1257
        }
1258

    
1259
        @Override
1260
        public String getName() {
1261
            return "Constant attribute " + name;
1262
        }
1263
    }
1264

    
1265
    public void setConstantValue(boolean isConstantValue) {
1266
        if (isConstantValue) {
1267
            /* Cuando un attributo tiene asociado un evaluador, este se interpreta
1268
             * como que no debe cargarse de la fuente de datos subyacente, siendo
1269
             * el evaluador el que se encarga de proporcionar su valor.
1270
             * Nos limitamos a asignar un evaluador que retorna simpre el valor
1271
             * por defecto para ese attributo.
1272
             */
1273
            this.evaluator = new ConstantValueEvaluator();
1274
        } else {
1275
            this.evaluator = null;
1276
        }
1277
    }
1278

    
1279
    @Override
1280
    public boolean isComputed() {
1281
        return featureAttributeEmulator!=null || evaluator!=null || isCalculated();
1282
    }
1283

    
1284
    @Override
1285
    public FeatureStore getStore() {
1286
        FeatureType ftype = this.getFeatureType();
1287
        if( ftype == null ) {
1288
            return null;
1289
        }
1290
        return ftype.getStore();
1291
    }
1292
    
1293
    @Override
1294
    public FeatureType getFeatureType() {
1295
        if( this.typeRef==null ) {
1296
            return null;
1297
        }
1298
        FeatureType ftype = (FeatureType) this.typeRef.get();
1299
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] get FeatureType [%08x], ref [%08x].", this.hashCode(), ftype.hashCode(), typeRef.hashCode()));
1300
        return ftype;
1301
    }
1302

    
1303
    public FeatureAttributeDescriptor setInterval(Interval interval) {
1304
        this.interval = interval;
1305
        return this;
1306
    }
1307

    
1308
    public void fixAll() {
1309
        switch(this.getType()) {
1310
            case DataTypes.INSTANT:
1311
            case DataTypes.INTERVAL:
1312
            case DataTypes.DATE:
1313
                if( this.getInterval()!=null ) {
1314
                    this.isTime = true;
1315
                }
1316
                break;
1317
        }
1318
    }
1319

    
1320
    @Override
1321
    public String[] getRequiredFieldNames() {
1322
        FeatureAttributeEmulator emulator = this.getFeatureAttributeEmulator();
1323
        if( emulator==null ) {
1324
            return null;
1325
        }
1326
        return emulator.getRequiredFieldNames();
1327
    }
1328

    
1329
    @Override
1330
    public void recentUsed() {
1331
        DefaultFeatureType.RECENTS_USEDS.add(this);
1332
    }
1333

    
1334
}