svn-gvsig-desktop / 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 / impl / expressionevaluator / DefaultFeatureAttributeEmulatorExpression.java @ 44149
History | View | Annotate | Download (7.65 KB)
1 | 43989 | jjdelcerro | package org.gvsig.fmap.dal.impl.expressionevaluator; |
---|---|---|---|
2 | 43978 | omartinez | |
3 | 43981 | omartinez | import java.util.ArrayList; |
4 | import java.util.List; |
||
5 | import org.apache.commons.lang3.StringUtils; |
||
6 | 43978 | omartinez | import org.gvsig.expressionevaluator.Code; |
7 | 43981 | omartinez | import org.gvsig.expressionevaluator.Code.Identifier; |
8 | 43989 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
9 | 43978 | omartinez | import org.gvsig.fmap.dal.feature.EditableFeature; |
10 | import org.gvsig.fmap.dal.feature.Feature; |
||
11 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
12 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
13 | 43989 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
14 | import org.gvsig.fmap.dal.DataManager; |
||
15 | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
||
16 | 43981 | omartinez | import org.gvsig.fmap.dal.feature.FeatureType; |
17 | 43978 | omartinez | import org.gvsig.tools.ToolsLocator; |
18 | import org.gvsig.tools.dynobject.DynStruct; |
||
19 | 43981 | omartinez | import org.gvsig.tools.exception.BaseException; |
20 | 43978 | omartinez | import org.gvsig.tools.persistence.PersistenceManager; |
21 | import org.gvsig.tools.persistence.PersistentState; |
||
22 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
23 | 43981 | omartinez | import org.gvsig.tools.visitor.VisitCanceledException; |
24 | import org.gvsig.tools.visitor.Visitor; |
||
25 | 43989 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
26 | 43993 | jjdelcerro | import org.gvsig.tools.logger.FilteredLogger; |
27 | import org.slf4j.Logger; |
||
28 | import org.slf4j.LoggerFactory; |
||
29 | 43978 | omartinez | |
30 | /**
|
||
31 | *
|
||
32 | * @author osc
|
||
33 | */
|
||
34 | 43989 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
35 | public class DefaultFeatureAttributeEmulatorExpression implements FeatureAttributeEmulatorExpression { |
||
36 | 43981 | omartinez | |
37 | 43993 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeEmulatorExpression.class); |
38 | |||
39 | 43989 | jjdelcerro | private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME = "EmulatedFieldExpression"; |
40 | |||
41 | private String[] requiredFields = null; |
||
42 | private Expression expression = null; |
||
43 | private SymbolTable symbolTable = null; |
||
44 | private FeatureSymbolTable featureSymbolTable = null; |
||
45 | |||
46 | 43981 | omartinez | private boolean valid; |
47 | private String errorMessage; |
||
48 | 43993 | jjdelcerro | |
49 | private FilteredLogger logger;
|
||
50 | |||
51 | 43989 | jjdelcerro | public DefaultFeatureAttributeEmulatorExpression() {
|
52 | 43978 | omartinez | } |
53 | |||
54 | 43989 | jjdelcerro | public DefaultFeatureAttributeEmulatorExpression(FeatureType featureType, Expression expression) { |
55 | this.expression = expression;
|
||
56 | 43981 | omartinez | this.checkVars(featureType);
|
57 | 43978 | omartinez | } |
58 | 43981 | omartinez | |
59 | 43993 | jjdelcerro | private FilteredLogger getLogger() {
|
60 | if( this.logger == null ) { |
||
61 | this.logger = new FilteredLogger(LOGGER, "FeatureAttributeEmulatorExpression", 10); |
||
62 | } |
||
63 | return this.logger; |
||
64 | } |
||
65 | |||
66 | 43981 | omartinez | private void checkVars(final FeatureType featureType) { |
67 | this.valid = true; |
||
68 | final List<String> theUsedFields = new ArrayList<>(); |
||
69 | final List<String> undefinedFields = new ArrayList<>(); |
||
70 | |||
71 | try {
|
||
72 | 43989 | jjdelcerro | Code code = this.expression.getCode();
|
73 | code.accept(new Visitor() {
|
||
74 | 43981 | omartinez | @Override
|
75 | public void visit(Object obj) throws VisitCanceledException, BaseException { |
||
76 | Code code = (Code) obj; |
||
77 | if (code instanceof Identifier) { |
||
78 | String name = ((Identifier) code).name();
|
||
79 | if (featureType.get(name) == null) { |
||
80 | undefinedFields.add(name); |
||
81 | valid = false;
|
||
82 | } else {
|
||
83 | theUsedFields.add(name); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | }); |
||
88 | if(!undefinedFields.isEmpty()){
|
||
89 | this.errorMessage = "undefined fields " + StringUtils.join(undefinedFields,", "); |
||
90 | } |
||
91 | 43989 | jjdelcerro | } catch (Exception ex) { |
92 | 43981 | omartinez | valid = false;
|
93 | this.errorMessage = ex.getMessage();
|
||
94 | } |
||
95 | this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]); |
||
96 | } |
||
97 | |||
98 | 43989 | jjdelcerro | @Override
|
99 | public SymbolTable getSymbolTable() {
|
||
100 | if (this.symbolTable == null) { |
||
101 | ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager(); |
||
102 | this.featureSymbolTable = (FeatureSymbolTable) expressionManager.getSymbolTable(
|
||
103 | DataManager.FEATURE_SYMBOL_TABLE |
||
104 | 43978 | omartinez | ); |
105 | 43989 | jjdelcerro | this.symbolTable = expressionManager.createSymbolTable();
|
106 | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
||
107 | 43978 | omartinez | } |
108 | 43989 | jjdelcerro | return this.symbolTable; |
109 | 43978 | omartinez | } |
110 | 43981 | omartinez | |
111 | 43989 | jjdelcerro | private FeatureSymbolTable getFeatureSymbolTable() {
|
112 | if (this.featureSymbolTable == null) { |
||
113 | this.getSymbolTable();
|
||
114 | 43981 | omartinez | } |
115 | 43989 | jjdelcerro | return this.featureSymbolTable; |
116 | 43981 | omartinez | } |
117 | |||
118 | 43989 | jjdelcerro | @Override
|
119 | public boolean isValid() { |
||
120 | return this.valid; |
||
121 | 43981 | omartinez | } |
122 | |||
123 | 43978 | omartinez | @Override
|
124 | public Object get(Feature feature) { |
||
125 | 43981 | omartinez | if (!this.valid) { |
126 | return null; |
||
127 | } |
||
128 | 43989 | jjdelcerro | this.getFeatureSymbolTable().setFeature(feature);
|
129 | 43993 | jjdelcerro | try {
|
130 | Object result = this.expression.execute(this.getSymbolTable()); |
||
131 | return result;
|
||
132 | } catch(Exception ex) { |
||
133 | this.getLogger().warn("Problems evaluating expression '"+this.expression.getPhrase()+"' with feature '"+feature.getReference().toString()+"'.", ex); |
||
134 | return null; |
||
135 | } |
||
136 | 43978 | omartinez | } |
137 | |||
138 | @Override
|
||
139 | public void set(EditableFeature feature, Object value) { |
||
140 | 43989 | jjdelcerro | // Do nothing
|
141 | 43978 | omartinez | } |
142 | |||
143 | @Override
|
||
144 | public boolean allowSetting() { |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | @Override
|
||
149 | public String[] getRequiredFieldNames() { |
||
150 | 43981 | omartinez | return this.requiredFields; |
151 | 43978 | omartinez | } |
152 | 43981 | omartinez | |
153 | 43989 | jjdelcerro | @Override
|
154 | public Expression getExpression() { |
||
155 | return this.expression; |
||
156 | 43978 | omartinez | } |
157 | |||
158 | @Override
|
||
159 | 43989 | jjdelcerro | public String getErrorMessage() { |
160 | return this.errorMessage; |
||
161 | } |
||
162 | |||
163 | @Override
|
||
164 | 43978 | omartinez | public void saveToState(PersistentState state) throws PersistenceException { |
165 | 43989 | jjdelcerro | state.set("fields", this.requiredFields); |
166 | state.set("expression", this.expression); |
||
167 | state.set("valid", this.valid); |
||
168 | 43981 | omartinez | |
169 | 43978 | omartinez | } |
170 | |||
171 | @Override
|
||
172 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
173 | 43981 | omartinez | this.valid = state.getBoolean("valid"); |
174 | 43989 | jjdelcerro | this.expression = (Expression) state.get("expression"); |
175 | this.requiredFields = state.getStringArray("fields"); |
||
176 | 43978 | omartinez | } |
177 | |||
178 | 44149 | jjdelcerro | @Override
|
179 | public String toString() { |
||
180 | StringBuilder builder = new StringBuilder(); |
||
181 | builder.append("{ (").append(StringUtils.right(super.toString(),9)).append(")\n"); |
||
182 | builder.append("isValid: ").append(this.isValid()).append(",\n"); |
||
183 | builder.append("requiredFields: ").append(StringUtils.join(this.requiredFields)).append(",\n"); |
||
184 | builder.append("errorMessage: \"").append(this.errorMessage).append("\",\n"); |
||
185 | builder.append("expression: ").append(this.expression).append("\n"); |
||
186 | builder.append("}");
|
||
187 | return builder.toString();
|
||
188 | } |
||
189 | |||
190 | 43989 | jjdelcerro | public static void registerPersistenceDefinition() { |
191 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
192 | 43978 | omartinez | |
193 | 43989 | jjdelcerro | if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
|
194 | == null) {
|
||
195 | DynStruct definition = manager.addDefinition(DefaultFeatureAttributeEmulatorExpression.class, |
||
196 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME, |
||
197 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME |
||
198 | + " Persistent definition",
|
||
199 | null,
|
||
200 | null
|
||
201 | ); |
||
202 | definition.addDynFieldObject("expression")
|
||
203 | .setMandatory(true)
|
||
204 | .setPersistent(true)
|
||
205 | .setClassOfValue(Expression.class);
|
||
206 | definition.addDynFieldArray("fields")
|
||
207 | .setClassOfItems(String.class)
|
||
208 | .setMandatory(true)
|
||
209 | .setPersistent(true);
|
||
210 | definition.addDynFieldBoolean("valid")
|
||
211 | .setMandatory(true)
|
||
212 | .setPersistent(true);
|
||
213 | } |
||
214 | 43978 | omartinez | } |
215 | 43981 | omartinez | |
216 | } |