1 /**
2 * Copyright 2005-2014 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.datadictionary.validator;
17
18 import java.io.BufferedWriter;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.PrintStream;
22
23 import org.apache.commons.logging.Log;
24 import org.kuali.rice.krad.datadictionary.DefaultListableBeanFactory;
25 import org.kuali.rice.krad.uif.component.Component;
26 import org.springframework.core.io.ResourceLoader;
27
28 /**
29 * A combination view controller for the Rice Dictionary Validator that handles both the setup/execution of the
30 * validation and the output of the results.
31 *
32 * @author Kuali Rice Team (rice.collab@kuali.org)
33 */
34 public class ValidationController {
35 protected static final String endl = System.getProperty("line.separator");
36
37 protected boolean displayWarnings;
38 protected boolean displayErrors;
39 protected boolean displayXmlPages;
40 protected boolean displayErrorMessages;
41 protected boolean displayWarningMessages;
42
43 /**
44 * Constructor creating a new Rice Dictionary Validator with limited information during output
45 *
46 * @param displayErrors - True if the Validator should show the number of error during output
47 * @param displayWarnings - True if the Validator should show the number of warnings during output
48 * @param displayErrorMessages - True if the Validator should show the messages for the error reports
49 * @param displayWarningMessages - True if the Validator should show messages involving warnings
50 * @param displayXmlPages - True if the Validator should show the list of xml pages for the error reports
51 */
52 public ValidationController(boolean displayErrors, boolean displayWarnings, boolean displayErrorMessages,
53 boolean displayWarningMessages, boolean displayXmlPages) {
54 //LOG.debug("Creating new Rice Dictionary Validator with limited output");
55 this.displayErrors = displayErrors;
56 this.displayWarnings = displayWarnings;
57 this.displayErrorMessages = displayErrorMessages;
58 this.displayWarningMessages = displayWarningMessages;
59 this.displayXmlPages = displayXmlPages;
60 }
61
62 /**
63 * Constructor creating a new Rice Dictionary Validator
64 */
65 public ValidationController() {
66 //LOG.debug("Creating new Rice Dictionary Validator");
67 displayErrors = true;
68 displayWarnings = true;
69 displayErrorMessages = true;
70 displayWarningMessages = true;
71 displayXmlPages = true;
72 }
73
74 /**
75 * Validates a collection of Spring Beans with no output
76 *
77 * @param xmlFiles The collection of xml files used to load the provided beans
78 * @param loader resource loader
79 * @param beans Collection of preloaded beans
80 * @param failOnWarning Whether detecting a warning should cause the validation to fail
81 * @return Returns true if the beans past validation
82 */
83 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
84 boolean failOnWarning) {
85 // LOG.debug("Validating without output");
86 Validator validator = new Validator();
87
88 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
89
90 return passed;
91 }
92
93 /**
94 * Validates a collection of Spring Beans with output going to a file
95 *
96 * @param xmlFiles - The collection of xml files used to load the provided beans
97 * @param loader - The source that was used to load the beans
98 * @param beans - Collection of preloaded beans
99 * @param outputFile - The file location to save the output to
100 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
101 * @return Returns true if the beans past validation
102 */
103 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
104 String outputFile, boolean failOnWarning) {
105 Validator validator = new Validator();
106 // LOG.debug("Validating with file output to "+outputFile);
107
108 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
109
110 writeToFile(outputFile, validator, passed);
111
112 return passed;
113 }
114
115 /**
116 * Validates a collection of Spring Beans with output going to a print stream
117 *
118 * @param xmlFiles - The collection of xml files used to load the provided beans
119 * @param loader - The source that was used to load the beans
120 * @param beans - Collection of preloaded beans
121 * @param stream - The PrintStream the output is sent to
122 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
123 * @return Returns true if the beans past validation
124 */
125 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
126 PrintStream stream, boolean failOnWarning) {
127 Validator validator = new Validator();
128 // LOG.debug("Validating with Print Stream output");
129
130 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
131
132 writeToStream(stream, validator, passed);
133
134 return passed;
135 }
136
137 /**
138 * Validates a collection of Spring Beans with output going to Log4j
139 *
140 * @param xmlFiles - The collection of xml files used to load the provided beans
141 * @param loader - The source that was used to load the beans
142 * @param beans - Collection of preloaded beans
143 * @param log - The Log4j logger the output is sent to
144 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
145 * @return Returns true if the beans past validation
146 */
147 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans, Log log,
148 boolean failOnWarning) {
149 Validator validator = new Validator();
150 //LOG.debug("Validating with Log4j output");
151
152 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
153
154 writeToLog(log, validator, passed);
155
156 return passed;
157 }
158
159 /**
160 * Validates a collection of Spring Beans with no output
161 *
162 * @param xmlFiles - The collection of xml files used to load the beans
163 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
164 * @return Returns true if the beans past validation
165 */
166 public boolean validate(String[] xmlFiles, boolean failOnWarning) {
167 // LOG.debug("Validating without output");
168 Validator validator = new Validator();
169
170 boolean passed = validator.validate(xmlFiles, failOnWarning);
171
172 return passed;
173 }
174
175 /**
176 * Validates a collection of Spring Beans with output going to a file
177 *
178 * @param xmlFiles - The collection of xml files used to load the beans
179 * @param outputFile - The file location to save the output to
180 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
181 * @return Returns true if the beans past validation
182 */
183 public boolean validate(String[] xmlFiles, String outputFile, boolean failOnWarning) {
184 Validator validator = new Validator();
185 //LOG.debug("Validating with file output to "+outputFile);
186
187 boolean passed = validator.validate(xmlFiles, failOnWarning);
188
189 writeToFile(outputFile, validator, passed);
190
191 return passed;
192 }
193
194 /**
195 * Validates a collection of Spring Beans with output going to a print stream
196 *
197 * @param xmlFiles - The collection of xml files used to load the beans
198 * @param stream - The PrintStream the output is sent to
199 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
200 * @return Returns true if the beans past validation
201 */
202 public boolean validate(String[] xmlFiles, PrintStream stream, boolean failOnWarning) {
203 Validator validator = new Validator();
204 //LOG.debug("Validating with Print Stream output");
205
206 boolean passed = validator.validate(xmlFiles, failOnWarning);
207
208 writeToStream(stream, validator, passed);
209
210 return passed;
211 }
212
213 /**
214 * Validates a collection of Spring Beans with output going to Log4j
215 *
216 * @param xmlFiles - The collection of xml files used to load the provided beans
217 * @param log - The Log4j logger the output is sent to
218 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
219 * @return Returns true if the beans past validation
220 */
221 public boolean validate(String[] xmlFiles, Log log, boolean failOnWarning) {
222 Validator validator = new Validator();
223 //LOG.debug("Validating with Log4j output");
224
225 boolean passed = validator.validate(xmlFiles, failOnWarning);
226
227 writeToLog(log, validator, passed);
228
229 return passed;
230 }
231
232 /**
233 * Validates a Component with output going to Log4j
234 *
235 * @param object - The component to be validated
236 * @param log - The Log4j logger the output is sent to
237 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
238 * @return Returns true if the beans past validation
239 */
240 public boolean validate(Component object, Log log, boolean failOnWarning) {
241 Validator validator = new Validator();
242 //LOG.debug("Validating with Log4j output");
243
244 boolean passed = validator.validate(object, failOnWarning);
245
246 writeToLog(log, validator, passed);
247
248 return passed;
249 }
250
251 /**
252 * Writes the results of the validation to an output file
253 *
254 * @param path - The path to the file to write results to
255 * @param validator - The filled validator
256 * @param passed - Whether the validation passed or not
257 */
258 protected void writeToFile(String path, Validator validator, boolean passed) {
259 try {
260 BufferedWriter fout = new BufferedWriter(new FileWriter(path));
261
262 fout.write("Validation Results" + endl);
263 fout.write("Passed: " + passed + endl);
264 if (displayErrors) {
265 fout.write("Number of Errors: " + validator.getNumberOfErrors() + endl);
266 }
267 if (displayWarnings) {
268 fout.write("Number of Warnings: " + validator.getNumberOfWarnings() + endl);
269 }
270
271 if (displayErrorMessages) {
272 for (int i = 0; i < validator.getErrorReportSize(); i++) {
273 if (displayWarningMessages) {
274 fout.write(endl);
275 fout.write(validator.getErrorReport(i).errorMessage());
276 } else if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
277 fout.write(endl);
278 fout.write(validator.getErrorReport(i).errorMessage());
279 }
280
281 if (displayXmlPages) {
282 fout.write(validator.getErrorReport(i).errorPageList());
283 }
284 }
285 }
286
287 fout.close();
288 } catch (IOException e) {
289 //LOG.warn("Exception when writing file", e);
290 }
291 }
292
293 /**
294 * Writes the results of the validation to an output file
295 *
296 * @param stream - The PrintStream the output is sent to
297 * @param validator - The filled validator
298 * @param passed - Whether the validation passed or not
299 */
300 protected void writeToStream(PrintStream stream, Validator validator, boolean passed) {
301 stream.println("Validation Results");
302 stream.println("Passed: " + passed);
303 if (displayErrors) {
304 stream.println("Number of Errors: " + validator.getNumberOfErrors());
305 }
306 if (displayWarnings) {
307 stream.println("Number of Warnings: " + validator.getNumberOfWarnings());
308 }
309
310 if (displayErrorMessages) {
311 for (int i = 0; i < validator.getErrorReportSize(); i++) {
312 stream.println();
313 if (displayWarningMessages) {
314 stream.println(validator.getErrorReport(i).errorMessage());
315 } else if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
316 stream.println(validator.getErrorReport(i).errorMessage());
317 }
318
319 if (displayXmlPages) {
320 stream.println(validator.getErrorReport(i).errorPageList());
321 }
322 }
323 }
324 }
325
326 /**
327 * Writes the results of the validation to an output file
328 *
329 * @param log - The Log4j logger the output is sent to
330 * @param validator - The filled validator
331 * @param passed - Whether the validation passed or not
332 */
333 protected void writeToLog(Log log, Validator validator, boolean passed) {
334 log.info("Passed: " + passed);
335 if (displayErrors) {
336 log.info("Number of Errors: " + validator.getNumberOfErrors());
337 }
338 if (displayWarnings) {
339 log.info("Number of Warnings: " + validator.getNumberOfWarnings());
340 }
341
342 if (displayErrorMessages) {
343 for (int i = 0; i < validator.getErrorReportSize(); i++) {
344 if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
345 if (displayXmlPages) {
346 log.error(validator.getErrorReport(i).errorMessage() + validator.getErrorReport(i)
347 .errorPageList());
348 } else {
349 log.error(validator.getErrorReport(i).errorMessage());
350 }
351
352 } else {
353 if (displayWarningMessages) {
354 if (displayXmlPages) {
355 log.warn(validator.getErrorReport(i).errorMessage() + validator.getErrorReport(i)
356 .errorPageList());
357 } else {
358 log.warn(validator.getErrorReport(i).errorMessage());
359 }
360 }
361 }
362
363 }
364 }
365 }
366
367 /**
368 * Sets the displayWarnings
369 *
370 * @param display - Display or not
371 */
372 public void setDisplayWarnings(boolean display) {
373 displayWarnings = display;
374 }
375
376 /**
377 * Sets the displayErrors
378 *
379 * @param display - Display or not
380 */
381 public void setDisplayErrors(boolean display) {
382 displayErrors = display;
383 }
384
385 /**
386 * Sets the displayXmlPages
387 *
388 * @param display - Display or not
389 */
390 public void setDisplayXmlPages(boolean display) {
391 displayXmlPages = display;
392 }
393
394 /**
395 * Sets the displayErrorMessages
396 *
397 * @param display - Display or not
398 */
399 public void setDisplayErrorMessages(boolean display) {
400 displayErrorMessages = display;
401 }
402
403 /**
404 * Sets the displayWarningMessages
405 *
406 * @param display - Display or not
407 */
408 public void setDisplayWarningMessages(boolean display) {
409 displayWarningMessages = display;
410 }
411
412 /**
413 * Gets the displayWarnings, whether the number of warnings should be displayed
414 *
415 * @return displayWarnings
416 */
417 public boolean isDisplayWarnings() {
418 return displayWarnings;
419 }
420
421 /**
422 * Gets the displayErrors, whether the number of errors should be displayed
423 *
424 * @return displayErros
425 */
426 public boolean isDisplayErrors() {
427 return displayErrors;
428 }
429
430 /**
431 * Gets the displayXmlPages, whether the xml pages involved should be displayed
432 *
433 * @return displayXmlPages
434 */
435 public boolean isDisplayXmlPages() {
436 return displayXmlPages;
437 }
438
439 /**
440 * Gets the displayErrorMessages, whether the error messages should be displayed
441 *
442 * @return displayErrorMessages
443 */
444 public boolean isDisplayErrorMessages() {
445 return displayErrorMessages;
446 }
447
448 /**
449 * Gets the displayWarningMessages, whether the warning messages should be displayed
450 *
451 * @return displayWarningMessages
452 */
453 public boolean isDisplayWarningMessages() {
454 return displayWarningMessages;
455 }
456 }