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

History | View | Annotate | Download (52.7 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.math.BigDecimal;
27
import java.math.MathContext;
28
import java.math.RoundingMode;
29
import java.text.DateFormat;
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.LinkedHashMap;
33
import java.util.List;
34
import java.util.Locale;
35
import java.util.Map;
36
import java.util.Map.Entry;
37
import java.util.Objects;
38
import javax.json.JsonObject;
39
import org.apache.commons.lang3.ArrayUtils;
40
import org.apache.commons.lang3.StringUtils;
41
import org.apache.commons.lang3.tuple.Pair;
42
import org.cresques.cts.IProjection;
43
import org.gvsig.expressionevaluator.Expression;
44
import org.gvsig.expressionevaluator.ExpressionUtils;
45
import org.gvsig.fmap.dal.DALLocator;
46
import org.gvsig.fmap.dal.DataStore;
47
import org.gvsig.fmap.dal.DataTypes;
48
import org.gvsig.fmap.dal.feature.DataProfile;
49
import org.gvsig.fmap.dal.feature.Feature;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
51
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
52
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
53
import org.gvsig.fmap.dal.feature.FeatureStore;
54
import org.gvsig.fmap.dal.feature.FeatureType;
55
import org.gvsig.fmap.dal.feature.ForeingKey;
56
import org.gvsig.fmap.geom.Geometry;
57
import org.gvsig.fmap.geom.GeometryException;
58
import org.gvsig.fmap.geom.GeometryLocator;
59
import org.gvsig.fmap.geom.GeometryUtils;
60
import org.gvsig.fmap.geom.type.GeometryType;
61
import org.gvsig.timesupport.Interval;
62
import org.gvsig.timesupport.RelativeInterval;
63
import org.gvsig.timesupport.TimeSupportLocator;
64
import org.gvsig.tools.ToolsLocator;
65
import org.gvsig.tools.dataTypes.Coercion;
66
import org.gvsig.tools.dataTypes.CoercionException;
67
import org.gvsig.tools.dataTypes.DataType;
68
import org.gvsig.tools.dataTypes.DataTypeUtils;
69
import org.gvsig.tools.dynobject.DynField;
70
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
71
import org.gvsig.tools.dynobject.DynField_v2;
72
import org.gvsig.tools.dynobject.DynMethod;
73
import org.gvsig.tools.dynobject.DynObject;
74
import org.gvsig.tools.dynobject.DynObjectValueItem;
75
import org.gvsig.tools.dynobject.DynStruct;
76
import org.gvsig.tools.dynobject.Tags;
77
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
78
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
79
import org.gvsig.tools.dynobject.exception.DynMethodException;
80
import org.gvsig.tools.dynobject.impl.DefaultTags;
81
import org.gvsig.tools.evaluator.AbstractEvaluator;
82
import org.gvsig.tools.evaluator.Evaluator;
83
import org.gvsig.tools.evaluator.EvaluatorData;
84
import org.gvsig.tools.evaluator.EvaluatorException;
85
import org.gvsig.tools.i18n.I18nManager;
86
import org.gvsig.tools.persistence.PersistenceManager;
87
import org.gvsig.tools.persistence.Persistent;
88
import org.gvsig.tools.persistence.PersistentState;
89
import org.gvsig.tools.persistence.exception.PersistenceException;
90
import org.slf4j.Logger;
91
import org.slf4j.LoggerFactory;
92
import org.gvsig.tools.dataTypes.CoercionContext;
93
import org.gvsig.tools.dataTypes.DataType.NumberPrecisionAndScale;
94
import org.gvsig.tools.util.GetItemWithSize;
95
import org.gvsig.tools.util.LabeledValue;
96

    
97
@SuppressWarnings("UseSpecificCatch")
98
public class DefaultFeatureAttributeDescriptor implements
99
        FeatureAttributeDescriptor, Persistent, DynField_v2, DynField_LabelAttribute {
100

    
101
  protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeDescriptor.class);
102

    
103
  protected boolean allowNull;
104
  protected DataType dataType;
105
  protected String dataProfile;
106
  protected DateFormat dateFormat;
107
  protected Object defaultValue;
108
  protected int index;
109
  protected int maximumOccurrences;
110
  protected int minimumOccurrences;
111
  protected int size;
112
  protected String name;
113
  protected Class objectClass;
114
  protected int precision;
115
  protected int scale;
116
  protected int roundMode;
117
  protected Evaluator evaluator;
118
  protected boolean primaryKey;
119
  protected boolean readOnly;
120
  protected IProjection SRS;
121
  protected GeometryType geomType;
122
  protected int geometryType;
123
  protected int geometrySubType;
124
  protected Map additionalInfo;
125
  protected boolean isAutomatic;
126
  protected boolean isTime = false;
127
  protected Interval interval;
128
  protected FeatureAttributeGetter featureAttributeGetter = null;
129
  protected FeatureAttributeEmulator featureAttributeEmulator = null;
130
  protected boolean indexed = false;
131
  protected boolean isIndexAscending = true;
132
  protected boolean allowIndexDuplicateds = true;
133

    
134
  protected DynObjectValueItem[] availableValues;
135
  protected DynObjectValueItem[] availableValuesCache; // No persistente
136
  protected Expression availableValuesExpression;
137
  private Map<Object, String> labelOfValueMap; // No persistente
138
  protected String description;
139
  protected Object minValue;
140
  protected Object maxValue;
141
  protected String label;
142
  protected String shortLabel;
143
  protected int order;
144
  protected boolean hidden;
145
  protected String groupName;
146
  protected Tags tags = new DefaultTags();
147
  private DynMethod availableValuesMethod;
148
  private DynMethod calculateMethod;
149
  private WeakReference typeRef;
150
  protected DefaultForeingKey foreingKey = null;
151

    
152
  protected CoercionContext coerceContext = null; // not persistent
153
  protected MathContext mathContext = null; // not persistent
154

    
155
  private int relationType = RELATION_TYPE_NONE;
156
  protected Locale locale;
157
  protected int displaySize;
158

    
159
  public DefaultFeatureAttributeDescriptor() {
160
    // Usada en la persistencia
161
    this.precision = DataType.PRECISION_NONE;
162
    this.scale = DataType.SCALE_NONE;
163
    this.roundMode = BigDecimal.ROUND_HALF_UP;
164
  }
165

    
166
  protected DefaultFeatureAttributeDescriptor(FeatureType type) {
167
    this();
168
//        LOGGER.info(String.format("Created FeatureAttributeDescriptor [%08x].", this.hashCode()));
169
    setFeatureType(type);
170
    this.allowNull = true;
171
    this.dataType = null;
172
    this.dateFormat = null;
173
    this.defaultValue = null;
174
    this.index = -1;
175
    this.maximumOccurrences = 0;
176
    this.minimumOccurrences = 0;
177
    this.size = 0;
178
    this.name = null;
179
    this.objectClass = null;
180
    this.precision = DataType.PRECISION_NONE;
181
    this.scale = DataType.SCALE_NONE;
182
    this.roundMode = BigDecimal.ROUND_HALF_UP;
183
    this.evaluator = null;
184
    this.primaryKey = false;
185
    this.readOnly = false;
186
    this.SRS = null;
187
    this.geometryType = Geometry.TYPES.NULL;
188
    this.geometrySubType = Geometry.SUBTYPES.UNKNOWN;
189
    this.additionalInfo = null;
190
    this.isAutomatic = false;
191
    this.hidden = false;
192
    this.relationType = RELATION_TYPE_NONE;
193
    this.locale = null;
194
    this.displaySize = 0;
195
  }
196

    
197
  protected DefaultFeatureAttributeDescriptor(
198
          DefaultFeatureAttributeDescriptor other
199
  ) {
200
    this();
201
    copyFrom(other);
202
//        LOGGER.info(String.format("Created FeatureAttributeDescriptor [%08x] [%s] (copy).", this.hashCode(), this.name));
203
  }
204

    
205
  @Override
206
  public void copyFrom(DynField other1) {
207
    if (!(other1 instanceof DefaultFeatureAttributeDescriptor)) {
208
      throw new IllegalArgumentException("Can't copy from a non DefaultFeatureAttributeDescriptor");
209
    }
210
    DefaultFeatureAttributeDescriptor other = (DefaultFeatureAttributeDescriptor) other1;
211
    this.typeRef = other.typeRef;
212
    this.allowNull = other.allowNull;
213
    this.dataType = other.dataType;
214
    this.dateFormat = other.dateFormat;
215
    this.defaultValue = other.defaultValue;
216
    this.index = other.index;
217
    this.maximumOccurrences = other.maximumOccurrences;
218
    this.minimumOccurrences = other.minimumOccurrences;
219
    this.size = other.size;
220
    this.name = other.name;
221
    this.objectClass = other.objectClass;
222
    this.precision = other.precision;
223
    this.scale = other.scale;
224
    this.roundMode = other.roundMode;
225
    this.evaluator = other.evaluator;
226
    this.primaryKey = other.primaryKey;
227
    this.readOnly = other.readOnly;
228
    this.SRS = other.SRS;
229
    this.geometryType = other.geometryType;
230
    this.geometrySubType = other.geometrySubType;
231
    this.geomType = other.geomType;
232
    if (other.additionalInfo != null) {
233
      Iterator iter = other.additionalInfo.entrySet().iterator();
234
      Map.Entry entry;
235
      this.additionalInfo = new HashMap();
236
      while (iter.hasNext()) {
237
        entry = (Entry) iter.next();
238
        this.additionalInfo.put(entry.getKey(), entry.getValue());
239
      }
240
    } else {
241
      this.additionalInfo = null;
242
    }
243
    this.isAutomatic = other.isAutomatic;
244
    this.isTime = other.isTime;
245
    this.featureAttributeEmulator = other.featureAttributeEmulator;
246
    this.indexed = other.indexed;
247
    this.isIndexAscending = other.isIndexAscending;
248
    this.allowIndexDuplicateds = other.allowIndexDuplicateds;
249
    this.hidden = other.hidden;
250
    this.dataProfile = other.dataProfile;
251

    
252
    this.availableValues = other.availableValues;
253
    this.description = other.description;
254
    this.minValue = other.minValue;
255
    this.maxValue = other.maxValue;
256
    this.label = other.label;
257
    this.order = other.order;
258
    this.groupName = other.groupName;
259
    if (other.tags == null) {
260
      this.tags = null;
261
    } else {
262
      try {
263
        this.tags = (Tags) other.tags.clone();
264
      } catch (Exception ex) {
265
      }
266
    }
267
    this.foreingKey = null;
268
    if (other.foreingKey != null) {
269
      try {
270
        this.foreingKey = (DefaultForeingKey) other.foreingKey.clone();
271
      } catch (CloneNotSupportedException ex) {
272
      }
273
    }
274
    if (this.foreingKey != null) {
275
      this.foreingKey.setDescriptor(this);
276
    }
277

    
278
    // TODO: ? Habria que clonarlos ?
279
    this.availableValuesMethod = other.availableValuesMethod;
280
    this.calculateMethod = other.calculateMethod;
281
    this.relationType = other.relationType;
282
    this.locale = other.locale;
283
    this.displaySize = other.displaySize;
284
  }
285

    
286
  public void setFeatureType(FeatureType type) {
287
    // Usada en la persistencia
288
    if (type == null) {
289
      this.typeRef = null;
290
    } else {
291
      this.typeRef = new WeakReference(type);
292
//            LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] set FeatureType [%08x], ref [%08x].", this.hashCode(), type.hashCode(), typeRef.hashCode()));
293
    }
294
  }
295

    
296
  @Override
297
  public String getDataTypeName() {
298
    if (this.getDataType() == null) {
299
      return "(unknow)";
300
    }
301
    return this.getDataType().getName();
302
  }
303

    
304
  @Override
305
  public DefaultFeatureAttributeDescriptor getCopy() {
306
    return new DefaultFeatureAttributeDescriptor(this);
307
  }
308

    
309
  @Override
310
  public Object clone() throws CloneNotSupportedException {
311
    return new DefaultFeatureAttributeDescriptor(this);
312
  }
313

    
314
  @Override
315
  public boolean allowNull() {
316
    return allowNull;
317
  }
318

    
319
  @Override
320
  public Locale getLocale() {
321
    if( this.locale == null ) {
322
      if( this.dataType.isNumeric() ) {
323
        this.locale = Locale.ENGLISH;
324
      } else {
325
        this.locale = Locale.getDefault();
326
      }
327
    }
328
    return this.locale;
329
  }
330

    
331
  @Override
332
  public DataType getDataType() {
333
    if (featureAttributeGetter != null) {
334
      return featureAttributeGetter.getDataType();
335
    }
336
    return this.dataType;
337
  }
338

    
339
  public FeatureAttributeDescriptor setDataType(int type) {
340
    this.dataType = ToolsLocator.getDataTypesManager().get(type);
341
    return this;
342
  }
343

    
344
  @Override
345
  public DateFormat getDateFormat() {
346
    return this.dateFormat;
347
  }
348

    
349
  @Override
350
  public Object getDefaultValue() {
351
    return this.defaultValue;
352
  }
353

    
354
  @Override
355
  public Object getDefaultValueCoerced() {
356
    try {
357
      Object value = this.defaultValue;
358
      if (value == null) {
359
        return null;
360
      }
361
      if (ExpressionUtils.isDynamicText(value.toString())) {
362
        value = ExpressionUtils.evaluateDynamicText(value.toString());
363
      }
364
      return this.getDataType().coerce(value);
365
    } catch (CoercionException ex) {
366
      return null;
367
    }
368
  }
369

    
370
  @Override
371
  public Evaluator getEvaluator() {
372
    return this.evaluator;
373
  }
374

    
375
  @Override
376
  public int getGeometryType() {
377
    if (this.dataType.getType() != DataTypes.GEOMETRY) {
378
      return Geometry.TYPES.UNKNOWN;
379
    }
380
    return this.geometryType;
381
  }
382

    
383
  @Override
384
  public int getGeometrySubType() {
385
    if (this.dataType.getType() != DataTypes.GEOMETRY) {
386
      return Geometry.SUBTYPES.UNKNOWN;
387
    }
388
    return this.geometrySubType;
389
  }
390

    
391
  @Override
392
  public GeometryType getGeomType() {
393
    if (this.dataType.getType() != DataTypes.GEOMETRY) {
394
      return null;
395
    }
396
    if (this.geomType == null) {
397
      try {
398
        this.geomType
399
                = GeometryLocator.getGeometryManager().getGeometryType(
400
                        this.geometryType, this.geometrySubType);
401
      } catch (GeometryException e) {
402
        throw new RuntimeException(
403
                "Error getting geometry type with type = "
404
                + this.geometryType + ", subtype = "
405
                + this.geometrySubType, e);
406
      }
407
    }
408
    return this.geomType;
409
  }
410

    
411
  @Override
412
  public int getIndex() {
413
    return this.index;
414
  }
415

    
416
  protected FeatureAttributeDescriptor setIndex(int index) {
417
    this.index = index;
418
    return this;
419
  }
420

    
421
  @Override
422
  public int getMaximumOccurrences() {
423
    return this.maximumOccurrences;
424
  }
425

    
426
  @Override
427
  public int getMinimumOccurrences() {
428
    return this.minimumOccurrences;
429
  }
430

    
431
  @Override
432
  public String getName() {
433
    return this.name;
434
  }
435

    
436
  public FeatureAttributeDescriptor setName(String name) {
437
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] set name [%s].", this.hashCode(), name));
438
    this.name = name;
439
    return this;
440
  }
