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

History | View | Annotate | Download (46.9 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 org.apache.commons.lang3.ArrayUtils;
39
import org.apache.commons.lang3.StringUtils;
40
import org.cresques.cts.IProjection;
41
import org.gvsig.expressionevaluator.ExpressionUtils;
42
import org.gvsig.fmap.dal.DALLocator;
43
import org.gvsig.fmap.dal.DataStore;
44
import org.gvsig.fmap.dal.DataTypes;
45
import org.gvsig.fmap.dal.feature.DataProfile;
46
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
48
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.dal.feature.FeatureType;
51
import org.gvsig.fmap.dal.feature.ForeingKey;
52
import org.gvsig.fmap.geom.Geometry;
53
import org.gvsig.fmap.geom.GeometryException;
54
import org.gvsig.fmap.geom.GeometryLocator;
55
import org.gvsig.fmap.geom.GeometryUtils;
56
import org.gvsig.fmap.geom.type.GeometryType;
57
import org.gvsig.timesupport.Interval;
58
import org.gvsig.timesupport.RelativeInterval;
59
import org.gvsig.timesupport.TimeSupportLocator;
60
import org.gvsig.tools.ToolsLocator;
61
import org.gvsig.tools.dataTypes.Coercion;
62
import org.gvsig.tools.dataTypes.CoercionException;
63
import org.gvsig.tools.dataTypes.DataType;
64
import org.gvsig.tools.dataTypes.DataTypeUtils;
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.i18n.I18nManager;
82
import org.gvsig.tools.persistence.PersistenceManager;
83
import org.gvsig.tools.persistence.Persistent;
84
import org.gvsig.tools.persistence.PersistentState;
85
import org.gvsig.tools.persistence.exception.PersistenceException;
86
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
88
import org.gvsig.tools.dataTypes.CoercionContext;
89
import org.gvsig.tools.dataTypes.DataType.NumberPrecisionAndScale;
90

    
91
@SuppressWarnings("UseSpecificCatch")
92
public class DefaultFeatureAttributeDescriptor implements
93
        FeatureAttributeDescriptor, Persistent, DynField_v2, DynField_LabelAttribute {
94

    
95
  protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeDescriptor.class);
96

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

    
128
  protected DynObjectValueItem[] availableValues;
129
  private Map<Object, String> labelOfValueMap; // No persistente
130
  protected String description;
131
  protected Object minValue;
132
  protected Object maxValue;
133
  protected String label;
134
  protected String shortLabel;
135
  protected int order;
136
  protected boolean hidden;
137
  protected String groupName;
138
  protected Tags tags = new DefaultTags();
139
  private DynMethod availableValuesMethod;
140
  private DynMethod calculateMethod;
141
  private WeakReference typeRef;
142
  protected DefaultForeingKey foreingKey = null;
143

    
144
  protected CoercionContext coerceContext = null; // not persistent
145
  protected MathContext mathContext = null; // not persistent
146

    
147
  private int relationType = RELATION_TYPE_NONE;
148
  protected Locale locale;
149
  protected int displaySize;
150

    
151
  public DefaultFeatureAttributeDescriptor() {
152
    // Usada en la persistencia
153
    this.precision = DataType.PRECISION_NONE;
154
    this.scale = DataType.SCALE_NONE;
155
    this.roundMode = BigDecimal.ROUND_HALF_UP;
156
  }
157

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

    
189
  protected DefaultFeatureAttributeDescriptor(
190
          DefaultFeatureAttributeDescriptor other
191
  ) {
192
    this();
193
    copyFrom(other);
194
//        LOGGER.info(String.format("Created FeatureAttributeDescriptor [%08x] [%s] (copy).", this.hashCode(), this.name));
195
  }
196

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

    
244
    this.availableValues = other.availableValues;
245
    this.description = other.description;
246
    this.minValue = other.minValue;
247
    this.maxValue = other.maxValue;
248
    this.label = other.label;
249
    this.order = other.order;
250
    this.groupName = other.groupName;
251
    if (other.tags == null) {
252
      this.tags = null;
253
    } else {
254
      try {
255
        this.tags = (Tags) other.tags.clone();
256
      } catch (Exception ex) {
257
      }
258
    }
259
    this.foreingKey = null;
260
    if (other.foreingKey != null) {
261
      try {
262
        this.foreingKey = (DefaultForeingKey) other.foreingKey.clone();
263
      } catch (CloneNotSupportedException ex) {
264
      }
265
    }
266
    if (this.foreingKey != null) {
267
      this.foreingKey.setDescriptor(this);
268
    }
269

    
270
    // TODO: ? Habria que clonarlos ?
271
    this.availableValuesMethod = other.availableValuesMethod;
272
    this.calculateMethod = other.calculateMethod;
273
    this.relationType = other.relationType;
274
    this.locale = other.locale;
275
    this.displaySize = other.displaySize;
276
  }
277

    
278
  public void setFeatureType(FeatureType type) {
279
    // Usada en la persistencia
280
    if (type == null) {
281
      this.typeRef = null;
282
    } else {
283
      this.typeRef = new WeakReference(type);
284
//            LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] set FeatureType [%08x], ref [%08x].", this.hashCode(), type.hashCode(), typeRef.hashCode()));
285
    }
286
  }
287

    
288
  @Override
289
  public String getDataTypeName() {
290
    if (this.getDataType() == null) {
291
      return "(unknow)";
292
    }
293
    return this.getDataType().getName();
294
  }
295

    
296
  @Override
