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

History | View | Annotate | Download (41.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.expressionevaluator.Expression;
37
import org.gvsig.expressionevaluator.ExpressionUtils;
38
import org.gvsig.expressionevaluator.SymbolTable;
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_CODE;
42
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_LABEL;
43
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_TABLE;
44
import org.gvsig.fmap.dal.DataStore;
45
import org.gvsig.fmap.dal.DataTypes;
46
import org.gvsig.fmap.dal.StoresRepository;
47
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
48
import org.gvsig.fmap.dal.feature.Feature;
49
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
51
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
52
import org.gvsig.fmap.dal.feature.FeatureStore;
53
import org.gvsig.fmap.dal.feature.FeatureType;
54
import org.gvsig.fmap.geom.Geometry;
55
import org.gvsig.fmap.geom.GeometryException;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryUtils;
58
import org.gvsig.fmap.geom.type.GeometryType;
59
import org.gvsig.timesupport.Interval;
60
import org.gvsig.timesupport.RelativeInterval;
61
import org.gvsig.timesupport.TimeSupportLocator;
62
import org.gvsig.tools.ToolsLocator;
63
import org.gvsig.tools.dataTypes.CoercionException;
64
import org.gvsig.tools.dataTypes.DataType;
65
import org.gvsig.tools.dynobject.DynField;
66
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
67
import org.gvsig.tools.dynobject.DynField_v2;
68
import org.gvsig.tools.dynobject.DynMethod;
69
import org.gvsig.tools.dynobject.DynObject;
70
import org.gvsig.tools.dynobject.DynObjectValueItem;
71
import org.gvsig.tools.dynobject.DynStruct;
72
import org.gvsig.tools.dynobject.Tags;
73
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
74
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
75
import org.gvsig.tools.dynobject.exception.DynMethodException;
76
import org.gvsig.tools.dynobject.impl.DefaultTags;
77
import org.gvsig.tools.evaluator.AbstractEvaluator;
78
import org.gvsig.tools.evaluator.Evaluator;
79
import org.gvsig.tools.evaluator.EvaluatorData;
80
import org.gvsig.tools.evaluator.EvaluatorException;
81
import org.gvsig.tools.persistence.PersistenceManager;
82
import org.gvsig.tools.persistence.Persistent;
83
import org.gvsig.tools.persistence.PersistentState;
84
import org.gvsig.tools.persistence.exception.PersistenceException;
85
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
87

    
88
@SuppressWarnings("UseSpecificCatch")
89
public class DefaultFeatureAttributeDescriptor implements
90
        FeatureAttributeDescriptor, Persistent, DynField_v2, DynField_LabelAttribute {
91

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

    
123
    protected DynObjectValueItem[] availableValues;
124
    protected String description;
125
    protected Object minValue;
126
    protected Object maxValue;
127
    protected String label;
128
    protected int order;
129
    protected boolean hidden;
130
    protected String groupName;
131
    protected Tags tags = new DefaultTags();
132
    private DynMethod availableValuesMethod;
133
    private DynMethod calculateMethod;
134
    private WeakReference typeRef;
135
    
136
    private int relationType = RELATION_TYPE_NONE;
137

    
138
    public DefaultFeatureAttributeDescriptor() {
139
        // Usada en la persistencia
140
    }
141

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

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

    
261
    @Override
262
    public FeatureAttributeDescriptor getCopy() {
263
        return new DefaultFeatureAttributeDescriptor(this);
264
    }
265

    
266
    @Override
267
    public Object clone() throws CloneNotSupportedException {
268
        return new DefaultFeatureAttributeDescriptor(this);
269
    }
270
    
271
    @Override
272
    public boolean allowNull() {
273
        return allowNull;
274
    }
275

    
276
    @Override
277
    public DataType getDataType() {
278
        if (featureAttributeGetter != null) {
279
            return featureAttributeGetter.getDataType();
280
        }
281
        return this.dataType;
282
    }
283

    
284
    public FeatureAttributeDescriptor setDataType(int type) {
285
        this.dataType = ToolsLocator.getDataTypesManager().get(type);
286
        return this;
287
    }
288

    
289
    @Override
290
    public DateFormat getDateFormat() {
291
        return this.dateFormat;
292
    }
293

    
294
    @Override
295
    public Object getDefaultValue() {
296
        return this.defaultValue;
297
    }
298

    
299
    @Override
300
    public Evaluator getEvaluator() {
301
        return this.evaluator;
302
    }
303

    
304
    @Override
305
    public int getGeometryType() {
306
        if( this.dataType.getType()!=DataTypes.GEOMETRY ) {
307
            return Geometry.TYPES.UNKNOWN;
308
        }
309
        return this.geometryType;
310
    }
311

    
312
    @Override
313
    public int getGeometrySubType() {
314
        if( this.dataType.getType()!=DataTypes.GEOMETRY ) {
315
            return Geometry.SUBTYPES.UNKNOWN;
316
        }
317
        return this.geometrySubType;
318
    }
319

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

    
340
    @Override
341
    public int getIndex() {
342
        return this.index;
343
    }
344

    
345
    protected FeatureAttributeDescriptor setIndex(int index) {
346
        this.index = index;
347
        return this;
348
    }
349

    
350
    @Override
351
    public int getMaximumOccurrences() {
352
        return this.maximumOccurrences;
353
    }
354

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

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

    
379
    @Override
380
    public int getPrecision() {
381
        return this.precision;
382
    }
383

    
384
    @Override
385
    public IProjection getSRS() {
386
        return this.SRS;
387
    }
388

    
389
    @Override
390
    public Interval getInterval() {
391
        return this.interval;
392
    }
393

    
394
    public IProjection getSRS(WeakReference storeRef) {
395
        if( this.SRS==null ) {
396
            FeatureStore store = (FeatureStore) storeRef.get();
397
            this.SRS = (IProjection) store.getDynValue(DataStore.METADATA_CRS);
398
        }
399
        return this.SRS;
400
    }
401

    
402

    
403
    @Override
404
    public int getSize() {
405
        return this.size;
406
    }
407

    
408
    @Override
409
    public boolean isPrimaryKey() {
410
        return this.primaryKey;
411
    }
412

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

    
427
    @Override
428
    public Object getAdditionalInfo(String infoName) {
429
        if (this.additionalInfo == null) {
430
            return null;
431
        }
432
        return this.additionalInfo.get(infoName);
433
    }
434

    
435
    @Override
436
    public boolean isAutomatic() {
437
        return this.isAutomatic;
438
    }
439

    
440
    @Override
441
    public boolean equals(Object obj) {
442
        if (this == obj) {
443
            return true;
444
        }
445
        if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
446
            return false;
447
        }
448
        DefaultFeatureAttributeDescriptor other
449
                = (DefaultFeatureAttributeDescriptor) obj;
450

    
451
        if (this.allowNull != other.allowNull) {
452
            return false;
453
        }
454

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

    
459
        if (!Objects.equals(this.name, other.name)) {
460
            return false;
461
        }
462

    
463
        if (this.getDataType() != other.getDataType()) {
464
            return false;
465
        }
466

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

    
471
        if (!Objects.equals(this.defaultValue, other.defaultValue)) {
472
            return false;
473
        }
474

    
475
        if (this.primaryKey != other.primaryKey) {
476
            return false;
477
        }
478

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

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

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

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

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

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

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

    
507
        if (!Objects.equals(this.evaluator, other.evaluator)) {
508
            return false;
509
        }
510

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

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

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

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

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

    
531
        return true;
532
    }
533

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

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

    
589
        Map<String,Object> values = state.getMap("availableValues");
590
        if( values == null || values.isEmpty()  ) {
591
            this.availableValues = null;
592
        } else {
593
            this.availableValues = new DynObjectValueItem[values.size()];
594
            int n = 0;
595
            for (Entry<String, Object> entry : values.entrySet()) {
596
                this.availableValues[n++] = new DynObjectValueItem(entry.getValue(), entry.getKey());
597
            }
598
        }
599
        
600
        description = state.getString("description");
601
        minValue = state.get("minValue");
602
        maxValue = state.get("maxValue");
603
        label = state.getString("label");
604
        order = state.getInt("order");
605
        hidden = state.getBoolean("hidden");
606
        groupName = state.getString("groupName");
607
        relationType = state.getInt("relationType");
608
    }