441

    
442
  @Override
443
  public Class getObjectClass() {
444
    if (getDataType().getType() == DataTypes.OBJECT) {
445
      return objectClass;
446
    }
447
    return getDataType().getDefaultClass();
448
  }
449

    
450
  @Override
451
  public int getPrecision() {
452
    return this.precision;
453
  }
454

    
455
  @Override
456
  public int getScale() {
457
    return this.scale;
458
  }
459

    
460
  @Override
461
  public Coercion getCoercion() {
462
    return this.getDataType().getCoercion();
463
  }
464

    
465
  @Override
466
  public MathContext getMathContext() {
467
    if (this.mathContext == null) {
468
      if (this.getDataType().isNumeric()) {
469
        this.mathContext = new MathContext(
470
                this.getPrecision(),
471
                RoundingMode.valueOf(this.getRoundMode())
472
        );
473
      } else {
474
        this.mathContext = MathContext.UNLIMITED;
475
      }
476
    }
477
    return this.mathContext;
478
  }
479

    
480
  @Override
481
  public CoercionContext getCoercionContext() {
482
    if (this.coerceContext == null) {
483
      if (this.getDataType().isNumeric()) {
484
        this.coerceContext = DataTypeUtils.coerceContextDecimal(
485
                this.getLocale(),
486
                this.getPrecision(),
487
                this.getScale(),
488
                this.getRoundMode()
489
        );
490
      } else {
491
        this.coerceContext = DataTypeUtils.coerceContextLocale(
492
                this.getLocale()
493
        );
494
      }
495
    }
496
    return this.coerceContext;
497
  }