297
  public DefaultFeatureAttributeDescriptor getCopy() {
298
    return new DefaultFeatureAttributeDescriptor(this);
299
  }
300

    
301
  @Override
302
  public Object clone() throws CloneNotSupportedException {
303
    return new DefaultFeatureAttributeDescriptor(this);
304
  }
305

    
306
  @Override
307
  public boolean allowNull() {
308
    return allowNull;
309
  }
310

    
311
  @Override
312
  public Locale getLocale() {
313
    if( this.locale == null ) {
314
      if( this.dataType.isNumeric() ) {
315
        this.locale = Locale.ENGLISH;
316
      } else {
317
        this.locale = Locale.getDefault();
318
      }
319
    }
320
    return this.locale;
321
  }
322

    
323
  @Override
324
  public DataType getDataType() {
325
    if (featureAttributeGetter != null) {
326
      return featureAttributeGetter.getDataType();
327
    }
328
    return this.dataType;
329
  }
330

    
331
  public FeatureAttributeDescriptor setDataType(int type) {
332
    this.dataType = ToolsLocator.getDataTypesManager().get(type);
333
    return this;
334
  }
335

    
336
  @Override
337
  public DateFormat getDateFormat() {
338
    return this.dateFormat;
339
  }
340

    
341
  @Override
342
  public Object getDefaultValue() {
343
    return this.defaultValue;
344
  }
345

    
346
  @Override
347
  public Object getDefaultValueCoerced() {
348
    try {
349
      Object value = this.defaultValue;
350
      if (value == null) {
351
        return null;
352
      }
353
      if (ExpressionUtils.isDynamicText(value.toString())) {
354
        value = ExpressionUtils.evaluateDynamicText(value.toString());
355
      }
356
      return this.getDataType().coerce(value);
357
    } catch (CoercionException ex) {
358
      return null;
359
    }
360
  }
361

    
362
  @Override
363
  public Evaluator getEvaluator() {
364
    return this.evaluator;
365
  }
366

    
367
  @Override
368
  public int getGeometryType() {
369
    if (this.dataType.getType() != DataTypes.GEOMETRY) {
370
      return Geometry.TYPES.UNKNOWN;
371
    }
372
    return this.geometryType;
373
  }
374

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

    
383
  @Override
384
  public GeometryType getGeomType() {
385
    if (this.dataType.getType() != DataTypes.GEOMETRY) {
386
      return null;
387
    }
388
    if (this.geomType == null) {
389
      try {
390
        this.geomType
391
                = GeometryLocator.getGeometryManager().getGeometryType(
392
                        this.geometryType, this.geometrySubType);
393
      } catch (GeometryException e) {
394
        throw new RuntimeException(
395
                "Error getting geometry type with type = "
396
                + this.geometryType + ", subtype = "
397
                + this.geometrySubType, e);
398
      }
399
    }
400
    return this.geomType;
401
  }
402

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

    
408
  protected FeatureAttributeDescriptor setIndex(int index) {
409
    this.index = index;
410
    return this;
411
  }
412

    
413
  @Override
414
  public int getMaximumOccurrences() {
415
    return this.maximumOccurrences;
416
  }
417

    
418
  @Override
419
  public int getMinimumOccurrences() {
420
    return this.minimumOccurrences;
421
  }
422

    
423
  @Override
424
  public String getName() {
425
    return this.name;
426
  }
427

    
428
  public FeatureAttributeDescriptor setName(String name) {
429
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] set name [%s].", this.hashCode(), name));
430
    this.name = name;
431
    return this;
432
  }
433

    
434
  @Override
435
  public Class getObjectClass() {
436
    if (getDataType().getType() == DataTypes.OBJECT) {
437
      return objectClass;
438
    }
439
    return getDataType().getDefaultClass();
440
  }
441

    
442
  @Override
443
  public int getPrecision() {
444
    return this.precision;
445
  }
446

    
447
  @Override
448
  public int getScale() {
449
    return this.scale;
450
  }
451

    
452
  @Override
453
  public Coercion getCoercion() {
454
    return this.getDataType().getCoercion();
455
  }
456

    
457
  @Override
458
  public MathContext getMathContext() {
459
    if (this.mathContext == null) {
460
      if (this.getDataType().isNumeric()) {
461
        this.mathContext = new MathContext(
462
                this.getPrecision(),
463
                RoundingMode.valueOf(this.getRoundMode())
464
        );
465
      } else {
466
        this.mathContext = MathContext.UNLIMITED;
467
      }
468
    }
469
    return this.mathContext;
470
  }
471

    
472
  @Override
473
  public CoercionContext getCoercionContext() {
474
    if (this.coerceContext == null) {
475
      if (this.getDataType().isNumeric()) {
476
        this.coerceContext = DataTypeUtils.coerceContextDecimal(
477
                this.getLocale(),
478
                this.getPrecision(),
479
                this.getScale(),
480
                this.getRoundMode()
481
        );
482
      } else {
483
        this.coerceContext = DataTypeUtils.coerceContextLocale(
484
                this.getLocale()
485
        );
486
      }
487
    }
488
    return this.coerceContext;
489
  }
490

    
491
  @Override
492
  public int getRoundMode() {
493
    return this.roundMode;
494
  }
495

    
496
  @Override
497
  public IProjection getSRS() {
498
    return this.SRS;
499
  }
500

    
501
  @Override
502
  public Interval getInterval() {
503
    return this.interval;
504
  }
