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 @ 47421
History | View | Annotate | Download (8.82 KB)
1 |
/**
|
---|---|
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 |
package org.gvsig.fmap.dal.feature.impl; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import javax.json.JsonArray; |
29 |
import javax.json.JsonObject; |
30 |
import javax.json.JsonValue; |
31 |
import org.gvsig.expressionevaluator.Expression; |
32 |
import org.gvsig.expressionevaluator.impl.DefaultFeatureRuleExpression; |
33 |
|
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
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 |
import org.gvsig.fmap.dal.feature.FeatureRule; |
43 |
import org.gvsig.fmap.dal.feature.FeatureRules; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
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 |
import org.gvsig.tools.dispose.DisposeUtils; |
52 |
import org.gvsig.tools.dynobject.exception.DynFieldValidateException; |
53 |
import org.gvsig.tools.persistence.PersistentState; |
54 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules, SupportJson { |
59 |
|
60 |
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class); |
61 |
|
62 |
/**
|
63 |
*
|
64 |
*/
|
65 |
private static final long serialVersionUID = -8084546505498274121L; |
66 |
|
67 |
@Override
|
68 |
public boolean add(FeatureRule rule) { |
69 |
return super.add(rule); |
70 |
} |
71 |
|
72 |
@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 |
|
78 |
@Override
|
79 |
public FeatureRule getRule(int index) { |
80 |
return (FeatureRule) super.get(index); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public boolean remove(FeatureRule rule) { |
85 |
return super.remove(rule); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public FeatureRules getCopy() {
|
90 |
DefaultFeatureRules copy = new DefaultFeatureRules();
|
91 |
copy.addAll(this);
|
92 |
return copy;
|
93 |
} |
94 |
|
95 |
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 |
} |
105 |
|
106 |
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 |
|
134 |
private void checkRequireds(Feature feature) { |
135 |
for (FeatureAttributeDescriptor attr : feature.getType()) {
|
136 |
if (attr.isAutomatic() || attr.isComputed()) {
|
137 |
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 |
|
166 |
private void checkBasics(Feature feature) { |
167 |
for (FeatureAttributeDescriptor attr : feature.getType()) {
|
168 |
if(attr.isComputed()){
|
169 |
continue;
|
170 |
} |
171 |
try {
|
172 |
Object value = feature.get(attr.getName());
|
173 |
attr.validate(value); |
174 |
// DisposeUtils.disposeQuietly(value);
|
175 |
} 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 |
|
201 |
|
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 |
@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 |
} |