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 |
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 |
public boolean isEnableExceptions() { |
102 |
return enableExceptions;
|
103 |
} |
104 |
|
105 |
public void setEnableExceptions(boolean enableExceptions) { |
106 |
this.enableExceptions = enableExceptions;
|
107 |
} |
108 |
|
109 |
@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 |
); |
116 |
this.symbolTable = expressionManager.createSymbolTable();
|
117 |
this.symbolTable.addSymbolTable(this.featureSymbolTable); |
118 |
} |
119 |
return this.symbolTable; |
120 |
} |
121 |
|
122 |
private FeatureSymbolTable getFeatureSymbolTable() {
|
123 |
if (this.featureSymbolTable == null) { |
124 |
this.getSymbolTable();
|
125 |
} |
126 |
return this.featureSymbolTable; |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public boolean isValid() { |
131 |
return this.valid; |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public Object get(Feature feature) { |
136 |
if (!this.valid) { |
137 |
return null; |
138 |
} |
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 |
this.getLogger().warn("Problems evaluating expression '"+this.expression.getPhrase()+"' with feature '"+feature.getReference().toString()+"'.", ex); |
148 |
return null; |
149 |
} |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void set(EditableFeature feature, Object value) { |
154 |
// Do nothing
|
155 |
} |
156 |
|
157 |
@Override
|
158 |
public boolean allowSetting() { |
159 |
return false; |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public String[] getRequiredFieldNames() { |
164 |
return this.requiredFields; |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public Expression getExpression() { |
169 |
return this.expression; |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public String getErrorMessage() { |
174 |
return this.errorMessage; |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public void saveToState(PersistentState state) throws PersistenceException { |
179 |
state.set("fields", this.requiredFields); |
180 |
state.set("expression", this.expression); |
181 |
state.set("valid", this.valid); |
182 |
|
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void loadFromState(PersistentState state) throws PersistenceException { |
187 |
this.valid = state.getBoolean("valid"); |
188 |
this.expression = (Expression) state.get("expression"); |
189 |
this.requiredFields = state.getStringArray("fields"); |
190 |
} |
191 |
|
192 |
@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 |
public static void registerPersistenceDefinition() { |
205 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
206 |
|
207 |
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 |
} |
229 |
|
230 |
} |