35 |
35 |
import java.util.Map;
|
36 |
36 |
import java.util.Map.Entry;
|
37 |
37 |
import java.util.Objects;
|
|
38 |
import javax.json.JsonObject;
|
38 |
39 |
import org.apache.commons.lang3.ArrayUtils;
|
39 |
40 |
import org.apache.commons.lang3.StringUtils;
|
|
41 |
import org.apache.commons.lang3.tuple.Pair;
|
40 |
42 |
import org.cresques.cts.IProjection;
|
|
43 |
import org.gvsig.expressionevaluator.Expression;
|
41 |
44 |
import org.gvsig.expressionevaluator.ExpressionUtils;
|
42 |
45 |
import org.gvsig.fmap.dal.DALLocator;
|
43 |
46 |
import org.gvsig.fmap.dal.DataStore;
|
44 |
47 |
import org.gvsig.fmap.dal.DataTypes;
|
45 |
48 |
import org.gvsig.fmap.dal.feature.DataProfile;
|
|
49 |
import org.gvsig.fmap.dal.feature.Feature;
|
46 |
50 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
|
47 |
51 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
|
48 |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeGetter;
|
... | ... | |
87 |
91 |
import org.slf4j.LoggerFactory;
|
88 |
92 |
import org.gvsig.tools.dataTypes.CoercionContext;
|
89 |
93 |
import org.gvsig.tools.dataTypes.DataType.NumberPrecisionAndScale;
|
|
94 |
import org.gvsig.tools.util.GetItemWithSize;
|
|
95 |
import org.gvsig.tools.util.LabeledValue;
|
90 |
96 |
|
91 |
97 |
@SuppressWarnings("UseSpecificCatch")
|
92 |
98 |
public class DefaultFeatureAttributeDescriptor implements
|
... | ... | |
126 |
132 |
protected boolean allowIndexDuplicateds = true;
|
127 |
133 |
|
128 |
134 |
protected DynObjectValueItem[] availableValues;
|
|
135 |
protected DynObjectValueItem[] availableValuesCache; // No persistente
|
|
136 |
protected Expression availableValuesExpression;
|
129 |
137 |
private Map<Object, String> labelOfValueMap; // No persistente
|
130 |
138 |
protected String description;
|
131 |
139 |
protected Object minValue;
|
... | ... | |
900 |
908 |
|
901 |
909 |
@Override
|
902 |
910 |
public DynObjectValueItem[] getAvailableValues() {
|
903 |
|
if (this.availableValues == null) {
|
904 |
|
if (this.isForeingKey() && this.foreingKey.isClosedList()) {
|
905 |
|
return this.foreingKey.getAvailableValues(null);
|
906 |
|
}
|
|
911 |
if (this.availableValues != null) {
|
|
912 |
return this.availableValues;
|
907 |
913 |
}
|
908 |
|
return this.availableValues;
|
|
914 |
if (this.availableValuesCache != null) {
|
|
915 |
return this.availableValuesCache;
|
|
916 |
}
|
|
917 |
if (this.isForeingKey() && this.foreingKey.isClosedList()) {
|
|
918 |
this.availableValuesCache = this.foreingKey.getAvailableValues(null);
|
|
919 |
|
|
920 |
} else if( this.availableValuesExpression!=null ) {
|
|
921 |
this.availableValuesCache = this.getAvailableValuesFromExpression();
|
|
922 |
}
|
|
923 |
return this.availableValuesCache;
|
909 |
924 |
}
|
|
925 |
|
|
926 |
private DynObjectValueItem[] getAvailableValuesFrom(GetItemWithSize values) {
|
|
927 |
if (values.size() == 0) {
|
|
928 |
return null;
|
|
929 |
}
|
|
930 |
DynObjectValueItem[] r = null;
|
|
931 |
Object firstelement = values.get(0);
|
|
932 |
if (firstelement instanceof LabeledValue) {
|
|
933 |
r = new DynObjectValueItem[values.size()];
|
|
934 |
for (int i = 0; i < values.size(); i++) {
|
|
935 |
LabeledValue v = (LabeledValue) values.get(i);
|
|
936 |
r[i] = new DynObjectValueItem(
|
|
937 |
v.getValue(),
|
|
938 |
Objects.toString(v.getLabel(), Objects.toString(v.getValue(), String.valueOf(i)))
|
|
939 |
);
|
|
940 |
}
|
|
941 |
} else if (firstelement instanceof Pair) {
|
|
942 |
r = new DynObjectValueItem[values.size()];
|
|
943 |
for (int i = 0; i < values.size(); i++) {
|
|
944 |
Pair v = (Pair) values.get(i);
|
|
945 |
r[i] = new DynObjectValueItem(
|
|
946 |
v.getValue(),
|
|
947 |
Objects.toString(v.getKey(), Objects.toString(v.getValue(), String.valueOf(i)))
|
|
948 |
);
|
|
949 |
}
|
|
950 |
} else if (firstelement instanceof JsonObject) {
|
|
951 |
JsonObject v = (JsonObject) firstelement;
|
|
952 |
String labelname = null;
|
|
953 |
for (String theName : new String[]{
|
|
954 |
"name", "label", "key", "description"
|
|
955 |
}) {
|
|
956 |
if (v.containsKey(theName)) {
|
|
957 |
labelname = theName;
|
|
958 |
break;
|
|
959 |
}
|
|
960 |
if (v.containsKey(theName.toUpperCase())) {
|
|
961 |
labelname = theName.toLowerCase();
|
|
962 |
break;
|
|
963 |
}
|
|
964 |
}
|
|
965 |
String valuename = null;
|
|
966 |
if (v.containsKey("value")) {
|
|
967 |
valuename = "value";
|
|
968 |
}
|
|
969 |
r = new DynObjectValueItem[values.size()];
|
|
970 |
for (int i = 0; i < values.size(); i++) {
|
|
971 |
v = (JsonObject) values.get(i);
|
|
972 |
String theLabel;
|
|
973 |
Object theValue;
|
|
974 |
if (labelname == null) {
|
|
975 |
theLabel = v.getString(labelname);
|
|
976 |
} else {
|
|
977 |
theLabel = v.toString();
|
|
978 |
}
|
|
979 |
if (valuename == null) {
|
|
980 |
theValue = v.getString(valuename);
|
|
981 |
} else {
|
|
982 |
theValue = v;
|
|
983 |
}
|
|
984 |
r[i] = new DynObjectValueItem(theValue, theLabel);
|
|
985 |
}
|
|
986 |
} else if (firstelement instanceof Feature) {
|
|
987 |
Feature v = (Feature) firstelement;
|
|
988 |
FeatureType featureType = v.getType();
|
|
989 |
String valuename = null;
|
|
990 |
FeatureAttributeDescriptor[] pks = featureType.getPrimaryKey();
|
|
991 |
if( pks!=null && pks.length == 1) {
|
|
992 |
valuename = pks[0].getName();
|
|
993 |
}
|
|
994 |
String labelname = null;
|
|
995 |
for (String theName : new String[]{
|
|
996 |
"name", "label", "key", "description"
|
|
997 |
}) {
|
|
998 |
if (featureType.get(theName)!=null ) {
|
|
999 |
labelname = theName;
|
|
1000 |
break;
|
|
1001 |
}
|
|
1002 |
}
|
|
1003 |
r = new DynObjectValueItem[values.size()];
|
|
1004 |
for (int i = 0; i < values.size(); i++) {
|
|
1005 |
v = (Feature) values.get(i);
|
|
1006 |
String theLabel;
|
|
1007 |
Object theValue;
|
|
1008 |
if (labelname == null) {
|
|
1009 |
theLabel = v.getString(labelname);
|
|
1010 |
} else {
|
|
1011 |
theLabel = v.toString();
|
|
1012 |
}
|
|
1013 |
if( valuename == null ) {
|
|
1014 |
theValue = v.get(valuename);
|
|
1015 |
} else {
|
|
1016 |
theValue = v.getReference().getCode();
|
|
1017 |
}
|
|
1018 |
r[i] = new DynObjectValueItem(theValue, theLabel);
|
|
1019 |
}
|
|
1020 |
}
|
|
1021 |
return r;
|
|
1022 |
}
|
|
1023 |
|
910 |
1024 |
|
|
1025 |
private DynObjectValueItem[] getAvailableValuesFromExpression() {
|
|
1026 |
if( this.availableValuesExpression == null || this.availableValuesExpression.isEmpty() ) {
|
|
1027 |
return null;
|
|
1028 |
}
|
|
1029 |
Object value = this.availableValuesExpression.execute(null);
|
|
1030 |
if( value instanceof DynObjectValueItem[] ) {
|
|
1031 |
return (DynObjectValueItem[]) value;
|
|
1032 |
}
|
|
1033 |
if( value instanceof List ) {
|
|
1034 |
return this.getAvailableValuesFrom(new GetItemWithSize() {
|
|
1035 |
@Override
|
|
1036 |
public Object get(int i) {
|
|
1037 |
return ((List)value).get(i);
|
|
1038 |
}
|
|
1039 |
|
|
1040 |
@Override
|
|
1041 |
public int size() {
|
|
1042 |
return ((List)value).size();
|
|
1043 |
}
|
|
1044 |
});
|
|
1045 |
}
|
|
1046 |
return null;
|
|
1047 |
}
|
|
1048 |
|
911 |
1049 |
@Override
|
|
1050 |
public Expression getAvailableValuesExpression() {
|
|
1051 |
return this.availableValuesExpression;
|
|
1052 |
}
|
|
1053 |
|
|
1054 |
@Override
|
|
1055 |
public FeatureAttributeDescriptor setAvailableValuesExpression(String expression) {
|
|
1056 |
if (StringUtils.isBlank(expression)) {
|
|
1057 |
this.availableValuesExpression = null;
|
|
1058 |
return this;
|
|
1059 |
}
|
|
1060 |
this.availableValuesExpression = ExpressionUtils.createExpression(expression);
|
|
1061 |
return this;
|
|
1062 |
}
|
|
1063 |
|
|
1064 |
@Override
|
|
1065 |
public FeatureAttributeDescriptor setAvailableValuesExpression(Expression expression) {
|
|
1066 |
this.availableValuesExpression = expression;
|
|
1067 |
return this;
|
|
1068 |
}
|
|
1069 |
|
|
1070 |
@Override
|
912 |
1071 |
public String getLabelOfValue(Object value) {
|
913 |
1072 |
if (this.labelOfValueMap != null) {
|
914 |
1073 |
String theLabel = this.labelOfValueMap.get(value);
|