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

History | View | Annotate | Download (40.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 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.dynobject.DynField;
56
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
57
import org.gvsig.tools.dynobject.DynField_v2;
58
import org.gvsig.tools.dynobject.DynMethod;
59
import org.gvsig.tools.dynobject.DynObject;
60
import org.gvsig.tools.dynobject.DynObjectValueItem;
61
import org.gvsig.tools.dynobject.DynStruct;
62
import org.gvsig.tools.dynobject.Tags;
63
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
64
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
65
import org.gvsig.tools.dynobject.exception.DynMethodException;
66
import org.gvsig.tools.dynobject.impl.DefaultTags;
67
import org.gvsig.tools.evaluator.AbstractEvaluator;
68
import org.gvsig.tools.evaluator.Evaluator;
69
import org.gvsig.tools.evaluator.EvaluatorData;
70
import org.gvsig.tools.evaluator.EvaluatorException;
71
import org.gvsig.tools.i18n.I18nManager;
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 String description;
116
    protected Object minValue;
117
    protected Object maxValue;
118
    protected String label;
119
    protected String shortLabel;
120
    protected int order;
121
    protected boolean hidden;
122
    protected String groupName;
123
    protected Tags tags = new DefaultTags();
124
    private DynMethod availableValuesMethod;
125
    private DynMethod calculateMethod;
126
    private WeakReference typeRef;
127
    protected DefaultForeingKey foreingKey = null;
128
    
129
    private int relationType = RELATION_TYPE_NONE;
130

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
406

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
535
        return true;
536
    }
537

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

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

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

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

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

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

    
654
//      FIXME: additionalInfo
655

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

    
667
//      FIXME: featureAttributeGetter
668

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

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

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

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

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

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

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

    
777
    @Override
778
    public DynObjectValueItem[] getAvailableValues() {
779
        if( this.availableValues == null ) {
780
            if( this.isForeingKey() && this.foreingKey.isClosedList() ) {
781
                return this.foreingKey.getAvailableValues(null);
782
            }
783
        }
784
        return this.availableValues;
785
    }
786

    
787
    @Override
788
    public String getDescription() {
789
        if( this.description == null ) {
790
            return getName();
791
        }
792
        return this.description;
793
    }
794

    
795
    @Override
796
    public Object getMaxValue() {
797
        return this.maxValue;
798
    }
799

    
800
    @Override
801
    public Object getMinValue() {
802
        return this.minValue;
803
    }
804

    
805
    @Override
806
    public int getTheTypeOfAvailableValues() {
807
        return 1;
808
    }
809

    
810
    @Override
811
    public int getType() {
812
        if (featureAttributeGetter != null) {
813
            return featureAttributeGetter.getDataType().getType();
814
        }
815
        return getDataType().getType();
816
    }
817

    
818
    @Override
819
    public boolean isMandatory() {
820
        return !allowNull() || isPrimaryKey();
821
    }
822

    
823
    @Override
824
    public boolean isPersistent() {
825
        return false;
826
    }
827

    
828
    @Override
829
    public DynField setAvailableValues(DynObjectValueItem[] values) {
830
        if ( ArrayUtils.isEmpty(values) ) {
831
            this.availableValues = null;
832
        } else {
833
            this.availableValues = values;
834
        }
835
        return this;
836
    }
837

    
838
    @Override
839
    public DynField setDescription(String description) {
840
        this.description = description;
841
        return this;
842
    }
843

    
844
    @Override
845
    public DynField setMandatory(boolean mandatory) {
846
        throw new UnsupportedOperationException();
847
    }
848

    
849
    @Override
850
    public DynField setMaxValue(Object maxValue) {
851
        try {
852
            this.maxValue = this.coerce(maxValue);
853
        } catch (CoercionException e) {
854
            throw new IllegalArgumentException(e);
855
        }
856
        return this;
857
    }
858

    
859
    @Override
860
    public DynField setMinValue(Object minValue) {
861
        try {
862
            this.maxValue = this.coerce(minValue);
863
        } catch (CoercionException e) {
864
            throw new IllegalArgumentException(e);
865
        }
866
        return this;
867
    }
868

    
869
    @Override
870
    public DynField setPersistent(boolean persistent) {
871
        throw new UnsupportedOperationException();
872
    }