505

    
506
  public IProjection getSRS(WeakReference storeRef) {
507
    if (this.SRS == null) {
508
      FeatureStore store = (FeatureStore) storeRef.get();
509
      this.SRS = (IProjection) store.getDynValue(DataStore.METADATA_CRS);
510
    }
511
    return this.SRS;
512
  }
513

    
514
  @Override
515
  public int getSize() {
516
    return this.size;
517
  }
518

    
519
  @Override
520
  public boolean isPrimaryKey() {
521
    return this.primaryKey;
522
  }
523

    
524
  @Override
525
  public boolean isReadOnly() {
526
    if (this.readOnly) {
527
      return true;
528
    }
529
    return this.isComputed();
530
  }
531

    
532
  @Override
533
  public Object getAdditionalInfo(String infoName) {
534
    if (this.additionalInfo == null) {
535
      return null;
536
    }
537
    return this.additionalInfo.get(infoName);
538
  }
539

    
540
  @Override
541
  public boolean isAutomatic() {
542
    return this.isAutomatic;
543
  }
544

    
545
  @Override
546
  public boolean equals(Object obj) {
547
    if (this == obj) {
548
      return true;
549
    }
550
    if (!(obj instanceof DefaultFeatureAttributeDescriptor)) {
551
      return false;
552
    }
553
    DefaultFeatureAttributeDescriptor other
554
            = (DefaultFeatureAttributeDescriptor) obj;
555

    
556
    if (this.allowNull != other.allowNull) {
557
      return false;
558
    }
559

    
560
    if (this.index != other.index) {
561
      return false;
562
    }
563

    
564
    if (!Objects.equals(this.name, other.name)) {
565
      return false;
566
    }
567

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

    
572
    if (this.size != other.size) {
573
      return false;
574
    }
575

    
576
    if (!Objects.equals(this.defaultValue, other.defaultValue)) {
577
      return false;
578
    }
579

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

    
584
    if (this.isAutomatic != other.isAutomatic) {
585
      return false;
586
    }
587

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

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

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

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

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

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

    
612
    if (!Objects.equals(this.evaluator, other.evaluator)) {
613
      return false;
614
    }
615

    
616
    if (!Objects.equals(this.featureAttributeEmulator, other.featureAttributeEmulator)) {
617
      return false;
618
    }
619

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

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

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

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

    
636
    return true;
637
  }
638

    
639
  @Override
640
  public void loadFromState(PersistentState state)
641
          throws PersistenceException {
642
    allowNull = state.getBoolean("allowNull");
643
    dataType = ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
644
    dataProfile = state.getString("dataProfile");
645

    
646
//        FIXME: dateFormat;
647
    try {
648
      defaultValue = dataType.coerce(state.get("defaultValue"));
649
    } catch (CoercionException ex) {
650
    }
651

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

    
698
    Map<String, Object> values = state.getMap("availableValues");
699
    if (values == null || values.isEmpty()) {
700
      this.availableValues = null;
701
    } else {
702
      this.availableValues = new DynObjectValueItem[values.size()];
703
      int n = 0;
704
      for (Entry<String, Object> entry : values.entrySet()) {
705
        this.availableValues[n++] = new DynObjectValueItem(entry.getValue(), entry.getKey());
706
      }
707
    }
708

    
709
    description = state.getString("description");
710
    minValue = state.get("minValue");
711
    maxValue = state.get("maxValue");
712
    label = state.getString("label");
713
    order = state.getInt("order");
714
    hidden = state.getBoolean("hidden");
715
    groupName = state.getString("groupName");
716
    relationType = state.getInt("relationType", RELATION_TYPE_NONE);
717

    
718
    foreingKey = (DefaultForeingKey) state.get("foreingKey");
719
    if (foreingKey != null) {
720
      this.foreingKey.setDescriptor(this);
721
    }
722
    tags = (Tags) state.get("tags");
723
    if (tags == null) {
724
      this.tags = new DefaultTags();
725
    }
726
    displaySize = state.getInt("displaySize",0);
727
  }
728

    
729
  @Override
730
  public void saveToState(PersistentState state) throws PersistenceException {
731
    Coercion toString = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.STRING);
732
    
733
    state.set("allowNull", allowNull);
734
    state.set("dataType", dataType.getType());
735
    state.set("dataProfile", dataProfile);
736

    
737
//        FIXME: dateFormat;
738
    state.set("defaultValue", Objects.toString(defaultValue, null));
739

    
740
    state.set("index", index);
741
    state.set("maximumOccurrences", maximumOccurrences);
742
    state.set("minimumOccurrences", minimumOccurrences);
743
    state.set("size", size);
744
    state.set("name", name);
745
    state.set("objectClass", objectClass == null ? null : objectClass.getName());
746
    state.set("precision", precision);
747
    state.set("scale", scale);
748
    state.set("roundMode", roundMode);
749
    try {
750
      if( this.locale == null ) {
751
        state.setNull("locale");
752
      } else {
753
        state.set("locale", toString.coerce(this.locale));
754
      }
755
    } catch (CoercionException ex) {
756
      state.setNull("locale");
757
    }
758
    state.set("evaluator", evaluator);
759

    
760
    state.set("primaryKey", primaryKey);
761
    state.set("readOnly", readOnly);
762
    state.set("SRS", SRS);
763
    GeometryType theGeomType = this.getGeomType();
764
    if (theGeomType == null) {
765
      state.set("geometryType", Geometry.TYPES.UNKNOWN);
766
      state.set("geometrySubType", Geometry.SUBTYPES.UNKNOWN);
767
    } else {
768
      state.set("geometryType", theGeomType.getType());
769
      state.set("geometrySubType", theGeomType.getSubType());
770
    }
