Statistics
| Revision:

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
/**
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.JsonObjectBuilder;
48
import org.gvsig.json.SupportJson;
49
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules, SupportJson {
56

    
57
    private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class);
58

    
59
    /**
60
     *
61
     */
62
    private static final long serialVersionUID = -8084546505498274121L;
63

    
64
    @Override
65
    public boolean add(FeatureRule rule) {
66
        return super.add(rule);
67
    }
68

    
69
    @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

    
75
    @Override
76
    public FeatureRule getRule(int index) {
77
        return (FeatureRule) super.get(index);
78
    }
79

    
80
    @Override
81
    public boolean remove(FeatureRule rule) {
82
        return super.remove(rule);
83
    }
84

    
85
    @Override
86
    public FeatureRules getCopy() {
87
        DefaultFeatureRules copy = new DefaultFeatureRules();
88
        copy.addAll(this);
89
        return copy;
90
    }
91

    
92
    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
    }
102

    
103
    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
                if( rule == null ) {
117
                    continue;
118
                }
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 == null ) {
128
                    continue;
129
                }
130
                if (rule.checkAtUpdate()) {
131
                    rule.validate(feature, store);
132
                }
133
            }
134
        }
135
    }
136

    
137
    private void checkRequireds(Feature feature) {
138
        for (FeatureAttributeDescriptor attr : feature.getType()) {
139
            if (attr.isAutomatic() || attr.isComputed()) {
140
                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

    
169
    private void checkBasics(Feature feature) {
170
        for (FeatureAttributeDescriptor attr : feature.getType()) {
171
            if(attr.isComputed()){
172
                continue;
173
            }
174
            try {
175
                Object value = feature.get(attr.getName());
176
                attr.validate(value);
177
//                DisposeUtils.disposeQuietly(value);
178
            } 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

    
204

    
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
            if( rule!=null ) {
217
                this.add(rule);
218
            }
219
        }
220
    }
221

    
222
    @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
                if( rule != null ) {
229
                    this.add(rule);
230
                }
231
            }
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
}