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 @ 44633
History | View | Annotate | Download (8.33 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 | 44154 | jjdelcerro | private boolean enableExceptions; |
50 | |||
51 | 43993 | jjdelcerro | private FilteredLogger logger;
|
52 | |||
53 | 43989 | jjdelcerro | public DefaultFeatureAttributeEmulatorExpression() {
|
54 | 43978 | omartinez | } |
55 | |||
56 | 43989 | jjdelcerro | public DefaultFeatureAttributeEmulatorExpression(FeatureType featureType, Expression expression) { |
57 | this.expression = expression;
|
||
58 | 43981 | omartinez | this.checkVars(featureType);
|
59 | 44154 | jjdelcerro | this.enableExceptions = false; |
60 | 43978 | omartinez | } |
61 | 43981 | omartinez | |
62 | 43993 | jjdelcerro | private FilteredLogger getLogger() {
|
63 | if( this.logger == null ) { |
||
64 | this.logger = new FilteredLogger(LOGGER, "FeatureAttributeEmulatorExpression", 10); |
||
65 | } |
||
66 | return this.logger; |
||
67 | } |
||
68 | |||
69 | 43981 | omartinez | private void checkVars(final FeatureType featureType) { |
70 | this.valid = true; |
||
71 | final List<String> theUsedFields = new ArrayList<>(); |
||
72 | final List<String> undefinedFields = new ArrayList<>(); |
||
73 | |||
74 | try {
|
||
75 | 43989 | jjdelcerro | Code code = this.expression.getCode();
|
76 | code.accept(new Visitor() {
|
||
77 | 43981 | omartinez | @Override
|
78 | public void visit(Object obj) throws VisitCanceledException, BaseException { |
||
79 | Code code = (Code) obj; |
||
80 | if (code instanceof Identifier) { |
||
81 | String name = ((Identifier) code).name();
|
||
82 | if (featureType.get(name) == null) { |
||
83 | undefinedFields.add(name); |
||
84 | valid = false;
|
||
85 | } else {
|
||
86 | theUsedFields.add(name); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | }); |
||
91 | if(!undefinedFields.isEmpty()){
|
||
92 | this.errorMessage = "undefined fields " + StringUtils.join(undefinedFields,", "); |
||
93 | } |
||
94 | 43989 | jjdelcerro | } catch (Exception ex) { |
95 | 43981 | omartinez | valid = false;
|
96 | this.errorMessage = ex.getMessage();
|
||
97 | } |
||
98 | this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]); |
||
99 | } |
||
100 | |||
101 | 44604 | jjdelcerro | @Override
|
102 | 44154 | jjdelcerro | public boolean isEnableExceptions() { |
103 | return enableExceptions;
|
||
104 | } |
||
105 | |||
106 | 44604 | jjdelcerro | @Override
|
107 | 44154 | jjdelcerro | public void setEnableExceptions(boolean enableExceptions) { |
108 | this.enableExceptions = enableExceptions;
|
||
109 | } |
||
110 | |||
111 | 43989 | jjdelcerro | @Override
|
112 | public SymbolTable getSymbolTable() {
|
||
113 | if (this.symbolTable == null) { |
||
114 | ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager(); |
||
115 | 44262 | jjdelcerro | this.featureSymbolTable = (FeatureSymbolTable) expressionManager.getSymbolTable(DataManager.DAL_SYMBOL_TABLE_FEATURE
|
116 | 43978 | omartinez | ); |
117 | 43989 | jjdelcerro | this.symbolTable = expressionManager.createSymbolTable();
|
118 | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
||
119 | 43978 | omartinez | } |
120 | 43989 | jjdelcerro | return this.symbolTable; |
121 | 43978 | omartinez | } |
122 | 43981 | omartinez | |
123 | 43989 | jjdelcerro | private FeatureSymbolTable getFeatureSymbolTable() {
|
124 | if (this.featureSymbolTable == null) { |
||
125 | this.getSymbolTable();
|
||
126 | 43981 | omartinez | } |
127 | 43989 | jjdelcerro | return this.featureSymbolTable; |
128 | 43981 | omartinez | } |
129 | |||
130 | 43989 | jjdelcerro | @Override
|
131 | public boolean isValid() { |
||
132 | return this.valid; |
||
133 | 43981 | omartinez | } |
134 | |||
135 | 43978 | omartinez | @Override
|
136 | public Object get(Feature feature) { |
||
137 | 43981 | omartinez | if (!this.valid) { |
138 | return null; |
||
139 | } |
||
140 | 43989 | jjdelcerro | this.getFeatureSymbolTable().setFeature(feature);
|
141 | 43993 | jjdelcerro | try {
|
142 | Object result = this.expression.execute(this.getSymbolTable()); |
||
143 | return result;
|
||
144 | } catch(Exception ex) { |
||
145 | 44154 | jjdelcerro | if( this.enableExceptions ) { |
146 | throw ex;
|
||
147 | } |
||
148 | 44604 | jjdelcerro | String phrase = "unknon"; |
149 | String featureref ="unknon"; |
||
150 | try {
|
||
151 | phrase = this.expression.getPhrase();
|
||
152 | } catch(Throwable th) { |
||
153 | |||
154 | } |
||
155 | try {
|
||
156 | featureref = feature.getReference().toString(); |
||
157 | } catch(Throwable th) { |
||
158 | |||
159 | } |
||
160 | this.getLogger().warn("Problems evaluating expression '"+phrase+"' with feature '"+featureref+"'.", ex); |
||
161 | 43993 | jjdelcerro | return null; |
162 | } |
||
163 | 43978 | omartinez | } |
164 | |||
165 | @Override
|
||
166 | public void set(EditableFeature feature, Object value) { |
||
167 | 43989 | jjdelcerro | // Do nothing
|
168 | 43978 | omartinez | } |
169 | |||
170 | @Override
|
||
171 | public boolean allowSetting() { |
||
172 | return false; |
||
173 | } |
||
174 | |||
175 | @Override
|
||
176 | public String[] getRequiredFieldNames() { |
||
177 | 43981 | omartinez | return this.requiredFields; |
178 | 43978 | omartinez | } |
179 | 43981 | omartinez | |
180 | 43989 | jjdelcerro | @Override
|
181 | public Expression getExpression() { |
||
182 | return this.expression; |
||
183 | 43978 | omartinez | } |
184 | |||
185 | @Override
|
||
186 | 43989 | jjdelcerro | public String getErrorMessage() { |
187 | return this.errorMessage; |
||
188 | } |
||
189 | |||
190 | @Override
|
||
191 | 43978 | omartinez | public void saveToState(PersistentState state) throws PersistenceException { |
192 | 43989 | jjdelcerro | state.set("fields", this.requiredFields); |
193 | state.set("expression", this.expression); |
||
194 | state.set("valid", this.valid); |
||
195 | 43981 | omartinez | |
196 | 43978 | omartinez | } |
197 | |||
198 | @Override
|
||
199 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
200 | 43981 | omartinez | this.valid = state.getBoolean("valid"); |
201 | 43989 | jjdelcerro | this.expression = (Expression) state.get("expression"); |
202 | this.requiredFields = state.getStringArray("fields"); |
||
203 | 43978 | omartinez | } |
204 | |||
205 | 44149 | jjdelcerro | @Override
|
206 | public String toString() { |
||
207 | StringBuilder builder = new StringBuilder(); |
||
208 | builder.append("{ (").append(StringUtils.right(super.toString(),9)).append(")\n"); |
||
209 | builder.append("isValid: ").append(this.isValid()).append(",\n"); |
||
210 | builder.append("requiredFields: ").append(StringUtils.join(this.requiredFields)).append(",\n"); |
||
211 | builder.append("errorMessage: \"").append(this.errorMessage).append("\",\n"); |
||
212 | builder.append("expression: ").append(this.expression).append("\n"); |
||
213 | builder.append("}");
|
||
214 | return builder.toString();
|
||
215 | } |
||
216 | |||
217 | 43989 | jjdelcerro | public static void registerPersistenceDefinition() { |
218 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
219 | 43978 | omartinez | |
220 | 43989 | jjdelcerro | if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
|
221 | == null) {
|
||
222 | DynStruct definition = manager.addDefinition(DefaultFeatureAttributeEmulatorExpression.class, |
||
223 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME, |
||
224 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME |
||
225 | 44190 | jjdelcerro | + " persistent definition",
|
226 | 43989 | jjdelcerro | null,
|
227 | null
|
||
228 | ); |
||
229 | definition.addDynFieldObject("expression")
|
||
230 | .setMandatory(true)
|
||
231 | .setPersistent(true)
|
||
232 | .setClassOfValue(Expression.class);
|
||
233 | definition.addDynFieldArray("fields")
|
||
234 | .setClassOfItems(String.class)
|
||
235 | .setMandatory(true)
|
||
236 | .setPersistent(true);
|
||
237 | definition.addDynFieldBoolean("valid")
|
||
238 | .setMandatory(true)
|
||
239 | .setPersistent(true);
|
||
240 | } |
||
241 | 43978 | omartinez | } |
242 | 43981 | omartinez | |
243 | } |