771

    
772
//      FIXME: additionalInfo
773
    state.set("isAutomatic", isAutomatic);
774
    state.set("isTime", isTime);
775
    if (this.interval == null) {
776
      state.setNull("interval_start");
777
      state.setNull("interval_end");
778
    } else {
779
      state.set("interval_start", ((RelativeInterval) interval).getStart().toMillis());
780
      state.set("interval_end", ((RelativeInterval) interval).getEnd().toMillis());
781
    }
782
    state.set("SRS", SRS);
783

    
784
//      FIXME: featureAttributeGetter
785
    if (featureAttributeEmulator instanceof Persistent) {
786
      state.set("featureAttributeEmulator", featureAttributeEmulator);
787
    } else {
788
      state.setNull("featureAttributeEmulator");
789
    }
790

    
791
    state.set("indexed", indexed);
792
    state.set("isIndexAscending", isIndexAscending);
793
    state.set("allowIndexDuplicateds", allowIndexDuplicateds);
794

    
795
    if (this.availableValues == null) {
796
      state.setNull("availableValues");
797
    } else {
798
      Map<String, Object> values = new LinkedHashMap<>();
799
      for (DynObjectValueItem value : availableValues) {
800
        values.put(value.getLabel(), value.getValue());
801
      }
802
      state.set("availableValues", values);
803
    }
804
    state.set("description", description);
805
    state.set("minValue", minValue);
806
    state.set("maxValue", maxValue);
807
    state.set("label", label);
808
    state.set("order", order);
809
    state.set("hidden", hidden);
810
    state.set("groupName", groupName);
811
    state.set("relationType", relationType);
812

    
813
    state.set("foreingKey", this.foreingKey);
814
    state.set("tags", this.tags);
815

    
816
    state.set("displaySize", displaySize);
817
  }
818

    
819
  private static final String FEATATTRDESC_PERSISTENCE_DEFINITION_NAME = "FeatureAttributeDescriptor";
820

    
821
  public static void registerPersistenceDefinition() {
822
    PersistenceManager manager = ToolsLocator.getPersistenceManager();
823

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

    
876
      definition.addDynFieldObject("foreingKey")
877
              .setClassOfValue(DefaultForeingKey.class);
878

    
879
      definition.addDynFieldObject("tags")
880
              .setClassOfValue(Tags.class);
881

    
882
      definition.addDynFieldInt("displaySize").setMandatory(false);
883

    
884
    }
885
  }
886

    
887
  /*
888
     * Start of DynField interface Implementation
889
     *
890
   */
891
  @Override
892
  public Tags getTags() {
893
    return tags;
894
  }
895

    
896
  @Override
897
  public boolean hasConstantAvailableValues() {
898
    return this.availableValues != null;
899
  }
900

    
901
  @Override
902
  public DynObjectValueItem[] getAvailableValues() {
903
    if (this.availableValues == null) {
904
      if (this.isForeingKey() && this.foreingKey.isClosedList()) {
905
        return this.foreingKey.getAvailableValues(null);
906
      }
907
    }
908
    return this.availableValues;
909
  }
910

    
911
  @Override
912
  public String getLabelOfValue(Object value) {
913
    if (this.labelOfValueMap != null) {
914
      String theLabel = this.labelOfValueMap.get(value);
915
      if (theLabel == null) {
916
        theLabel = Objects.toString(value, "");
917
      }
918
      return theLabel;
919
    }
920
    DynObjectValueItem[] values = this.getAvailableValues();
921
    if (values == null) {
922
      return Objects.toString(value, "");
923
    }
924
    Map<Object, String> map = new LinkedHashMap<>();
925
    for (DynObjectValueItem theValue : values) {
926
      map.put(theValue.getValue(), theValue.getLabel());
927
    }
928
    this.labelOfValueMap = map;
929
    String theLabel = this.labelOfValueMap.get(value);
930
    if (theLabel == null) {
931
      theLabel = Objects.toString(value, "");
932
    }
933
    return theLabel;
934
  }
935

    
936
  @Override
937
  public String getDescription() {
938
    if (this.description == null) {
939
      return getName();
940
    }
941
    return this.description;
942
  }
943

    
944
  @Override
945
  public Object getMaxValue() {
946
    return this.maxValue;
947
  }
948

    
949
  @Override
950
  public Object getMinValue() {
951
    return this.minValue;
952
  }
953

    
954
  @Override
955
  public int getTheTypeOfAvailableValues() {
956
    return 1;
957
  }
958

    
959
  @Override
960
  public int getType() {
961
    if (featureAttributeGetter != null) {
962
      return featureAttributeGetter.getDataType().getType();
963
    }
964
    return getDataType().getType();
965
  }
966

    
967
  @Override
968
  public boolean isMandatory() {
969
    return !allowNull() || isPrimaryKey();
970
  }
971

    
972
  @Override
973
  public boolean isPersistent() {
974
    return false;
975
  }
976

    
977
  @Override
978
  public DynField setAvailableValues(DynObjectValueItem[] values) {
979
    if (ArrayUtils.isEmpty(values)) {
980
      this.availableValues = null;
981
    } else {
982
      this.availableValues = values;
983
    }
984
    return this;
985
  }
986

    
987
  @Override
988
  public DynField setDescription(String description) {
989
    this.description = description;
990
    return this;
991
  }
992

    
993
  @Override