498

    
499
  @Override
500
  public int getRoundMode() {
501
    return this.roundMode;
502
  }
503

    
504
  @Override
505
  public IProjection getSRS() {
506
    return this.SRS;
507
  }
508

    
509
  @Override
510
  public Interval getInterval() {
511
    return this.interval;
512
  }
513

    
514
  public IProjection getSRS(WeakReference storeRef) {
515
    if (this.SRS == null) {
516
      FeatureStore store = (FeatureStore) storeRef.get();
517
      this.SRS = (IProjection) store.getDynValue(DataStore.METADATA_CRS);
518
    }
519
    return this.SRS;
520
  }
521

    
522
  @Override
523
  public int getSize() {
524
    return this.size;
525
  }
526

    
527
  @Override
528
  public boolean isPrimaryKey() {
529
    return this.primaryKey;
530
  }
531

    
532
  @Override
533
  public boolean isReadOnly() {
534
    if (this.readOnly) {
535
      return true;
536
    }
537
    return this.isComputed();
538
  }
539

    
540
  @Override
541
  public Object getAdditionalInfo(String infoName) {
542
    if (this.additionalInfo == null) {
543
      return null;
544
    }
545
    return this.additionalInfo.get(infoName);
546
  }
547

    
548
  @Override
549
  public boolean isAutomatic() {
550
    return this.isAutomatic;
551
  }
552

    
553
  @Override
554
  public boolean equals(Object obj) {
555
    if (this == obj) {
556
      return true;
557
    }
558
    if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
559
      return false;
560
    }
561
    DefaultFeatureAttributeDescriptor other
562
            = (DefaultFeatureAttributeDescriptor) obj;
563

    
564
    if (this.allowNull != other.allowNull) {
565
      return false;
566
    }
567

    
568
    if (this.index != other.index) {
569
      return false;
570
    }
571

    
572
    if (!Objects.equals(this.name, other.name)) {
573
      return false;
574
    }
575

    
576
    if (this.getDataType() != other.getDataType()) {
577
      return false;
578
    }
579

    
580
    if (this.size != other.size) {
581
      return false;
582
    }
583

    
584
    if (!Objects.equals(this.defaultValue, other.defaultValue)) {
585
      return false;
586
    }
587

    
588
    if (this.primaryKey != other.primaryKey) {
589
      return false;
590
    }
591

    
592
    if (this.isAutomatic != other.isAutomatic) {
593
      return false;
594
    }
595

    
596
    if (this.readOnly != other.readOnly) {
597
      return false;
598
    }
599

    
600
    if (this.precision != other.precision) {
601
      return false;
602
    }
603

    
604
    if (this.maximumOccurrences != other.maximumOccurrences) {
605
      return false;
606
    }
607

    
608
    if (this.minimumOccurrences != other.minimumOccurrences) {
609
      return false;
610
    }
611

    
612
    if (this.geometryType != other.geometryType) {
613
      return false;
614
    }
615

    
616
    if (this.geometrySubType != other.geometrySubType) {
617
      return false;
618
    }
619

    
620
    if (!Objects.equals(this.evaluator, other.evaluator)) {
621
      return false;
622
    }
623

    
624
    if (!Objects.equals(this.featureAttributeEmulator, other.featureAttributeEmulator)) {
625
      return false;
626
    }
627

    
628
    if (!Objects.equals(this.SRS, other.SRS)) {
629
      return false;
630
    }
631

    
632
    if (!Objects.equals(this.dateFormat, other.dateFormat)) {
633
      return false;
634
    }
635

    
636
    if (!Objects.equals(this.objectClass, other.objectClass)) {
637
      return false;
638
    }
639

    
640
    if (!Objects.equals(this.dataProfile, other.dataProfile)) {
641
      return false;
642
    }
643

    
644
    return true;
645
  }
646

    
647
  @Override
648
  public void loadFromState(PersistentState state)
649
          throws PersistenceException {
650
    allowNull = state.getBoolean("allowNull");
651
    dataType = ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
652
    dataProfile = state.getString("dataProfile");
653

    
654
//        FIXME: dateFormat;
655
    try {
656
      defaultValue = dataType.coerce(state.get("defaultValue"));
657
    } catch (CoercionException ex) {
658
    }
659

    
660
    index = state.getInt("index");
661
    maximumOccurrences = state.getInt("maximumOccurrences");
662
    minimumOccurrences = state.getInt("minimumOccurrences");
663
    size = state.getInt("size");
664
    name = state.getString("name");
665
    try {
666
      String objectClassName = state.getString("objectClass");
667
      if (!StringUtils.isBlank(objectClassName)) {
668
        objectClass = Class.forName(objectClassName);
669
      }
670
    } catch (Throwable e) {
671
      LOGGER.warn("Can't restore the objectClass of the FeatureAttributreDescriptor", e);
672
    }
673
    precision = state.getInt("precision");
674
    scale = state.getInt("scale");
675
    roundMode = state.getInt("roundMode");
676
    String locale_s = state.getString("locale");
677
    locale = StringUtils.isBlank(locale_s) ? null : Locale.forLanguageTag(locale_s);
678
    evaluator = (Evaluator) state.get("evaluator");
679
    primaryKey = state.getBoolean("primaryKey");
680
    readOnly = state.getBoolean("readOnly");
681
    SRS = (IProjection) state.get("SRS");
682
    geometryType = state.getInt("geometryType");
683
    geometrySubType = state.getInt("geometrySubType");
684
    if (geometryType != Geometry.TYPES.UNKNOWN
685
            && geometrySubType != Geometry.SUBTYPES.UNKNOWN) {
686
      geomType = GeometryUtils.getGeometryType(
687
              geometryType,
688
              geometrySubType
689
      );
690
    }
691
//        additionalInfo = (Map) state.get("aditionalInfo");
692
    isAutomatic = state.getBoolean("isAutomatic");
693
    isTime = state.getBoolean("isTime");
694
    if (state.hasValue("intervalStart")) {
695
      long intervalStart = state.getLong("interval_start");
696
      long intervalEnd = state.getLong("interval_end");
697
      interval = TimeSupportLocator.getManager().createRelativeInterval(intervalStart, intervalEnd);
698
    } else {
699
      interval = null;
700
    }
701
    featureAttributeEmulator = (FeatureAttributeEmulator) state.get("featureAttributeEmulator");
702
    indexed = state.getBoolean("indexed");
703
    isIndexAscending = state.getBoolean("isIndexAscending");
704
    allowIndexDuplicateds = state.getBoolean("allowIndexDuplicateds");
705

    
706
    Map<String, Object> values = state.getMap("availableValues");
707
    if (values == null || values.isEmpty()) {
708
      this.availableValues = null;
709
    } else {
710
      this.availableValues = new DynObjectValueItem[values.size()];
711
      int n = 0;
712
      for (Entry<String, Object> entry : values.entrySet()) {
713
        this.availableValues[n++] = new DynObjectValueItem(entry.getValue(), entry.getKey());
714
      }
715
    }
716

    
717
    description = state.getString("description");
718
    minValue = state.get("minValue");
719
    maxValue = state.get("maxValue");
720
    label = state.getString("label");
721
    order = state.getInt("order");
722
    hidden = state.getBoolean("hidden");
723
    groupName = state.getString("groupName");
724
    relationType = state.getInt("relationType", RELATION_TYPE_NONE);
725

    
726
    foreingKey = (DefaultForeingKey) state.get("foreingKey");
727
    if (foreingKey != null) {
728
      this.foreingKey.setDescriptor(this);
729
    }
730
    tags = (Tags) state.get("tags");
731
    if (tags == null) {
732
      this.tags = new DefaultTags();
733
    }
734
    displaySize = state.getInt("displaySize",0);
735
  }
