Revision 44262 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
33 33
import org.apache.commons.lang3.ArrayUtils;
34 34
import org.apache.commons.lang3.StringUtils;
35 35
import org.cresques.cts.IProjection;
36
import org.gvsig.expressionevaluator.Expression;
37
import org.gvsig.expressionevaluator.ExpressionUtils;
38
import org.gvsig.expressionevaluator.SymbolTable;
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_CODE;
42
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_LABEL;
43
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_TABLE;
44 36
import org.gvsig.fmap.dal.DataStore;
45 37
import org.gvsig.fmap.dal.DataTypes;
46
import org.gvsig.fmap.dal.StoresRepository;
47
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
48
import org.gvsig.fmap.dal.feature.Feature;
49 38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
50 39
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
51 40
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
52 41
import org.gvsig.fmap.dal.feature.FeatureStore;
53 42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.ForeingKey;
54 44
import org.gvsig.fmap.geom.Geometry;
55 45
import org.gvsig.fmap.geom.GeometryException;
56 46
import org.gvsig.fmap.geom.GeometryLocator;
......
121 111
    protected boolean allowIndexDuplicateds = true;
122 112

  
123 113
    protected DynObjectValueItem[] availableValues;
114
    protected DynObjectValueItem[] availableValuesFromForeingKey;
124 115
    protected String description;
125 116
    protected Object minValue;
126 117
    protected Object maxValue;
......
132 123
    private DynMethod availableValuesMethod;
133 124
    private DynMethod calculateMethod;
134 125
    private WeakReference typeRef;
126
    protected DefaultForeingKey foreingKey = null;
135 127
    
136 128
    private int relationType = RELATION_TYPE_NONE;
137 129

  
......
234 226
            } catch (Exception ex) {
235 227
            }
236 228
        }
229
        this.foreingKey = null;
230
        if( other.foreingKey!=null ) {
231
            try {
232
                this.foreingKey = (DefaultForeingKey) other.foreingKey.clone();
233
            } catch (CloneNotSupportedException ex) {
234
            }
235
        }
236
        if( this.foreingKey!=null ) {
237
            this.foreingKey.setDescriptor(this);
238
        }
239
        
237 240
        // TODO: ? Habria que clonarlos ?
238 241
        this.availableValuesMethod = other.availableValuesMethod;
239 242
        this.calculateMethod = other.calculateMethod;
......
605 608
        hidden = state.getBoolean("hidden");
606 609
        groupName = state.getString("groupName");
607 610
        relationType = state.getInt("relationType");
611
        
612
        foreingKey = (DefaultForeingKey) state.get("foreingKey");
613
        if( foreingKey!=null ) {
614
            this.foreingKey.setDescriptor(this);
615
        }
616
        tags = (Tags) state.get("tags");
617
        if( tags == null ) {
618
            this.tags = new DefaultTags();
619
        }
608 620
    }
609 621

  
610 622
    @Override
......
680 692
        state.set("hidden", hidden);
681 693
        state.set("groupName", groupName);
682 694
        state.set("relationType",relationType);
695
        
696
        state.set("foreingKey" ,this.foreingKey);
697
        state.set("tags" ,this.tags);
683 698

  
684 699
    }
685 700
    
......
737 752
            definition.addDynFieldBoolean("hidden");
738 753
            definition.addDynFieldString("groupName");
739 754
            definition.addDynFieldInt("relationType");
755
            
756
            definition.addDynFieldObject("foreingKey")
757
                    .setClassOfValue(DefaultForeingKey.class);
758
            
759
            definition.addDynFieldObject("tags")
760
                    .setClassOfValue(Tags.class);
761
            
740 762
        }
741 763
    }
742 764

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

  
1116 1100
    @Override
1117 1101
    public boolean allowIndexDuplicateds() {
......
1328 1312
        return emulator.getRequiredFieldNames();
1329 1313
    }
1330 1314

  
1315
    @Override
1316
    public void recentUsed() {
1317
        DefaultFeatureType.RECENTS_USEDS.add(this);
1318
    }
1319

  
1331 1320
}

Also available in: Unified diff