1 package com.github.mygreen.supercsv.validation;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Objects;
10
11 import com.github.mygreen.supercsv.util.ArgUtils;
12 import com.github.mygreen.supercsv.util.Utils;
13
14
15
16
17
18
19
20
21
22
23
24 public class CsvBindingErrors implements Serializable {
25
26
27 private static final long serialVersionUID = -9014484172903397334L;
28
29
30 public static final String PATH_SEPARATOR = ".";
31
32
33 private final String objectName;
34
35
36 private final List<CsvError> errors = new ArrayList<>();
37
38
39 private MessageCodeGeneratorerator.html#MessageCodeGenerator">MessageCodeGenerator messageCodeGenerator = new MessageCodeGenerator();
40
41
42
43
44
45
46 public CsvBindingErrors(final String objectName) {
47 Objects.requireNonNull(objectName);
48 this.objectName = objectName;
49 }
50
51
52
53
54
55 public CsvBindingErrors(final Class<?> clazz) {
56 this(clazz.getSimpleName());
57 }
58
59
60
61
62
63 public String getObjectName() {
64 return objectName;
65 }
66
67
68
69
70
71 public List<CsvError> getAllErrors() {
72 return new ArrayList<>(errors);
73 }
74
75
76
77
78
79 public void addError(final CsvError error) {
80 this.errors.add(error);
81 }
82
83
84
85
86
87 public void addAllErrors(Collection<CsvError> errors) {
88 this.errors.addAll(errors);
89 }
90
91
92
93
94 public void clearAllErrors() {
95 this.errors.clear();
96 }
97
98
99
100
101
102 public boolean hasErrors() {
103 return !errors.isEmpty();
104 }
105
106
107
108
109
110 public List<CsvError> getGlobalErrors() {
111 final List<CsvError> list = new ArrayList<CsvError>();
112 for(CsvError item : this.errors) {
113 if(!(item instanceof CsvFieldError)) {
114 list.add(item);
115 }
116 }
117
118 return list;
119 }
120
121
122
123
124
125 public CsvError getFirstGlobalError() {
126 for(CsvError item : this.errors) {
127 if(!(item instanceof CsvFieldError)) {
128 return item;
129 }
130 }
131
132 return null;
133 }
134
135
136
137
138
139 public boolean hasGlobalErrors() {
140 return !getGlobalErrors().isEmpty();
141 }
142
143
144
145
146
147 public int getGlobalErrorCount() {
148 return getGlobalErrors().size();
149 }
150
151
152
153
154
155 public List<CsvFieldError> getFieldErrors() {
156 final List<CsvFieldError> list = new ArrayList<CsvFieldError>();
157 for(CsvError item : this.errors) {
158 if(item instanceof CsvFieldError) {
159 list.add((CsvFieldError) item);
160 }
161 }
162
163 return list;
164 }
165
166
167
168
169
170 public CsvFieldError getFirstFieldError() {
171 for(CsvError item : this.errors) {
172 if(item instanceof CsvFieldError) {
173 return (CsvFieldError) item;
174 }
175 }
176
177 return null;
178 }
179
180
181
182
183
184 public boolean hasFieldErrors() {
185 return !getFieldErrors().isEmpty();
186 }
187
188
189
190
191
192 public int getFieldErrorCount() {
193 return getFieldErrors().size();
194 }
195
196
197
198
199
200
201
202 public List<CsvFieldError> getFieldErrors(final String path) {
203 ArgUtils.notEmpty(path, "path");
204
205 final List<CsvFieldError> list = new ArrayList<CsvFieldError>();
206 for(CsvError item : this.errors) {
207 if(item instanceof CsvFieldErrorpercsv/validation/CsvFieldError.html#CsvFieldError">CsvFieldError && isMatchingFieldError(path, (CsvFieldError) item)) {
208 list.add((CsvFieldError) item);
209 }
210 }
211
212 return list;
213 }
214
215
216
217
218
219
220 public CsvFieldError getFirstFieldError(final String path) {
221 ArgUtils.notEmpty(path, "path");
222
223 for(CsvError item : this.errors) {
224 if(item instanceof CsvFieldErrorpercsv/validation/CsvFieldError.html#CsvFieldError">CsvFieldError && isMatchingFieldError(path, (CsvFieldError) item)) {
225 return (CsvFieldError) item;
226 }
227 }
228
229 return null;
230 }
231
232
233
234
235
236
237 public boolean hasFieldErrors(final String path) {
238 return !getFieldErrors(path).isEmpty();
239 }
240
241
242
243
244
245
246 public int getFieldErrorCount(final String path) {
247 return getFieldErrors(path).size();
248 }
249
250
251
252
253
254
255
256 private boolean isMatchingFieldError(final String path, final CsvFieldError fieldError) {
257
258 if (fieldError.getField().equals(path)) {
259 return true;
260 }
261
262 if(path.endsWith("*")) {
263 String subPath = path.substring(0, path.length()-1);
264 return fieldError.getField().startsWith(subPath);
265 }
266
267 return false;
268 }
269
270
271
272
273
274 public void reject(final String errorCode) {
275 reject(errorCode, Collections.emptyMap(), null);
276 }
277
278
279
280
281
282
283 public void reject(final String errorCode, final String defaultMessage) {
284 reject(errorCode, Collections.emptyMap(), defaultMessage);
285 }
286
287
288
289
290
291
292 public void reject(final String errorCode, final Map<String, Object> messageVariables) {
293 reject(errorCode, messageVariables, null);
294 }
295
296
297
298
299
300
301
302 public void reject(final String errorCode, final Map<String, Object> messageVariables, final String defaultMessage) {
303 reject(new String[]{errorCode}, messageVariables, defaultMessage);
304 }
305
306
307
308
309
310
311
312
313 public void reject(final String[] errorCodes, final Map<String, Object> messageVariables, final String defaultMessage) {
314
315 String[] codes = new String[0];
316 for(String errorCode : errorCodes) {
317 codes = Utils.concat(codes, messageCodeGenerator.generateCodes(errorCode, getObjectName()));
318 }
319 addError(new CsvError(getObjectName(), codes, messageVariables, defaultMessage));
320 }
321
322
323
324
325
326
327 public void rejectValue(final String field, final String errorCode) {
328 rejectValue(field, null, errorCode, Collections.emptyMap(), null);
329
330 }
331
332
333
334
335
336
337
338 public void rejectValue(final String field, final String errorCode, final String defaultMessage) {
339 rejectValue(field, null, errorCode, Collections.emptyMap(), defaultMessage);
340
341 }
342
343
344
345
346
347
348
349
350 public void rejectValue(final String field, final String errorCode,
351 final Map<String, Object> messageVariables, final String defaultMessage) {
352
353 rejectValue(field, null, errorCode, Collections.emptyMap(), defaultMessage);
354
355 }
356
357
358
359
360
361
362
363
364 public void rejectValue(final String field, final Class<?> fieldType, final String errorCode,
365 final Map<String, Object> messageVariables) {
366
367 rejectValue(field, fieldType, errorCode, messageVariables, null);
368
369 }
370
371
372
373
374
375
376
377
378
379 public void rejectValue(final String field, final Class<?> fieldType, final String errorCode,
380 final Map<String, Object> messageVariables, final String defaultMessage) {
381
382 rejectValue(field, fieldType, new String[]{errorCode}, messageVariables, defaultMessage);
383 }
384
385
386
387
388
389
390
391
392
393
394 public void rejectValue(final String field, final Class<?> fieldType, final String[] errorCodes,
395 final Map<String, Object> messageVariables, final String defaultMessage) {
396
397 String[] codes = new String[0];
398 for(String errorCode : errorCodes) {
399 codes = Utils.concat(codes, messageCodeGenerator.generateCodes(errorCode, getObjectName(), field, fieldType));
400 }
401
402 addError(new CsvFieldError(getObjectName(), field, false, codes, messageVariables, defaultMessage));
403
404 }
405
406 public MessageCodeGenerator getMessageCodeGenerator() {
407 return messageCodeGenerator;
408 }
409
410 public void setMessageCodeGenerator(MessageCodeGenerator messageCodeGenerator) {
411 this.messageCodeGenerator = messageCodeGenerator;
412 }
413
414
415 }