1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.nuiton.validator;
23
24 import org.apache.commons.collections4.MapUtils;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.EnumMap;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.TreeMap;
34
35
36
37
38
39
40
41
42 public class NuitonValidatorResult {
43
44 public static class FieldMap<V> extends TreeMap<String, V> {
45
46 private static final long serialVersionUID = 1L;
47 }
48
49 protected EnumMap<NuitonValidatorScope, FieldMap<List<String>>> messages;
50
51 protected Map<String, FieldMap<Object>> tagValues;
52
53 public boolean isValid() {
54 return !hasFatalMessages() && !hasErrorMessagess();
55 }
56
57 public void clear() {
58 messages.clear();
59 }
60
61 public boolean isEmpty() {
62 return messages.isEmpty();
63 }
64
65 public boolean hasMessagesForScope(NuitonValidatorScope scope) {
66 boolean result = false;
67 if (messages != null) {
68 FieldMap<List<String>> map = messages.get(scope);
69 result = !MapUtils.isEmpty(map);
70 }
71 return result;
72 }
73
74 public boolean hasMessagesForScope(String field,
75 NuitonValidatorScope scope) {
76 boolean result = false;
77 if (messages != null) {
78 result = messages.containsKey(scope);
79
80 if (result) {
81 FieldMap<List<String>> fieldMap = messages.get(scope);
82 result = fieldMap != null && fieldMap.containsKey(field);
83 }
84 }
85 return result;
86 }
87
88 public boolean hasFatalMessages() {
89 boolean result = hasMessagesForScope(NuitonValidatorScope.FATAL);
90 return result;
91 }
92
93 public boolean hasErrorMessagess() {
94 boolean result = hasMessagesForScope(NuitonValidatorScope.ERROR);
95 return result;
96 }
97
98 public boolean hasInfoMessages() {
99 boolean result = hasMessagesForScope(NuitonValidatorScope.INFO);
100 return result;
101 }
102
103 public boolean hasWarningMessages() {
104 boolean result = hasMessagesForScope(NuitonValidatorScope.WARNING);
105 return result;
106 }
107
108 public void addMessagesForScope(NuitonValidatorScope scope,
109 Map<String, List<String>> newMessages) {
110 if (messages == null) {
111 messages = new EnumMap<NuitonValidatorScope, FieldMap<List<String>>>(NuitonValidatorScope.class);
112 }
113
114 FieldMap<List<String>> fieldMap = messages.get(scope);
115
116 if (fieldMap == null) {
117 fieldMap = new FieldMap<List<String>>();
118 messages.put(scope, fieldMap);
119 }
120
121 for (Map.Entry<String, List<String>> entry : newMessages.entrySet()) {
122 String fieldName = entry.getKey();
123 List<String> messages = entry.getValue();
124 List<String> oldMessages = fieldMap.get(fieldName);
125 if (oldMessages == null) {
126 oldMessages = messages;
127 fieldMap.put(fieldName, oldMessages);
128 } else {
129 oldMessages.addAll(messages);
130 }
131 }
132 }
133
134 public void setMessagesForScope(NuitonValidatorScope scope,
135 String field,
136 List<String> messages) {
137
138 if (this.messages == null) {
139 this.messages = new EnumMap<NuitonValidatorScope, FieldMap<List<String>>>(NuitonValidatorScope.class);
140 }
141
142 FieldMap<List<String>> fieldMap = this.messages.get(scope);
143 if (fieldMap == null) {
144 fieldMap = new FieldMap<List<String>>();
145 this.messages.put(scope, fieldMap);
146 }
147 fieldMap.put(field, messages);
148 }
149
150 public List<String> getMessagesForScope(NuitonValidatorScope scope) {
151
152 List<String> result = new ArrayList<String>();
153 if (messages != null) {
154 FieldMap<List<String>> fieldMap = messages.get(scope);
155 for (List<String> messages : fieldMap.values()) {
156 result.addAll(messages);
157 }
158 }
159 return result;
160 }
161
162 public List<String> getMessagesForScope(String field,
163 NuitonValidatorScope scope) {
164
165 List<String> result = null;
166 if (messages != null) {
167 FieldMap<List<String>> fieldMap = messages.get(scope);
168 result = fieldMap.get(field);
169 }
170 if (result == null) {
171 result = Collections.emptyList();
172 }
173 return result;
174 }
175
176 public List<String> getFatalMessages(String field) {
177 List<String> result =
178 getMessagesForScope(field, NuitonValidatorScope.FATAL);
179 return result;
180 }
181
182 public List<String> getErrorMessages(String field) {
183 List<String> result =
184 getMessagesForScope(field, NuitonValidatorScope.ERROR);
185 return result;
186 }
187
188 public List<String> getInfoMessages(String field) {
189 List<String> result =
190 getMessagesForScope(field, NuitonValidatorScope.INFO);
191 return result;
192 }
193
194 public List<String> getWarningMessages(String field) {
195 List<String> result =
196 getMessagesForScope(field, NuitonValidatorScope.WARNING);
197 return result;
198 }
199
200 public Map<String, Object> getTagValues(String field) {
201 Map<String, Object> result = null;
202 if (tagValues != null) {
203 result = tagValues.get(field);
204 }
205 if (result == null) {
206 result = Collections.emptyMap();
207 }
208 return result;
209 }
210
211 public List<String> getFieldsForScope(NuitonValidatorScope scope) {
212
213 List<String> result = null;
214 if (messages != null) {
215 FieldMap<List<String>> fieldMap = messages.get(scope);
216 if (fieldMap != null) {
217 result = new ArrayList<String>(fieldMap.keySet());
218 }
219 }
220 if (result == null) {
221 result = Collections.emptyList();
222 }
223 return result;
224 }
225
226 public List<String> getFieldsForFatal() {
227 List<String> result = getFieldsForScope(NuitonValidatorScope.FATAL);
228 return result;
229 }
230
231 public List<String> getFieldsForError() {
232 List<String> result = getFieldsForScope(NuitonValidatorScope.ERROR);
233 return result;
234 }
235
236 public List<String> getFieldsForInfo() {
237 List<String> result = getFieldsForScope(NuitonValidatorScope.INFO);
238 return result;
239 }
240
241 public List<String> getFieldsForWarning() {
242 List<String> result = getFieldsForScope(NuitonValidatorScope.WARNING);
243 return result;
244 }
245
246 public void clearMessagesForScope(NuitonValidatorScope scope) {
247 if (messages != null) {
248 messages.remove(scope);
249 }
250 }
251
252 public NuitonValidatorScope getFieldHighestScope(String field) {
253 if (messages == null) {
254 return null;
255 }
256 if (containsField(field, NuitonValidatorScope.FATAL)) {
257 return NuitonValidatorScope.FATAL;
258 }
259 if (containsField(field, NuitonValidatorScope.ERROR)) {
260 return NuitonValidatorScope.ERROR;
261 }
262 if (containsField(field, NuitonValidatorScope.WARNING)) {
263 return NuitonValidatorScope.WARNING;
264 }
265 if (containsField(field, NuitonValidatorScope.INFO)) {
266 return NuitonValidatorScope.INFO;
267 }
268
269
270 return null;
271 }
272
273 public NuitonValidatorScope[] getFieldScopes(String field) {
274 Set<NuitonValidatorScope> result = new HashSet<NuitonValidatorScope>();
275 if (messages != null) {
276
277 if (containsField(field, NuitonValidatorScope.FATAL)) {
278 result.add(NuitonValidatorScope.FATAL);
279 }
280 if (containsField(field, NuitonValidatorScope.ERROR)) {
281 result.add(NuitonValidatorScope.ERROR);
282 }
283 if (containsField(field, NuitonValidatorScope.WARNING)) {
284 result.add(NuitonValidatorScope.WARNING);
285 }
286 if (containsField(field, NuitonValidatorScope.INFO)) {
287 result.add(NuitonValidatorScope.INFO);
288 }
289 }
290
291 return result.toArray(new NuitonValidatorScope[result.size()]);
292 }
293
294 protected boolean containsField(String field, NuitonValidatorScope scope) {
295 FieldMap<List<String>> fieldMap = messages.get(scope);
296 return fieldMap != null && fieldMap.containsKey(field);
297 }
298
299 protected EnumMap<NuitonValidatorScope, FieldMap<List<String>>> getMessages() {
300 return messages;
301 }
302
303 protected Map<String, FieldMap<Object>> getTagValues() {
304 return tagValues;
305 }
306 }