736

    
737
  @Override
738
  public void saveToState(PersistentState state) throws PersistenceException {
739
    Coercion toString = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.STRING);
740
    
741
    state.set("allowNull", allowNull);
742
    state.set("dataType", dataType.getType());
743
    state.set("dataProfile", dataProfile);
744

    
745
//        FIXME: dateFormat;
746
    state.set("defaultValue", Objects.toString(defaultValue, null));
747

    
748
    state.set("index", index);
749
    state.set("maximumOccurrences", maximumOccurrences);
750
    state.set("minimumOccurrences", minimumOccurrences);
751
    state.set("size", size);
752
    state.set("name", name);
753
    state.set("objectClass", objectClass == null ? null : objectClass.getName());
754
    state.set("precision", precision);
755
    state.set("scale", scale);
756
    state.set("roundMode", roundMode);
757
    try {
758
      if( this.locale == null ) {
759
        state.setNull("locale");
760
      } else {
761
        state.set("locale", toString.coerce(this.locale));
762
      }
763
    } catch (CoercionException ex) {
764
      state.setNull("locale");
765
    }
766
    state.set("evaluator", evaluator);
767

    
768
    state.set("primaryKey", primaryKey);
769
    state.set("readOnly", readOnly);
770
    state.set("SRS", SRS);
771
    GeometryType theGeomType = this.getGeomType();
772
    if (theGeomType == null) {
773
      state.set("geometryType", Geometry.TYPES.UNKNOWN);
774
      state.set("geometrySubType", Geometry.SUBTYPES.UNKNOWN);
775
    } else {
776
      state.set("geometryType", theGeomType.getType());
777
      state.set("geometrySubType", theGeomType.getSubType());
778
    }
779

    
780
//      FIXME: additionalInfo
781
    state.set("isAutomatic", isAutomatic);
782
    state.set("isTime", isTime);
783
    if (this.interval == null) {
784
      state.setNull("interval_start");
785
      state.setNull("interval_end");
786
    } else {
787
      state.set("interval_start", ((RelativeInterval) interval).getStart().toMillis());
788
      state.set("interval_end", ((RelativeInterval) interval).getEnd().toMillis());
789
    }
790
    state.set("SRS", SRS);
791

    
792
//      FIXME: featureAttributeGetter
793
    if (featureAttributeEmulator instanceof Persistent) {
794
      state.set("featureAttributeEmulator", featureAttributeEmulator);
795
    } else {
796
      state.setNull("featureAttributeEmulator");
797
    }
798

    
799
    state.set("indexed", indexed);
800
    state.set("isIndexAscending", isIndexAscending);
801
    state.set("allowIndexDuplicateds", allowIndexDuplicateds);
802

    
803
    if (this.availableValues == null) {
804
      state.setNull("availableValues");
805
    } else {
806
      Map<String, Object> values = new LinkedHashMap<>();
807
      for (DynObjectValueItem value : availableValues) {
808
        values.put(value.getLabel(), value.getValue());
809
      }
810
      state.set("availableValues", values);
811
    }
812
    state.set("description", description);
813
    state.set("minValue", minValue);
814
    state.set("maxValue", maxValue);
815
    state.set("label", label);
816
    state.set("order", order);
817
    state.set("hidden", hidden);
818
    state.set("groupName", groupName);
819
    state.set("relationType", relationType);
820

    
821
    state.set("foreingKey", this.foreingKey);
822
    state.set("tags", this.tags);
823

    
824
    state.set("displaySize", displaySize);
825
  }
826

    
827
  private static final String FEATATTRDESC_PERSISTENCE_DEFINITION_NAME = "FeatureAttributeDescriptor";
828

    
829
  public static void registerPersistenceDefinition() {
830
    PersistenceManager manager = ToolsLocator.getPersistenceManager();
831

    
832
    if (manager.getDefinition(FEATATTRDESC_PERSISTENCE_DEFINITION_NAME)
833
            == null) {
834
      DynStruct definition = manager.addDefinition(DefaultFeatureAttributeDescriptor.class,
835
              FEATATTRDESC_PERSISTENCE_DEFINITION_NAME,
836
              FEATATTRDESC_PERSISTENCE_DEFINITION_NAME
837
              + " persistent definition",
838
              null,
839
              null
840
      );
841
      definition.addDynFieldBoolean("allowNull");
842
      definition.addDynFieldInt("dataType");
843
      definition.addDynFieldString("dataProfile");
844
//            definition.addDynFieldString("dateFormat");
845
      definition.addDynFieldString("defaultValue");
846
      definition.addDynFieldInt("index");
847
      definition.addDynFieldInt("maximumOccurrences");
848
      definition.addDynFieldInt("minimumOccurrences");
849
      definition.addDynFieldInt("size");
850
      definition.addDynFieldString("name");
851
      definition.addDynFieldString("objectClass");
852
      definition.addDynFieldInt("precision");
853
      definition.addDynFieldInt("scale").setMandatory(false).setDefaultDynValue(DataType.SCALE_NONE);
854
      definition.addDynFieldInt("roundMode").setMandatory(false).setDefaultDynValue(BigDecimal.ROUND_HALF_UP);
855
      definition.addDynFieldString("locale").setMandatory(false).setDefaultDynValue("null");
856
      definition.addDynFieldObject("evaluator").setClassOfValue(Evaluator.class);
857
      definition.addDynFieldBoolean("primaryKey");
858
      definition.addDynFieldBoolean("readOnly");
859
      definition.addDynFieldObject("SRS")
860
              .setClassOfValue(IProjection.class);
861
      definition.addDynFieldInt("geometryType");
862
      definition.addDynFieldInt("geometrySubType");
863
//            definition.addDynFieldMap("additionalInfo");
864
      definition.addDynFieldBoolean("isAutomatic");
865
      definition.addDynFieldBoolean("isTime");
866
      definition.addDynFieldLong("interval_start");
867
      definition.addDynFieldLong("interval_end");
868
      definition.addDynFieldObject("featureAttributeEmulator")
869
              .setClassOfValue(FeatureAttributeEmulator.class);
870
      definition.addDynFieldBoolean("indexed");
871
      definition.addDynFieldBoolean("isIndexAscending");
872
      definition.addDynFieldBoolean("allowIndexDuplicateds");
873
      definition.addDynFieldMap("availableValues")
874
              .setClassOfItems(Object.class);
875
      definition.addDynFieldString("description");
876
      definition.addDynFieldObject("minValue");
877
      definition.addDynFieldObject("maxValue");
878
      definition.addDynFieldString("label");
879
      definition.addDynFieldInt("order");
880
      definition.addDynFieldBoolean("hidden");
881
      definition.addDynFieldString("groupName");
882
      definition.addDynFieldInt("relationType");
883

    
884
      definition.addDynFieldObject("foreingKey")
885
              .setClassOfValue(DefaultForeingKey.class);
886

    
887
      definition.addDynFieldObject("tags")
888
              .setClassOfValue(Tags.class);
889

    
890
      definition.addDynFieldInt("displaySize").setMandatory(false);
891

    
892
    }
893
  }
