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 @ 47779
History | View | Annotate | Download (9.89 KB)
1 |
package org.gvsig.fmap.dal.impl.expressionevaluator; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import javax.json.JsonObject; |
6 |
import org.apache.commons.lang3.StringUtils; |
7 |
import org.gvsig.expressionevaluator.Code; |
8 |
import org.gvsig.expressionevaluator.Code.Identifier; |
9 |
import org.gvsig.expressionevaluator.Expression; |
10 |
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 |
import org.gvsig.expressionevaluator.SymbolTable; |
15 |
import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
16 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
17 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.dynobject.DynStruct; |
20 |
import org.gvsig.tools.exception.BaseException; |
21 |
import org.gvsig.tools.persistence.PersistenceManager; |
22 |
import org.gvsig.tools.persistence.PersistentState; |
23 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
24 |
import org.gvsig.tools.visitor.VisitCanceledException; |
25 |
import org.gvsig.tools.visitor.Visitor; |
26 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
27 |
import org.gvsig.json.Json; |
28 |
import org.gvsig.json.JsonManager; |
29 |
import org.gvsig.json.JsonObjectBuilder; |
30 |
import org.gvsig.json.SupportToJson; |
31 |
import org.gvsig.tools.logger.FilteredLogger; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author osc
|
38 |
*/
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public class DefaultFeatureAttributeEmulatorExpression implements FeatureAttributeEmulatorExpression { |
41 |
|
42 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureAttributeEmulatorExpression.class); |
43 |
|
44 |
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 |
private boolean valid; |
52 |
private String errorMessage; |
53 |
|
54 |
private boolean enableExceptions; |
55 |
private List<String> undefinedSymbols; |
56 |
|
57 |
private FilteredLogger logger;
|
58 |
|
59 |
public DefaultFeatureAttributeEmulatorExpression() {
|
60 |
} |
61 |
|
62 |
public DefaultFeatureAttributeEmulatorExpression(FeatureType featureType, Expression expression) { |
63 |
this.expression = expression;
|
64 |
this.checkVars(featureType);
|
65 |
this.enableExceptions = false; |
66 |
} |
67 |
|
68 |
private FilteredLogger getLogger() {
|
69 |
if( this.logger == null ) { |
70 |
this.logger = new FilteredLogger(LOGGER, "FeatureAttributeEmulatorExpression", 10); |
71 |
} |
72 |
return this.logger; |
73 |
} |
74 |
|
75 |
private void checkVars(final FeatureType featureType) { |
76 |
this.valid = true; |
77 |
final List<String> theUsedFields = new ArrayList<>(); |
78 |
this.undefinedSymbols = new ArrayList<>(); |
79 |
|
80 |
try {
|
81 |
Code code = this.expression.getCode();
|
82 |
code.accept(new Visitor() {
|
83 |
@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 |
undefinedSymbols.add(name); |
90 |
} else {
|
91 |
theUsedFields.add(name); |
92 |
} |
93 |
} |
94 |
} |
95 |
}); |
96 |
} catch (Exception ex) { |
97 |
valid = false;
|
98 |
this.errorMessage = ex.getMessage();
|
99 |
} |
100 |
this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public List<String> getUndefinedSymbols() { |
105 |
return undefinedSymbols;
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public boolean isEnableExceptions() { |
110 |
return enableExceptions;
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void setEnableExceptions(boolean enableExceptions) { |
115 |
this.enableExceptions = enableExceptions;
|
116 |
} |
117 |
|
118 |
@Override
|
119 |
public SymbolTable getSymbolTable() {
|
120 |
if (this.symbolTable == null || this.featureSymbolTable==null ) { |
121 |
ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager(); |
122 |
this.featureSymbolTable = new FeatureSymbolTableImpl(); |
123 |
this.symbolTable = expressionManager.createSymbolTable();
|
124 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
125 |
} |
126 |
return this.symbolTable; |
127 |
} |
128 |
|
129 |
private FeatureSymbolTable getFeatureSymbolTable() {
|
130 |
if (this.featureSymbolTable == null) { |
131 |
this.getSymbolTable();
|
132 |
} |
133 |
return this.featureSymbolTable; |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public boolean isValid() { |
138 |
return this.valid; |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public Object get(Feature feature) { |
143 |
this.getFeatureSymbolTable().setFeature(feature);
|
144 |
try {
|
145 |
Object result = this.expression.execute(this.getSymbolTable()); |
146 |
return result;
|
147 |
} catch(Exception ex) { |
148 |
if( this.enableExceptions ) { |
149 |
throw ex;
|
150 |
} |
151 |
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 |
return null; |
165 |
} |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public void set(EditableFeature feature, Object value) { |
170 |
// Do nothing
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public boolean allowSetting() { |
175 |
return false; |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public String[] getRequiredFieldNames() { |
180 |
return this.requiredFields; |
181 |
} |
182 |
|
183 |
@Override
|
184 |
public Expression getExpression() { |
185 |
return this.expression; |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public String getErrorMessage() { |
190 |
return this.errorMessage; |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void saveToState(PersistentState state) throws PersistenceException { |
195 |
state.set("fields", this.requiredFields); |
196 |
state.set("expression", this.expression); |
197 |
state.set("valid", this.valid); |
198 |
|
199 |
} |
200 |
|
201 |
@Override
|
202 |
public void loadFromState(PersistentState state) throws PersistenceException { |
203 |
this.valid = state.getBoolean("valid"); |
204 |
this.expression = (Expression) state.get("expression"); |
205 |
this.requiredFields = state.getStringArray("fields"); |
206 |
} |
207 |
|
208 |
@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 |
private static void registerPersistenceDefinition() { |
221 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
222 |
|
223 |
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 |
+ " persistent definition",
|
229 |
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 |
} |
245 |
|
246 |
@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 |
} |