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 @ 44154
History | View | Annotate | Download (8 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 | 44154 | jjdelcerro | public boolean isEnableExceptions() { |
102 | return enableExceptions;
|
||
103 | } |
||
104 | |||
105 | public void setEnableExceptions(boolean enableExceptions) { |
||
106 | this.enableExceptions = enableExceptions;
|
||
107 | } |
||
108 | |||
109 | 43989 | jjdelcerro | @Override
|
110 | public SymbolTable getSymbolTable() {
|
||
111 | if (this.symbolTable == null) { |
||
112 | ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager(); |
||
113 | this.featureSymbolTable = (FeatureSymbolTable) expressionManager.getSymbolTable(
|
||
114 | DataManager.FEATURE_SYMBOL_TABLE |
||
115 | 43978 | omartinez | ); |
116 | 43989 | jjdelcerro | this.symbolTable = expressionManager.createSymbolTable();
|
117 | this.symbolTable.addSymbolTable(this.featureSymbolTable); |
||
118 | 43978 | omartinez | } |
119 | 43989 | jjdelcerro | return this.symbolTable; |
120 | 43978 | omartinez | } |
121 | 43981 | omartinez | |
122 | 43989 | jjdelcerro | private FeatureSymbolTable getFeatureSymbolTable() {
|
123 | if (this.featureSymbolTable == null) { |
||
124 | this.getSymbolTable();
|
||
125 | 43981 | omartinez | } |
126 | 43989 | jjdelcerro | return this.featureSymbolTable; |
127 | 43981 | omartinez | } |
128 | |||
129 | 43989 | jjdelcerro | @Override
|
130 | public boolean isValid() { |
||
131 | return this.valid; |
||
132 | 43981 | omartinez | } |
133 | |||
134 | 43978 | omartinez | @Override
|
135 | public Object get(Feature feature) { |
||
136 | 43981 | omartinez | if (!this.valid) { |
137 | return null; |
||
138 | } |
||
139 | 43989 | jjdelcerro | this.getFeatureSymbolTable().setFeature(feature);
|
140 | 43993 | jjdelcerro | try {
|
141 | Object result = this.expression.execute(this.getSymbolTable()); |
||
142 | return result;
|
||
143 | } catch(Exception ex) { |
||
144 | 44154 | jjdelcerro | if( this.enableExceptions ) { |
145 | throw ex;
|
||
146 | } |
||
147 | 43993 | jjdelcerro | this.getLogger().warn("Problems evaluating expression '"+this.expression.getPhrase()+"' with feature '"+feature.getReference().toString()+"'.", ex); |
148 | return null; |
||
149 | } |
||
150 | 43978 | omartinez | } |
151 | |||
152 | @Override
|
||
153 | public void set(EditableFeature feature, Object value) { |
||
154 | 43989 | jjdelcerro | // Do nothing
|
155 | 43978 | omartinez | } |
156 | |||
157 | @Override
|
||
158 | public boolean allowSetting() { |
||
159 | return false; |
||
160 | } |
||
161 | |||
162 | @Override
|
||
163 | public String[] getRequiredFieldNames() { |
||
164 | 43981 | omartinez | return this.requiredFields; |
165 | 43978 | omartinez | } |
166 | 43981 | omartinez | |
167 | 43989 | jjdelcerro | @Override
|
168 | public Expression getExpression() { |
||
169 | return this.expression; |
||
170 | 43978 | omartinez | } |
171 | |||
172 | @Override
|
||
173 | 43989 | jjdelcerro | public String getErrorMessage() { |
174 | return this.errorMessage; |
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | 43978 | omartinez | public void saveToState(PersistentState state) throws PersistenceException { |
179 | 43989 | jjdelcerro | state.set("fields", this.requiredFields); |
180 | state.set("expression", this.expression); |
||
181 | state.set("valid", this.valid); |
||
182 | 43981 | omartinez | |
183 | 43978 | omartinez | } |
184 | |||
185 | @Override
|
||
186 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
187 | 43981 | omartinez | this.valid = state.getBoolean("valid"); |
188 | 43989 | jjdelcerro | this.expression = (Expression) state.get("expression"); |
189 | this.requiredFields = state.getStringArray("fields"); |
||
190 | 43978 | omartinez | } |
191 | |||
192 | 44149 | jjdelcerro | @Override
|
193 | public String toString() { |
||
194 | StringBuilder builder = new StringBuilder(); |
||
195 | builder.append("{ (").append(StringUtils.right(super.toString(),9)).append(")\n"); |
||
196 | builder.append("isValid: ").append(this.isValid()).append(",\n"); |
||
197 | builder.append("requiredFields: ").append(StringUtils.join(this.requiredFields)).append(",\n"); |
||
198 | builder.append("errorMessage: \"").append(this.errorMessage).append("\",\n"); |
||
199 | builder.append("expression: ").append(this.expression).append("\n"); |
||
200 | builder.append("}");
|
||
201 | return builder.toString();
|
||
202 | } |
||
203 | |||
204 | 43989 | jjdelcerro | public static void registerPersistenceDefinition() { |
205 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
206 | 43978 | omartinez | |
207 | 43989 | jjdelcerro | if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
|
208 | == null) {
|
||
209 | DynStruct definition = manager.addDefinition(DefaultFeatureAttributeEmulatorExpression.class, |
||
210 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME, |
||
211 | EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME |
||
212 | + " Persistent definition",
|
||
213 | null,
|
||
214 | null
|
||
215 | ); |
||
216 | definition.addDynFieldObject("expression")
|
||
217 | .setMandatory(true)
|
||
218 | .setPersistent(true)
|
||
219 | .setClassOfValue(Expression.class);
|
||
220 | definition.addDynFieldArray("fields")
|
||
221 | .setClassOfItems(String.class)
|
||
222 | .setMandatory(true)
|
||
223 | .setPersistent(true);
|
||
224 | definition.addDynFieldBoolean("valid")
|
||
225 | .setMandatory(true)
|
||
226 | .setPersistent(true);
|
||
227 | } |
||
228 | 43978 | omartinez | } |
229 | 43981 | omartinez | |
230 | } |