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

History | View | Annotate | Download (8.59 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.dynobject.exception.DynFieldValidateException;
52
import org.gvsig.tools.persistence.PersistentState;
53
import org.gvsig.tools.persistence.exception.PersistenceException;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules, SupportJson {
58

    
59
    private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class);
60

    
61
    /**
62
     *
63
     */
64
    private static final long serialVersionUID = -8084546505498274121L;
65

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

    
71
    @Override
72
    public boolean add(String name, String description, boolean checkAtUpdate, boolean checkAtFinishEdition, Expression expression) {
73
        FeatureRule rule = new DefaultFeatureRuleExpression(name, description, checkAtUpdate, checkAtFinishEdition, expression);
74
        return this.add(rule);
75
    }
76

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

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

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

    
94
    public void validate(Feature feature, int mode) throws DataException {
95
        if (feature == null) {
96
            return;
97
        }
98
        if (feature instanceof EditableFeature) {
99
            this.validate((EditableFeature) feature, mode);
100
            return;
101
        }
102
        this.validate(feature.getEditable(), mode);
103
    }
104

    
105
    public void validate(EditableFeature feature, int check) throws DataException {
106
        if (feature == null) {
107
            return;
108
        }
109
        if ((check & CHECK_REQUIREDS) == CHECK_REQUIREDS) {
110
            checkRequireds(feature);
111
        }
112
        if ((check & CHECK_BASIC) == CHECK_BASIC) {
113
            checkBasics(feature);
114
        }
115
        if ((check & CHECK_RULES_AT_FINISH) == CHECK_RULES_AT_FINISH) {
116
            FeatureStore store = ((DefaultFeature) feature).getStore();
117
            for (FeatureRule rule : this) {
118
                if (rule.checkAtFinishEditing()) {
119
                    rule.validate(feature, store);
120
                }
121
            }
122
        }
123
        if ((check & CHECK_RULES_AT_EDITING) == CHECK_RULES_AT_EDITING) {
124
            FeatureStore store = ((DefaultFeature) feature).getStore();
125
            for (FeatureRule rule : this) {
126
                if (rule.checkAtUpdate()) {
127
                    rule.validate(feature, store);
128
                }
129
            }
130
        }
131
    }
132

    
133
    private void checkRequireds(Feature feature) {
134
        for (FeatureAttributeDescriptor attr : feature.getType()) {
135
            if (attr.isAutomatic()) {
136
                continue;
137
            }
138
            if (attr.isPrimaryKey() || !attr.allowNull()) {
139
                if (feature.get(attr.getIndex()) == null) {
140
                    String featstr = "unknown";
141
                    String storename = "unknown";
142
                    try {
143
                        featstr = feature.toJsonBuilder().toString();
144
                    } catch (Throwable th) {
145
                        LOGGER.debug("Can't convert feature to string", th);
146
                    }
147
                    try {
148
                        storename = feature.getStore().getName();
149
                    } catch (Throwable th) {
150
                        LOGGER.debug("Can't convert feature to string", th);
151
                    }
152
                    throw new IllegalStateException(
153
                            String.format(
154
                                    "The field '%s' in store '%s' can't have null values (%s).",
155
                                    attr.getName(),
156
                                    storename,
157
                                    featstr
158
                            )
159
                    );
160
                }
161
            }
162
        }
163
    }
164

    
165
    private void checkBasics(Feature feature) {
166
        for (FeatureAttributeDescriptor attr : feature.getType()) {
167
            try {
168
                attr.validate(feature.get(attr.getName()));
169
            } catch (DynFieldValidateException ex) {
170
                String featstr = "unknown";
171
                try {
172
                    featstr = feature.toJsonBuilder().toString();
173
                } catch (Throwable th) {
174
                    LOGGER.debug("Can't convert feature to string", th);
175
                }
176
                String storename = "unknown";
177
                try {
178
                    storename = feature.getStore().getName();
179
                } catch (Throwable th) {
180
                    LOGGER.debug("Can't convert feature to string", th);
181
                }
182
                throw new IllegalStateException(
183
                        String.format(
184
                                "The field '%s' in store '%s' can't have a invalid value. %s. (%s).",
185
                                attr.getName(),
186
                                storename,
187
                                ex.getMessage(),
188
                                featstr
189
                        )
190
                );
191
            }
192
        }
193
    }
194

    
195

    
196
    @Override
197
    public void saveToState(PersistentState state) throws PersistenceException {
198
        state.set("rules", this.iterator());
199
    }
200

    
201
    @Override
202
    public void loadFromState(PersistentState state) throws PersistenceException {
203
        this.clear();
204
        Iterator<FeatureRule> it = state.getIterator("rules");
205
        while (it.hasNext()) {
206
            FeatureRule rule = it.next();
207
            this.add(rule);
208
        }
209
    }
210

    
211
    @Override
212
    public void fromJson(JsonObject json) {
213
        JsonArray items = json.getJsonArray("items");
214
        if( items!=null ) {
215
            for (JsonValue item : items) {
216
                FeatureRule rule = (FeatureRule) Json.toObject(item);
217
                this.add(rule);
218
            }
219
        }
220
    }
221

    
222
    @Override
223
    public JsonObject toJson() {
224
        return this.toJsonBuilder().build();
225
    }
226

    
227
    @Override
228
    public JsonObjectBuilder toJsonBuilder() {
229
        JsonArrayBuilder arraybuilder = Json.createArrayBuilder();
230
        for (FeatureRule rule : this) {
231
            if( rule instanceof SupportJson ) {
232
                arraybuilder.add(((SupportJson)rule).toJsonBuilder());
233
            }
234
        }
235
        JsonObjectBuilder builder = Json.createObjectBuilder();
236
        builder.add_class(this);
237
        builder.add("items", arraybuilder);
238
        return builder;
239
    }
240

    
241
    public static void selfRegister() {
242
        Json.registerSerializer(DefaultFeatureRules.class);
243
    }
244
    
245
}