894

    
895
  /*
896
     * Start of DynField interface Implementation
897
     *
898
   */
899
  @Override
900
  public Tags getTags() {
901
    return tags;
902
  }
903

    
904
  @Override
905
  public boolean hasConstantAvailableValues() {
906
    return this.availableValues != null;
907
  }
908

    
909
  @Override
910
  public DynObjectValueItem[] getAvailableValues() {
911
    if (this.availableValues != null) {
912
        return this.availableValues;
913
    }
914
    if (this.availableValuesCache != null) {
915
        return this.availableValuesCache;
916
    }
917
    if (this.isForeingKey() && this.foreingKey.isClosedList()) {
918
      this.availableValuesCache = this.foreingKey.getAvailableValues(null);
919

    
920
    } else if( this.availableValuesExpression!=null ) {
921
      this.availableValuesCache = this.getAvailableValuesFromExpression();
922
    }
923
    return this.availableValuesCache;
924
  }
925
  
926
    private DynObjectValueItem[] getAvailableValuesFrom(GetItemWithSize values) {
927
        if (values.size() == 0) {
928
            return null;
929
        }
930
        DynObjectValueItem[] r = null;
931
        Object firstelement = values.get(0);
932
        if (firstelement instanceof LabeledValue) {
933
            r = new DynObjectValueItem[values.size()];
934
            for (int i = 0; i < values.size(); i++) {
935
                LabeledValue v = (LabeledValue) values.get(i);
936
                r[i] = new DynObjectValueItem(
937
                        v.getValue(),
938
                        Objects.toString(v.getLabel(), Objects.toString(v.getValue(), String.valueOf(i)))
939
                );
940
            }
941
        } else if (firstelement instanceof Pair) {
942
            r = new DynObjectValueItem[values.size()];
943
            for (int i = 0; i < values.size(); i++) {
944
                Pair v = (Pair) values.get(i);
945
                r[i] = new DynObjectValueItem(
946
                        v.getValue(),
947
                        Objects.toString(v.getKey(), Objects.toString(v.getValue(), String.valueOf(i)))
948
                );
949
            }
950
        } else if (firstelement instanceof JsonObject) {
951
            JsonObject v = (JsonObject) firstelement;
952
            String labelname = null;
953
            for (String theName : new String[]{
954
                "name", "label", "key", "description"
955
            }) {
956
                if (v.containsKey(theName)) {
957
                    labelname = theName;
958
                    break;
959
                }
960
                if (v.containsKey(theName.toUpperCase())) {
961
                    labelname = theName.toLowerCase();
962
                    break;
963
                }
964
            }
965
            String valuename = null;
966
            if (v.containsKey("value")) {
967
                valuename = "value";
968
            }
969
            r = new DynObjectValueItem[values.size()];
970
            for (int i = 0; i < values.size(); i++) {
971
                v = (JsonObject) values.get(i);
972
                String theLabel;
973
                Object theValue;
974
                if (labelname == null) {
975
                    theLabel = v.getString(labelname);
976
                } else {
977
                    theLabel = v.toString();
978
                }
979
                if (valuename == null) {
980
                    theValue = v.getString(valuename);
981
                } else {
982
                    theValue = v;
983
                }
984
                r[i] = new DynObjectValueItem(theValue, theLabel);
985
            }
986
        } else if (firstelement instanceof Feature) {
987
            Feature v = (Feature) firstelement;
988
            FeatureType featureType = v.getType();
989
            String valuename = null;
990
            FeatureAttributeDescriptor[] pks = featureType.getPrimaryKey();
991
            if( pks!=null && pks.length == 1) {
992
                valuename = pks[0].getName();
993
            }
994
            String labelname = null;
995
            for (String theName : new String[]{
996
                "name", "label", "key", "description"
997
            }) {
998
                if (featureType.get(theName)!=null ) {
999
                    labelname = theName;
1000
                    break;
1001
                }
1002
            }
1003
            r = new DynObjectValueItem[values.size()];
1004
            for (int i = 0; i < values.size(); i++) {
1005
                v = (Feature) values.get(i);
1006
                String theLabel;
1007
                Object theValue;
1008
                if (labelname == null) {
1009
                    theLabel = v.getString(labelname);
1010
                } else {
1011
                    theLabel = v.toString();
1012
                }
1013
                if( valuename == null ) {
1014
                    theValue = v.get(valuename);
1015
                } else {
1016
                    theValue = v.getReference().getCode();
1017
                }
1018
                r[i] = new DynObjectValueItem(theValue, theLabel);
1019
            }
1020
        }
1021
        return r;
1022
    }
1023
  
1024

    
1025
  private DynObjectValueItem[] getAvailableValuesFromExpression() {
1026
      if( this.availableValuesExpression == null || this.availableValuesExpression.isEmpty() ) {
1027
          return null;
1028
      }
1029
      Object value = this.availableValuesExpression.execute(null);
1030
      if( value instanceof DynObjectValueItem[] ) {
1031
          return (DynObjectValueItem[]) value;
1032
      }
1033
      if( value instanceof List ) {
1034
          return this.getAvailableValuesFrom(new GetItemWithSize() {
1035
              @Override
1036
              public Object get(int i) {
1037
                  return ((List)value).get(i);
1038
              }
1039

    
1040
              @Override
1041
              public int size() {
1042
                  return ((List)value).size();
1043
              }
1044
          });
1045
      }
1046
      return null;
1047
  }
1048

    
1049
  @Override
1050
  public Expression getAvailableValuesExpression() {
1051
      return this.availableValuesExpression;
1052
  }
1053
  
1054
  @Override
1055
    public FeatureAttributeDescriptor setAvailableValuesExpression(String expression) {
1056
        if (StringUtils.isBlank(expression)) {
1057
            this.availableValuesExpression = null;
1058
            return this;
1059
        }
1060
        this.availableValuesExpression = ExpressionUtils.createExpression(expression);
1061
        return this;
1062
    }
1063

    
1064
  @Override
1065
    public FeatureAttributeDescriptor setAvailableValuesExpression(Expression expression) {
1066
        this.availableValuesExpression = expression;
1067
        return this;
1068
    }
1069
  
1070
  @Override
1071
  public String getLabelOfValue(Object value) {
1072
    if (this.labelOfValueMap != null) {
1073
      String theLabel = this.labelOfValueMap.get(value);
1074
      if (theLabel == null) {
1075
        theLabel = Objects.toString(value, "");
1076
      }
1077
      return theLabel;
1078
    }
1079
    DynObjectValueItem[] values = this.getAvailableValues();
1080
    if (values == null) {
1081
      return Objects.toString(value, "");
1082
    }
1083
    Map<Object, String> map = new LinkedHashMap<>();
1084
    for (DynObjectValueItem theValue : values) {
1085
      map.put(theValue.getValue(), theValue.getLabel());
1086
    }
1087
    this.labelOfValueMap = map;
1088
    String theLabel = this.labelOfValueMap.get(value);
1089
    if (theLabel == null) {
1090
      theLabel = Objects.toString(value, "");
1091
    }
1092
    return theLabel;
1093
  }
