001/**
002 * Copyright 2005-2015 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 */
016package org.kuali.rice.krad.datadictionary.validator;
017
018import java.io.BufferedWriter;
019import java.io.FileWriter;
020import java.io.IOException;
021import java.io.PrintStream;
022
023import org.apache.commons.logging.Log;
024import org.kuali.rice.krad.datadictionary.DefaultListableBeanFactory;
025import org.kuali.rice.krad.uif.component.Component;
026import org.springframework.core.io.ResourceLoader;
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 */
034public 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 loader resource loader
079     * @param beans Collection of preloaded beans
080     * @param failOnWarning Whether detecting a warning should cause the validation to fail
081     * @return Returns true if the beans past validation
082     */
083    public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
084            boolean failOnWarning) {
085        // LOG.debug("Validating without output");
086        Validator validator = new Validator();
087
088        boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
089
090        return passed;
091    }
092
093    /**
094     * Validates a collection of Spring Beans with output going to a file
095     *
096     * @param xmlFiles - The collection of xml files used to load the provided beans
097     * @param loader - The source that was used to load the beans
098     * @param beans - Collection of preloaded beans
099     * @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}