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/fmap/dal/feature/impl/DefaultFeatureRules.java
DefaultFeatureRules.java | ||
---|---|---|
29 | 29 |
import org.gvsig.expressionevaluator.impl.DefaultFeatureRuleExpression; |
30 | 30 |
|
31 | 31 |
import org.gvsig.fmap.dal.exception.DataException; |
32 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
32 | 33 |
import org.gvsig.fmap.dal.feature.Feature; |
34 |
import static org.gvsig.fmap.dal.feature.Feature.CHECK_BASIC; |
|
35 |
import static org.gvsig.fmap.dal.feature.Feature.CHECK_REQUIREDS; |
|
36 |
import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_EDITING; |
|
37 |
import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_FINISH; |
|
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
33 | 39 |
import org.gvsig.fmap.dal.feature.FeatureRule; |
34 | 40 |
import org.gvsig.fmap.dal.feature.FeatureRules; |
35 | 41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
|
36 | 43 |
import org.gvsig.tools.persistence.PersistentState; |
37 | 44 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
38 | 45 |
import org.slf4j.Logger; |
... | ... | |
40 | 47 |
|
41 | 48 |
public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules { |
42 | 49 |
|
43 |
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class); |
|
50 |
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class);
|
|
44 | 51 |
|
45 |
/** |
|
46 |
* |
|
47 |
*/ |
|
48 |
private static final long serialVersionUID = -8084546505498274121L; |
|
52 |
/**
|
|
53 |
*
|
|
54 |
*/
|
|
55 |
private static final long serialVersionUID = -8084546505498274121L;
|
|
49 | 56 |
|
50 |
@Override |
|
51 |
public boolean add(FeatureRule rule) { |
|
52 |
return super.add(rule); |
|
53 |
} |
|
57 |
@Override
|
|
58 |
public boolean add(FeatureRule rule) {
|
|
59 |
return super.add(rule);
|
|
60 |
}
|
|
54 | 61 |
|
55 |
@Override |
|
56 |
public boolean add(String name, String description, boolean checkAtUpdate, boolean checkAtFinishEdition, Expression expression) { |
|
57 |
FeatureRule rule = new DefaultFeatureRuleExpression(name, description, checkAtUpdate, checkAtFinishEdition, expression); |
|
58 |
return this.add(rule); |
|
59 |
} |
|
62 |
@Override
|
|
63 |
public boolean add(String name, String description, boolean checkAtUpdate, boolean checkAtFinishEdition, Expression expression) {
|
|
64 |
FeatureRule rule = new DefaultFeatureRuleExpression(name, description, checkAtUpdate, checkAtFinishEdition, expression);
|
|
65 |
return this.add(rule);
|
|
66 |
}
|
|
60 | 67 |
|
61 |
@Override |
|
62 |
public FeatureRule getRule(int index) { |
|
63 |
return (FeatureRule) super.get(index); |
|
64 |
} |
|
68 |
@Override
|
|
69 |
public FeatureRule getRule(int index) {
|
|
70 |
return (FeatureRule) super.get(index);
|
|
71 |
}
|
|
65 | 72 |
|
66 |
@Override |
|
67 |
public boolean remove(FeatureRule rule) { |
|
68 |
return super.remove(rule); |
|
69 |
} |
|
73 |
@Override
|
|
74 |
public boolean remove(FeatureRule rule) {
|
|
75 |
return super.remove(rule);
|
|
76 |
}
|
|
70 | 77 |
|
71 |
@Override |
|
72 |
public FeatureRules getCopy() { |
|
73 |
DefaultFeatureRules copy = new DefaultFeatureRules(); |
|
74 |
copy.addAll(this); |
|
75 |
return copy; |
|
76 |
} |
|
78 |
@Override
|
|
79 |
public FeatureRules getCopy() {
|
|
80 |
DefaultFeatureRules copy = new DefaultFeatureRules();
|
|
81 |
copy.addAll(this);
|
|
82 |
return copy;
|
|
83 |
}
|
|
77 | 84 |
|
78 |
// /** |
|
79 |
// * @deprecated use validate(Feature feature, int mode) |
|
80 |
// * @param feature |
|
81 |
// * @throws DataException |
|
82 |
// */ |
|
83 |
// public void validate(Feature feature) throws DataException { |
|
84 |
// logger.warn("Calling deprecated method validate without mode."); |
|
85 |
// FeatureStore store = ((DefaultFeature)feature).getStore(); |
|
86 |
// for( FeatureRule rule : this ) { |
|
87 |
// rule.validate(feature, store); |
|
88 |
// } |
|
89 |
// |
|
90 |
// } |
|
91 |
public void validate(Feature feature, int mode) throws DataException { |
|
92 |
FeatureStore store = ((DefaultFeature) feature).getStore(); |
|
93 |
for (FeatureRule rule : this) { |
|
94 |
if (rule.checkAtFinishEditing() && mode == Feature.FINISH_EDITING) { |
|
95 |
rule.validate(feature, store); |
|
96 |
} |
|
97 |
if (rule.checkAtUpdate() && mode == Feature.UPDATE) { |
|
98 |
rule.validate(feature, store); |
|
99 |
} |
|
85 |
public void validate(Feature feature, int mode) throws DataException { |
|
86 |
if (feature == null) { |
|
87 |
return; |
|
88 |
} |
|
89 |
if (feature instanceof EditableFeature) { |
|
90 |
this.validate((EditableFeature) feature, mode); |
|
91 |
return; |
|
92 |
} |
|
93 |
this.validate(feature.getEditable(), mode); |
|
100 | 94 |
} |
101 | 95 |
|
102 |
} |
|
96 |
public void validate(EditableFeature feature, int check) throws DataException { |
|
97 |
if (feature == null) { |
|
98 |
return; |
|
99 |
} |
|
100 |
if ((check & CHECK_REQUIREDS) == CHECK_REQUIREDS) { |
|
101 |
checkRequireds(feature); |
|
102 |
} |
|
103 |
if ((check & CHECK_BASIC) == CHECK_BASIC) { |
|
104 |
checkBasics(feature); |
|
105 |
} |
|
106 |
if ((check & CHECK_RULES_AT_FINISH) == CHECK_RULES_AT_FINISH) { |
|
107 |
FeatureStore store = ((DefaultFeature) feature).getStore(); |
|
108 |
for (FeatureRule rule : this) { |
|
109 |
if (rule.checkAtFinishEditing()) { |
|
110 |
rule.validate(feature, store); |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
if ((check & CHECK_RULES_AT_EDITING) == CHECK_RULES_AT_EDITING) { |
|
115 |
FeatureStore store = ((DefaultFeature) feature).getStore(); |
|
116 |
for (FeatureRule rule : this) { |
|
117 |
if (rule.checkAtUpdate()) { |
|
118 |
rule.validate(feature, store); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
103 | 123 |
|
104 |
public static void selfRegister() { |
|
105 |
|
|
106 |
} |
|
124 |
private void checkRequireds(Feature feature) { |
|
125 |
for (FeatureAttributeDescriptor attr : feature.getType()) { |
|
126 |
if (attr.isAutomatic()) { |
|
127 |
continue; |
|
128 |
} |
|
129 |
if (attr.isPrimaryKey() || !attr.allowNull()) { |
|
130 |
if (feature.get(attr.getIndex()) == null) { |
|
131 |
String featstr = "unknown"; |
|
132 |
String storename = "unknown"; |
|
133 |
try { |
|
134 |
featstr = feature.toJsonBuilder().toString(); |
|
135 |
} catch (Throwable th) { |
|
136 |
LOGGER.debug("Can't convert feature to string", th); |
|
137 |
} |
|
138 |
try { |
|
139 |
storename = feature.getStore().getName(); |
|
140 |
} catch (Throwable th) { |
|
141 |
LOGGER.debug("Can't convert feature to string", th); |
|
142 |
} |
|
143 |
throw new IllegalStateException( |
|
144 |
String.format( |
|
145 |
"The field '%s' in store '%s' can't have null values (%s).", |
|
146 |
attr.getName(), |
|
147 |
storename, |
|
148 |
featstr |
|
149 |
) |
|
150 |
); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
107 | 155 |
|
108 |
@Override |
|
109 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
110 |
state.set("rules", this.iterator()); |
|
111 |
} |
|
156 |
private void checkBasics(Feature feature) { |
|
157 |
for (FeatureAttributeDescriptor attr : feature.getType()) { |
|
158 |
try { |
|
159 |
attr.validate(feature); |
|
160 |
} catch (DynFieldValidateException ex) { |
|
161 |
String featstr = "unknown"; |
|
162 |
try { |
|
163 |
featstr = feature.toJsonBuilder().toString(); |
|
164 |
} catch (Throwable th) { |
|
165 |
LOGGER.debug("Can't convert feature to string", th); |
|
166 |
} |
|
167 |
String storename = "unknown"; |
|
168 |
try { |
|
169 |
storename = feature.getStore().getName(); |
|
170 |
} catch (Throwable th) { |
|
171 |
LOGGER.debug("Can't convert feature to string", th); |
|
172 |
} |
|
173 |
throw new IllegalStateException( |
|
174 |
String.format( |
|
175 |
"The field '%s' in store '%s' can't have a invalid value. %s. (%s).", |
|
176 |
attr.getName(), |
|
177 |
storename, |
|
178 |
ex.getMessage(), |
|
179 |
featstr |
|
180 |
) |
|
181 |
); |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
112 | 185 |
|
113 |
@Override |
|
114 |
public void loadFromState(PersistentState state) throws PersistenceException { |
|
115 |
this.clear(); |
|
116 |
Iterator<FeatureRule> it = state.getIterator("rules"); |
|
117 |
while (it.hasNext()) { |
|
118 |
FeatureRule rule = it.next(); |
|
119 |
this.add(rule); |
|
186 |
public static void selfRegister() { |
|
187 |
|
|
120 | 188 |
} |
121 |
} |
|
122 |
|
|
189 |
|
|
190 |
@Override |
|
191 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
192 |
state.set("rules", this.iterator()); |
|
193 |
} |
|
194 |
|
|
195 |
@Override |
|
196 |
public void loadFromState(PersistentState state) throws PersistenceException { |
|
197 |
this.clear(); |
|
198 |
Iterator<FeatureRule> it = state.getIterator("rules"); |
|
199 |
while (it.hasNext()) { |
|
200 |
FeatureRule rule = it.next(); |
|
201 |
this.add(rule); |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
123 | 205 |
} |
Also available in: Unified diff