873

    
874
    @Override
875
    public DynField setTheTypeOfAvailableValues(int type) {
876
        throw new UnsupportedOperationException();
877
    }
878

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

    
884
    @Override
885
    public DynField setDefaultDynValue(Object defaultValue) {
886
        throw new UnsupportedOperationException();
887
    }
888

    
889
    @Override
890
    public Class getClassOfValue() {
891
        return null;
892
    }
893

    
894
    @Override
895
    public DynField getElementsType() {
896
        return null;
897
    }
898

    
899
    @Override
900
    public DynField setClassOfValue(Class theClass)
901
            throws DynFieldIsNotAContainerException {
902
        throw new UnsupportedOperationException();
903
    }
904

    
905
    @Override
906
    public DynField setElementsType(DynStruct type)
907
            throws DynFieldIsNotAContainerException {
908
        throw new UnsupportedOperationException();
909
    }
910

    
911
    @Override
912
    public DynField setElementsType(int type)
913
            throws DynFieldIsNotAContainerException {
914
        throw new UnsupportedOperationException();
915
    }
916

    
917
    public FeatureAttributeDescriptor setDataProfileName(String dataProfile) {
918
        this.dataProfile = dataProfile;
919
        return this;
920
    }
921

    
922
    @Override
923
    public String getDataProfileName() {
924
        return dataProfile;
925
    }
926

    
927
    @Override
928
    public void validate(Object value) throws DynFieldValidateException {
929

    
930
        if (value == null && !this.allowNull()) {
931
            throw new DynFieldValidateException(value, this, null);
932
        }
933

    
934
        try {
935
            this.dataType.coerce(value);
936
        } catch (CoercionException e) {
937
            throw new DynFieldValidateException(value, this, e);
938
        }
939

    
940
        /*
941
         * Other checks will be needed
942
         */
943
    }
944

    
945
    @Override
946
    public String getSubtype() {
947
        if (featureAttributeGetter != null) {
948
            return featureAttributeGetter.getDataType().getSubtype();
949
        }
950
        return this.dataType.getSubtype();
951
    }
952

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

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

    
977
    @Override
978
    public String getGroup() {
979
        return this.groupName;
980
    }
981

    
982
    @Override
983
    public int getOder() {
984
        return this.order;
985
    }
986

    
987
    @Override
988
    public String getLabel() {
989
        if( this.label == null ) {
990
            return this.getName();
991
        }
992
        return this.label;
993
    }
994
    
995
    @Override
996
    public String getLocalizedLabel() {
997
        if( StringUtils.isBlank(this.label) ) {
998
            return this.getName();
999
        }
1000
        I18nManager i18n = ToolsLocator.getI18nManager();
1001
        return i18n.getTranslation(this.label);
1002
    }
1003

    
1004
    @Override
1005
    public DynField setLabel(String label) {
1006
        this.label = label;
1007
        return this;
1008
    }
1009

    
1010
    @Override
1011
    public DynField setShortLabel(String shortLabel) {
1012
        this.shortLabel = shortLabel;
1013
        return this;
1014
    }
1015

    
1016
    @Override
1017
    public String getShortLabel() {
1018
        return StringUtils.isBlank(shortLabel) ? getLabel() : shortLabel;
1019
    }
1020

    
1021
    @Override
1022
    public String getLocalizedShortLabel() {
1023
        if( StringUtils.isBlank(shortLabel) ) {
1024
            return this.getLocalizedLabel();
1025
        }
1026
        I18nManager i18n = ToolsLocator.getI18nManager();
1027
        return i18n.getTranslation(shortLabel);
1028
    }
1029

    
1030
    @Override
1031
    public DynField setGroup(String groupName) {
1032
        this.groupName = groupName;
1033
        return this;
1034
    }
1035

    
1036
    @Override
1037
    public DynField setOrder(int order) {
1038
        this.order = order;
1039
        return this;
1040
    }
1041

    
1042
    @Override
1043
    public DynField setHidden(boolean hidden) {
1044
        this.hidden = hidden;
1045
        return this;
1046
    }
1047

    
1048
    @Override
1049
    public boolean isHidden() {
1050
        return this.hidden;
1051
    }
1052

    
1053
    @Override
1054
    public DynField setReadOnly(boolean readOnly) {
1055
        this.readOnly = readOnly;
1056
        return this;
1057
    }
