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 / fmap / dal / feature / impl / DefaultFeatureRules.java @ 47722
History | View | Annotate | Download (8.97 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl; |
25 | |||
26 | import java.util.ArrayList; |
||
27 | 44871 | jjdelcerro | import java.util.Iterator; |
28 | 46737 | jjdelcerro | import javax.json.JsonArray; |
29 | import javax.json.JsonObject; |
||
30 | import javax.json.JsonValue; |
||
31 | 44871 | jjdelcerro | import org.gvsig.expressionevaluator.Expression; |
32 | import org.gvsig.expressionevaluator.impl.DefaultFeatureRuleExpression; |
||
33 | 40435 | jjdelcerro | |
34 | import org.gvsig.fmap.dal.exception.DataException; |
||
35 | 45739 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeature; |
36 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
37 | 45739 | jjdelcerro | import static org.gvsig.fmap.dal.feature.Feature.CHECK_BASIC; |
38 | import static org.gvsig.fmap.dal.feature.Feature.CHECK_REQUIREDS; |
||
39 | import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_EDITING; |
||
40 | import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_FINISH; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
42 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureRule; |
43 | import org.gvsig.fmap.dal.feature.FeatureRules; |
||
44 | 41251 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStore; |
45 | 46737 | jjdelcerro | import org.gvsig.json.Json; |
46 | import org.gvsig.json.JsonArrayBuilder; |
||
47 | import org.gvsig.json.JsonObjectBuilder; |
||
48 | import org.gvsig.json.SupportJson; |
||
49 | 45739 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
50 | 44871 | jjdelcerro | import org.gvsig.tools.persistence.PersistentState; |
51 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
52 | 41251 | jjdelcerro | import org.slf4j.Logger; |
53 | import org.slf4j.LoggerFactory; |
||
54 | 40435 | jjdelcerro | |
55 | 46737 | jjdelcerro | public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules, SupportJson { |
56 | 40435 | jjdelcerro | |
57 | 45739 | jjdelcerro | private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class); |
58 | 40435 | jjdelcerro | |
59 | 45739 | jjdelcerro | /**
|
60 | *
|
||
61 | */
|
||
62 | private static final long serialVersionUID = -8084546505498274121L; |
||
63 | 40435 | jjdelcerro | |
64 | 45739 | jjdelcerro | @Override
|
65 | public boolean add(FeatureRule rule) { |
||
66 | return super.add(rule); |
||
67 | } |
||
68 | 40435 | jjdelcerro | |
69 | 45739 | jjdelcerro | @Override
|
70 | public boolean add(String name, String description, boolean checkAtUpdate, boolean checkAtFinishEdition, Expression expression) { |
||
71 | FeatureRule rule = new DefaultFeatureRuleExpression(name, description, checkAtUpdate, checkAtFinishEdition, expression);
|
||
72 | return this.add(rule); |
||
73 | } |
||
74 | 40435 | jjdelcerro | |
75 | 45739 | jjdelcerro | @Override
|
76 | public FeatureRule getRule(int index) { |
||
77 | return (FeatureRule) super.get(index); |
||
78 | } |
||
79 | 40435 | jjdelcerro | |
80 | 45739 | jjdelcerro | @Override
|
81 | public boolean remove(FeatureRule rule) { |
||
82 | return super.remove(rule); |
||
83 | } |
||
84 | 40435 | jjdelcerro | |
85 | 45739 | jjdelcerro | @Override
|
86 | public FeatureRules getCopy() {
|
||
87 | DefaultFeatureRules copy = new DefaultFeatureRules();
|
||
88 | copy.addAll(this);
|
||
89 | return copy;
|
||
90 | } |
||
91 | 40435 | jjdelcerro | |
92 | 45739 | jjdelcerro | public void validate(Feature feature, int mode) throws DataException { |
93 | if (feature == null) { |
||
94 | return;
|
||
95 | } |
||
96 | if (feature instanceof EditableFeature) { |
||
97 | this.validate((EditableFeature) feature, mode);
|
||
98 | return;
|
||
99 | } |
||
100 | this.validate(feature.getEditable(), mode);
|
||
101 | 44871 | jjdelcerro | } |
102 | 40435 | jjdelcerro | |
103 | 45739 | jjdelcerro | public void validate(EditableFeature feature, int check) throws DataException { |
104 | if (feature == null) { |
||
105 | return;
|
||
106 | } |
||
107 | if ((check & CHECK_REQUIREDS) == CHECK_REQUIREDS) {
|
||
108 | checkRequireds(feature); |
||
109 | } |
||
110 | if ((check & CHECK_BASIC) == CHECK_BASIC) {
|
||
111 | checkBasics(feature); |
||
112 | } |
||
113 | if ((check & CHECK_RULES_AT_FINISH) == CHECK_RULES_AT_FINISH) {
|
||
114 | FeatureStore store = ((DefaultFeature) feature).getStore(); |
||
115 | for (FeatureRule rule : this) { |
||
116 | 47722 | jjdelcerro | if( rule == null ) { |
117 | continue;
|
||
118 | } |
||
119 | 45739 | jjdelcerro | if (rule.checkAtFinishEditing()) {
|
120 | rule.validate(feature, store); |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | if ((check & CHECK_RULES_AT_EDITING) == CHECK_RULES_AT_EDITING) {
|
||
125 | FeatureStore store = ((DefaultFeature) feature).getStore(); |
||
126 | for (FeatureRule rule : this) { |
||
127 | 47722 | jjdelcerro | if( rule == null ) { |
128 | continue;
|
||
129 | } |
||
130 | 45739 | jjdelcerro | if (rule.checkAtUpdate()) {
|
131 | rule.validate(feature, store); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | 40435 | jjdelcerro | |
137 | 45739 | jjdelcerro | private void checkRequireds(Feature feature) { |
138 | for (FeatureAttributeDescriptor attr : feature.getType()) {
|
||
139 | 47102 | fdiaz | if (attr.isAutomatic() || attr.isComputed()) {
|
140 | 45739 | jjdelcerro | continue;
|
141 | } |
||
142 | if (attr.isPrimaryKey() || !attr.allowNull()) {
|
||
143 | if (feature.get(attr.getIndex()) == null) { |
||
144 | String featstr = "unknown"; |
||
145 | String storename = "unknown"; |
||
146 | try {
|
||
147 | featstr = feature.toJsonBuilder().toString(); |
||
148 | } catch (Throwable th) { |
||
149 | LOGGER.debug("Can't convert feature to string", th);
|
||
150 | } |
||
151 | try {
|
||
152 | storename = feature.getStore().getName(); |
||
153 | } catch (Throwable th) { |
||
154 | LOGGER.debug("Can't convert feature to string", th);
|
||
155 | } |
||
156 | throw new IllegalStateException( |
||
157 | String.format(
|
||
158 | "The field '%s' in store '%s' can't have null values (%s).",
|
||
159 | attr.getName(), |
||
160 | storename, |
||
161 | featstr |
||
162 | ) |
||
163 | ); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | 41251 | jjdelcerro | |
169 | 45739 | jjdelcerro | private void checkBasics(Feature feature) { |
170 | for (FeatureAttributeDescriptor attr : feature.getType()) {
|
||
171 | 47102 | fdiaz | if(attr.isComputed()){
|
172 | continue;
|
||
173 | } |
||
174 | 45739 | jjdelcerro | try {
|
175 | 47102 | fdiaz | Object value = feature.get(attr.getName());
|
176 | attr.validate(value); |
||
177 | // DisposeUtils.disposeQuietly(value);
|
||
178 | 45739 | jjdelcerro | } catch (DynFieldValidateException ex) {
|
179 | String featstr = "unknown"; |
||
180 | try {
|
||
181 | featstr = feature.toJsonBuilder().toString(); |
||
182 | } catch (Throwable th) { |
||
183 | LOGGER.debug("Can't convert feature to string", th);
|
||
184 | } |
||
185 | String storename = "unknown"; |
||
186 | try {
|
||
187 | storename = feature.getStore().getName(); |
||
188 | } catch (Throwable th) { |
||
189 | LOGGER.debug("Can't convert feature to string", th);
|
||
190 | } |
||
191 | throw new IllegalStateException( |
||
192 | String.format(
|
||
193 | "The field '%s' in store '%s' can't have a invalid value. %s. (%s).",
|
||
194 | attr.getName(), |
||
195 | storename, |
||
196 | ex.getMessage(), |
||
197 | featstr |
||
198 | ) |
||
199 | ); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | 41251 | jjdelcerro | |
204 | 45739 | jjdelcerro | |
205 | @Override
|
||
206 | public void saveToState(PersistentState state) throws PersistenceException { |
||
207 | state.set("rules", this.iterator()); |
||
208 | } |
||
209 | |||
210 | @Override
|
||
211 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
212 | this.clear();
|
||
213 | Iterator<FeatureRule> it = state.getIterator("rules"); |
||
214 | while (it.hasNext()) {
|
||
215 | FeatureRule rule = it.next(); |
||
216 | 47722 | jjdelcerro | if( rule!=null ) { |
217 | this.add(rule);
|
||
218 | } |
||
219 | 45739 | jjdelcerro | } |
220 | } |
||
221 | |||
222 | 46737 | jjdelcerro | @Override
|
223 | public void fromJson(JsonObject json) { |
||
224 | JsonArray items = json.getJsonArray("items");
|
||
225 | if( items!=null ) { |
||
226 | for (JsonValue item : items) {
|
||
227 | FeatureRule rule = (FeatureRule) Json.toObject(item); |
||
228 | 47722 | jjdelcerro | if( rule != null ) { |
229 | this.add(rule);
|
||
230 | } |
||
231 | 46737 | jjdelcerro | } |
232 | } |
||
233 | } |
||
234 | |||
235 | @Override
|
||
236 | public JsonObject toJson() {
|
||
237 | return this.toJsonBuilder().build(); |
||
238 | } |
||
239 | |||
240 | @Override
|
||
241 | public JsonObjectBuilder toJsonBuilder() {
|
||
242 | JsonArrayBuilder arraybuilder = Json.createArrayBuilder(); |
||
243 | for (FeatureRule rule : this) { |
||
244 | if( rule instanceof SupportJson ) { |
||
245 | arraybuilder.add(((SupportJson)rule).toJsonBuilder()); |
||
246 | } |
||
247 | } |
||
248 | JsonObjectBuilder builder = Json.createObjectBuilder(); |
||
249 | builder.add_class(this);
|
||
250 | builder.add("items", arraybuilder);
|
||
251 | return builder;
|
||
252 | } |
||
253 | |||
254 | public static void selfRegister() { |
||
255 | Json.registerSerializer(DefaultFeatureRules.class); |
||
256 | } |
||
257 | |||
258 | 40435 | jjdelcerro | } |