1094

    
1095
  @Override
1096
  public String getDescription() {
1097
    if (this.description == null) {
1098
      return getName();
1099
    }
1100
    return this.description;
1101
  }
1102

    
1103
  @Override
1104
  public Object getMaxValue() {
1105
    return this.maxValue;
1106
  }
1107

    
1108
  @Override
1109
  public Object getMinValue() {
1110
    return this.minValue;
1111
  }
1112

    
1113
  @Override
1114
  public int getTheTypeOfAvailableValues() {
1115
    return 1;
1116
  }
1117

    
1118
  @Override
1119
  public int getType() {
1120
    if (featureAttributeGetter != null) {
1121
      return featureAttributeGetter.getDataType().getType();
1122
    }
1123
    return getDataType().getType();
1124
  }
1125

    
1126
  @Override
1127
  public boolean isMandatory() {
1128
    return !allowNull() || isPrimaryKey();
1129
  }
1130

    
1131
  @Override
1132
  public boolean isPersistent() {
1133
    return false;
1134
  }
1135

    
1136
  @Override
1137
  public DynField setAvailableValues(DynObjectValueItem[] values) {
1138
    if (ArrayUtils.isEmpty(values)) {
1139
      this.availableValues = null;
1140
    } else {
1141
      this.availableValues = values;
1142
    }
1143
    return this;
1144
  }
1145

    
1146
  @Override
1147
  public DynField setDescription(String description) {
1148
    this.description = description;
1149
    return this;
1150
  }
1151

    
1152
  @Override
1153
  public DynField setMandatory(boolean mandatory) {
1154
    throw new UnsupportedOperationException();
1155
  }
1156

    
1157
  @Override
1158
  public DynField setMaxValue(Object maxValue) {
1159
    try {
1160
      this.maxValue = this.coerce(maxValue);
1161
    } catch (CoercionException e) {
1162
      throw new IllegalArgumentException(e);
1163
    }
1164
    return this;
1165
  }
1166

    
1167
  @Override
1168
  public DynField setMinValue(Object minValue) {
1169
    try {
1170
      this.maxValue = this.coerce(minValue);
1171
    } catch (CoercionException e) {
1172
      throw new IllegalArgumentException(e);
1173
    }
1174
    return this;
1175
  }
1176

    
1177
  @Override
1178
  public DynField setPersistent(boolean persistent) {
1179
    throw new UnsupportedOperationException();
1180
  }
1181

    
1182
  @Override
1183
  public DynField setTheTypeOfAvailableValues(int type) {
1184
    throw new UnsupportedOperationException();
1185
  }
1186

    
1187
  @Override
1188
  public DynField setType(int type) {
1189
    throw new UnsupportedOperationException();
1190
  }
1191

    
1192
  @Override
1193
  public DynField setDefaultDynValue(Object defaultValue) {
1194
    throw new UnsupportedOperationException();
1195
  }
1196

    
1197
  @Override
1198
  public Class getClassOfValue() {
1199
    return null;
1200
  }
1201

    
1202
  @Override
1203
  public DynField getElementsType() {
1204
    return null;
1205
  }
1206

    
1207
  @Override
1208
  public DynField setClassOfValue(Class theClass)
1209
          throws DynFieldIsNotAContainerException {
1210
    throw new UnsupportedOperationException();
1211
  }
1212

    
1213
  @Override
1214
  public DynField setElementsType(DynStruct type)
1215
          throws DynFieldIsNotAContainerException {
1216
    throw new UnsupportedOperationException();
1217
  }
1218

    
1219
  @Override
1220
  public DynField setElementsType(int type)
1221
          throws DynFieldIsNotAContainerException {
1222
    throw new UnsupportedOperationException();
1223
  }
1224

    
1225
  public FeatureAttributeDescriptor setDataProfileName(String dataProfile) {
1226
    this.dataProfile = dataProfile;
1227
    return this;
1228
  }
1229

    
1230
  @Override
1231
  public String getDataProfileName() {
1232
    return dataProfile;
1233
  }
1234

    
1235
  @Override
1236
  public DataProfile getDataProfile() {
1237
    if (StringUtils.isBlank(dataProfile)) {
1238
      return null;
1239
    }
1240
    DataProfile profile = DALLocator.getDataManager().getDataProfile(dataProfile);
1241
    return profile;
1242
  }
1243

    
1244
  @Override
1245
  public void validate(Object value) throws DynFieldValidateException {
1246

    
1247
    if (value == null && !this.allowNull()) {
1248
      throw new DynFieldValidateException(value, this, null);
1249
    }
1250

    
1251
    try {
1252
      this.dataType.coerce(value);
1253
    } catch (CoercionException e) {
1254
      throw new DynFieldValidateException(value, this, e);
1255
    }
1256

    
1257
    /*
1258
         * Other checks will be needed
1259
     */
1260
  }
1261

    
1262
  @Override
1263
  public String getSubtype() {
1264
    if (featureAttributeGetter != null) {
1265
      return featureAttributeGetter.getDataType().getSubtype();
1266
    }
1267
    return this.dataType.getSubtype();
1268
  }
1269

    
1270
  @Override
1271
  public Object coerce(Object value) throws CoercionException {
1272
    if (value == null) {
1273
      return value; // O debe devolver this.defaultValue
1274
    }
1275
    try {
1276
      return this.getDataType().coerce(value, this.getCoercionContext());
1277
    } catch (Exception ex) {
1278
      throw new RuntimeException(ex);
1279
    }
1280
  }
1281

    
1282
  @Override
1283
  public DynField setAvailableValues(List values) {
1284
    if (values == null || values.isEmpty()) {
1285
      this.availableValues = null;
1286
    } else {
1287
      this.availableValues = (DynObjectValueItem[]) values.toArray(
1288
              new DynObjectValueItem[values.size()]
1289
      );
1290
    }
1291
    return this;
1292
  }
1293

    
1294
  @Override
1295
  public String getGroup() {
1296
    return this.groupName;
1297
  }
1298

    
1299
  @Override
1300
  public int getOder() {
1301
    return this.order;
1302
  }
1303

    
1304
  @Override
1305
  public String getLabel() {
1306
    if (this.label == null) {
1307
      return this.getName();
1308
    }
1309
    return this.label;
1310
  }
1311

    
1312
  @Override
1313
  public String getLocalizedLabel() {
1314
    if (StringUtils.isBlank(this.label)) {
1315
      return this.getName();
1316
    }
1317
    I18nManager i18n = ToolsLocator.getI18nManager();
1318
    return i18n.getTranslation(this.label);
1319
  }
1320

    
1321
  @Override
1322
  public DynField setLabel(String label) {
1323
    this.label = label;
1324
    return this;
1325
  }
1326

    
1327
  @Override
1328
  public DynField setShortLabel(String shortLabel) {
1329
    this.shortLabel = shortLabel;
1330
    return this;
1331
  }
1332

    
1333
  @Override
1334
  public String getShortLabel() {
1335
    return StringUtils.isBlank(shortLabel) ? getLabel() : shortLabel;
1336
  }
1337

    
1338
  @Override
1339
  public String getLocalizedShortLabel() {
1340
    if (StringUtils.isBlank(shortLabel)) {
1341
      return this.getLocalizedLabel();
1342
    }
1343
    I18nManager i18n = ToolsLocator.getI18nManager();
1344
    return i18n.getTranslation(shortLabel);
1345
  }
1346

    
1347
  @Override
1348
  public DynField setGroup(String groupName) {
1349
    this.groupName = groupName;
1350
    return this;
1351
  }
1352

    
1353
  @Override
1354
  public DynField setOrder(int order) {
1355
    this.order = order;
1356
    return this;
1357
  }
