Revision 45739 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
27 27
import java.math.MathContext;
28 28
import java.math.RoundingMode;
29 29
import java.text.DateFormat;
30
import java.util.ArrayList;
30 31
import java.util.HashMap;
31 32
import java.util.LinkedHashMap;
32 33
import java.util.List;
......
42 43
import org.cresques.cts.IProjection;
43 44
import org.gvsig.expressionevaluator.Expression;
44 45
import org.gvsig.expressionevaluator.ExpressionUtils;
46
import org.gvsig.expressionevaluator.MutableSymbolTable;
45 47
import org.gvsig.fmap.dal.DALLocator;
46 48
import org.gvsig.fmap.dal.DataStore;
47 49
import org.gvsig.fmap.dal.DataTypes;
......
54 56
import org.gvsig.fmap.dal.feature.FeatureStore;
55 57
import org.gvsig.fmap.dal.feature.FeatureType;
56 58
import org.gvsig.fmap.dal.feature.ForeingKey;
59
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey;
57 60
import org.gvsig.fmap.geom.Geometry;
58 61
import org.gvsig.fmap.geom.GeometryCoercionContext;
59 62
import org.gvsig.fmap.geom.GeometryException;
......
74 77
import org.gvsig.tools.dataTypes.DataType;
75 78
import org.gvsig.tools.dataTypes.DataType.NumberPrecisionAndScale;
76 79
import org.gvsig.tools.dataTypes.DataTypeUtils;
80
import org.gvsig.tools.dynobject.AbstractDynMethod;
77 81
import org.gvsig.tools.dynobject.DynField;
78 82
import org.gvsig.tools.dynobject.DynField_LabelAttribute;
79 83
import org.gvsig.tools.dynobject.DynField_v2;
......
325 329

  
326 330
    @Override