609

    
610
    @Override
611
    public void saveToState(PersistentState state) throws PersistenceException {
612
        state.set("allowNull", allowNull);
613
        state.set("dataType", dataType.getType());
614
        state.set("dataProfile", dataProfile);
615
        
616
//        FIXME: dateFormat;
617

    
618
        state.set("defaultValue", Objects.toString(defaultValue));
619

    
620
        state.set("index", index);
621
        state.set("maximumOccurrences", maximumOccurrences);
622
        state.set("minimumOccurrences", minimumOccurrences);
623
        state.set("size", size);
624
        state.set("name", name);
625
        state.set("objectClass", objectClass==null? null:objectClass.getName());
626
        state.set("precision", precision);
627
        state.set("evaluator", evaluator);
628
        
629
        state.set("primaryKey", primaryKey);
630
        state.set("readOnly", readOnly);
631
        state.set("SRS", SRS);
632
        GeometryType theGeomType = this.getGeomType();
633
        if( theGeomType==null ) {
634
            state.set("geometryType", Geometry.TYPES.UNKNOWN);
635
            state.set("geometrySubType", Geometry.SUBTYPES.UNKNOWN);
636
        } else {
637
            state.set("geometryType", theGeomType.getType());
638
            state.set("geometrySubType", theGeomType.getSubType());
639
        }
640

    
641
//      FIXME: additionalInfo
642

    
643
        state.set("isAutomatic", isAutomatic);
644
        state.set("isTime", isTime);
645
        if( this.interval==null ) {
646
            state.setNull("interval_start");
647
            state.setNull("interval_end");
648
        } else {
649
            state.set("interval_start", ((RelativeInterval)interval).getStart().toMillis());
650
            state.set("interval_end", ((RelativeInterval)interval).getEnd().toMillis());
651
        }
652
        state.set("SRS", SRS);
653

    
654
//      FIXME: featureAttributeGetter
655

    
656
        if( featureAttributeEmulator instanceof Persistent ) {
657
            state.set("featureAttributeEmulator", featureAttributeEmulator);
658
        } else {
659
            state.setNull("featureAttributeEmulator");
660
        }
661

    
662
        state.set("indexed", indexed);
663
        state.set("isIndexAscending", isIndexAscending);
664
        state.set("allowIndexDuplicateds", allowIndexDuplicateds);
665
        
666
        if( this.availableValues==null ) {
667
            state.setNull("availableValues");
668
        } else {
669
            Map<String,Object> values = new HashMap<>();
670
            for (DynObjectValueItem value : availableValues) {
671
                values.put(value.getLabel(),value.getValue());
672
            }
673
            state.set("availableValues", values);
674
        }
675
        state.set("description", description);
676
        state.set("minValue", minValue);
677
        state.set("maxValue", maxValue);
678
        state.set("label", label);
679
        state.set("order", order);
680
        state.set("hidden", hidden);
681
        state.set("groupName", groupName);
682
        state.set("relationType",relationType);
683

    
684
    }