1058

    
1059
    @Override
1060
    public boolean isContainer() {
1061
        return false;
1062
    }
1063

    
1064
    @Override
1065
    public Class getClassOfItems() {
1066
        return null;
1067
    }
1068

    
1069
    @Override
1070
    public DynField setDefaultFieldValue(Object defaultValue) {
1071
        throw new UnsupportedOperationException();
1072
    }
1073

    
1074
    @Override
1075
    public DynField setClassOfItems(Class theClass) {
1076
        throw new UnsupportedOperationException();
1077
    }
1078

    
1079
    @Override
1080
    public DynField setType(DataType type) {
1081
        throw new UnsupportedOperationException();
1082
    }
1083

    
1084
    @Override
1085
    public DynField setSubtype(String subtype) {
1086
        throw new UnsupportedOperationException();
1087
    }
1088

    
1089
    @Override
1090
    public boolean isTime() {
1091
        return isTime;
1092
    }
1093

    
1094
    @Override
1095
    public FeatureAttributeGetter getFeatureAttributeGetter() {
1096
        return featureAttributeGetter;
1097
    }
1098

    
1099
    @Override
1100
    public void setFeatureAttributeGetter(
1101
            FeatureAttributeGetter featureAttributeTransform) {
1102
        this.featureAttributeGetter = featureAttributeTransform;
1103
    }
1104

    
1105
    @Override
1106
    public FeatureAttributeEmulator getFeatureAttributeEmulator() {
1107
        return this.featureAttributeEmulator;
1108
    }
1109

    
1110
    public FeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
1111
        this.featureAttributeEmulator = featureAttributeEmulator;
1112
        return this;
1113
    }
1114
        
1115
    @Override
1116
    public boolean isIndexed() {
1117
        return this.indexed;
1118
    }
1119
    
1120
    public boolean isForeingKey() {
1121
        return this.foreingKey!=null && this.foreingKey.isForeingKey();
1122
    }
1123
    
1124
    public ForeingKey getForeingKey() {
1125
        return this.foreingKey;
1126
    }
1127

    
1128
    @Override
1129
    public boolean allowIndexDuplicateds() {
1130
        return this.allowIndexDuplicateds;
1131
    }
1132

    
1133
    @Override
1134
    public boolean isIndexAscending() {
1135
        return this.isIndexAscending;
1136
    }
1137

    
1138
    @Override
1139
    public DynField setClassOfValue(DynStruct dynStrct) {
1140
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1141
    }
1142

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

    
1148
    @Override
1149
    public String getClassNameOfValue() {
1150
        return null;
1151
    }
1152

    
1153
    @Override
1154
    public DynStruct getDynClassOfValue() {
1155
        return null;
1156
    }
1157

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

    
1163
    @Override
1164
    public int getTypeOfItems() {
1165
        return DataTypes.INVALID;
1166
    }
1167

    
1168
    @Override
1169
    public DynField setClassOfItems(DynStruct dynStrct) {
1170
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1171
    }
1172

    
1173
    @Override
1174
    public DynField setClassOfItems(String theClassNameOfValue) {
1175
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1176
    }
1177

    
1178
    @Override
1179
    public String getClassNameOfItems() {
1180
        return null;
1181
    }
1182

    
1183
    @Override
1184
    public DynStruct getDynClassOfItems() {
1185
        return null;
1186
    }
1187

    
1188
    @Override
1189
    public DynField setRelationType(int relationType) {
1190
        this.relationType = relationType;
1191
        return this;
1192
    }
1193

    
1194
    @Override
1195
    public int getRelationType() {
1196
        return this.relationType;
1197
    }
1198

    
1199
    @Override
1200
    public DynField setAvailableValues(DynMethod availableValuesMethod) {
1201
        this.availableValuesMethod = availableValuesMethod;
1202
        return this;
1203
    }
1204

    
1205
    @Override
1206
    public DynObjectValueItem[] getAvailableValues(DynObject self) {
1207
        if( this.availableValuesMethod != null ) {
1208
            DynObjectValueItem[] values;
1209
            try {
1210
                values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(self,new Object[] {this});
1211
            } catch (DynMethodException ex) {
1212
                return this.availableValues;
1213
            }
1214
            if( values != null ) {
1215
                return values;
1216
            }
1217
        }
1218
        return this.availableValues;
1219
    }