327 331
    public Locale getLocale() {
328
    if( this.locale == null ) {
329
      if( this.dataType.isNumeric() ) {
332
        if (this.locale == null) {
333
            if (this.dataType.isNumeric()) {
330 334
                this.locale = Locale.ENGLISH;
331 335
            } else {
332 336
                this.locale = Locale.getDefault();
......
391 395
        return this;
392 396
    }
393 397

  
394
  
395

  
396 398
    @Override
397 399
    public Evaluator getEvaluator() {
398 400
        return this.evaluator;
......
513 515
                        this.getScale(),
514 516
                        this.getRoundMode()
515 517
                );
516
      } else if( this.getType() == DataTypes.GEOMETRY ) {
518
            } else if (this.getType() == DataTypes.GEOMETRY) {
517 519
                GeometryCoercionContext context = GeometryLocator.getGeometryManager().createGeometryCoercionContext();
518 520
                context.setGeometryType(this.getGeomType());
519 521
                context.setMode(GeometryCoercionContext.MODE_ONERROR_DONTCONVERT);
......
762 764
        if (tags == null) {
763 765
            this.tags = new DefaultTags();
764 766
        }
765
    displaySize = state.getInt("displaySize",0);
767
        displaySize = state.getInt("displaySize", 0);
766 768
        availableValuesExpression = (Expression) state.get("availableValuesExpression");
767
    avoidCachingAvailableValues = state.getBoolean("avoidCachingAvailableValues",false);
769
        avoidCachingAvailableValues = state.getBoolean("avoidCachingAvailableValues", false);
768 770
        availableValuesCache = null;
769 771

  
770 772
    }
......
789 791
        state.set("precision", precision);
790 792
        state.set("scale", scale);
791 793
        state.set("roundMode", roundMode);
792
    if( this.locale == null ) {
794
        if (this.locale == null) {
793 795
            state.setNull("locale");
794 796
        } else {
795 797
            state.set("locale", this.locale.toLanguageTag()); //toString.coerce(this.locale));
......
937 939
        return tags;
938 940
    }
939 941

  
942
    private Expression availableValuesFilter;
943

  
940 944
    @Override
945
    public Expression getAvailableValuesFilter() {
946
        return this.availableValuesFilter;
947
    }
948

  
949
    public FeatureAttributeDescriptor setAvailableValuesFilter(Expression filter) {
950
        this.availableValuesFilter = filter;
951
        return this;
952
    }
953

  
954
    @Override
941 955
    public boolean hasConstantAvailableValues() {
942 956
        return this.availableValues != null;
943 957
    }
......
948 962
    }
949 963

  
950 964
    public boolean hasAvailableValues() {
951
      return getAvailableValues()!=null;
965
        return getAvailableValues() != null;
952 966
    }
953 967

  
954 968
    @Override
969
    public DynObjectValueItem[] getAvailableValues(DynObject context) {
970
        if (this.availableValuesMethod != null) {
971
            DynObjectValueItem[] values;
972
            try {
973
                values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(context, new Object[]{this});
974
            } catch (DynMethodException ex) {
975
                return this.getAvailableValues();
976
            }
977
            if (values != null) {
978
                return values;
979
            }
980
        }
981
        Expression filter = this.availableValuesFilter;
982
        if (!ExpressionUtils.isEmpty(filter)) {
983
            if (this.isForeingKey() && this.foreingKey.isClosedList()) {
984
                ContextForeingKey foreingkeyContext = this.foreingKey.createContext();
985
                foreingkeyContext.setContextValues(context);
986
                DynObjectValueItem[] values = this.foreingKey.getAvailableValues(foreingkeyContext);
987
                return values;
988
            }
989
            MutableSymbolTable contextSymbolTable = ExpressionUtils.createSymbolTable("feature", context);
990
            MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable();
991
            symbolTable.addSymbolTable(contextSymbolTable);
992

  
993
            DynObjectValueItem[] allValues = this.getAvailableValues();
994
            List<DynObjectValueItem> filteredValues = new ArrayList<>();
995
            for (DynObjectValueItem value : allValues) {
996
                symbolTable.setVar("$value", value.getValue());
997
                symbolTable.setVar("$label", value.getLabel());
998
                Object include = filter.execute(symbolTable);
999
                if (DataTypeUtils.isFalse(include, false)) {
1000
                    continue;
1001
                }
1002
                filteredValues.add(value);
1003
            }
1004
            DynObjectValueItem[] values = filteredValues.toArray(
1005
                    new DynObjectValueItem[filteredValues.size()]
1006
            );
1007
            return values;
1008
        }
1009
        return this.getAvailableValues();
1010
    }
1011

  
1012
    @Override
1013
    public boolean isAvailableValuesCalculated() {
1014
        return true; //this.availableValuesMethod != null;
1015
    }
1016

  
1017
    @Override
955 1018
    public DynObjectValueItem[] getAvailableValues() {
956 1019
        DynObjectValueItem[] values = this.availableValues;
957 1020

  
......
965 1028
        if (this.isForeingKey() && this.foreingKey.isClosedList()) {
966 1029
            values = this.foreingKey.getAvailableValues(null);
967 1030

  
968
    } else if( this.availableValuesExpression!=null ) {
1031
        } else if (this.availableValuesExpression != null) {
969 1032
            values = this.getAvailableValuesFromExpression();
970 1033
        }
971
    if( !this.avoidCachingAvailableValues ) {
1034
        if (!this.avoidCachingAvailableValues) {
972 1035
            this.availableValuesCache = values;
973 1036
        }
974 1037
        return values;
......
1035 1098
                r[i] = new DynObjectValueItem(theValue, theLabel);
1036 1099
            }
1037 1100
        } else if (firstelement instanceof Map) {
1038
            Map<String,Object> v = (Map<String,Object>) firstelement;
1101
            Map<String, Object> v = (Map<String, Object>) firstelement;
1039 1102
            String labelname = null;
1040 1103
            for (String theName : new String[]{
1041 1104
                "name", "label", "key", "description"
......
1055 1118
            }
1056 1119
            r = new DynObjectValueItem[values.size()];
1057 1120
            for (int i = 0; i < values.size(); i++) {
1058
                v = (Map<String,Object>) values.get(i);
1121
                v = (Map<String, Object>) values.get(i);
1059 1122
                String theLabel;
1060 1123
                Object theValue;
1061 1124
                if (labelname == null) {
......
1075 1138
            FeatureType featureType = v.getType();
1076 1139
            String valuename = null;
1077 1140
            FeatureAttributeDescriptor[] pks = featureType.getPrimaryKey();
1078
            if( pks!=null && pks.length == 1) {
1141
            if (pks != null && pks.length == 1) {
1079 1142
                valuename = pks[0].getName();
1080 1143
            }
1081 1144
            String labelname = null;
1082 1145
            for (String theName : new String[]{
1083 1146
                "name", "label", "key", "description"
1084 1147
            }) {
1085
                if (featureType.get(theName)!=null ) {
1148
                if (featureType.get(theName) != null) {
1086 1149
                    labelname = theName;
1087 1150
                    break;
1088 1151
                }
......
1097 1160
                } else {
1098 1161
                    theLabel = v.getString(labelname);
1099 1162
                }
1100
                if( valuename == null ) {
1163
                if (valuename == null) {
1101 1164
                    theValue = v.getReference().getCode();
1102 1165
                } else {
1103 1166
                    theValue = v.get(valuename);
......
1108 1171
        return r;
1109 1172
    }
1110 1173

  
1111

  
1112 1174
    private DynObjectValueItem[] getAvailableValuesFromExpression() {
1113
      if( this.availableValuesExpression == null || this.availableValuesExpression.isEmpty() ) {
1175
        if (this.availableValuesExpression == null || this.availableValuesExpression.isEmpty()) {
1114 1176
            return null;
1115 1177
        }
1116 1178
        try {
1117 1179
            Object value = this.availableValuesExpression.execute(null);
1118
        if( value instanceof DynObjectValueItem[] ) {
1180
            if (value instanceof DynObjectValueItem[]) {
1119 1181
                return (DynObjectValueItem[]) value;
1120 1182
            }
1121
        if( value instanceof List ) {
1183
            if (value instanceof List) {
1122 1184
                return this.getAvailableValuesFrom(new GetItemWithSize() {
1123 1185
                    @Override
1124 1186
                    public Object get(int i) {
1125
                    return ((List)value).get(i);
1187
                        return ((List) value).get(i);
1126 1188
                    }
1127 1189

  
1128 1190
                    @Override
1129 1191
                    public int size() {
1130
                    return ((List)value).size();
1192
                        return ((List) value).size();
1131 1193
                    }
1132 1194
                });
1133 1195
            }
1134
      } catch(Throwable th) {
1196
        } catch (Throwable th) {
1135 1197
            LOGGER.warn("Can't get available values from expression", th);
1136 1198
        }
1137 1199
        return null;
......
1615 1677
    }
1616 1678

  
1617 1679
    @Override
1618
    public DynObjectValueItem[] getAvailableValues(DynObject self) {
1680
    public DynMethod getAvailableValuesMethod() {
1619 1681
        if (this.availableValuesMethod != null) {
1620
            DynObjectValueItem[] values;
1621
            try {
1622
                values = (DynObjectValueItem[]) this.availableValuesMethod.invoke(self, new Object[]{this});
1623
            } catch (DynMethodException ex) {
1624
                return this.availableValues;
1682
            return this.availableValuesMethod;
1683
        }
1684
        if (this.availableValuesFilter == null && !this.isForeingKey()) {
1685
            return null;
1686
        }
1687
        DynMethod method = new AbstractDynMethod("getAvailableValuesOf" + this.getName()) {
1688
            @Override
1689
            public Object invoke(DynObject context, Object[] args) throws DynMethodException {
1690
                return getAvailableValues(context);
1625 1691
            }
1626
            if (values != null) {
1627
                return values;
1628
            }
1629
        }
1630
        return this.availableValues;
1692
        };
1693
        return method;
1631 1694
    }
1632 1695

  
1633 1696
    @Override
1634
    public DynMethod getAvailableValuesMethod() {
1635
        return this.availableValuesMethod;
1636
    }
1637

  
1638
    @Override
1639
    public boolean isAvailableValuesCalculated() {
1640
        return this.availableValuesMethod != null;
1641
    }
1642

  
1643
    @Override
1644 1697
    public DynMethod getCalculateMethod() {
1645 1698
        return this.calculateMethod;
1646 1699
    }
......
1702 1755

  
1703 1756
    @Override
1704 1757
    public boolean isInAvailableValues(Object valueToCheck) {
1705
        if (this.getAvailableValues()!=null) {
1758
        if (this.getAvailableValues() != null) {
1706 1759
            for (DynObjectValueItem availableValue : this.getAvailableValues()) {
1707 1760
                if (Objects.equals(valueToCheck, availableValue.getValue())) {
1708 1761
                    return true;
......
1843 1896

  
1844 1897
        private String name;
1845 1898
        private DataType type;
1846
        private Map<String,String> sets;
1899
        private Map<String, String> sets;
1847 1900
        private String sep;
1848 1901

  
1849 1902
        public PropertiesBuilder() {
......
1863 1916
        }
1864 1917

  
1865 1918
        public void set(String name, ForeingKey fk) {
1866
          if( fk == null ) {
1919
            if (fk == null) {
1867 1920
                return;
1868 1921
            }
1869
          if( !fk.isForeingKey() ) {
1922
            if (!fk.isForeingKey()) {
1870 1923
                return;
1871 1924
            }
1872 1925
            this.set(name, fk.isForeingKey());
1873
          this.set(name+"_code", fk.getCodeName());
1874
          this.set(name+"_label", fk.getLabelFormula());
1875
          this.set(name+"_closedlist", fk.isClosedList());
1876
          this.set(name+"_table", fk.getTableName());
1926
            this.set(name + "_code", fk.getCodeName());
1927
            this.set(name + "_label", fk.getLabelFormula());
1928
            this.set(name + "_closedlist", fk.isClosedList());
1929
            this.set(name + "_table", fk.getTableName());
1877 1930
        }
1878 1931

  
1879 1932
        public void set(String name, FeatureAttributeEmulator value) {
1880
          if( value == null ) {
1933
            if (value == null) {
1881 1934
                return;
1882 1935
            }
1883
          if( value instanceof FeatureAttributeEmulatorExpression ) {
1884
            this.set(name, ((FeatureAttributeEmulatorExpression)value).getExpression().getPhrase());
1936
            if (value instanceof FeatureAttributeEmulatorExpression) {
1937
                this.set(name, ((FeatureAttributeEmulatorExpression) value).getExpression().getPhrase());
1885 1938
            }
1886 1939
        }
1887 1940

  
1888 1941
        public void set(String name, IProjection value) {
1889
          if( value == null ) {
1942
            if (value == null) {
1890 1943
                return;
1891 1944
            }
1892 1945
            this.set(name, value.getAbrev());
1893 1946
        }
1894 1947

  
1895 1948
        public void set(String name, GeometryType value) {
1896
          if( value == null ) {
1949
            if (value == null) {
1897 1950
                return;
1898 1951
            }
1899 1952
            this.set(name, value.getFullName().replace(":", "@"));
1900 1953
        }
1901 1954

  
1902 1955
        public void set(String name, Object value) {
1903
          if( value == null ) {
1956
            if (value == null) {
1904 1957
                return;
1905 1958
            }
1906 1959
            String s = Objects.toString(value, "");
1907
          if( StringUtils.isBlank(s) ) {
1960
            if (StringUtils.isBlank(s)) {
1908 1961
                return;
1909 1962
            }
1910 1963
            this.sets.put(name, s);
1911 1964
        }
1912 1965

  
1913 1966
        public void set(String name, Object value, Object skipValue) {
1914
          if( value == null || value == skipValue) {
1967
            if (value == null || value == skipValue) {
1915 1968
                return;
1916 1969
            }
1917 1970
            String s = Objects.toString(value, "");
1918
          if( StringUtils.isBlank(s) ) {
1971
            if (StringUtils.isBlank(s)) {
1919 1972
                return;
1920 1973
            }
1921 1974
            this.sets.put(name, s);
......
1945 1998
        builder.name(this.name);
1946 1999
        builder.type(this.dataType);
1947 2000
        builder.set("size", this.size, 0);
1948
    switch(this.getType()) {
2001
        switch (this.getType()) {
1949 2002
            case DataTypes.BYTE:
1950 2003
            case DataTypes.INTEGER:
1951 2004
            case DataTypes.LONG:
......
1967 2020
                break;
1968 2021
            case DataTypes.GEOMETRY:
1969 2022
                IProjection proj = this.getSRS();
1970
            if( proj!=null ) {
2023
                if (proj != null) {
1971 2024
                    builder.set("srs", proj.getAbrev().replace(":", "@"));
1972 2025
                }
1973 2026
                GeometryType theGeomType = this.getGeomType();
1974
            if( theGeomType!=null ) {
2027
                if (theGeomType != null) {
1975 2028
                    String geomTypeName = GeometryUtils.getGeometryTypeName(this.getGeomType().getType());
1976 2029
                    String geomSubtypeName = GeometryUtils.getGeometrySubtypeName(this.getGeomType().getSubType());
1977
                builder.set("geomtype", geomTypeName+"@"+geomSubtypeName);
2030
                    builder.set("geomtype", geomTypeName + "@" + geomSubtypeName);
1978 2031
                }
1979 2032
                break;
1980 2033
        }
......
1990 2043
        builder.set("label", this.label);
1991 2044
        builder.set("shortLabel", this.shortLabel);
1992 2045
        builder.set("order", this.getOder());
1993
    if( this.getFeatureAttributeEmulator() instanceof FeatureAttributeEmulatorExpression ) {
1994
        Expression exp = ((FeatureAttributeEmulatorExpression)this.getFeatureAttributeEmulator()).getExpression();
1995
        if( exp!=null ) {
2046
        if (this.getFeatureAttributeEmulator() instanceof FeatureAttributeEmulatorExpression) {
2047
            Expression exp = ((FeatureAttributeEmulatorExpression) this.getFeatureAttributeEmulator()).getExpression();
2048
            if (exp != null) {
1996 2049
                builder.set("expression", exp.getPhrase());
1997 2050
            }
1998 2051
        }
1999 2052
        builder.set("isAvoidCachingAvailableValues", this.isAvoidCachingAvailableValues(), false);
2000
    if( this.isForeingKey() ) {
2053
        if (this.isForeingKey()) {
2001 2054
            builder.set("fk", this.isForeingKey());
2002 2055
            builder.set("fk_table", this.getForeingKey().getTableName());
2003 2056
            builder.set("fk_code", this.getForeingKey().getCodeName());
......
2131 2184
        builder.add("hidden", this.hidden);
2132 2185
        builder.add("avoidCachingAvailableValues", this.avoidCachingAvailableValues);
2133 2186

  
2134
        builder.add("geometryType",this.getGeomType());
2187
        builder.add("geometryType", this.getGeomType());
2135 2188
        builder.add("srs", this.getSRS());
2136 2189

  
2137 2190
        builder.add("relationType", this.relationType);
2138 2191
        builder.add("displaySize", this.displaySize);
2139 2192
        builder.add("locale", this.getLocale());
2140 2193
        builder.add("expression", this.getFeatureAttributeEmulator());
2141
        if( this.isForeingKey() ) {
2194
        if (this.isForeingKey()) {
2142 2195
            builder.add("fk", this.isForeingKey());
2143 2196
            builder.add("fk_table", this.getForeingKey().getTableName());
2144 2197
            builder.add("fk_code", this.getForeingKey().getCodeName());
......
2146 2199
            builder.add("fk_closedlist", this.getForeingKey().isClosedList());
2147 2200
        }
2148 2201
        builder.add("availableValuesExpression", this.availableValuesExpression);
2149
        builder.add("defaultValue", Objects.toString(this.defaultValue,""));
2202
        builder.add("defaultValue", Objects.toString(this.defaultValue, ""));
2150 2203
        builder.add("dataProfile", this.getDataProfileName());
2151 2204
        builder.add("tags", tags);
2152 2205
        builder.add("availableValues", availableValues);
......
2180 2233
        this.relationType = json.getInt("relationType");
2181 2234
        this.displaySize = json.getInt("displaySize");
2182 2235

  
2183
        this.dataType = (DataType) Json.toObject(json,"dataType");
2236
        this.dataType = (DataType) Json.toObject(json, "dataType");
2184 2237
        this.geomType = (GeometryType) Json.toObject(json, "geometryType");
2185 2238
        this.SRS = (IProjection) Json.toObject(json, "srs");
2186 2239
        this.locale = (Locale) Json.toObject(json, "locale");
2187
        this.featureAttributeEmulator = (FeatureAttributeEmulator) Json.toObject(json,"expression");
2240
        this.featureAttributeEmulator = (FeatureAttributeEmulator) Json.toObject(json, "expression");
2188 2241

  
2189 2242
        this.tags = (Tags) Json.toObject(json, "tags");
2190 2243
        this.additionalInfo = Json.toMap(json, "additionalInfo");
......
2192 2245
        this.dataProfile = json.getString("dataProfile", null);
2193 2246
        try {
2194 2247
            this.defaultValue = this.coerce(json.getString("defaultValue", null));
2195
        } catch(Exception ex) {
2196
            LOGGER.warn("Can't retrive default value for attribute '"+this.name+"'.",ex);
2248
        } catch (Exception ex) {
2249
            LOGGER.warn("Can't retrive default value for attribute '" + this.name + "'.", ex);
2197 2250
        }
2198
        this.availableValuesExpression = (Expression) Json.toObject(json,"availableValuesExpression");
2199
        if( json.getBoolean("fk", false) ) {
2251
        this.availableValuesExpression = (Expression) Json.toObject(json, "availableValuesExpression");
2252
        if (json.getBoolean("fk", false)) {
2200 2253
            this.foreingKey = new DefaultForeingKey();
2201 2254
            this.foreingKey.setForeingKey(true);
2202 2255
            this.foreingKey.setTableName(json.getString("fk_table", null));
......
2227 2280

  
2228 2281
        @Override
2229 2282
        public JsonObjectBuilder toJsonBuilder(Object value) {
2230
            return ((SupportToJson)value).toJsonBuilder();
2283
            return ((SupportToJson) value).toJsonBuilder();
2231 2284
        }
2232 2285

  
2233 2286
    }

Also available in: Unified diff