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