685
    
686
    private static final String FEATATTRDESC_PERSISTENCE_DEFINITION_NAME = "FeatureAttributeDescriptor";
687

    
688
    public static void registerPersistenceDefinition() {
689
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
690
        
691

    
692
        if (manager.getDefinition(FEATATTRDESC_PERSISTENCE_DEFINITION_NAME)
693
                == null) {
694
            DynStruct definition = manager.addDefinition(DefaultFeatureAttributeDescriptor.class,
695
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME,
696
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME
697
                        + " persistent definition",
698
                    null,
699
                    null
700
            );
701
            definition.addDynFieldBoolean("allowNull");
702
            definition.addDynFieldInt("dataType");
703
            definition.addDynFieldString("dataProfile");
704
//            definition.addDynFieldString("dateFormat");
705
            definition.addDynFieldString("defaultValue");
706
            definition.addDynFieldInt("index");
707
            definition.addDynFieldInt("maximumOccurrences");
708
            definition.addDynFieldInt("minimumOccurrences");
709
            definition.addDynFieldInt("size");
710
            definition.addDynFieldString("name");
711
            definition.addDynFieldString("objectClass");
712
            definition.addDynFieldInt("precision");
713
            definition.addDynFieldObject("evaluator")
714
                    .setClassOfValue(Evaluator.class);
715
            definition.addDynFieldBoolean("primaryKey");
716
            definition.addDynFieldBoolean("readOnly");
717
            definition.addDynFieldObject("SRS")
718
                    .setClassOfValue(IProjection.class);
719
            definition.addDynFieldInt("geometryType");
720
            definition.addDynFieldInt("geometrySubType");
721
//            definition.addDynFieldMap("additionalInfo");
722
            definition.addDynFieldBoolean("isAutomatic");
723
            definition.addDynFieldBoolean("isTime");
724
            definition.addDynFieldLong("interval_start");
725
            definition.addDynFieldLong("interval_end");
726
            definition.addDynFieldObject("featureAttributeEmulator")
727
                    .setClassOfValue(FeatureAttributeEmulator.class);
728
            definition.addDynFieldBoolean("indexed");
729
            definition.addDynFieldBoolean("isIndexAscending");
730
            definition.addDynFieldBoolean("allowIndexDuplicateds");
731
            definition.addDynFieldMap("availableValues");
732
            definition.addDynFieldString("description");
733
            definition.addDynFieldObject("minValue");
734
            definition.addDynFieldObject("maxValue");
735
            definition.addDynFieldString("label");
736
            definition.addDynFieldInt("order");
737
            definition.addDynFieldBoolean("hidden");
738
            definition.addDynFieldString("groupName");
739
            definition.addDynFieldInt("relationType");
740
        }
741
    }
