Revision 45739 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
DefaultFeatureRuleExpression.java | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.impl; |
2 | 2 |
|
3 |
import javax.json.JsonObject; |
|
3 | 4 |
import org.gvsig.expressionevaluator.Expression; |
5 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
4 | 6 |
import org.gvsig.expressionevaluator.impl.symboltable.FeatureSymbolTableImpl; |
5 | 7 |
import org.gvsig.fmap.dal.exception.DataException; |
6 | 8 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
7 | 9 |
import org.gvsig.fmap.dal.feature.AbstractFeatureRule; |
8 |
import org.gvsig.fmap.dal.feature.Feature; |
|
10 |
import org.gvsig.fmap.dal.feature.EditableFeature;
|
|
9 | 11 |
import org.gvsig.fmap.dal.feature.FeatureRuleExpression; |
10 | 12 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
11 | 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; |
|
12 | 17 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
18 |
import org.gvsig.tools.dynobject.DynStruct; |
|
19 |
import org.gvsig.tools.persistence.PersistenceManager; |
|
13 | 20 |
import org.gvsig.tools.persistence.PersistentState; |
14 | 21 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
15 | 22 |
|
... | ... | |
17 | 24 |
* |
18 | 25 |
* @author gvSIG team |
19 | 26 |
*/ |
20 |
public class DefaultFeatureRuleExpression |
|
21 |
extends AbstractFeatureRule |
|
22 |
implements FeatureRuleExpression |
|
23 |
{ |
|
27 |
public class DefaultFeatureRuleExpression |
|
28 |
extends AbstractFeatureRule |
|
29 |
implements FeatureRuleExpression { |
|
24 | 30 |
|
25 |
private Expression expression; |
|
26 |
private FeatureSymbolTable symbolTable; |
|
31 |
private static final String FEATURERULEEXPRESSION_PERSISTENCE_DEFINITION_NAME = "FeatureRuleExpression"; |
|
27 | 32 |
|
28 |
public DefaultFeatureRuleExpression(String name, String description, boolean checkAtUpdate, |
|
29 |
boolean checkAtFinishEdition, Expression expression) { |
|
30 |
super(name, description, checkAtUpdate, checkAtFinishEdition); |
|
31 |
this.expression = expression; |
|
32 |
this.symbolTable = new FeatureSymbolTableImpl(); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public void validate(Feature feature, FeatureStore featureStore) throws DataException { |
|
37 |
this.symbolTable.setFeature(feature); |
|
38 |
Object value = this.expression.execute(symbolTable); |
|
39 |
boolean ok = false; |
|
40 |
if( value!=null ) { |
|
41 |
if( value instanceof Boolean ) { |
|
42 |
ok = (Boolean)value; |
|
43 |
} else { |
|
44 |
ok = DataTypeUtils.toBoolean(value, false); |
|
45 |
} |
|
33 |
private Expression expression; |
|
34 |
private FeatureSymbolTable symbolTable; |
|
35 |
|
|
36 |
public DefaultFeatureRuleExpression() { |
|
37 |
super(null, null, false, false); |
|
46 | 38 |
} |
47 |
if( ok ) { |
|
48 |
return; |
|
39 |
|
|
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(); |
|
49 | 45 |
} |
50 |
throw new ValidateFeaturesException(null, featureStore.getName()); |
|
51 |
} |
|
52 |
|
|
53 |
public static void selfRegister() { |
|
54 |
|
|
55 |
} |
|
56 | 46 |
|
57 |
@Override |
|
58 |
public void loadFromState(PersistentState state) throws PersistenceException { |
|
59 |
this.name = state.getString("name"); |
|
60 |
this.description = state.getString("description"); |
|
61 |
this.checkAtFinishEdition = state.getBoolean("checkAtFinishEdition"); |
|
62 |
this.checkAtUpdate = state.getBoolean("checkAtUpdate"); |
|
63 |
this.expression = (Expression) state.get("expression"); |
|
64 |
this.symbolTable = new FeatureSymbolTableImpl(); |
|
65 |
} |
|
47 |
@Override |
|
48 |
public void setName(String name) { |
|
49 |
this.name = name; |
|
50 |
} |
|
66 | 51 |
|
67 |
@Override |
|
68 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
69 |
state.set("name", this.name); |
|
70 |
state.set("description", this.description); |
|
71 |
state.set("checkAtFinishEdition", this.checkAtFinishEdition); |
|
72 |
state.set("checkAtUpdate", this.checkAtUpdate); |
|
73 |
state.set("expression", this.expression); |
|
74 |
} |
|
52 |
@Override |
|
53 |
public void setDescription(String description) { |
|
54 |
this.description = description; |
|
55 |
} |
|
75 | 56 |
|
76 |
@Override |
|
77 |
public void setCheckAtUpdate(boolean checkAtUpdate) { |
|
78 |
this.checkAtUpdate = checkAtUpdate; |
|
79 |
} |
|
57 |
@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 |
} |
|
80 | 74 |
|
81 |
@Override |
|
82 |
public void setCheckAtFinishEditing(boolean checkAtFinishEditing) { |
|
83 |
this.checkAtFinishEdition = checkAtFinishEditing; |
|
84 |
} |
|
75 |
public static void selfRegister() { |
|
76 |
Json.registerSerializer(FeatureRuleExpression.class); |
|
77 |
registerPersistenceDefinition(); |
|
85 | 78 |
|
86 |
@Override |
|
87 |
public Expression getExpression() { |
|
88 |
return this.expression; |
|
89 |
} |
|
79 |
} |
|
90 | 80 |
|
91 |
@Override |
|
92 |
public void setExpression(Expression expression) { |
|
93 |
this.expression = expression; |
|
94 |
} |
|
95 |
|
|
81 |
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 |
|
|
96 | 167 |
} |
Also available in: Unified diff