Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ValidationResultInfo |
|
| 1.5238095238095237;1.524 | ||||
ValidationResultInfo$ErrorLevel |
|
| 1.5238095238095237;1.524 |
1 | /** | |
2 | * Copyright 2010 The Kuali Foundation Licensed under the | |
3 | * Educational Community License, Version 2.0 (the "License"); you may | |
4 | * not use this file except in compliance with the License. You may | |
5 | * obtain a copy of the License at | |
6 | * | |
7 | * http://www.osedu.org/licenses/ECL-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, | |
10 | * software distributed under the License is distributed on an "AS IS" | |
11 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
12 | * or implied. See the License for the specific language governing | |
13 | * permissions and limitations under the License. | |
14 | */ | |
15 | ||
16 | package org.kuali.student.common.validation.dto; | |
17 | ||
18 | import java.io.Serializable; | |
19 | import java.util.List; | |
20 | ||
21 | import javax.xml.bind.annotation.XmlAccessType; | |
22 | import javax.xml.bind.annotation.XmlAccessorType; | |
23 | import javax.xml.bind.annotation.XmlElement; | |
24 | ||
25 | @XmlAccessorType(XmlAccessType.FIELD) | |
26 | public class ValidationResultInfo implements Serializable { | |
27 | ||
28 | private static final long serialVersionUID = 1L; | |
29 | ||
30 | 0 | public enum ErrorLevel { |
31 | 0 | OK(0), WARN(1), ERROR(2); |
32 | ||
33 | int level; | |
34 | ||
35 | 0 | private ErrorLevel(int level) { |
36 | 0 | this.level = level; |
37 | 0 | } |
38 | ||
39 | public int getLevel() { | |
40 | 0 | return level; |
41 | } | |
42 | ||
43 | public static ErrorLevel min(ErrorLevel e1, ErrorLevel e2) { | |
44 | 0 | return e1.ordinal() < e2.ordinal() ? e1 : e2; |
45 | } | |
46 | ||
47 | public static ErrorLevel max(ErrorLevel e1, ErrorLevel e2) { | |
48 | 0 | return e1.ordinal() > e2.ordinal() ? e1 : e2; |
49 | } | |
50 | } | |
51 | ||
52 | 0 | private transient Object invalidData = null; |
53 | ||
54 | @XmlElement | |
55 | protected String element; | |
56 | ||
57 | 0 | @XmlElement |
58 | protected ErrorLevel level = ErrorLevel.OK; | |
59 | ||
60 | @XmlElement | |
61 | protected String message; | |
62 | ||
63 | public ValidationResultInfo() { | |
64 | 0 | super(); |
65 | 0 | this.level = ErrorLevel.OK; |
66 | 0 | } |
67 | ||
68 | public ValidationResultInfo(String element) { | |
69 | 0 | super(); |
70 | 0 | this.level = ErrorLevel.OK; |
71 | 0 | this.element = element; |
72 | 0 | } |
73 | ||
74 | public ValidationResultInfo(String element, Object invalidData) { | |
75 | 0 | this(element); |
76 | 0 | this.invalidData = invalidData; |
77 | 0 | } |
78 | ||
79 | /** | |
80 | * @return the level | |
81 | */ | |
82 | public ErrorLevel getLevel() { | |
83 | 0 | return level; |
84 | } | |
85 | ||
86 | /** | |
87 | * @param level | |
88 | * the level to set | |
89 | */ | |
90 | public void setLevel(ErrorLevel level) { | |
91 | 0 | this.level = level; |
92 | 0 | } |
93 | ||
94 | /** | |
95 | * @return the message | |
96 | */ | |
97 | public String getMessage() { | |
98 | 0 | return message; |
99 | } | |
100 | ||
101 | /** | |
102 | * @param message | |
103 | * the message to set | |
104 | */ | |
105 | public void setMessage(String message) { | |
106 | 0 | this.message = message; |
107 | 0 | } |
108 | ||
109 | public String getElement() { | |
110 | 0 | return element; |
111 | } | |
112 | ||
113 | public void setElement(String element) { | |
114 | 0 | this.element = element; |
115 | 0 | } |
116 | ||
117 | /** | |
118 | * Returns the ValidationResult's error level | |
119 | * | |
120 | * @return | |
121 | */ | |
122 | public ErrorLevel getErrorLevel() { | |
123 | 0 | return level; |
124 | } | |
125 | ||
126 | /** | |
127 | * Convenience method. Adds a message with an error level of WARN | |
128 | * | |
129 | * @param message | |
130 | * the message to add | |
131 | */ | |
132 | public void setWarning(String message) { | |
133 | 0 | this.level = ErrorLevel.WARN; |
134 | 0 | this.message = message; |
135 | 0 | } |
136 | ||
137 | /** | |
138 | * Convenience method. Adds a message with an error level of ERROR | |
139 | * | |
140 | * @param message | |
141 | * the message to add | |
142 | */ | |
143 | public void setError(String message) { | |
144 | 0 | this.level = ErrorLevel.ERROR; |
145 | 0 | this.message = message; |
146 | 0 | } |
147 | ||
148 | /** | |
149 | * Convenience method. Returns true if getErrorLevel() == ErrorLevel.OK | |
150 | * | |
151 | * @return true if getErrorLevel() == ErrorLevel.OK | |
152 | */ | |
153 | public boolean isOk() { | |
154 | 0 | return getErrorLevel() == ErrorLevel.OK; |
155 | } | |
156 | ||
157 | /** | |
158 | * Convenience method. Returns true if getErrorLevel() == ErrorLevel.WARN | |
159 | * | |
160 | * @return true if getErrorLevel() == ErrorLevel.WARN | |
161 | */ | |
162 | public boolean isWarn() { | |
163 | 0 | return getErrorLevel() == ErrorLevel.WARN; |
164 | } | |
165 | ||
166 | /** | |
167 | * Convenience method. Returns true if getErrorLevel() == ErrorLevel.ERROR | |
168 | * | |
169 | * @return true if getErrorLevel() == ErrorLevel.ERROR | |
170 | */ | |
171 | public boolean isError() { | |
172 | 0 | return getErrorLevel() == ErrorLevel.ERROR; |
173 | } | |
174 | ||
175 | public String toString() { | |
176 | 0 | return "[" + level + "] Path: [" + element + "] - " + message |
177 | + " data=[" + invalidData + "]"; | |
178 | } | |
179 | ||
180 | /** | |
181 | * Checks if there are any validation errors exceeding the threshold and ignoring the list of fieldPaths passed in | |
182 | * @param validationResults | |
183 | * @param threshold | |
184 | * @param ignoreFields | |
185 | * @return | |
186 | */ | |
187 | public static boolean hasValidationErrors(List<ValidationResultInfo> validationResults, ErrorLevel threshold, List<String> ignoreFields){ | |
188 | 0 | if (validationResults != null) { |
189 | 0 | for (ValidationResultInfo validationResult : validationResults) { |
190 | //Ignore any fields that are in the list | |
191 | 0 | if(ignoreFields!=null && !ignoreFields.contains(validationResult.getElement())){ |
192 | //Return true if any of the validation results exceed your threshold | |
193 | 0 | if (validationResult.getLevel() == ErrorLevel.ERROR) { |
194 | 0 | return true; |
195 | } | |
196 | 0 | if (ErrorLevel.WARN.equals(threshold) && validationResult.getLevel() == ErrorLevel.WARN) { |
197 | 0 | return true; |
198 | } | |
199 | } | |
200 | } | |
201 | } | |
202 | 0 | return false; |
203 | } | |
204 | ||
205 | } |