742

    
743
    
744
    /*
745
     * Start of DynField interface Implementation
746
     *
747
     */
748

    
749
    @Override
750
    public Tags getTags() {
751
        return tags;
752
    }
753

    
754
    @Override
755
    public DynObjectValueItem[] getAvailableValues() {
756
        if( this.availableValues == null ) {
757
            if( StringUtils.isBlank(this.getDataProfileName()) ) {
758
                return null;
759
            }
760
            if( !StringUtils.equalsIgnoreCase(
761
                    this.getDataProfileName(), 
762
                    DataManager.DAL_SELECTABLE_FOREING_KEY) 
763
                    ) {
764
                return null;
765
            }
766
            Tags theTags = this.getTags();
767
            if( theTags==null ) {
768
                return null;
769
            }
770
            String foreingLabel = theTags.getString(DAL_FOREING_LABEL, null);
771
            if( StringUtils.isBlank(foreingLabel) ) {
772
                return null;
773
            }
774
            String foreingTableName = theTags.getString(DAL_FOREING_TABLE, null);
775
            if( StringUtils.isBlank(foreingTableName) ) {
776
                return null;
777
            }
778
            String foreingCodeName = theTags.getString(DAL_FOREING_CODE, null);
779
            if( StringUtils.isBlank(foreingCodeName) ) {
780
                return null;
781
            }
782
            try {
783
                DataManager dataManager = DALLocator.getDataManager();
784
                StoresRepository repository = this.getStore().getStoresRepository();
785
                FeatureStore store = (FeatureStore) repository.get(foreingTableName);
786
                if( store == null ) {
787
                    LOGGER.warn("Can't locate store '"+foreingTableName+"' to get available values of field '"+this.getName()+"'.");
788
                    return null;
789
                }
790
                        
791
                Expression labelExpression = ExpressionUtils.createExpression(foreingLabel);
792
                FeatureSymbolTable featureSymbolTable = dataManager.createFeatureSymbolTable();
793
                SymbolTable symbolTable = featureSymbolTable.createParent();
794
                
795
                int count = (int) store.getFeatureCount();
796
                DynObjectValueItem[] values = new DynObjectValueItem[count];
797
                int n = 0;
798
                for (Feature feature : store.getFeatureSet()) {
799
                    featureSymbolTable.setFeature(feature);
800
                    Object code = feature.get(foreingCodeName);
801
                    Object label = labelExpression.execute(symbolTable);
802
                    values[n++] = new DynObjectValueItem(code, Objects.toString(label, Objects.toString(code, "##ERROR##")));
803
                }
804
                this.availableValues = values;
805
            } catch (Exception ex) {
806
                LOGGER.warn("Can't get values from table '" + foreingTableName + "' for field '" + this.getName() + "'.", ex);
807
                return null;
808
            }
809
        }
810
        return this.availableValues;
811
    }
812

    
813
    @Override
814
    public String getDescription() {
815
        if( this.description == null ) {
816
            return getName();
817
        }
818
        return this.description;
819
    }
820

    
821
    @Override
