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 @ 47248
History | View | Annotate | Download (8.82 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.JsonManager; |
||
48 | import org.gvsig.json.JsonObjectBuilder; |
||
49 | import org.gvsig.json.SupportJson; |
||
50 | import org.gvsig.json.SupportToJson; |
||
51 | 47102 | fdiaz | import org.gvsig.tools.dispose.DisposeUtils; |
52 | 45739 | jjdelcerro | import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
53 | 44871 | jjdelcerro | import org.gvsig.tools.persistence.PersistentState; |
54 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
55 | 41251 | jjdelcerro | import org.slf4j.Logger; |
56 | import org.slf4j.LoggerFactory; |
||
57 | 40435 | jjdelcerro | |
58 | 46737 | jjdelcerro | public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules, SupportJson { |
59 | 40435 | jjdelcerro | |
60 | 45739 | jjdelcerro | private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class); |
61 | 40435 | jjdelcerro | |
62 | 45739 | jjdelcerro | /**
|
63 | *
|
||
64 | */
|
||
65 | private static final long serialVersionUID = -8084546505498274121L; |
||
66 | 40435 | jjdelcerro | |
67 | 45739 | jjdelcerro | @Override
|
68 | public boolean add(FeatureRule rule) { |
||
69 | return super.add(rule); |
||
70 | } |
||
71 | 40435 | jjdelcerro | |
72 | 45739 | jjdelcerro | @Override
|
73 | public boolean add(String name, String description, boolean checkAtUpdate, boolean checkAtFinishEdition, Expression expression) { |
||
74 | FeatureRule rule = new DefaultFeatureRuleExpression(name, description, checkAtUpdate, checkAtFinishEdition, expression);
|
||
75 | return this.add(rule); |
||
76 | } |
||
77 | 40435 | jjdelcerro | |
78 | 45739 | jjdelcerro | @Override
|
79 | public FeatureRule getRule(int index) { |
||
80 | return (FeatureRule) super.get(index); |
||
81 | } |
||
82 | 40435 | jjdelcerro | |
83 | 45739 | jjdelcerro | @Override
|
84 | public boolean remove(FeatureRule rule) { |
||
85 | return super.remove(rule); |
||
86 | } |
||
87 | 40435 | jjdelcerro | |
88 | 45739 | jjdelcerro | @Override
|
89 | public FeatureRules getCopy() {
|
||
90 | DefaultFeatureRules copy = new DefaultFeatureRules();
|
||
91 | copy.addAll(this);
|
||
92 | return copy;
|
||
93 | } |
||
94 | 40435 | jjdelcerro | |
95 | 45739 | jjdelcerro | public void validate(Feature feature, int mode) throws DataException { |
96 | if (feature == null) { |
||
97 | return;
|
||
98 | } |
||
99 | if (feature instanceof EditableFeature) { |
||
100 | this.validate((EditableFeature) feature, mode);
|
||
101 | return;
|
||
102 | } |
||
103 | this.validate(feature.getEditable(), mode);
|
||
104 | 44871 | jjdelcerro | } |
105 | 40435 | jjdelcerro | |
106 | 45739 | jjdelcerro | public void validate(EditableFeature feature, int check) throws DataException { |
107 | if (feature == null) { |
||
108 | return;
|
||
109 | } |
||
110 | if ((check & CHECK_REQUIREDS) == CHECK_REQUIREDS) {
|
||
111 | checkRequireds(feature); |
||
112 | } |
||
113 | if ((check & CHECK_BASIC) == CHECK_BASIC) {
|
||
114 | checkBasics(feature); |
||
115 | } |
||
116 | if ((check & CHECK_RULES_AT_FINISH) == CHECK_RULES_AT_FINISH) {
|
||
117 | FeatureStore store = ((DefaultFeature) feature).getStore(); |
||
118 | for (FeatureRule rule : this) { |
||
119 | 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 | if (rule.checkAtUpdate()) {
|
||
128 | rule.validate(feature, store); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | 40435 | jjdelcerro | |
134 | 45739 | jjdelcerro | private void checkRequireds(Feature feature) { |
135 | for (FeatureAttributeDescriptor attr : feature.getType()) {
|
||
136 | 47102 | fdiaz | if (attr.isAutomatic() || attr.isComputed()) {
|
137 | 45739 | jjdelcerro | continue;
|
138 | } |
||
139 | if (attr.isPrimaryKey() || !attr.allowNull()) {
|
||
140 | if (feature.get(attr.getIndex()) == null) { |
||
141 | String featstr = "unknown"; |
||
142 | String storename = "unknown"; |
||
143 | try {
|
||
144 | featstr = feature.toJsonBuilder().toString(); |
||
145 | } catch (Throwable th) { |
||
146 | LOGGER.debug("Can't convert feature to string", th);
|
||
147 | } |
||
148 | try {
|
||
149 | storename = feature.getStore().getName(); |
||
150 | } catch (Throwable th) { |
||
151 | LOGGER.debug("Can't convert feature to string", th);
|
||
152 | } |
||
153 | throw new IllegalStateException( |
||
154 | String.format(
|
||
155 | "The field '%s' in store '%s' can't have null values (%s).",
|
||
156 | attr.getName(), |
||
157 | storename, |
||
158 | featstr |
||
159 | ) |
||
160 | ); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | 41251 | jjdelcerro | |
166 | 45739 | jjdelcerro | private void checkBasics(Feature feature) { |
167 | for (FeatureAttributeDescriptor attr : feature.getType()) {
|
||
168 | 47102 | fdiaz | if(attr.isComputed()){
|
169 | continue;
|
||
170 | } |
||
171 | 45739 | jjdelcerro | try {
|
172 | 47102 | fdiaz | Object value = feature.get(attr.getName());
|
173 | attr.validate(value); |
||
174 | // DisposeUtils.disposeQuietly(value);
|
||
175 | 45739 | jjdelcerro | } catch (DynFieldValidateException ex) {
|
176 | String featstr = "unknown"; |
||
177 | try {
|
||
178 | featstr = feature.toJsonBuilder().toString(); |
||
179 | } catch (Throwable th) { |
||
180 | LOGGER.debug("Can't convert feature to string", th);
|
||
181 | } |
||
182 | String storename = "unknown"; |
||
183 | try {
|
||
184 | storename = feature.getStore().getName(); |
||
185 | } catch (Throwable th) { |
||
186 | LOGGER.debug("Can't convert feature to string", th);
|
||
187 | } |
||
188 | throw new IllegalStateException( |
||
189 | String.format(
|
||
190 | "The field '%s' in store '%s' can't have a invalid value. %s. (%s).",
|
||
191 | attr.getName(), |
||
192 | storename, |
||
193 | ex.getMessage(), |
||
194 | featstr |
||
195 | ) |
||
196 | ); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | 41251 | jjdelcerro | |
201 | 45739 | jjdelcerro | |
202 | @Override
|
||
203 | public void saveToState(PersistentState state) throws PersistenceException { |
||
204 | state.set("rules", this.iterator()); |
||
205 | } |
||
206 | |||
207 | @Override
|
||
208 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
209 | this.clear();
|
||
210 | Iterator<FeatureRule> it = state.getIterator("rules"); |
||
211 | while (it.hasNext()) {
|
||
212 | FeatureRule rule = it.next(); |
||
213 | this.add(rule);
|
||
214 | } |
||
215 | } |
||
216 | |||
217 | 46737 | jjdelcerro | @Override
|
218 | public void fromJson(JsonObject json) { |
||
219 | JsonArray items = json.getJsonArray("items");
|
||
220 | if( items!=null ) { |
||
221 | for (JsonValue item : items) {
|
||
222 | FeatureRule rule = (FeatureRule) Json.toObject(item); |
||
223 | this.add(rule);
|
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | @Override
|
||
229 | public JsonObject toJson() {
|
||
230 | return this.toJsonBuilder().build(); |
||
231 | } |
||
232 | |||
233 | @Override
|
||
234 | public JsonObjectBuilder toJsonBuilder() {
|
||
235 | JsonArrayBuilder arraybuilder = Json.createArrayBuilder(); |
||
236 | for (FeatureRule rule : this) { |
||
237 | if( rule instanceof SupportJson ) { |
||
238 | arraybuilder.add(((SupportJson)rule).toJsonBuilder()); |
||
239 | } |
||
240 | } |
||
241 | JsonObjectBuilder builder = Json.createObjectBuilder(); |
||
242 | builder.add_class(this);
|
||
243 | builder.add("items", arraybuilder);
|
||
244 | return builder;
|
||
245 | } |
||
246 | |||
247 | public static void selfRegister() { |
||
248 | Json.registerSerializer(DefaultFeatureRules.class); |
||
249 | } |
||
250 | |||
251 | 40435 | jjdelcerro | } |