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 @ 47176
History | View | Annotate | Download (9.89 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 | 45425 | jjdelcerro | import javax.json.JsonObject; |
6 | 43981 | omartinez | import org.apache.commons.lang3.StringUtils; |
7 | 43978 | omartinez | import org.gvsig.expressionevaluator.Code; |
8 | 43981 | omartinez | import org.gvsig.expressionevaluator.Code.Identifier; |
9 | 43989 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
10 | 43978 | omartinez | import org.gvsig.fmap.dal.feature.EditableFeature; |
11 | import org.gvsig.fmap.dal.feature.Feature; |
||
12 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
13 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
14 | 43989 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
15 | 44750 | jjdelcerro | import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
16 | 43989 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
17 | 43981 | omartinez | import org.gvsig.fmap.dal.feature.FeatureType; |
18 | 43978 | omartinez | import org.gvsig.tools.ToolsLocator; |
19 | import org.gvsig.tools.dynobject.DynStruct; |
||
20 | 43981 | omartinez | import org.gvsig.tools.exception.BaseException; |
21 | 43978 | omartinez | import org.gvsig.tools.persistence.PersistenceManager; |
22 | import org.gvsig.tools.persistence.PersistentState; |
||
23 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
24 | 43981 | omartinez | import org.gvsig.tools.visitor.VisitCanceledException; |
25 | import org.gvsig.tools.visitor.Visitor; |
||
26 | 43989 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
27 | 45425 | jjdelcerro | import org.gvsig.json.Json; |
28 | import org.gvsig.json.JsonManager; |
||
29 | import org.gvsig.json.JsonObjectBuilder; |
||
30 | import org.gvsig.json.SupportToJson; |
||
31 | 43993 | jjdelcerro | import org.gvsig.tools.logger.FilteredLogger; |
32 | import org.slf4j.Logger; |
||
33 | import org.slf4j.LoggerFactory; |
||
34 | 43978 | omartinez | |
35 | /**
|
||
36 | *
|
||
37 | * @author osc
|
||
38 | */
|
||
39 | 43989 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
40 | public class DefaultFeatureAttributeEmulatorExpression implements FeatureAttributeEmulatorExpression { |
||
41 | 43981 | omartinez | |
42 | 43993 | jjdelcerro | private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeEmulatorExpression.class); |
43 | |||
44 | 43989 | jjdelcerro | private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME = "EmulatedFieldExpression"; |
45 | |||
46 | private String[] requiredFields = null; |
||
47 | private Expression expression = null; |
||
48 | private SymbolTable symbolTable = null; |
||
49 | private FeatureSymbolTable featureSymbolTable = null; |
||
50 | |||
51 | 43981 | omartinez | private boolean valid; |
52 | private String errorMessage; |
||
53 | 43993 | jjdelcerro | |
54 | 44154 | jjdelcerro | private boolean enableExceptions; |
55 | 44744 | jjdelcerro | private List<String> undefinedSymbols; |
56 | 44154 | jjdelcerro | |
57 | 43993 | jjdelcerro | private FilteredLogger logger;
|
58 | |||
59 | 43989 | jjdelcerro | public DefaultFeatureAttributeEmulatorExpression() {
|
60 | 43978 | omartinez | } |
61 | |||
62 | 43989 | jjdelcerro | public DefaultFeatureAttributeEmulatorExpression(FeatureType featureType, Expression expression) { |
63 | this.expression = expression;
|
||
64 | 43981 | omartinez | this.checkVars(featureType);
|
65 | 44154 | jjdelcerro | this.enableExceptions = false; |
66 | 43978 | omartinez | } |
67 | 43981 | omartinez | |
68 | 43993 | jjdelcerro | private FilteredLogger getLogger() {
|
69 | if( this.logger == null ) { |
||
70 | this.logger = new FilteredLogger(LOGGER, "FeatureAttributeEmulatorExpression", 10); |
||
71 | } |
||
72 | return this.logger; |
||
73 | } |
||
74 | |||
75 | 43981 | omartinez | private void checkVars(final FeatureType featureType) { |
76 | this.valid = true; |
||
77 | final List<String> theUsedFields = new ArrayList<>(); |
||
78 | 44744 | jjdelcerro | this.undefinedSymbols = new ArrayList<>(); |
79 | 43981 | omartinez | |
80 | try {
|
||
81 | 43989 | jjdelcerro | Code code = this.expression.getCode();
|
82 | code.accept(new Visitor() {
|
||
83 | 43981 | omartinez | @Override
|
84 | public void visit(Object obj) throws VisitCanceledException, BaseException { |
||
85 | Code code = (Code) obj; |
||
86 | if (code instanceof Identifier) { |
||
87 | String name = ((Identifier) code).name();
|
||
88 | if (featureType.get(name) == null) { |
||
89 | 44744 | jjdelcerro | undefinedSymbols.add(name); |
90 | 43981 | omartinez | } else {
|
91 | theUsedFields.add(name); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | }); |
||
96 | 43989 | jjdelcerro | } catch (Exception ex) { |
97 | 43981 | omartinez | valid = false;
|
98 | this.errorMessage = ex.getMessage();
|
||
99 | } |
||
100 | this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]); |
||
101 | } |
||
102 | |||
103 | 44604 | jjdelcerro | @Override
|
104 | 44744 | jjdelcerro | public List<String> getUndefinedSymbols() { |
105 | return undefinedSymbols;
|
||
106 | } |
||
107 | |||
108 | @Override
|
||
109 | 44154 | jjdelcerro | public boolean isEnableExceptions() { |
110 | return enableExceptions;
|
||
111 | } |
||
112 | |||
113 | 44604 | jjdelcerro | @Override
|
114 | 44154 | jjdelcerro | public void setEnableExceptions(boolean enableExceptions) { |
115 | this.enableExceptions = enableExceptions;
|
||
116 | } |
||
117 | |||
118 | 43989 | jjdelcerro | @Override
|
119 | public SymbolTable getSymbolTable() {
|
||
120 | 44750 | jjdelcerro | if (this.symbolTable == null || this.featureSymbolTable==null ) { |
121 | 43989 | jjdelcerro | ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager(); |
122 | 44750 | jjdelcerro | this.featureSymbolTable = new FeatureSymbolTableImpl(); |
123 | 43989 | jjdelcerro | this.symbolTable = expressionManager.createSymbolTable();
|
124 | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
||
125 | 43978 | omartinez | } |
126 | 43989 | jjdelcerro | return this.symbolTable; |
127 | 43978 | omartinez | } |
128 | 43981 | omartinez | |
129 | 43989 | jjdelcerro | private FeatureSymbolTable getFeatureSymbolTable() {
|
130 | if (this.featureSymbolTable == null) { |
||
131 | this.getSymbolTable();
|
||
132 | 43981 | omartinez | } |
133 | 43989 | jjdelcerro | return this.featureSymbolTable; |
134 | 43981 | omartinez | } |
135 | |||
136 | 43989 | jjdelcerro | @Override
|
137 | public boolean isValid() { |
||
138 | return this.valid; |
||
139 | 43981 | omartinez | } |
140 | |||
141 | 43978 | omartinez | @Override
|
142 | public Object get(Feature feature) { |
||
143 | 43989 | jjdelcerro | this.getFeatureSymbolTable().setFeature(feature);
|
144 | 43993 | jjdelcerro | try {
|
145 | Object result = this.expression.execute(this.getSymbolTable()); |
||
146 | return result;
|
||
147 | } catch(Exception ex) { |
||
148 | 44154 | jjdelcerro | if( this.enableExceptions ) { |
149 | throw ex;
|
||
150 | } |
||
151 | 44604 | jjdelcerro | String phrase = "unknon"; |
152 | String featureref ="unknon"; |
||
153 | try {
|
||
154 | phrase = this.expression.getPhrase();
|
||
155 | } catch(Throwable th) { |
||
156 | |||
157 | } |
||
158 | try {
|
||
159 | featureref = feature.getReference().toString(); |
||
160 | } catch(Throwable th) { |
||
161 | |||
162 | } |
||
163 | this.getLogger().warn("Problems evaluating expression '"+phrase+"' with feature '"+featureref+"'.", ex); |
||
164 | 43993 | jjdelcerro | return null; |
165 | } |
||
166 | 43978 | omartinez | } |
167 | |||
168 | @Override
|
||
169 | public void set(EditableFeature feature, Object value) { |
||
170 | 43989 | jjdelcerro | // Do nothing
|
171 | 43978 | omartinez | } |
172 | |||
173 | @Override
|
||
174 | public boolean allowSetting() { |
||
175 | return false; |
||
176 | } |
||
177 | |||
178 | @Override
|
||
179 | public String[] getRequiredFieldNames() { |
||
180 | 43981 | omartinez | return this.requiredFields; |
181 | 43978 | omartinez | } |
182 | 43981 | omartinez | |
183 | 43989 | jjdelcerro | @Override
|
184 | public Expression getExpression() { |
||
185 | return this.expression; |
||
186 | 43978 | omartinez | } |
187 | |||
188 | @Override
|
||
189 | 43989 | jjdelcerro | public String getErrorMessage() { |
190 | return this.errorMessage; |
||
191 | } |
||
192 | |||
193 | @Override
|
||
194 | 43978 | omartinez | public void saveToState(PersistentState state) throws PersistenceException { |
195 | 43989 | jjdelcerro | state.set("fields", this.requiredFields); |
196 | state.set("expression", this.expression); |
||
197 | state.set("valid", this.valid); |
||
198 | 43981 | omartinez | |
199 | 43978 | omartinez | } |
200 | |||
201 | @Override
|
||
202 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
203 | 43981 | omartinez | this.valid = state.getBoolean("valid"); |
204 | 43989 | jjdelcerro | this.expression = (Expression) state.get("expression"); |
205 | this.requiredFields = state.getStringArray("fields"); |
||
206 | 43978 | omartinez | } |
207 | |||
208 | 44149 | jjdelcerro | @Override
|
209 | public String toString() { |
||
210 | StringBuilder builder = new StringBuilder(); |
||
211 | builder.append("{ (").append(StringUtils.right(super.toString(),9)).append(")\n"); |
||
212 | builder.append("isValid: ").append(this.isValid()).append(",\n"); |
||
213 | builder.append("requiredFields: ").append(StringUtils.join(this.requiredFields)).append(",\n"); |
||
214 | builder.append("errorMessage: \"").append(this.errorMessage).append("\",\n"); |
||
215 | builder.append("expression: ").append(this.expression).append("\n"); |
||
216 | builder.append("}");
|
||
217 | return builder.toString();
|
||
218 | } |
||
219 | |||
220 | 45425 | jjdelcerro | private static void registerPersistenceDefinition() { |
221 | 43989 | jjdelcerro | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
222 | 43978 | omartinez | |
223 | 43989 | jjdelcerro | if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
|
224 | == null) {
|
||
225 | DynStruct definition = manager.addDefinition(DefaultFeatureAttributeEmulatorExpression.class, |
||
226 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME, |
||
227 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME |
||
228 | 44190 | jjdelcerro | + " persistent definition",
|
229 | 43989 | jjdelcerro | null,
|
230 | null
|
||
231 | ); |
||
232 | definition.addDynFieldObject("expression")
|
||
233 | .setMandatory(true)
|
||
234 | .setPersistent(true)
|
||
235 | .setClassOfValue(Expression.class);
|
||
236 | definition.addDynFieldArray("fields")
|
||
237 | .setClassOfItems(String.class)
|
||
238 | .setMandatory(true)
|
||
239 | .setPersistent(true);
|
||
240 | definition.addDynFieldBoolean("valid")
|
||
241 | .setMandatory(true)
|
||
242 | .setPersistent(true);
|
||
243 | } |
||
244 | 43978 | omartinez | } |
245 | 43981 | omartinez | |
246 | 45425 | jjdelcerro | @Override
|
247 | public JsonObject toJson() {
|
||
248 | return this.toJsonBuilder().build(); |
||
249 | } |
||
250 | |||
251 | @Override
|
||
252 | public JsonObjectBuilder toJsonBuilder() {
|
||
253 | JsonObjectBuilder builder = Json.createObjectBuilder(); |
||
254 | builder.add_class(this);
|
||
255 | builder.add("expression",this.expression); |
||
256 | builder.add("fields", this.requiredFields); |
||
257 | builder.add("valid", this.valid); |
||
258 | return builder;
|
||
259 | } |
||
260 | |||
261 | @Override
|
||
262 | public void fromJson(JsonObject json) { |
||
263 | this.expression = (Expression) Json.toObject(json, "expression"); |
||
264 | this.requiredFields = (String[]) Json.toArray(json, "fields", new String[0]); |
||
265 | this.valid = json.getBoolean("valid", true); |
||
266 | } |
||
267 | |||
268 | private static class TheJsonSerializer implements JsonManager.JsonSerializer { |
||
269 | |||
270 | public TheJsonSerializer() {
|
||
271 | |||
272 | } |
||
273 | |||
274 | @Override
|
||
275 | public Class getObjectClass() { |
||
276 | return DefaultFeatureAttributeEmulatorExpression.class;
|
||
277 | } |
||
278 | |||
279 | @Override
|
||
280 | public Object toObject(JsonObject json) { |
||
281 | DefaultFeatureAttributeEmulatorExpression o = new DefaultFeatureAttributeEmulatorExpression();
|
||
282 | o.fromJson(json); |
||
283 | return o;
|
||
284 | } |
||
285 | |||
286 | @Override
|
||
287 | public JsonObjectBuilder toJsonBuilder(Object value) { |
||
288 | return ((SupportToJson)value).toJsonBuilder();
|
||
289 | } |
||
290 | |||
291 | } |
||
292 | |||
293 | public static void selfRegister() { |
||
294 | registerPersistenceDefinition(); |
||
295 | Json.registerSerializer(new TheJsonSerializer());
|
||
296 | } |
||
297 | 43981 | omartinez | } |