822
    public Object getMaxValue() {
823
        return this.maxValue;
824
    }
825

    
826
    @Override
827
    public Object getMinValue() {
828
        return this.minValue;
829
    }
830

    
831
    @Override
832
    public int getTheTypeOfAvailableValues() {
833
        return 1;
834
    }
835

    
836
    @Override
837
    public int getType() {
838
        if (featureAttributeGetter != null) {
839
            return featureAttributeGetter.getDataType().getType();
840
        }
841
        return getDataType().getType();
842
    }
843

    
844
    @Override
845
    public boolean isMandatory() {
846
        return !allowNull() || isPrimaryKey();
847
    }
848

    
849
    @Override
850
    public boolean isPersistent() {
851
        return false;
852
    }
853

    
854
    @Override
855
    public DynField setAvailableValues(DynObjectValueItem[] values) {
856
        if ( ArrayUtils.isEmpty(values) ) {
857
            this.availableValues = null;
858
        } else {
859
            this.availableValues = values;
860
        }
861
        return this;
862
    }
863

    
864
    @Override
865
    public DynField setDescription(String description) {
866
        this.description = description;
867
        return this;
868
    }
869

    
870
    @Override
871
    public DynField setMandatory(boolean mandatory) {
872
        throw new UnsupportedOperationException();
873
    }
874

    
875
    @Override
876
    public DynField setMaxValue(Object maxValue) {
877
        try {
878
            this.maxValue = this.coerce(maxValue);
879
        } catch (CoercionException e) {
880
            throw new IllegalArgumentException(e);
881
        }
882
        return this;
883
    }
884

    
885
    @Override
886
    public DynField setMinValue(Object minValue) {
887
        try {
888
            this.maxValue = this.coerce(minValue);
889
        } catch (CoercionException e) {
890
            throw new IllegalArgumentException(e);
891
        }
892
        return this;
893
    }
894

    
895
    @Override
896
    public DynField setPersistent(boolean persistent) {
897
        throw new UnsupportedOperationException();
898
    }
899

    
900
    @Override
901
    public DynField setTheTypeOfAvailableValues(int type) {
902
        throw new UnsupportedOperationException();
903
    }
904

    
905
    @Override
906
    public DynField setType(int type) {
907
        throw new UnsupportedOperationException();
908
    }
909

    
910
    @Override
911
    public DynField setDefaultDynValue(Object defaultValue) {
912
        throw new UnsupportedOperationException();
913
    }
914

    
915
    @Override
916
    public Class getClassOfValue() {
917
        return null;
918
    }
919

    
920
    @Override
921
    public DynField getElementsType() {
922
        return null;
923
    }
924

    
925
    @Override
926
    public DynField setClassOfValue(Class theClass)
927
            throws DynFieldIsNotAContainerException {
928
        throw new UnsupportedOperationException();
929
    }
930

    
931
    @Override
932
    public DynField setElementsType(DynStruct type)
933
            throws DynFieldIsNotAContainerException {
934
        throw new UnsupportedOperationException();
935
    }
936

    
937
    @Override
938
    public DynField setElementsType(int type)
939
            throws DynFieldIsNotAContainerException {
940
        throw new UnsupportedOperationException();
941
    }
942

    
943
    public FeatureAttributeDescriptor setDataProfileName(String dataProfile) {
944
        this.dataProfile = dataProfile;
945
        return this;
946
    }
947

    
948
    @Override
949
    public String getDataProfileName() {
950
        return dataProfile;
951
    }
952

    
953
    @Override
954
    public void validate(Object value) throws DynFieldValidateException {
955

    
956
        if (value == null && !this.allowNull()) {
957
            throw new DynFieldValidateException(value, this, null);
958
        }
959

    
960
        try {
961
            this.dataType.coerce(value);
962
        } catch (CoercionException e) {
963
            throw new DynFieldValidateException(value, this, e);
964
        }
965

    
966
        /*
967
         * Other checks will be needed
968
         */
969
    }
970

    
971
    @Override
972
    public String getSubtype() {
973
        if (featureAttributeGetter != null) {
974
            return featureAttributeGetter.getDataType().getSubtype();
975
        }
976
        return this.dataType.getSubtype();
977
    }
978

    
979
    @Override
