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 @ 44604
History | View | Annotate | Download (8.33 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 |
|
51 |
private FilteredLogger logger;
|
52 |
|
53 |
public DefaultFeatureAttributeEmulatorExpression() {
|
54 |
} |
55 |
|
56 |
public DefaultFeatureAttributeEmulatorExpression(FeatureType featureType, Expression expression) { |
57 |
this.expression = expression;
|
58 |
this.checkVars(featureType);
|
59 |
this.enableExceptions = false; |
60 |
} |
61 |
|
62 |
private FilteredLogger getLogger() {
|
63 |
if( this.logger == null ) { |
64 |
this.logger = new FilteredLogger(LOGGER, "FeatureAttributeEmulatorExpression", 10); |
65 |
} |
66 |
return this.logger; |
67 |
} |
68 |
|
69 |
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 |
Code code = this.expression.getCode();
|
76 |
code.accept(new Visitor() {
|
77 |
@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 |
} catch (Exception ex) { |
95 |
valid = false;
|
96 |
this.errorMessage = ex.getMessage();
|
97 |
} |
98 |
this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public boolean isEnableExceptions() { |
103 |
return enableExceptions;
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void setEnableExceptions(boolean enableExceptions) { |
108 |
this.enableExceptions = enableExceptions;
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public SymbolTable getSymbolTable() {
|
113 |
if (this.symbolTable == null) { |
114 |
ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager(); |
115 |
this.featureSymbolTable = (FeatureSymbolTable) expressionManager.getSymbolTable(DataManager.DAL_SYMBOL_TABLE_FEATURE
|
116 |
); |
117 |
this.symbolTable = expressionManager.createSymbolTable();
|
118 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
119 |
} |
120 |
return this.symbolTable; |
121 |
} |
122 |
|
123 |
private FeatureSymbolTable getFeatureSymbolTable() {
|
124 |
if (this.featureSymbolTable == null) { |
125 |
this.getSymbolTable();
|
126 |
} |
127 |
return this.featureSymbolTable; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public boolean isValid() { |
132 |
return this.valid; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public Object get(Feature feature) { |
137 |
if (!this.valid) { |
138 |
return null; |
139 |
} |
140 |
this.getFeatureSymbolTable().setFeature(feature);
|
141 |
try {
|
142 |
Object result = this.expression.execute(this.getSymbolTable()); |
143 |
return result;
|
144 |
} catch(Exception ex) { |
145 |
if( this.enableExceptions ) { |
146 |
throw ex;
|
147 |
} |
148 |
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 |
return null; |
162 |
} |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void set(EditableFeature feature, Object value) { |
167 |
// Do nothing
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public boolean allowSetting() { |
172 |
return false; |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public String[] getRequiredFieldNames() { |
177 |
return this.requiredFields; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public Expression getExpression() { |
182 |
return this.expression; |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public String getErrorMessage() { |
187 |
return this.errorMessage; |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public void saveToState(PersistentState state) throws PersistenceException { |
192 |
state.set("fields", this.requiredFields); |
193 |
state.set("expression", this.expression); |
194 |
state.set("valid", this.valid); |
195 |
|
196 |
} |
197 |
|
198 |
@Override
|
199 |
public void loadFromState(PersistentState state) throws PersistenceException { |
200 |
this.valid = state.getBoolean("valid"); |
201 |
this.expression = (Expression) state.get("expression"); |
202 |
this.requiredFields = state.getStringArray("fields"); |
203 |
} |
204 |
|
205 |
@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 |
public static void registerPersistenceDefinition() { |
218 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
219 |
|
220 |
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 |
+ " persistent definition",
|
226 |
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 |
} |
242 |
|
243 |
} |