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