980
    public Object coerce(Object value) throws CoercionException {
981
        if ( value == null ) {
982
            return value; // O debe devolver this.defaultValue
983
        }
984
        try {
985
            return this.getDataType().coerce(value);
986
        } catch(Exception ex){
987
            throw new RuntimeException(ex);
988
        }
989
    }
990

    
991
    @Override
992
    public DynField setAvailableValues(List values) {
993
        if (  values == null || values.isEmpty() ) {
994
            this.availableValues = null;
995
        } else {
996
            this.availableValues = (DynObjectValueItem[]) values.toArray(
997
                new DynObjectValueItem[values.size()]
998
            );
999
        }
1000
        return this;
1001
    }
1002

    
1003
    @Override
1004
    public String getGroup() {
1005
        return this.groupName;
1006
    }
1007

    
1008
    @Override
1009
    public int getOder() {
1010
        return this.order;
1011
    }
1012

    
1013
    @Override
1014
    public String getLabel() {
1015
        if( this.label == null ) {
1016
            return this.getName();
1017
        }
1018
        return this.label;
1019
    }
1020

    
1021
    @Override
1022
    public DynField setLabel(String label) {
1023
        this.label = label;
1024
        return this;
1025
    }
1026

    
1027
    @Override
1028
    public DynField setGroup(String groupName) {
1029
        this.groupName = groupName;
1030
        return this;
1031
    }
1032

    
1033
    @Override
1034
    public DynField setOrder(int order) {
1035
        this.order = order;
1036
        return this;
1037
    }
1038

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

    
1045
    @Override
1046
    public boolean isHidden() {
1047
        return this.hidden;
1048
    }
1049

    
1050
    @Override
1051
    public DynField setReadOnly(boolean arg0) {
1052
        throw new UnsupportedOperationException();
1053
    }
1054

    
1055
    @Override
1056
    public boolean isContainer() {
1057
        return false;
1058
    }
1059

    
1060
    @Override
1061
    public Class getClassOfItems() {
1062
        return null;
1063
    }
1064

    
1065
    @Override
1066
    public DynField setDefaultFieldValue(Object defaultValue) {
1067
        throw new UnsupportedOperationException();
1068
    }
1069

    
1070
    @Override
1071
    public DynField setClassOfItems(Class theClass) {
1072
        throw new UnsupportedOperationException();
1073
    }
1074

    
1075
    @Override
1076
    public DynField setType(DataType type) {
1077
        throw new UnsupportedOperationException();
1078
    }
1079

    
1080
    @Override
1081
    public DynField setSubtype(String subtype) {
1082
        throw new UnsupportedOperationException();
1083
    }
1084

    
1085
    @Override
1086
    public boolean isTime() {
1087
        return isTime;
1088
    }
1089

    
1090
    @Override
1091
    public FeatureAttributeGetter getFeatureAttributeGetter() {
1092
        return featureAttributeGetter;
1093
    }
1094

    
1095
    @Override
1096
    public void setFeatureAttributeGetter(
1097
            FeatureAttributeGetter featureAttributeTransform) {
1098
        this.featureAttributeGetter = featureAttributeTransform;
1099
    }
1100

    
1101
    @Override
1102
    public FeatureAttributeEmulator getFeatureAttributeEmulator() {
1103
        return this.featureAttributeEmulator;
1104
    }
1105

    
1106
    public FeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
1107
        this.featureAttributeEmulator = featureAttributeEmulator;
1108
        return this;
1109
    }
1110
        
1111
    @Override
1112
    public boolean isIndexed() {
1113
        return this.indexed;
1114
    }
1115

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

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

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

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

    
1136
    @Override
1137
    public String getClassNameOfValue() {
1138
        return null;
1139
    }
1140

    
1141
    @Override
1142
    public DynStruct getDynClassOfValue() {
1143
        return null;
1144
    }
1145

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

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

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

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

    
1166
    @Override
1167
    public String getClassNameOfItems() {
1168
        return null;
1169
    }
1170

    
1171
    @Override
1172
    public DynStruct getDynClassOfItems() {
1173
        return null;
1174
    }
1175

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

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

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

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

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

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

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

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

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

    
1249
    @Override
1250
    public boolean getValidateElements() {
1251
        return false;
1252
    }
1253

    
1254
    private class ConstantValueEvaluator extends AbstractEvaluator {
1255

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

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

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

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

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

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

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

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

    
1331
}