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

History | View | Annotate | Download (7.32 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 org.gvsig.expressionevaluator.Expression;
29
import org.gvsig.expressionevaluator.impl.DefaultFeatureRuleExpression;
30

    
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.EditableFeature;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import static org.gvsig.fmap.dal.feature.Feature.CHECK_BASIC;
35
import static org.gvsig.fmap.dal.feature.Feature.CHECK_REQUIREDS;
36
import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_EDITING;
37
import static org.gvsig.fmap.dal.feature.Feature.CHECK_RULES_AT_FINISH;
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.dal.feature.FeatureRule;
40
import org.gvsig.fmap.dal.feature.FeatureRules;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
public class DefaultFeatureRules extends ArrayList<FeatureRule> implements FeatureRules {
49

    
50
    private final static Logger LOGGER = LoggerFactory.getLogger(DefaultFeatureRules.class);
51

    
52
    /**
53
     *
54
     */
55
    private static final long serialVersionUID = -8084546505498274121L;
56

    
57
    @Override
58
    public boolean add(FeatureRule rule) {
59
        return super.add(rule);
60
    }
61

    
62
    @Override
63
    public boolean add(String name, String description, boolean checkAtUpdate, boolean checkAtFinishEdition, Expression expression) {
64
        FeatureRule rule = new DefaultFeatureRuleExpression(name, description, checkAtUpdate, checkAtFinishEdition, expression);
65
        return this.add(rule);
66
    }
67

    
68
    @Override
69
    public FeatureRule getRule(int index) {
70
        return (FeatureRule) super.get(index);
71
    }
72

    
73
    @Override
74
    public boolean remove(FeatureRule rule) {
75
        return super.remove(rule);
76
    }
77

    
78
    @Override
79
    public FeatureRules getCopy() {
80
        DefaultFeatureRules copy = new DefaultFeatureRules();
81
        copy.addAll(this);
82
        return copy;
83
    }
84

    
85
    public void validate(Feature feature, int mode) throws DataException {
86
        if (feature == null) {
87
            return;
88
        }
89
        if (feature instanceof EditableFeature) {
90
            this.validate((EditableFeature) feature, mode);
91
            return;
92
        }
93
        this.validate(feature.getEditable(), mode);
94
    }
95

    
96
    public void validate(EditableFeature feature, int check) throws DataException {
97
        if (feature == null) {
98
            return;
99
        }
100
        if ((check & CHECK_REQUIREDS) == CHECK_REQUIREDS) {
101
            checkRequireds(feature);
102
        }
103
        if ((check & CHECK_BASIC) == CHECK_BASIC) {
104
            checkBasics(feature);
105
        }
106
        if ((check & CHECK_RULES_AT_FINISH) == CHECK_RULES_AT_FINISH) {
107
            FeatureStore store = ((DefaultFeature) feature).getStore();
108
            for (FeatureRule rule : this) {
109
                if (rule.checkAtFinishEditing()) {
110
                    rule.validate(feature, store);
111
                }
112
            }
113
        }
114
        if ((check & CHECK_RULES_AT_EDITING) == CHECK_RULES_AT_EDITING) {
115
            FeatureStore store = ((DefaultFeature) feature).getStore();
116
            for (FeatureRule rule : this) {
117
                if (rule.checkAtUpdate()) {
118
                    rule.validate(feature, store);
119
                }
120
            }
121
        }
122
    }
123

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

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

    
186
    public static void selfRegister() {
187

    
188
    }
189

    
190
    @Override
191
    public void saveToState(PersistentState state) throws PersistenceException {
192
        state.set("rules", this.iterator());
193
    }
194

    
195
    @Override
196
    public void loadFromState(PersistentState state) throws PersistenceException {
197
        this.clear();
198
        Iterator<FeatureRule> it = state.getIterator("rules");
199
        while (it.hasNext()) {
200
            FeatureRule rule = it.next();
201
            this.add(rule);
202
        }
203
    }
204

    
205
}