994
  public DynField setMandatory(boolean mandatory) {
995
    throw new UnsupportedOperationException();
996
  }
997

    
998
  @Override
999
  public DynField setMaxValue(Object maxValue) {
1000
    try {
1001
      this.maxValue = this.coerce(maxValue);
1002
    } catch (CoercionException e) {
1003
      throw new IllegalArgumentException(e);
1004
    }
1005
    return this;
1006
  }
1007

    
1008
  @Override
1009
  public DynField setMinValue(Object minValue) {
1010
    try {
1011
      this.maxValue = this.coerce(minValue);
1012
    } catch (CoercionException e) {
1013
      throw new IllegalArgumentException(e);
1014
    }
1015
    return this;
1016
  }
1017

    
1018
  @Override
1019
  public DynField setPersistent(boolean persistent) {
1020
    throw new UnsupportedOperationException();
1021
  }
1022

    
1023
  @Override
1024
  public DynField setTheTypeOfAvailableValues(int type) {
1025
    throw new UnsupportedOperationException();
1026
  }
1027

    
1028
  @Override
1029
  public DynField setType(int type) {
1030
    throw new UnsupportedOperationException();
1031
  }
1032

    
1033
  @Override
1034
  public DynField setDefaultDynValue(Object defaultValue) {
1035
    throw new UnsupportedOperationException();
1036
  }
1037

    
1038
  @Override
1039
  public Class getClassOfValue() {
1040
    return null;
1041
  }
1042

    
1043
  @Override
1044
  public DynField getElementsType() {
1045
    return null;
1046
  }
1047

    
1048
  @Override
1049
  public DynField setClassOfValue(Class theClass)
1050
          throws DynFieldIsNotAContainerException {
1051
    throw new UnsupportedOperationException();
1052
  }
1053

    
1054
  @Override
1055
  public DynField setElementsType(DynStruct type)
1056
          throws DynFieldIsNotAContainerException {
1057
    throw new UnsupportedOperationException();
1058
  }
1059

    
1060
  @Override
1061
  public DynField setElementsType(int type)
1062
          throws DynFieldIsNotAContainerException {
1063
    throw new UnsupportedOperationException();
1064
  }
1065

    
1066
  public FeatureAttributeDescriptor setDataProfileName(String dataProfile) {
1067
    this.dataProfile = dataProfile;
1068
    return this;
1069
  }
1070

    
1071
  @Override
1072
  public String getDataProfileName() {
1073
    return dataProfile;
1074
  }
1075

    
1076
  @Override
1077
  public DataProfile getDataProfile() {
1078
    if (StringUtils.isBlank(dataProfile)) {
1079
      return null;
1080
    }
1081
    DataProfile profile = DALLocator.getDataManager().getDataProfile(dataProfile);
1082
    return profile;
1083
  }
1084

    
1085
  @Override
1086
  public void validate(Object value) throws DynFieldValidateException {
1087

    
1088
    if (value == null && !this.allowNull()) {
1089
      throw new DynFieldValidateException(value, this, null);
1090
    }
1091

    
1092
    try {
1093
      this.dataType.coerce(value);
1094
    } catch (CoercionException e) {
1095
      throw new DynFieldValidateException(value, this, e);
1096
    }
1097

    
1098
    /*
1099
         * Other checks will be needed
1100
     */
1101
  }
1102

    
1103
  @Override
1104
  public String getSubtype() {
1105
    if (featureAttributeGetter != null) {
1106
      return featureAttributeGetter.getDataType().getSubtype();
1107
    }
1108
    return this.dataType.getSubtype();
1109
  }
1110

    
1111
  @Override
1112
  public Object coerce(Object value) throws CoercionException {
1113
    if (value == null) {
1114
      return value; // O debe devolver this.defaultValue
1115
    }
1116
    try {
1117
      return this.getDataType().coerce(value, this.getCoercionContext());
1118
    } catch (Exception ex) {
1119
      throw new RuntimeException(ex);
1120
    }
1121
  }
1122

    
1123
  @Override
1124
  public DynField setAvailableValues(List values) {
1125
    if (values == null || values.isEmpty()) {
1126
      this.availableValues = null;
1127
    } else {
1128
      this.availableValues = (DynObjectValueItem[]) values.toArray(
1129
              new DynObjectValueItem[values.size()]
1130
      );
1131
    }
1132
    return this;
1133
  }
1134

    
1135
  @Override
1136
  public String getGroup() {
1137
    return this.groupName;
1138
  }
1139

    
1140
  @Override
1141
  public int getOder() {
1142
    return this.order;
1143
  }
1144

    
1145
  @Override
1146
  public String getLabel() {
1147
    if (this.label == null) {
1148
      return this.getName();
1149
    }
1150
    return this.label;
1151
  }
1152

    
1153
  @Override
1154
  public String getLocalizedLabel() {
1155
    if (StringUtils.isBlank(this.label)) {
1156
      return this.getName();
1157
    }
1158
    I18nManager i18n = ToolsLocator.getI18nManager();
1159
    return i18n.getTranslation(this.label);
1160
  }
1161

    
1162
  @Override
1163
  public DynField setLabel(String label) {
1164
    this.label = label;
1165
    return this;
1166
  }
1167

    
1168
  @Override
1169
  public DynField setShortLabel(String shortLabel) {
1170
    this.shortLabel = shortLabel;
1171
    return this;
1172
  }
1173

    
1174
  @Override
1175
  public String getShortLabel() {
1176
    return StringUtils.isBlank(shortLabel) ? getLabel() : shortLabel;
1177
  }
