View Javadoc

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 beans - Collection of preloaded beans
79       * @param failOnWarning - Whether detecting a warning should cause the validation to fail
80       * @return Returns true if the beans past validation
81       */
82      public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
83              boolean failOnWarning) {
84          // LOG.debug("Validating without output");
85          Validator validator = new Validator();
86  
87          boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
88  
89          return passed;
90      }
91  
92      /**
93       * Validates a collection of Spring Beans with output going to a file
94       *
95       * @param xmlFiles - The collection of xml files used to load the provided beans
96       * @param loader - The source that was used to load the beans
97       * @param beans - Collection of preloaded beans
98       * @param outputFile - The file location to save the output to
99       * @param failOnWarning - Whether detecting a warning should cause the validation to fail
100      * @return Returns true if the beans past validation
101      */
102     public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
103             String outputFile, boolean failOnWarning) {
104         Validator validator = new Validator();
105         // LOG.debug("Validating with file output to "+outputFile);
106 
107         boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
108 
109         writeToFile(outputFile, validator, passed);
110 
111         return passed;
112     }
113 
114     /**
115      * Validates a collection of Spring Beans with output going to a print stream
116      *
117      * @param xmlFiles - The collection of xml files used to load the provided beans
118      * @param loader - The source that was used to load the beans
119      * @param beans - Collection of preloaded beans
120      * @param stream - The PrintStream the output is sent to
121      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
122      * @return Returns true if the beans past validation
123      */
124     public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
125             PrintStream stream, boolean failOnWarning) {
126         Validator validator = new Validator();
127         // LOG.debug("Validating with Print Stream output");
128 
129         boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
130 
131         writeToStream(stream, validator, passed);
132 
133         return passed;
134     }
135 
136     /**
137      * Validates a collection of Spring Beans with output going to Log4j
138      *
139      * @param xmlFiles - The collection of xml files used to load the provided beans
140      * @param loader - The source that was used to load the beans
141      * @param beans - Collection of preloaded beans
142      * @param log - The Log4j logger the output is sent to
143      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
144      * @return Returns true if the beans past validation
145      */
146     public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans, Log log,
147             boolean failOnWarning) {
148         Validator validator = new Validator();
149         //LOG.debug("Validating with Log4j output");
150 
151         boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
152 
153         writeToLog(log, validator, passed);
154 
155         return passed;
156     }
157 
158     /**
159      * Validates a collection of Spring Beans with no output
160      *
161      * @param xmlFiles - The collection of xml files used to load the beans
162      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
163      * @return Returns true if the beans past validation
164      */
165     public boolean validate(String[] xmlFiles, boolean failOnWarning) {
166         // LOG.debug("Validating without output");
167         Validator validator = new Validator();
168 
169         boolean passed = validator.validate(xmlFiles, failOnWarning);
170 
171         return passed;
172     }
173 
174     /**
175      * Validates a collection of Spring Beans with output going to a file
176      *
177      * @param xmlFiles - The collection of xml files used to load the beans
178      * @param outputFile - The file location to save the output to
179      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
180      * @return Returns true if the beans past validation
181      */
182     public boolean validate(String[] xmlFiles, String outputFile, boolean failOnWarning) {
183         Validator validator = new Validator();
184         //LOG.debug("Validating with file output to "+outputFile);
185 
186         boolean passed = validator.validate(xmlFiles, failOnWarning);
187 
188         writeToFile(outputFile, validator, passed);
189 
190         return passed;
191     }
192 
193     /**
194      * Validates a collection of Spring Beans with output going to a print stream
195      *
196      * @param xmlFiles - The collection of xml files used to load the beans
197      * @param stream - The PrintStream the output is sent to
198      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
199      * @return Returns true if the beans past validation
200      */
201     public boolean validate(String[] xmlFiles, PrintStream stream, boolean failOnWarning) {
202         Validator validator = new Validator();
203         //LOG.debug("Validating with Print Stream output");
204 
205         boolean passed = validator.validate(xmlFiles, failOnWarning);
206 
207         writeToStream(stream, validator, passed);
208 
209         return passed;
210     }
211 
212     /**
213      * Validates a collection of Spring Beans with output going to Log4j
214      *
215      * @param xmlFiles - The collection of xml files used to load the provided beans
216      * @param log - The Log4j logger the output is sent to
217      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
218      * @return Returns true if the beans past validation
219      */
220     public boolean validate(String[] xmlFiles, Log log, boolean failOnWarning) {
221         Validator validator = new Validator();
222         //LOG.debug("Validating with Log4j output");
223 
224         boolean passed = validator.validate(xmlFiles, failOnWarning);
225 
226         writeToLog(log, validator, passed);
227 
228         return passed;
229     }
230 
231     /**
232      * Validates a Component with output going to Log4j
233      *
234      * @param object - The component to be validated
235      * @param log - The Log4j logger the output is sent to
236      * @param failOnWarning - Whether detecting a warning should cause the validation to fail
237      * @return Returns true if the beans past validation
238      */
239     public boolean validate(Component object, Log log, boolean failOnWarning) {
240         Validator validator = new Validator();
241         //LOG.debug("Validating with Log4j output");
242 
243         boolean passed = validator.validate(object, failOnWarning);
244 
245         writeToLog(log, validator, passed);
246 
247         return passed;
248     }
249 
250     /**
251      * Writes the results of the validation to an output file
252      *
253      * @param path - The path to the file to write results to
254      * @param validator - The filled validator
255      * @param passed - Whether the validation passed or not
256      */
257     protected void writeToFile(String path, Validator validator, boolean passed) {
258         try {
259             BufferedWriter fout = new BufferedWriter(new FileWriter(path));
260 
261             fout.write("Validation Results" + endl);
262             fout.write("Passed: " + passed + endl);
263             if (displayErrors) {
264                 fout.write("Number of Errors: " + validator.getNumberOfErrors() + endl);
265             }
266             if (displayWarnings) {
267                 fout.write("Number of Warnings: " + validator.getNumberOfWarnings() + endl);
268             }
269 
270             if (displayErrorMessages) {
271                 for (int i = 0; i < validator.getErrorReportSize(); i++) {
272                     if (displayWarningMessages) {
273                         fout.write(endl);
274                         fout.write(validator.getErrorReport(i).errorMessage());
275                     } else if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
276                         fout.write(endl);
277                         fout.write(validator.getErrorReport(i).errorMessage());
278                     }
279 
280                     if (displayXmlPages) {
281                         fout.write(validator.getErrorReport(i).errorPageList());
282                     }
283                 }
284             }
285 
286             fout.close();
287         } catch (IOException e) {
288             //LOG.warn("Exception when writing file", e);
289         }
290     }
291 
292     /**
293      * Writes the results of the validation to an output file
294      *
295      * @param stream - The PrintStream the output is sent to
296      * @param validator - The filled validator
297      * @param passed - Whether the validation passed or not
298      */
299     protected void writeToStream(PrintStream stream, Validator validator, boolean passed) {
300         stream.println("Validation Results");
301         stream.println("Passed: " + passed);
302         if (displayErrors) {
303             stream.println("Number of Errors: " + validator.getNumberOfErrors());
304         }
305         if (displayWarnings) {
306             stream.println("Number of Warnings: " + validator.getNumberOfWarnings());
307         }
308 
309         if (displayErrorMessages) {
310             for (int i = 0; i < validator.getErrorReportSize(); i++) {
311                 stream.println();
312                 if (displayWarningMessages) {
313                     stream.println(validator.getErrorReport(i).errorMessage());
314                 } else if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
315                     stream.println(validator.getErrorReport(i).errorMessage());
316                 }
317 
318                 if (displayXmlPages) {
319                     stream.println(validator.getErrorReport(i).errorPageList());
320                 }
321             }
322         }
323     }
324 
325     /**
326      * Writes the results of the validation to an output file
327      *
328      * @param log - The Log4j logger the output is sent to
329      * @param validator - The filled validator
330      * @param passed - Whether the validation passed or not
331      */
332     protected void writeToLog(Log log, Validator validator, boolean passed) {
333         log.info("Passed: " + passed);
334         if (displayErrors) {
335             log.info("Number of Errors: " + validator.getNumberOfErrors());
336         }
337         if (displayWarnings) {
338             log.info("Number of Warnings: " + validator.getNumberOfWarnings());
339         }
340 
341         if (displayErrorMessages) {
342             for (int i = 0; i < validator.getErrorReportSize(); i++) {
343                 if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
344                     if (displayXmlPages) {
345                         log.error(validator.getErrorReport(i).errorMessage() + validator.getErrorReport(i)
346                                 .errorPageList());
347                     } else {
348                         log.error(validator.getErrorReport(i).errorMessage());
349                     }
350 
351                 } else {
352                     if (displayWarningMessages) {
353                         if (displayXmlPages) {
354                             log.warn(validator.getErrorReport(i).errorMessage() + validator.getErrorReport(i)
355                                     .errorPageList());
356                         } else {
357                             log.warn(validator.getErrorReport(i).errorMessage());
358                         }
359                     }
360                 }
361 
362             }
363         }
364     }
365 
366     /**
367      * Sets the displayWarnings
368      *
369      * @param display - Display or not
370      */
371     public void setDisplayWarnings(boolean display) {
372         displayWarnings = display;
373     }
374 
375     /**
376      * Sets the displayErrors
377      *
378      * @param display - Display or not
379      */
380     public void setDisplayErrors(boolean display) {
381         displayErrors = display;
382     }
383 
384     /**
385      * Sets the displayXmlPages
386      *
387      * @param display - Display or not
388      */
389     public void setDisplayXmlPages(boolean display) {
390         displayXmlPages = display;
391     }
392 
393     /**
394      * Sets the displayErrorMessages
395      *
396      * @param display - Display or not
397      */
398     public void setDisplayErrorMessages(boolean display) {
399         displayErrorMessages = display;
400     }
401 
402     /**
403      * Sets the displayWarningMessages
404      *
405      * @param display - Display or not
406      */
407     public void setDisplayWarningMessages(boolean display) {
408         displayWarningMessages = display;
409     }
410 
411     /**
412      * Gets the displayWarnings, whether the number of warnings should be displayed
413      *
414      * @return displayWarnings
415      */
416     public boolean isDisplayWarnings() {
417         return displayWarnings;
418     }
419 
420     /**
421      * Gets the displayErrors, whether the number of errors should be displayed
422      *
423      * @return displayErros
424      */
425     public boolean isDisplayErrors() {
426         return displayErrors;
427     }
428 
429     /**
430      * Gets the displayXmlPages, whether the xml pages involved should be displayed
431      *
432      * @return displayXmlPages
433      */
434     public boolean isDisplayXmlPages() {
435         return displayXmlPages;
436     }
437 
438     /**
439      * Gets the displayErrorMessages, whether the error messages should be displayed
440      *
441      * @return displayErrorMessages
442      */
443     public boolean isDisplayErrorMessages() {
444         return displayErrorMessages;
445     }
446 
447     /**
448      * Gets the displayWarningMessages, whether the warning messages should be displayed
449      *
450      * @return displayWarningMessages
451      */
452     public boolean isDisplayWarningMessages() {
453         return displayWarningMessages;
454     }
455 }