Revision 44190 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

View differences:

DefaultFeatureAttributeDescriptor.java
31 31
import java.util.Map.Entry;
32 32
import java.util.Objects;
33 33
import org.apache.commons.lang3.ArrayUtils;
34
import org.apache.commons.lang3.StringUtils;
34 35
import org.cresques.cts.IProjection;
35
import org.gvsig.fmap.crs.CRSFactory;
36 36
import org.gvsig.fmap.dal.DataStore;
37 37
import org.gvsig.fmap.dal.DataTypes;
38
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
39 38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40 39
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
41 40
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
......
44 43
import org.gvsig.fmap.geom.Geometry;
45 44
import org.gvsig.fmap.geom.GeometryException;
46 45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryUtils;
47 47
import org.gvsig.fmap.geom.type.GeometryType;
48 48
import org.gvsig.timesupport.Interval;
49
import org.gvsig.timesupport.RelativeInterval;
49 50
import org.gvsig.timesupport.TimeSupportLocator;
50 51
import org.gvsig.tools.ToolsLocator;
51 52
import org.gvsig.tools.dataTypes.CoercionException;
......
66 67
import org.gvsig.tools.evaluator.Evaluator;
67 68
import org.gvsig.tools.evaluator.EvaluatorData;
68 69
import org.gvsig.tools.evaluator.EvaluatorException;
70
import org.gvsig.tools.persistence.PersistenceManager;
69 71
import org.gvsig.tools.persistence.Persistent;
70 72
import org.gvsig.tools.persistence.PersistentState;
71 73
import org.gvsig.tools.persistence.exception.PersistenceException;
74
import org.slf4j.Logger;
75
import org.slf4j.LoggerFactory;
72 76

  
73 77
public class DefaultFeatureAttributeDescriptor implements
74 78
        FeatureAttributeDescriptor, Persistent, DynField_v2, DynField_LabelAttribute {
75 79

  
80
    protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeDescriptor.class);
81
    
76 82
    protected boolean allowNull;
77 83
    protected DataType dataType;
78 84
    protected String dataProfile; 
......
115 121
    private DynMethod calculateMethod;
116 122
    private WeakReference typeRef;
117 123

  
124
    public DefaultFeatureAttributeDescriptor() {
125
        // Usada en la persistencia
126
    }
127

  
118 128
    protected DefaultFeatureAttributeDescriptor(FeatureType type) {
119
        if( type == null ) {
120
            this.typeRef = null;
121
        } else {
122
            this.typeRef = new WeakReference(type);
123
        }
129
        setFeatureType(type);
124 130
        this.allowNull = true;
125 131
        this.dataType = null;
126 132
        this.dateFormat = null;
......
195 201
        this.dataProfile = other.dataProfile;
196 202
    }
197 203
    
204
    public void setFeatureType(FeatureType type) {
205
        // Usada en la persistencia
206
        if( type == null ) {
207
            this.typeRef = null;
208
        } else {
209
            this.typeRef = new WeakReference(type);
210
        }
211
    }
212
    
198 213
    @Override