1178

    
1179
  @Override
1180
  public String getLocalizedShortLabel() {
1181
    if (StringUtils.isBlank(shortLabel)) {
1182
      return this.getLocalizedLabel();
1183
    }
1184
    I18nManager i18n = ToolsLocator.getI18nManager();
1185
    return i18n.getTranslation(shortLabel);
1186
  }
1187

    
1188
  @Override
1189
  public DynField setGroup(String groupName) {
1190
    this.groupName = groupName;
1191
    return this;
1192
  }
1193

    
1194
  @Override
1195
  public DynField setOrder(int order) {
1196
    this.order = order;
1197
    return this;
1198
  }
1199

    
1200
  @Override
1201
  public DynField setHidden(boolean hidden) {
1202
    this.hidden = hidden;
1203
    return this;
1204
  }
1205

    
1206
  @Override
1207
  public boolean isHidden() {
1208
    return this.hidden;
1209
  }
1210

    
1211
  @Override
1212
  public DynField setReadOnly(boolean readOnly) {
1213
    this.readOnly = readOnly;
1214
    return this;
1215
  }
1216

    
1217
  @Override
1218
  public boolean isContainer() {
1219
    return false;
1220
  }
1221

    
1222
  @Override
1223
  public Class getClassOfItems() {
1224
    return null;
1225
  }
1226

    
1227
  @Override
1228
  public DynField setDefaultFieldValue(Object defaultValue) {
1229
    throw new UnsupportedOperationException();
1230
  }
1231

    
1232
  @Override
1233
  public DynField setClassOfItems(Class theClass) {
1234
    throw new UnsupportedOperationException();
1235
  }
1236

    
1237
  @Override
1238
  public DynField setType(DataType type) {
1239
    throw new UnsupportedOperationException();
1240
  }
1241

    
1242
  @Override
1243
  public DynField setSubtype(String subtype) {
1244
    throw new UnsupportedOperationException();
1245
  }
1246

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

    
1252
  @Override
1253
  public FeatureAttributeGetter getFeatureAttributeGetter() {
1254
    return featureAttributeGetter;
1255
  }
1256

    
1257
  @Override
1258
  public void setFeatureAttributeGetter(
1259
          FeatureAttributeGetter featureAttributeTransform) {
1260
    this.featureAttributeGetter = featureAttributeTransform;
1261
  }
1262

    
1263
  @Override
1264
  public FeatureAttributeEmulator getFeatureAttributeEmulator() {
1265
    return this.featureAttributeEmulator;
1266
  }
1267

    
1268
  public FeatureAttributeDescriptor setFeatureAttributeEmulator(FeatureAttributeEmulator featureAttributeEmulator) {
1269
    this.featureAttributeEmulator = featureAttributeEmulator;
1270
    return this;
1271
  }
1272

    
1273
  @Override
1274
  public boolean isIndexed() {
1275
    return this.indexed;
1276
  }
1277

    
1278
  @Override
1279
  public boolean isForeingKey() {
1280
    return this.foreingKey != null && this.foreingKey.isForeingKey();
1281
  }
1282

    
1283
  @Override
1284
  public ForeingKey getForeingKey() {
1285
    return this.foreingKey;
1286
  }
1287

    
1288
  @Override
1289
  public boolean allowIndexDuplicateds() {
1290
    return this.allowIndexDuplicateds;
1291
  }
1292

    
1293
  @Override
1294
  public boolean isIndexAscending() {
1295
    return this.isIndexAscending;
1296
  }
1297

    
1298
  @Override
1299
  public DynField setClassOfValue(DynStruct dynStrct) {
1300
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1301
  }
1302

    
1303
  @Override
1304
  public DynField setClassOfValue(String theClassNameOfValue) {
1305
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1306
  }
1307

    
1308
  @Override
1309
  public String getClassNameOfValue() {
1310
    return null;
1311
  }
1312

    
1313
  @Override
1314
  public DynStruct getDynClassOfValue() {
1315
    return null;
1316
  }
1317

    
1318
  @Override
1319
  public DynField setTypeOfItems(int type) {
1320
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1321
  }
1322

    
1323
  @Override
1324
  public int getTypeOfItems() {
1325
    return DataTypes.INVALID;
1326
  }
1327

    
1328
  @Override
1329
  public DynField setClassOfItems(DynStruct dynStrct) {
1330
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1331
  }
1332

    
1333
  @Override
1334
  public DynField setClassOfItems(String theClassNameOfValue) {
1335
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1336
  }
1337

    
1338
  @Override
1339
  public String getClassNameOfItems() {
1340
    return null;
1341
  }
1342

    
1343
  @Override
1344
  public DynStruct getDynClassOfItems() {
1345
    return null;
1346
  }
1347

    
1348
  @Override
1349
  public DynField setRelationType(int relationType) {
1350
    this.relationType = relationType;
1351
    return this;
1352
  }
1353

    
1354
  @Override
1355
  public int getRelationType() {
1356
    return this.relationType;
1357
  }
1358

    
1359
  @Override
1360
  public DynField setAvailableValues(DynMethod availableValuesMethod) {
1361
    this.availableValuesMethod = availableValuesMethod;
1362
    return this;
1363
  }
1364

    
1365
  @Override
1366
  public DynObjectValueItem[] getAvailableValues(DynObject self) {
1367
    if (this.availableValuesMethod != null) {
1368
      DynObjectValueItem[] values;
1369
      try {
1370
        values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(self, new Object[]{this});
1371
      } catch (DynMethodException ex) {
1372
        return this.availableValues;
1373
      }
1374
      if (values != null) {
1375
        return values;
1376
      }
1377
    }
1378
    return this.availableValues;
1379
  }
