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 / expressionevaluator / impl / DefaultFeatureRuleExpression.java @ 47723
History | View | Annotate | Download (8.16 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import javax.json.JsonObject; |
4 |
import org.gvsig.expressionevaluator.Code; |
5 |
import org.gvsig.expressionevaluator.Expression; |
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
7 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
8 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
9 |
import org.gvsig.expressionevaluator.Interpreter; |
10 |
import org.gvsig.expressionevaluator.Optimizer; |
11 |
import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
12 |
import org.gvsig.fmap.dal.exception.DataException; |
13 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
14 |
import org.gvsig.fmap.dal.feature.AbstractFeatureRule; |
15 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
16 |
import org.gvsig.fmap.dal.feature.FeatureRuleExpression; |
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
18 |
import org.gvsig.fmap.dal.feature.exception.ValidateFeaturesException; |
19 |
import org.gvsig.json.Json; |
20 |
import org.gvsig.json.JsonObjectBuilder; |
21 |
import org.gvsig.tools.ToolsLocator; |
22 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
23 |
import org.gvsig.tools.dynobject.DynStruct; |
24 |
import org.gvsig.tools.persistence.PersistenceManager; |
25 |
import org.gvsig.tools.persistence.PersistentState; |
26 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author gvSIG team
|
31 |
*/
|
32 |
public class DefaultFeatureRuleExpression |
33 |
extends AbstractFeatureRule
|
34 |
implements FeatureRuleExpression {
|
35 |
|
36 |
private static final String FEATURERULEEXPRESSION_PERSISTENCE_DEFINITION_NAME = "FeatureRuleExpression"; |
37 |
|
38 |
private Expression expression; |
39 |
private FeatureSymbolTable featureSymbolTable;
|
40 |
private Interpreter interpreter;
|
41 |
private ExpressionEvaluatorManager manager;
|
42 |
|
43 |
public DefaultFeatureRuleExpression() {
|
44 |
super(null, null, false, false); |
45 |
} |
46 |
|
47 |
public DefaultFeatureRuleExpression(String name, String description, boolean checkAtUpdate, |
48 |
boolean checkAtFinishEdition, Expression expression) { |
49 |
super(name, description, checkAtUpdate, checkAtFinishEdition);
|
50 |
this.expression = expression;
|
51 |
} |
52 |
|
53 |
@Override
|
54 |
public void setName(String name) { |
55 |
this.name = name;
|
56 |
} |
57 |
|
58 |
@Override
|
59 |
public void setDescription(String description) { |
60 |
this.description = description;
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
public void validate(EditableFeature feature, FeatureStore featureStore) throws DataException { |
65 |
Object value = this.execute(feature); |
66 |
boolean ok = false; |
67 |
if (value != null) { |
68 |
if (value instanceof Boolean) { |
69 |
ok = (Boolean) value;
|
70 |
} else {
|
71 |
ok = DataTypeUtils.toBoolean(value, false);
|
72 |
} |
73 |
} |
74 |
if (ok) {
|
75 |
return;
|
76 |
} |
77 |
throw new ValidateFeaturesException(featureStore.getName(), null); |
78 |
} |
79 |
|
80 |
public Object execute(EditableFeature feature) { |
81 |
if (this.interpreter == null) { |
82 |
this.manager = ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
|
83 |
this.interpreter = this.manager.createInterpreter(); |
84 |
this.featureSymbolTable = new FeatureSymbolTableImpl(); |
85 |
this.featureSymbolTable.addSymbolTable(this.manager.createSymbolTable()); |
86 |
this.interpreter.setSymbolTable(this.featureSymbolTable); |
87 |
Optimizer optimizer = new DefaultOptimizer(this.manager, featureSymbolTable); |
88 |
Code code = optimizer.optimize(this.expression.getCode());
|
89 |
this.interpreter.link(code);
|
90 |
} |
91 |
this.featureSymbolTable.setFeature(feature);
|
92 |
Code code_exp = this.expression.getCode();
|
93 |
if( this.manager.hasHostExpressions(code_exp) ) { |
94 |
code_exp = this.manager.resolveHostExpressions(code_exp, interpreter);
|
95 |
} |
96 |
Object x = this.interpreter.run(code_exp); |
97 |
return x;
|
98 |
} |
99 |
|
100 |
|
101 |
public static void selfRegister() { |
102 |
Json.registerSerializer(DefaultFeatureRuleExpression.class); |
103 |
registerPersistenceDefinition(); |
104 |
|
105 |
} |
106 |
|
107 |
private static void registerPersistenceDefinition() { |
108 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
109 |
|
110 |
if (manager.getDefinition(FEATURERULEEXPRESSION_PERSISTENCE_DEFINITION_NAME)
|
111 |
== null) {
|
112 |
DynStruct definition = manager.addDefinition(DefaultFeatureRuleExpression.class, |
113 |
FEATURERULEEXPRESSION_PERSISTENCE_DEFINITION_NAME, |
114 |
FEATURERULEEXPRESSION_PERSISTENCE_DEFINITION_NAME |
115 |
+ " persistent definition",
|
116 |
null,
|
117 |
null
|
118 |
); |
119 |
definition.addDynFieldString("name");
|
120 |
definition.addDynFieldString("description");
|
121 |
definition.addDynFieldBoolean("checkAtFinishEdition");
|
122 |
definition.addDynFieldBoolean("checkAtUpdate");
|
123 |
definition.addDynFieldObject("expression").setClassOfValue(Expression.class); |
124 |
definition.addDynFieldInt("checkWhen").setMandatory(false); |
125 |
} |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public void loadFromState(PersistentState state) throws PersistenceException { |
130 |
this.name = state.getString("name"); |
131 |
this.description = state.getString("description"); |
132 |
this.checkwhen.set(state.getInt("checkWhen",0)); |
133 |
if( state.getBoolean("checkAtFinishEdition") ) { |
134 |
this.checkwhen.setBits(CHECK_WHEN_FINISH_EDITING);
|
135 |
} |
136 |
if( state.getBoolean("checkAtUpdate") ) { |
137 |
this.checkwhen.setBits(CHECK_WHEN_INSERT_OR_UPDATE_FEATURE);
|
138 |
} |
139 |
this.expression = (Expression) state.get("expression"); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void saveToState(PersistentState state) throws PersistenceException { |
144 |
state.set("name", this.name); |
145 |
state.set("description", this.description); |
146 |
state.set("checkWhen", this.checkwhen.get()); |
147 |
state.set("checkAtFinishEdition", this.checkwhen.isSetBits(CHECK_WHEN_FINISH_EDITING)); |
148 |
state.set("checkAtUpdate", this.checkwhen.isSetBits(CHECK_WHEN_INSERT_OR_UPDATE_FEATURE)); |
149 |
state.set("expression", this.expression); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public void setCheckAtUpdate(boolean checkAtUpdate) { |
154 |
if( checkAtUpdate ) {
|
155 |
this.checkwhen.setBits(CHECK_WHEN_INSERT_OR_UPDATE_FEATURE);
|
156 |
} else {
|
157 |
this.checkwhen.clearBits(CHECK_WHEN_INSERT_OR_UPDATE_FEATURE);
|
158 |
} |
159 |
} |
160 |
|
161 |
@Override
|
162 |
public void setCheckAtFinishEditing(boolean checkAtFinishEditing) { |
163 |
if( checkAtFinishEditing ) {
|
164 |
this.checkwhen.setBits(CHECK_WHEN_FINISH_EDITING);
|
165 |
} else {
|
166 |
this.checkwhen.clearBits(CHECK_WHEN_FINISH_EDITING);
|
167 |
} |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public Expression getExpression() { |
172 |
return this.expression; |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public void setExpression(Expression expression) { |
177 |
this.expression = expression;
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public JsonObject toJson() {
|
182 |
JsonObjectBuilder builder = this.toJsonBuilder();
|
183 |
return builder.build();
|
184 |
} |
185 |
|
186 |
@Override
|
187 |
public JsonObjectBuilder toJsonBuilder() {
|
188 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
189 |
builder.add_class(this);
|
190 |
builder.add("name", this.name); |
191 |
builder.add("description", this.description); |
192 |
builder.add("checkWhen", this.checkwhen.get()); |
193 |
builder.add("checkAtFinishEdition", this.checkwhen.isSetBits(CHECK_WHEN_FINISH_EDITING)); |
194 |
builder.add("checkAtUpdate", this.checkwhen.isSetBits(CHECK_WHEN_INSERT_OR_UPDATE_FEATURE)); |
195 |
builder.add("expression", this.expression.getPhrase()); |
196 |
return builder;
|
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void fromJson(JsonObject json) { |
201 |
this.name = json.getString("name"); |
202 |
this.description = json.getString("description"); |
203 |
this.checkwhen.set(json.getInt("checkWhen", 0)); |
204 |
if( json.getBoolean("checkAtFinishEdition", false) ) { |
205 |
this.checkwhen.setBits(CHECK_WHEN_FINISH_EDITING);
|
206 |
} |
207 |
if( json.getBoolean("checkAtUpdate", false) ) { |
208 |
this.checkwhen.setBits(CHECK_WHEN_INSERT_OR_UPDATE_FEATURE);
|
209 |
} |
210 |
this.expression = ExpressionUtils.createExpression(json.getString("expression", null)); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public void setCheckWhen(int mask) { |
215 |
super.setCheckWhen(mask);
|
216 |
} |
217 |
|
218 |
} |