001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.datadictionary.validator;
017
018 import org.apache.commons.logging.Log;
019 import org.kuali.rice.krad.uif.component.Component;
020 import org.springframework.beans.factory.support.KualiDefaultListableBeanFactory;
021 import org.springframework.core.io.ResourceLoader;
022
023 import java.io.BufferedWriter;
024 import java.io.FileWriter;
025 import java.io.IOException;
026 import java.io.PrintStream;
027
028 /**
029 * A combination view controller for the Rice Dictionary Validator that handles both the setup/execution of the
030 * validation and the output of the results.
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034 public class ValidationController {
035 protected static final String endl = System.getProperty("line.separator");
036
037 protected boolean displayWarnings;
038 protected boolean displayErrors;
039 protected boolean displayXmlPages;
040 protected boolean displayErrorMessages;
041 protected boolean displayWarningMessages;
042
043 /**
044 * Constructor creating a new Rice Dictionary Validator with limited information during output
045 *
046 * @param displayErrors - True if the Validator should show the number of error during output
047 * @param displayWarnings - True if the Validator should show the number of warnings during output
048 * @param displayErrorMessages - True if the Validator should show the messages for the error reports
049 * @param displayWarningMessages - True if the Validator should show messages involving warnings
050 * @param displayXmlPages - True if the Validator should show the list of xml pages for the error reports
051 */
052 public ValidationController(boolean displayErrors, boolean displayWarnings, boolean displayErrorMessages,
053 boolean displayWarningMessages, boolean displayXmlPages) {
054 //LOG.debug("Creating new Rice Dictionary Validator with limited output");
055 this.displayErrors = displayErrors;
056 this.displayWarnings = displayWarnings;
057 this.displayErrorMessages = displayErrorMessages;
058 this.displayWarningMessages = displayWarningMessages;
059 this.displayXmlPages = displayXmlPages;
060 }
061
062 /**
063 * Constructor creating a new Rice Dictionary Validator
064 */
065 public ValidationController() {
066 //LOG.debug("Creating new Rice Dictionary Validator");
067 displayErrors = true;
068 displayWarnings = true;
069 displayErrorMessages = true;
070 displayWarningMessages = true;
071 displayXmlPages = true;
072 }
073
074 /**
075 * Validates a collection of Spring Beans with no output
076 *
077 * @param xmlFiles - The collection of xml files used to load the provided beans
078 * @param beans - Collection of preloaded beans
079 * @param failOnWarning - Whether detecting a warning should cause the validation to fail
080 * @return Returns true if the beans past validation
081 */
082 public boolean validate(String[] xmlFiles, ResourceLoader loader, KualiDefaultListableBeanFactory beans,
083 boolean failOnWarning) {
084 // LOG.debug("Validating without output");
085 Validator validator = new Validator();
086
087 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
088
089 return passed;
090 }
091
092 /**
093 * Validates a collection of Spring Beans with output going to a file
094 *
095 * @param xmlFiles - The collection of xml files used to load the provided beans
096 * @param loader - The source that was used to load the beans
097 * @param beans - Collection of preloaded beans
098 * @param outputFile - The file location to save the output to
099 * @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, KualiDefaultListableBeanFactory 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, KualiDefaultListableBeanFactory 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, KualiDefaultListableBeanFactory 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 }