1358

    
1359
  @Override
1360
  public DynField setHidden(boolean hidden) {
1361
    this.hidden = hidden;
1362
    return this;
1363
  }
1364

    
1365
  @Override
1366
  public boolean isHidden() {
1367
    return this.hidden;
1368
  }
1369

    
1370
  @Override
1371
  public DynField setReadOnly(boolean readOnly) {
1372
    this.readOnly = readOnly;
1373
    return this;
1374
  }
1375

    
1376
  @Override
1377
  public boolean isContainer() {
1378
    return false;
1379
  }
1380

    
1381
  @Override
1382
  public Class getClassOfItems() {
1383
    return null;
1384
  }
1385

    
1386
  @Override
1387
  public DynField setDefaultFieldValue(Object defaultValue) {
1388
    throw new UnsupportedOperationException();
1389
  }
1390

    
1391
  @Override
1392
  public DynField setClassOfItems(Class theClass) {
1393
    throw new UnsupportedOperationException();
1394
  }
1395

    
1396
  @Override
1397
  public DynField setType(DataType type) {
1398
    throw new UnsupportedOperationException();
1399
  }
1400

    
1401
  @Override
1402
  public DynField setSubtype(String subtype) {
1403
    throw new UnsupportedOperationException();
1404
  }
1405

    
1406
  @Override
1407
  public boolean isTime() {
1408
    return isTime;
1409
  }
1410

    
1411
  @Override
1412
  public FeatureAttributeGetter getFeatureAttributeGetter() {
1413
    return featureAttributeGetter;
1414
  }
1415

    
1416
  @Override
1417
  public void setFeatureAttributeGetter(
1418
          FeatureAttributeGetter featureAttributeTransform) {
1419
    this.featureAttributeGetter = featureAttributeTransform;
1420
  }
1421

    
1422
  @Override
1423
  public FeatureAttributeEmulator getFeatureAttributeEmulator() {
1424
    return this.featureAttributeEmulator;
1425
  }
1426

    
1427
  public FeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
1428
    this.featureAttributeEmulator = featureAttributeEmulator;
1429
    return this;
1430
  }
1431

    
1432
  @Override
1433
  public boolean isIndexed() {
1434
    return this.indexed;
1435
  }
1436

    
1437
  @Override
1438
  public boolean isForeingKey() {
1439
    return this.foreingKey != null && this.foreingKey.isForeingKey();
1440
  }
1441

    
1442
  @Override
1443
  public ForeingKey getForeingKey() {
1444
    return this.foreingKey;
1445
  }
1446

    
1447
  @Override
1448
  public boolean allowIndexDuplicateds() {
1449
    return this.allowIndexDuplicateds;
1450
  }
1451

    
1452
  @Override
1453
  public boolean isIndexAscending() {
1454
    return this.isIndexAscending;
1455
  }
1456

    
1457
  @Override
1458
  public DynField setClassOfValue(DynStruct dynStrct) {
1459
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1460
  }
1461

    
1462
  @Override
1463
  public DynField setClassOfValue(String theClassNameOfValue) {
1464
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1465
  }
1466

    
1467
  @Override
1468
  public String getClassNameOfValue() {
1469
    return null;
1470
  }
1471

    
1472
  @Override
1473
  public DynStruct getDynClassOfValue() {
1474
    return null;
1475
  }
1476

    
1477
  @Override
1478
  public DynField setTypeOfItems(int type) {
1479
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1480
  }
1481

    
1482
  @Override
1483
  public int getTypeOfItems() {
1484
    return DataTypes.INVALID;
1485
  }
1486

    
1487
  @Override
1488
  public DynField setClassOfItems(DynStruct dynStrct) {
1489
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1490
  }
1491

    
1492
  @Override
1493
  public DynField setClassOfItems(String theClassNameOfValue) {
1494
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1495
  }
1496

    
1497
  @Override
1498
  public String getClassNameOfItems() {
1499
    return null;
1500
  }
1501

    
1502
  @Override
1503
  public DynStruct getDynClassOfItems() {
1504
    return null;
1505
  }
1506

    
1507
  @Override
1508
  public DynField setRelationType(int relationType) {
1509
    this.relationType = relationType;
1510
    return this;
1511
  }
1512

    
1513
  @Override
1514
  public int getRelationType() {
1515
    return this.relationType;
1516
  }
1517

    
1518
  @Override
1519
  public DynField setAvailableValues(DynMethod availableValuesMethod) {
1520
    this.availableValuesMethod = availableValuesMethod;
1521
    return this;
1522
  }
1523

    
1524
  @Override
1525
  public DynObjectValueItem[] getAvailableValues(DynObject self) {
1526
    if (this.availableValuesMethod != null) {
1527
      DynObjectValueItem[] values;
1528
      try {
1529
        values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(self, new Object[]{this});
1530
      } catch (DynMethodException ex) {
1531
        return this.availableValues;
1532
      }
1533
      if (values != null) {
1534
        return values;
1535
      }
1536
    }
1537
    return this.availableValues;
1538
  }
1539

    
1540
  @Override
1541
  public DynMethod getAvailableValuesMethod() {
1542
    return this.availableValuesMethod;
1543
  }
1544

    
1545
  @Override
1546
  public boolean isAvailableValuesCalculated() {
1547
    return this.availableValuesMethod != null;
1548
  }
1549

    
1550
  @Override
1551
  public DynMethod getCalculateMethod() {
1552
    return this.calculateMethod;
1553
  }
1554

    
1555
  @Override
1556
  public DynField setCalculateMethod(DynMethod method) {
1557
    this.calculateMethod = method;
1558
    return this;
1559
  }
1560

    
1561
  @Override
1562
  public boolean isCalculated() {
1563
    return this.calculateMethod != null;
1564
  }
1565

    
1566
  @Override
1567
  public Object getCalculatedValue(DynObject self) {
1568
    try {
1569
      return this.calculateMethod.invoke(self, new Object[]{this});
1570
    } catch (DynMethodException ex) {
1571
      throw new RuntimeException(ex);
1572
    }
1573
  }
1574

    
1575
  @Override
1576
  public DynField setValidateElements(boolean validate) {
1577
    return this;
1578
  }
1579

    
1580
  @Override
1581
  public boolean getValidateElements() {
1582
    return false;
1583
  }
1584

    
1585
  @Override
1586
  public boolean hasLabel() {
1587
    return StringUtils.isNotBlank(this.label);
1588
  }
1589

    
1590
  @Override
1591
  public boolean hasShortLabel() {
1592
    return StringUtils.isNotBlank(this.shortLabel);
1593
  }
1594

    
1595
  @Override
1596
  public boolean hasDescription() {
1597
    return StringUtils.isNotBlank(this.description);
1598
  }
1599

    
1600
  @Override
1601
  public FeatureAttributeDescriptor getValue() {
1602
    return this;
1603
  }
1604

    
1605
  @Override
1606
  public int getDisplaySize() {
1607
    return this.displaySize;
1608
  }
1609

    
1610
  private class ConstantValueEvaluator extends AbstractEvaluator {
1611

    
1612
    @Override
1613
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
1614
      return defaultValue;
1615
    }
1616

    
1617
    @Override
1618
    public String getName() {
1619
      return "Constant attribute " + name;
1620
    }
1621
  }
1622

    
1623
  public void setConstantValue(boolean isConstantValue) {
1624
    if (isConstantValue) {
1625
      /* Cuando un attributo tiene asociado un evaluador, este se interpreta
1626
             * como que no debe cargarse de la fuente de datos subyacente, siendo
1627
             * el evaluador el que se encarga de proporcionar su valor.
1628
             * Nos limitamos a asignar un evaluador que retorna simpre el valor
1629
             * por defecto para ese attributo.
1630
       */
1631
      this.evaluator = new ConstantValueEvaluator();
1632
    } else {
1633
      this.evaluator = null;
1634
    }
1635
  }