199 214
    public String getDataTypeName() {
200 215
        if (this.getDataType() == null) {
......
480 495
            throws PersistenceException {
481 496
        allowNull = state.getBoolean("allowNull");
482 497
        dataType = ToolsLocator.getDataTypesManager().get(state.getInt("dataType"));
483
        // FIXME how persist dateFormat ???
484
        // dateFormat;
485
        defaultValue = state.get("defaultValue");
498
        dataProfile = state.getString("dataProfile");
499
        
500
//        FIXME: dateFormat;
501
        try {
502
            defaultValue = dataType.coerce(state.get("defaultValue"));
503
        } catch (CoercionException ex) {
504
        }
486 505

  
487 506
        index = state.getInt("index");
488 507
        maximumOccurrences = state.getInt("maximumOccurrences");
......
490 509
        size = state.getInt("size");
491 510
        name = state.getString("name");
492 511
        try {
493
            objectClass = Class.forName(state.getString("objectClass"));
494
        } catch (ClassNotFoundException e) {
495
            throw new PersistenceException(e);
512
            String objectClassName = state.getString("objectClass"); 
513
            if( !StringUtils.isBlank(objectClassName) ) { 
514
                objectClass = Class.forName(objectClassName); 
515
            }
516
        } catch (Throwable e) {
517
            LOGGER.warn("Can't restore the objectClass of the FeatureAttributreDescriptor", e);
496 518
        }
497 519
        precision = state.getInt("precision");
498 520
        evaluator = (Evaluator) state.get("evaluator");
499 521
        primaryKey = state.getBoolean("primaryKey");
500 522
        readOnly = state.getBoolean("readOnly");
501
        String srsId = state.getString("srsId");
502
        if (srsId != null) {
503
            SRS = CRSFactory.getCRS(srsId);
504
        }
523
        SRS = (IProjection) state.get("SRS");
505 524
        geometryType = state.getInt("geometryType");
506 525
        geometrySubType = state.getInt("geometrySubType");
507
        additionalInfo = (Map) state.get("aditionalInfo");
526
        if( geometryType!=Geometry.TYPES.UNKNOWN && 
527
                geometrySubType!=Geometry.SUBTYPES.UNKNOWN ) {
528
            geomType = GeometryUtils.getGeometryType(
529
                    geometryType, 
530
                    geometrySubType
531
            );
532
        }
533
//        additionalInfo = (Map) state.get("aditionalInfo");
508 534
        isAutomatic = state.getBoolean("isAutomatic");
509 535
        isTime = state.getBoolean("isTime");
510 536
        if( state.hasValue("intervalStart") ) {
511
            long intervalStart = state.getLong("intervalStart");
512
            long intervalEnd = state.getLong("intervalEnd");
537
            long intervalStart = state.getLong("interval_start");
538
            long intervalEnd = state.getLong("interval_end");
513 539
            interval = TimeSupportLocator.getManager().createRelativeInterval(intervalStart, intervalEnd);
514 540
        } else {
515 541
            interval = null;
516 542
        }
543
        featureAttributeEmulator = (FeatureAttributeEmulator) state.get("featureAttributeEmulator");
544
        indexed = state.getBoolean("indexed");
545
        isIndexAscending = state.getBoolean("isIndexAscending");
546
        allowIndexDuplicateds = state.getBoolean("allowIndexDuplicateds");
547

  
548
//        FIXME: availableValues 
549

  
550
        description = state.getString("description");
551
        minValue = state.get("minValue");
552
        maxValue = state.get("maxValue");
553
        label = state.getString("label");
554
        order = state.getInt("order");
555
        hidden = state.getBoolean("hidden");
556
        groupName = state.getString("groupName");
517 557
    }
518 558

  
519 559
    @Override
520 560
    public void saveToState(PersistentState state) throws PersistenceException {
521 561
        state.set("allowNull", allowNull);
522
        state.set("dataType", dataType);
523
        // FIXME how persist dateFormat ???
524
        // dateFormat;
562
        state.set("dataType", dataType.getType());
563
        state.set("dataProfile", dataProfile);
564
        
565
//        FIXME: dateFormat;
525 566

  
526
        defaultValue = state.get("defaultValue");
567
        state.set("defaultValue", Objects.toString(defaultValue));
527 568

  
528
        index = state.getInt("index");
529
        maximumOccurrences = state.getInt("maximumOccurrences");
530
        minimumOccurrences = state.getInt("minimumOccurrences");
531
        size = state.getInt("size");
532
        name = state.getString("name");
533
        try {
534
            objectClass = Class.forName(state.getString("objectClass"));
535
        } catch (ClassNotFoundException e) {
536
            throw new PersistenceException(e);
569
        state.set("index", index);
570
        state.set("maximumOccurrences", maximumOccurrences);
571
        state.set("minimumOccurrences", minimumOccurrences);
572
        state.set("size", size);
573
        state.set("name", name);
574
        state.set("objectClass", objectClass==null? null:objectClass.getName());
575
        state.set("precision", precision);
576
        state.set("evaluator", evaluator);
577
        
578
        state.set("primaryKey", primaryKey);
579
        state.set("readOnly", readOnly);
580
        state.set("SRS", SRS);
581
        GeometryType theGeomType = this.getGeomType();
582
        if( theGeomType==null ) {
583
            state.set("geometryType", Geometry.TYPES.UNKNOWN);
584
            state.set("geometrySubType", Geometry.SUBTYPES.UNKNOWN);
585
        } else {
586
            state.set("geometryType", theGeomType.getType());
587
            state.set("geometrySubType", theGeomType.getSubType());
537 588
        }
538
        precision = state.getInt("precision");
539
        evaluator = (Evaluator) state.get("evaluator");
540
        primaryKey = state.getBoolean("primaryKey");
541
        readOnly = state.getBoolean("readOnly");
542
        String srsId = state.getString("srsId");
543
        if (srsId != null) {
544
            SRS = CRSFactory.getCRS(srsId);
589

  
590
//      FIXME: additionalInfo
591

  
592
        state.set("isAutomatic", isAutomatic);
593
        state.set("isTime", isTime);
594
        if( this.interval==null ) {
595
            state.setNull("interval_start");
596
            state.setNull("interval_end");
597
        } else {
598
            state.set("interval_start", ((RelativeInterval)interval).getStart().toMillis());
599
            state.set("interval_end", ((RelativeInterval)interval).getEnd().toMillis());
545 600
        }
546
        geometryType = state.getInt("geometryType");
547
        geometrySubType = state.getInt("geometrySubType");
548
        additionalInfo = (Map) state.get("aditionalInfo");
549
        isAutomatic = state.getBoolean("isAutomatic");
601
        state.set("SRS", SRS);
602

  
603
//      FIXME: featureAttributeGetter
604

  
605
        if( featureAttributeEmulator instanceof Persistent ) {
606
            state.set("featureAttributeEmulator", featureAttributeEmulator);
607
        } else {
608
            state.setNull("featureAttributeEmulator");
609
        }
610

  
611
        state.set("indexed", indexed);
612
        state.set("isIndexAscending", isIndexAscending);
613
        state.set("allowIndexDuplicateds", allowIndexDuplicateds);
614
        
615
//        FIXME: availableValues 
616

  
617
        state.set("description", description);
618
        state.set("minValue", minValue);
619
        state.set("maxValue", maxValue);
620
        state.set("label", label);
621
        state.set("order", order);
622
        state.set("hidden", hidden);
623
        state.set("groupName", groupName);
624

  
550 625
    }
626
    
627
    private static final String FEATATTRDESC_PERSISTENCE_DEFINITION_NAME = "FeatureAttributeDescriptor";
551 628

  
629
    public static void registerPersistenceDefinition() {
630
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
631
        
632

  
633
        if (manager.getDefinition(FEATATTRDESC_PERSISTENCE_DEFINITION_NAME)
634
                == null) {
635
            DynStruct definition = manager.addDefinition(DefaultFeatureAttributeDescriptor.class,
636
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME,
637
                    FEATATTRDESC_PERSISTENCE_DEFINITION_NAME
638
                        + " persistent definition",
639
                    null,
640
                    null
641
            );
642
            definition.addDynFieldBoolean("allowNull");
643
            definition.addDynFieldInt("dataType");
644
            definition.addDynFieldString("dataProfile");
645
//            definition.addDynFieldString("dateFormat");
646
            definition.addDynFieldString("defaultValue");
647
            definition.addDynFieldInt("index");
648
            definition.addDynFieldInt("maximumOccurrences");
649
            definition.addDynFieldInt("minimumOccurrences");
650
            definition.addDynFieldInt("size");
651
            definition.addDynFieldString("name");
652
            definition.addDynFieldString("objectClass");
653
            definition.addDynFieldInt("precision");
654
            definition.addDynFieldObject("evaluator")
655
                    .setClassOfValue(Evaluator.class);
656
            definition.addDynFieldBoolean("primaryKey");
657
            definition.addDynFieldBoolean("readOnly");
658
            definition.addDynFieldObject("SRS")
659
                    .setClassOfValue(IProjection.class);
660
            definition.addDynFieldInt("geometryType");
661
            definition.addDynFieldInt("geometrySubType");
662
//            definition.addDynFieldMap("additionalInfo");
663
            definition.addDynFieldBoolean("isAutomatic");
664
            definition.addDynFieldBoolean("isTime");
665
            definition.addDynFieldLong("interval_start");
666
            definition.addDynFieldLong("interval_end");
667
            definition.addDynFieldObject("featureAttributeEmulator")
668
                    .setClassOfValue(FeatureAttributeEmulator.class);
669
            definition.addDynFieldBoolean("indexed");
670
            definition.addDynFieldBoolean("isIndexAscending");
671
            definition.addDynFieldBoolean("allowIndexDuplicateds");
672
//            definition.addDynFieldInt("availableValues");
673
            definition.addDynFieldInt("description");
674
            definition.addDynFieldInt("minValue");
675
            definition.addDynFieldInt("maxValue");
676
            definition.addDynFieldInt("label");
677
            definition.addDynFieldInt("order");
678
            definition.addDynFieldBoolean("hidden");
679
            definition.addDynFieldInt("groupName");
680
        }
681
    }
682

  
683
    
552 684
    /*
553 685
     * Start of DynField interface Implementation
554 686
     *
......
1033 1165

  
1034 1166
    @Override
1035 1167
    public boolean isComputed() {
1036
        return featureAttributeEmulator!=null || evaluator!=null;
1168
        return featureAttributeEmulator!=null || evaluator!=null || isCalculated();
1037 1169
    }
1038 1170

  
1039 1171
    @Override

Also available in: Unified diff