Revision 44262 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/featureform/swing/impl/dynformfield/selectableforeingkey/JDynFormFieldSelectableForeingKey.java
JDynFormFieldSelectableForeingKey.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.featureform.swing.impl.dynformfield.selectableforeingkey; |
24 | 24 |
|
25 |
import java.util.Objects; |
|
26 |
import org.gvsig.expressionevaluator.Expression; |
|
27 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
28 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
29 | 25 |
import org.gvsig.featureform.swing.JFeaturesForm.FeaturesFormContext; |
30 |
import org.gvsig.fmap.dal.DALLocator; |
|
31 |
import org.gvsig.fmap.dal.DataManager; |
|
32 |
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_CODE; |
|
33 |
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_LABEL; |
|
34 |
import static org.gvsig.fmap.dal.DataManager.DAL_FOREING_TABLE; |
|
35 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
|
36 |
import org.gvsig.fmap.dal.feature.Feature; |
|
37 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
26 |
import org.gvsig.fmap.dal.StoresRepository; |
|
27 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
28 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
29 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
|
30 |
import org.gvsig.tools.ToolsLocator; |
|
38 | 31 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
39 | 32 |
import org.gvsig.tools.dynform.JDynForm; |
40 | 33 |
import org.gvsig.tools.dynform.JDynFormField; |
... | ... | |
56 | 49 |
super(serviceManager, componentsFactory, factory, definition, value); |
57 | 50 |
} |
58 | 51 |
|
52 |
|
|
53 |
private ForeingKey getForeingKey() { |
|
54 |
JDynForm.DynFormContext context = this.getForm().getContext(); |
|
55 |
if( !(context instanceof FeaturesFormContext) ) { |
|
56 |
return null; |
|
57 |
} |
|
58 |
FeatureType featureType = ((FeaturesFormContext)context).getFeatureType(); |
|
59 |
if( featureType==null ) { |
|
60 |
return null; |
|
61 |
} |
|
62 |
FeatureAttributeDescriptor attribute = featureType.getAttributeDescriptor(this.getName()); |
|
63 |
if( attribute == null ) { |
|
64 |
return null; |
|
65 |
} |
|
66 |
ForeingKey foreingKey = attribute.getForeingKey(); |
|
67 |
return foreingKey; |
|
68 |
} |
|
69 |
|
|
59 | 70 |
@Override |
60 | 71 |
protected DynObjectValueItem[] getAvailableValues() { |
61 |
String foreingLabel = getTagValueAsString(DAL_FOREING_LABEL, null); |
|
62 |
String foreingTableName = getTagValueAsString(DAL_FOREING_TABLE, null); |
|
63 |
String foreingCodeName = getTagValueAsString(DAL_FOREING_CODE, null); |
|
64 |
try { |
|
65 |
|
|
66 |
DataManager dataManager = DALLocator.getDataManager(); |
|
67 |
Expression labelExpression = ExpressionUtils.createExpression(foreingLabel); |
|
68 |
FeatureSymbolTable featureSymbolTable = dataManager.createFeatureSymbolTable(); |
|
69 |
SymbolTable symbolTable = featureSymbolTable.createParent(); |
|
70 |
|
|
71 |
|
|
72 |
FeatureStore store = null; |
|
73 |
JDynForm.DynFormContext context = this.getForm().getContext(); |
|
74 |
if( context instanceof FeaturesFormContext ) { |
|
75 |
store = ((FeaturesFormContext) context).getFeatureStore(foreingTableName); |
|
76 |
} |
|
77 |
if( store == null ) { |
|
78 |
LOGGER.warn("Can't locate store '"+foreingTableName+"' to get available values of field '"+this.getName()+"'."); |
|
79 |
return null; |
|
80 |
} |
|
81 |
|
|
82 |
int count = (int) store.getFeatureCount(); |
|
83 |
DynObjectValueItem[] values = new DynObjectValueItem[count]; |
|
84 |
int n = 0; |
|
85 |
for (Feature feature : store.getFeatureSet()) { |
|
86 |
featureSymbolTable.setFeature(feature); |
|
87 |
Object code = feature.get(foreingCodeName); |
|
88 |
Object label = labelExpression.execute(symbolTable); |
|
89 |
values[n++] = new DynObjectValueItem(code, Objects.toString(label, Objects.toString(code, "##ERROR##"))); |
|
90 |
} |
|
91 |
return values; |
|
92 |
} catch (Exception ex) { |
|
93 |
LOGGER.warn("Can't get values from table '" + foreingTableName + "' for field '" + this.getForm().getDefinition().getName() + ":" + this.getName() + "'.", ex); |
|
72 |
final ForeingKey foreingKey = this.getForeingKey(); |
|
73 |
if( foreingKey==null || !foreingKey.isSelectable() ) { |
|
74 |
return null; |
|
94 | 75 |
} |
95 |
return null; |
|
76 |
DynObjectValueItem[] values = foreingKey.getAvailableValues(null); |
|
77 |
return values; |
|
96 | 78 |
} |
97 | 79 |
|
98 | 80 |
} |
Also available in: Unified diff