1380

    
1381
  @Override
1382
  public DynMethod getAvailableValuesMethod() {
1383
    return this.availableValuesMethod;
1384
  }
1385

    
1386
  @Override
1387
  public boolean isAvailableValuesCalculated() {
1388
    return this.availableValuesMethod != null;
1389
  }
1390

    
1391
  @Override
1392
  public DynMethod getCalculateMethod() {
1393
    return this.calculateMethod;
1394
  }
1395

    
1396
  @Override
1397
  public DynField setCalculateMethod(DynMethod method) {
1398
    this.calculateMethod = method;
1399
    return this;
1400
  }
1401

    
1402
  @Override
1403
  public boolean isCalculated() {
1404
    return this.calculateMethod != null;
1405
  }
1406

    
1407
  @Override
1408
  public Object getCalculatedValue(DynObject self) {
1409
    try {
1410
      return this.calculateMethod.invoke(self, new Object[]{this});
1411
    } catch (DynMethodException ex) {
1412
      throw new RuntimeException(ex);
1413
    }
1414
  }
1415

    
1416
  @Override
1417
  public DynField setValidateElements(boolean validate) {
1418
    return this;
1419
  }
1420

    
1421
  @Override
1422
  public boolean getValidateElements() {
1423
    return false;
1424
  }
1425

    
1426
  @Override
1427
  public boolean hasLabel() {
1428
    return StringUtils.isNotBlank(this.label);
1429
  }
1430

    
1431
  @Override
1432
  public boolean hasShortLabel() {
1433
    return StringUtils.isNotBlank(this.shortLabel);
1434
  }
1435

    
1436
  @Override
1437
  public boolean hasDescription() {
1438
    return StringUtils.isNotBlank(this.description);
1439
  }
1440

    
1441
  @Override
1442
  public FeatureAttributeDescriptor getValue() {
1443
    return this;
1444
  }
1445

    
1446
  @Override
1447
  public int getDisplaySize() {
1448
    return this.displaySize;
1449
  }
1450

    
1451
  private class ConstantValueEvaluator extends AbstractEvaluator {
1452

    
1453
    @Override
1454
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
1455
      return defaultValue;
1456
    }
1457

    
1458
    @Override
1459
    public String getName() {
1460
      return "Constant attribute " + name;
1461
    }
1462
  }
1463

    
1464
  public void setConstantValue(boolean isConstantValue) {
1465
    if (isConstantValue) {
1466
      /* Cuando un attributo tiene asociado un evaluador, este se interpreta
1467
             * como que no debe cargarse de la fuente de datos subyacente, siendo
1468
             * el evaluador el que se encarga de proporcionar su valor.
1469
             * Nos limitamos a asignar un evaluador que retorna simpre el valor
1470
             * por defecto para ese attributo.
1471
       */
1472
      this.evaluator = new ConstantValueEvaluator();
1473
    } else {
1474
      this.evaluator = null;
1475
    }
1476
  }
1477

    
1478
  @Override
1479
  public boolean isComputed() {
1480
    return featureAttributeEmulator != null || evaluator != null || isCalculated();
1481
  }
1482

    
1483
  @Override
1484
  public FeatureStore getStore() {
1485
    FeatureType ftype = this.getFeatureType();
1486
    if (ftype == null) {
1487
      return null;
1488
    }
1489
    return ftype.getStore();
1490
  }
1491

    
1492
  @Override
1493
  public FeatureType getFeatureType() {
1494
    if (this.typeRef == null) {
1495
      return null;
1496
    }
1497
    FeatureType ftype = (FeatureType) this.typeRef.get();
1498
//        LOGGER.info(String.format("FeatureAttributeDescriptor[%08x] get FeatureType [%08x], ref [%08x].", this.hashCode(), ftype.hashCode(), typeRef.hashCode()));
1499
    return ftype;
1500
  }
1501

    
1502
  public FeatureAttributeDescriptor setInterval(Interval interval) {
1503
    this.interval = interval;
1504
    return this;
1505
  }
1506

    
1507
  public void fixAll() {
1508
    if (!this.getDataType().supportSize()) {
1509
      this.size = 0;
1510
    }
1511
    NumberPrecisionAndScale ps = this.getDataType().fixPrecisionAndScale(this.precision, this.scale);
1512
    this.precision = ps.getPrecision();
1513
    this.scale = ps.getScale();
1514

    
1515
    switch (this.getType()) {
1516
      case DataTypes.INSTANT:
1517
      case DataTypes.INTERVAL:
1518
      case DataTypes.DATE:
1519
      case DataTypes.TIME:
1520
      case DataTypes.TIMESTAMP:
1521
        if (this.getInterval() != null) {
1522
          this.isTime = true;
1523
        }
1524
        break;
1525
    }
1526
  }
1527

    
1528
  @Override
1529
  public String[] getRequiredFieldNames() {
1530
    FeatureAttributeEmulator emulator = this.getFeatureAttributeEmulator();
1531
    if (emulator == null) {
1532
      return null;
1533
    }
1534
    return emulator.getRequiredFieldNames();
1535
  }
1536

    
1537
  @Override
1538
  public void recentUsed() {
1539
    DefaultFeatureType.RECENTS_USEDS.add(this);
1540
  }
1541

    
1542
  @Override