1220

    
1221
    @Override
1222
    public DynMethod getAvailableValuesMethod() {
1223
        return this.availableValuesMethod;
1224
    }
1225

    
1226
    @Override
1227
    public boolean isAvailableValuesCalculated() {
1228
        return this.availableValuesMethod!=null;
1229
    }
1230

    
1231
    @Override
1232
    public DynMethod getCalculateMethod() {
1233
        return this.calculateMethod;
1234
    }
1235

    
1236
    @Override
1237
    public DynField setCalculateMethod(DynMethod method) {
1238
        this.calculateMethod = method;
1239
        return this;
1240
    }
1241
    
1242
    @Override
1243
    public boolean isCalculated() {
1244
        return this.calculateMethod != null;
1245
    }
1246
    
1247
    @Override
1248
    public Object getCalculatedValue(DynObject self) {
1249
        try {
1250
            return this.calculateMethod.invoke(self, new Object[] { this });
1251
        } catch (DynMethodException ex) {
1252
            throw new RuntimeException(ex);
1253
        }
1254
    }
1255

    
1256
    @Override
1257
    public DynField setValidateElements(boolean validate) {
1258
        return this;
1259
    }
1260

    
1261
    @Override
1262
    public boolean getValidateElements() {
1263
        return false;
1264
    }
1265

    
1266
    private class ConstantValueEvaluator extends AbstractEvaluator {
1267

    
1268
        @Override
1269
        public Object evaluate(EvaluatorData data) throws EvaluatorException {
1270
            return defaultValue;
1271
        }
1272

    
1273
        @Override
1274
        public String getName() {
1275
            return "Constant attribute " + name;
1276
        }
1277
    }
1278

    
1279
    public void setConstantValue(boolean isConstantValue) {
1280
        if (isConstantValue) {
1281
            /* Cuando un attributo tiene asociado un evaluador, este se interpreta
1282
             * como que no debe cargarse de la fuente de datos subyacente, siendo
1283
             * el evaluador el que se encarga de proporcionar su valor.
1284
             * Nos limitamos a asignar un evaluador que retorna simpre el valor
1285
             * por defecto para ese attributo.
1286
             */
1287
            this.evaluator = new ConstantValueEvaluator();
1288
        } else {
1289
            this.evaluator = null;
1290
        }
1291
    }
1292

    
1293
    @Override
1294
    public boolean isComputed() {
1295
        return featureAttributeEmulator!=null || evaluator!=null || isCalculated();
1296
    }
1297

    
1298
    @Override
1299
    public FeatureStore getStore() {
1300
        FeatureType ftype = this.getFeatureType();
1301
        if( ftype == null ) {
1302
            return null;
1303
        }
1304
        return ftype.getStore();
1305
    }
1306
    
1307
    @Override
1308
    public FeatureType getFeatureType() {
1309
        if( this.typeRef==null ) {
1310
            return null;
1311
        }
1312
        FeatureType ftype = (FeatureType) this.typeRef.get();
1313
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] get FeatureType [%08x], ref [%08x].", this.hashCode(), ftype.hashCode(), typeRef.hashCode()));
1314
        return ftype;
1315
    }
1316

    
1317
    public FeatureAttributeDescriptor setInterval(Interval interval) {
1318
        this.interval = interval;
1319
        return this;
1320
    }
1321

    
1322
    public void fixAll() {
1323
        switch(this.getType()) {
1324
            case DataTypes.INSTANT:
1325
            case DataTypes.INTERVAL:
1326
            case DataTypes.DATE:
1327
                if( this.getInterval()!=null ) {
1328
                    this.isTime = true;
1329
                }
1330
                break;
1331
        }
1332
    }
1333

    
1334
    @Override
1335
    public String[] getRequiredFieldNames() {
1336
        FeatureAttributeEmulator emulator = this.getFeatureAttributeEmulator();
1337
        if( emulator==null ) {
1338
            return null;
1339
        }
1340
        return emulator.getRequiredFieldNames();
1341
    }
1342

    
1343
    @Override
1344
    public void recentUsed() {
1345
        DefaultFeatureType.RECENTS_USEDS.add(this);
1346
    }
1347

    
1348
}