ValidationException.java

// license-header java merge-point
// Generated by andromda-jsf cartridge (exception\ValidationException.java.vsl) DO NOT EDIT!
package org.andromda.samples.animalquiz;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author walter
 *
 * Use this class to show erros messages to the user.
 * FieldNames must be id or parentId.id ou parentId:id 
 *
 */
public class ValidationException extends RuntimeException {

    /**
     * The serial version UID of this class. Needed for serialization.
     */
    private static final long serialVersionUID = -4111097815415667821L;

    /**
     * @author walter
     *
     * This inner class is use to store info about a validation exception
     */
    public class ValidationExceptionInfo{
        private String fieldName;
        private String message;
        private Object[] args;
        /**
         * Constructor
         *
         * @param name fieldName
         * @param message
         * @param args arguments of the message
         */
        public ValidationExceptionInfo(String name, String message, Object[] args) {
            this.fieldName = name;
            this.message = message;
            this.args = args;
        }
        /**
         * Getter of the attribute fieldName
         *
         * @return the value of attribute fieldName
         */
        public String getFieldName() {
            return this.fieldName;
        }
        /**
         * Getter of the attribute message
         *
         * @return the value of attribute message
         */
        public String getMessage() {
            return this.message;
        }
        /** 
         * Getter of the attribute args
         *
         * @return the value of the attribute args
         */
        public Object[] getArgs(){
            return args;
        }
    }
    
    /**
     * Collection of error messages
     */
    private Collection<ValidationExceptionInfo> errors=new ArrayList<ValidationExceptionInfo>();
    
    /**
     * Collection of warning messages
     */
    private Collection<ValidationExceptionInfo> warnings=new ArrayList<ValidationExceptionInfo>();

    /**
     * Default constructor
     */
    public ValidationException() {
        super();
    }

    /**
     * Constructor
     *
     * @param errorMessage an error message to be associated with the exception
     */
    public ValidationException(String errorMessage) {
        super(errorMessage);
        addError(errorMessage);
    }

    /**
     * Constructor
     *
     * @param fieldName the client identifier with which this message is associated (if any)
     * @param errorMessage the error message
     * @param args the arguments of the error message (if any)
     */
    public ValidationException(String fieldName, String errorMessage, Object ... args) {
        super(errorMessage);
        addError(fieldName,errorMessage,args);
    }

    /**
     * Adds a new ValidationExceptionInfo to the collection of error messages
     *
     * @param fieldName the client identifier with which this message is associated (if any)
     * @param message the error message
     * @param args the arguments of the error message (if any)
     */
    public void addError(String fieldName, String message, Object ... args){
        errors.add(new ValidationExceptionInfo(fieldName,message, args));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of warning messages
     *
     * @param fieldName the client identifier with which this message is associated (if any)
     * @param message the warning message
     * @param args the arguments of the warning message (if any)
     */    
    public void addWarning(String fieldName, String message, Object ... args){
        warnings.add(new ValidationExceptionInfo(fieldName,message, args));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of error messages
     *
     * @param fieldName the client identifier with which this message is associated (if any)
     * @param message the error message
     */    
    public void addError(String fieldName, String message){
        errors.add(new ValidationExceptionInfo(fieldName,message, null));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of warning messages
     *
     * @param message the warning message
     * @param args the arguments of the warning message (if any)
     */    
    public void addWarning(String message, Object ... args){
        warnings.add(new ValidationExceptionInfo(null,message,args));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of error messages
     *
     * @param message the warning message
     * @param args the arguments of the error message (if any)
     */    
    public void addErrorWithParameters(String message, Object ... args){
        errors.add(new ValidationExceptionInfo(null,message,args));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of warning messages
     *
     * @param message the warning message
     * @param args the arguments of the warning message (if any)
     */    
    public void addWarningWithParameters(String message, Object ... args){
        warnings.add(new ValidationExceptionInfo(null,message,args));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of error messages
     *
     * @param message the error message
     */    
    public void addError(String message){
        errors.add(new ValidationExceptionInfo(null,message,null));
    }
    
    /**
     * Adds a new ValidationExceptionInfo to the collection of warning messages
     *
     * @param message the warning message
     */    
    public void addWarning(String message){
        warnings.add(new ValidationExceptionInfo(null,message,null));
    }
    
    /**
     * Recover the collection of error messages
     * 
     * @return collection of error messages
     */
    public Collection<ValidationExceptionInfo> getErrors(){
        return errors;
    }
    
    /**
     * Recover the collection of warning messages
     * 
     * @return collection of warning messages
     */
    public Collection<ValidationExceptionInfo> getWarnings(){
        return warnings;
    }
    
    /**
     * Indicates if there are error messages
     *
     * @return true if there are error messages
     */
    public boolean hasError(){
        return !errors.isEmpty();
    }

    /**
     * Indicates if there are warning messages
     *
     * @return true if there are warning messages
     */
    public boolean hasWarning(){
        return !warnings.isEmpty();
    }
    
    /**
     * Indicates if there are error or warning messages
     *
     * @return true if there are error or warning messages
     */    
    public boolean hasErrorOrWarning(){
        return hasError() || hasWarning();
    }
}