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