1636

    
1637
  @Override
1638
  public boolean isComputed() {
1639
    return featureAttributeEmulator != null || evaluator != null || isCalculated();
1640
  }
1641

    
1642
  @Override
1643
  public FeatureStore getStore() {
1644
    FeatureType ftype = this.getFeatureType();
1645
    if (ftype == null) {
1646
      return null;
1647
    }
1648
    return ftype.getStore();
1649
  }
1650

    
1651
  @Override
1652
  public FeatureType getFeatureType() {
1653
    if (this.typeRef == null) {
1654
      return null;
1655
    }
1656
    FeatureType ftype = (FeatureType) this.typeRef.get();
1657
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] get FeatureType [%08x], ref [%08x].", this.hashCode(), ftype.hashCode(), typeRef.hashCode()));
1658
    return ftype;
1659
  }
1660

    
1661
  public FeatureAttributeDescriptor setInterval(Interval interval) {
1662
    this.interval = interval;
1663
    return this;
1664
  }
1665

    
1666
  public void fixAll() {
1667
    if (!this.getDataType().supportSize()) {
1668
      this.size = 0;
1669
    }
1670
    NumberPrecisionAndScale ps = this.getDataType().fixPrecisionAndScale(this.precision, this.scale);
1671
    this.precision = ps.getPrecision();
1672
    this.scale = ps.getScale();
1673

    
1674
    switch (this.getType()) {
1675
      case DataTypes.INSTANT:
1676
      case DataTypes.INTERVAL:
1677
      case DataTypes.DATE:
1678
      case DataTypes.TIME:
1679
      case DataTypes.TIMESTAMP:
1680
        if (this.getInterval() != null) {
1681
          this.isTime = true;
1682
        }
1683
        break;
1684
    }
1685
  }
1686

    
1687
  @Override
1688
  public String[] getRequiredFieldNames() {
1689
    FeatureAttributeEmulator emulator = this.getFeatureAttributeEmulator();
1690
    if (emulator == null) {
1691
      return null;
1692
    }
1693
    return emulator.getRequiredFieldNames();
1694
  }
1695

    
1696
  @Override
1697
  public void recentUsed() {
1698
    DefaultFeatureType.RECENTS_USEDS.add(this);
1699
  }
1700

    
1701
  @Override
1702
  public boolean hasOnlyMetadataChanges(FeatureAttributeDescriptor other) {
1703
    if (other == null) {
1704
      throw new NullPointerException();
1705
    }
1706
    DefaultFeatureAttributeDescriptor old = (DefaultFeatureAttributeDescriptor) other;
1707
    if (!StringUtils.equalsIgnoreCase(old.name, this.name)) {
1708
      return false;
1709
    }
1710
    if (old.isComputed() && old.isComputed() == this.isComputed()) {
1711
      return true;
1712
    }
1713
    if (this.dataType != old.dataType) {
1714
      return false;
1715
    }
1716
    if (this.size != old.size) {
1717
      return false;
1718
    }
1719
    if (this.precision != old.precision) {
1720
      return false;
1721
    }
1722
//        if( this.primaryKey != old.primaryKey ) {
1723
//            return false;
1724
//        }
1725
    if (this.geomType != old.geomType) {
1726
      return false;
1727
    }
1728
    if (this.SRS != old.SRS) {
1729
      return false;
1730
    }
1731
    if (this.isAutomatic != old.isAutomatic) {
1732
      return false;
1733
    }
1734
    return true;
1735
  }
1736

    
1737
  private String getAll() {
1738
    StringBuilder builder = new StringBuilder();
1739
    builder.append(this.name).append("__");
1740
    builder.append(this.dataType.getName());
1741
    if( this.size>0 ) {
1742
        builder.append("__set__size=").append(this.size);
1743
    }
1744
    if( this.precision>0 ) {
1745
        builder.append("__set__precision=").append(this.precision);
1746
    }
1747
    if( this.isHidden() ) { 
1748
        builder.append("__set__hidden=true");
1749
    }
1750
    if( this.isReadOnly() ) {
1751
        builder.append("__set__readonly=true");
1752
    }
1753
    builder.append("__set__allownull=").append(this.allowNull());
1754
    if( this.isPrimaryKey()) { 
1755
        builder.append("__set__pk=true");
1756
    }
1757
    if( this.isAutomatic()) { 
1758
        builder.append("__set__automatic=true");
1759
    }
1760
    if( this.isTime()) { 
1761
        builder.append("__set__istime=true");
1762
    }
1763
    if( !StringUtils.isBlank(this.getDataProfileName()) ) {
1764
        builder.append("__set__profile=").append(this.getDataProfileName());
1765
    }
1766
    if( !StringUtils.isBlank(this.getGroup()) ) {
1767
        builder.append("__set__group=").append(this.getGroup());
1768
    }
1769
    if( !StringUtils.isBlank(this.description) ) {
1770
        builder.append("__set__description=").append(this.description);
1771
    }
1772
    if( !StringUtils.isBlank(this.label) ) {
1773
        builder.append("__set__label=").append(this.label);
1774
    }
1775
    if( !StringUtils.isBlank(this.shortLabel) ) {
1776
        builder.append("__set__shortLabel=").append(this.shortLabel);
1777
    }
1778
    if( this.locale!=null ) {
1779
        builder.append("__set__locale=").append(this.getLocale());
1780
    }
1781
    builder.append("__set__order=").append(this.getOder());
1782
    if( this.getSRS()!=null ) {
1783
        builder.append("__set__srs=").append(this.getSRS().getAbrev());
1784
    }
1785
    if( this.getGeomType()!=null ) {
1786
        builder.append("__set__geomtype=").append(this.getGeomType().getFullName().replace(":", "@"));
1787
    }
1788
    return builder.toString();
1789
  }
1790
  
1791
  @Override
1792
  public Object get(String name) {
1793
    if (StringUtils.isBlank(name)) {
1794
      throw new IllegalArgumentException("Name can't be empty");
1795
    }
1796
    switch (name.trim().toLowerCase()) {
1797
      case "all":
1798
          return this.getAll();
1799
      case "isreadonly":
1800
      case "readonly":
1801
        return this.isReadOnly();
1802
      case "hidden":
1803
        return this.isHidden();
1804
      case "allownull":
1805
        return this.allowNull();
1806
      case "pk":
1807
      case "ispk":
1808
      case "primarykey":
1809
      case "isprimarykey":
1810
        return this.isPrimaryKey();
1811
      case "isautomatic":
1812
      case "automatic":
1813
        return this.isAutomatic();
1814
      case "time":
1815
      case "istime":
1816
        return this.isTime();
1817
      case "profile":
1818
        return this.getDataProfile();
1819
      case "group":
1820
        return this.getGroup();
1821
      case "description":
1822
        return this.getDescription();
1823
      case "label":
1824
        return this.getLabel();
1825
      case "shortlabel":
1826
        return this.getShortLabel();
1827
      case "expression":
1828
        return this.getFeatureAttributeEmulator();
1829
      case "size":
1830
        return this.getSize();
1831
      case "precision":
1832
        return this.getPrecision();
1833
      case "scale":
1834
        return this.getScale();
1835
      case "roundmode":
1836
        return this.getRoundMode();
1837
      case "locale":
1838
        return this.getLocale();
1839
      case "order":
1840
        return this.getOder();
1841
      case "foreingkey":
1842
        return this.getForeingKey();
1843
      case "interval":
1844
        return this.getInterval();
1845
      case "geomtype":
1846
      case "geometrytype":
1847
        return this.getGeomType();
1848
      case "srs":
1849
        return this.getSRS();
1850
      default:
1851
        throw new IllegalArgumentException("Name attribute '" + name + "' not valid.");
1852
    }
1853
  }
1854

    
1855
}