1543
  public boolean hasOnlyMetadataChanges(FeatureAttributeDescriptor other) {
1544
    if (other == null) {
1545
      throw new NullPointerException();
1546
    }
1547
    DefaultFeatureAttributeDescriptor old = (DefaultFeatureAttributeDescriptor) other;
1548
    if (!StringUtils.equalsIgnoreCase(old.name, this.name)) {
1549
      return false;
1550
    }
1551
    if (old.isComputed() && old.isComputed() == this.isComputed()) {
1552
      return true;
1553
    }
1554
    if (this.dataType != old.dataType) {
1555
      return false;
1556
    }
1557
    if (this.size != old.size) {
1558
      return false;
1559
    }
1560
    if (this.precision != old.precision) {
1561
      return false;
1562
    }
1563
//        if( this.primaryKey != old.primaryKey ) {
1564
//            return false;
1565
//        }
1566
    if (this.geomType != old.geomType) {
1567
      return false;
1568
    }
1569
    if (this.SRS != old.SRS) {
1570
      return false;
1571
    }
1572
    if (this.isAutomatic != old.isAutomatic) {
1573
      return false;
1574
    }
1575
    return true;
1576
  }
1577

    
1578
  private String getAll() {
1579
    StringBuilder builder = new StringBuilder();
1580
    builder.append(this.name).append("__");
1581
    builder.append(this.dataType.getName());
1582
    if( this.size>0 ) {
1583
        builder.append("__set__size=").append(this.size);
1584
    }
1585
    if( this.precision>0 ) {
1586
        builder.append("__set__precision=").append(this.precision);
1587
    }
1588
    if( this.isHidden() ) { 
1589
        builder.append("__set__hidden=true");
1590
    }
1591
    if( this.isReadOnly() ) {
1592
        builder.append("__set__readonly=true");
1593
    }
1594
    builder.append("__set__allownull=").append(this.allowNull());
1595
    if( this.isPrimaryKey()) { 
1596
        builder.append("__set__pk=true");
1597
    }
1598
    if( this.isAutomatic()) { 
1599
        builder.append("__set__automatic=true");
1600
    }
1601
    if( this.isTime()) { 
1602
        builder.append("__set__istime=true");
1603
    }
1604
    if( !StringUtils.isBlank(this.getDataProfileName()) ) {
1605
        builder.append("__set__profile=").append(this.getDataProfileName());
1606
    }
1607
    if( !StringUtils.isBlank(this.getGroup()) ) {
1608
        builder.append("__set__group=").append(this.getGroup());
1609
    }
1610
    if( !StringUtils.isBlank(this.description) ) {
1611
        builder.append("__set__description=").append(this.description);
1612
    }
1613
    if( !StringUtils.isBlank(this.label) ) {
1614
        builder.append("__set__label=").append(this.label);
1615
    }
1616
    if( !StringUtils.isBlank(this.shortLabel) ) {
1617
        builder.append("__set__shortLabel=").append(this.shortLabel);
1618
    }
1619
    if( this.locale!=null ) {
1620
        builder.append("__set__locale=").append(this.getLocale());
1621
    }
1622
    builder.append("__set__order=").append(this.getOder());
1623
    if( this.getSRS()!=null ) {
1624
        builder.append("__set__srs=").append(this.getSRS().getAbrev());
1625
    }
1626
    if( this.getGeomType()!=null ) {
1627
        builder.append("__set__geomtype=").append(this.getGeomType().getFullName().replace(":", "@"));
1628
    }
1629
    return builder.toString();
1630
  }
1631
  
1632
  @Override
1633
  public Object get(String name) {
1634
    if (StringUtils.isBlank(name)) {
1635
      throw new IllegalArgumentException("Name can't be empty");
1636
    }
1637
    switch (name.trim().toLowerCase()) {
1638
      case "all":
1639
          return this.getAll();
1640
      case "isreadonly":
1641
      case "readonly":
1642
        return this.isReadOnly();
1643
      case "hidden":
1644
        return this.isHidden();
1645
      case "allownull":
1646
        return this.allowNull();
1647
      case "pk":
1648
      case "ispk":
1649
      case "primarykey":
1650
      case "isprimarykey":
1651
        return this.isPrimaryKey();
1652
      case "isautomatic":
1653
      case "automatic":
1654
        return this.isAutomatic();
1655
      case "time":
1656
      case "istime":
1657
        return this.isTime();
1658
      case "profile":
1659
        return this.getDataProfile();
1660
      case "group":
1661
        return this.getGroup();
1662
      case "description":
1663
        return this.getDescription();
1664
      case "label":
1665
        return this.getLabel();
1666
      case "shortlabel":
1667
        return this.getShortLabel();
1668
      case "expression":
1669
        return this.getFeatureAttributeEmulator();
1670
      case "size":
1671
        return this.getSize();
1672
      case "precision":
1673
        return this.getPrecision();
1674
      case "scale":
1675
        return this.getScale();
1676
      case "roundmode":
1677
        return this.getRoundMode();
1678
      case "locale":
1679
        return this.getLocale();
1680
      case "order":
1681
        return this.getOder();
1682
      case "foreingkey":
1683
        return this.getForeingKey();
1684
      case "interval":
1685
        return this.getInterval();
1686
      case "geomtype":
1687
      case "geometrytype":
1688
        return this.getGeomType();
1689
      case "srs":
1690
        return this.getSRS();
1691
      default:
1692
        throw new IllegalArgumentException("Name attribute '" + name + "' not valid.");
1693
    }
1694
  }
1695

    
1696
}