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