Example usage for org.apache.commons.validator ValidatorResult getActionMap

List of usage examples for org.apache.commons.validator ValidatorResult getActionMap

Introduction

In this page you can find the example usage for org.apache.commons.validator ValidatorResult getActionMap.

Prototype

@Deprecated
public Map<String, ResultStatus> getActionMap() 

Source Link

Document

Return a Map of the validator actions in this Result.

Usage

From source file:com.sapienter.jbilling.common.GatewayBL.java

public boolean validate(String name, Object bean) throws ValidatorException {
    // only validate there's no errors already detected
    if (code != RES_CODE_OK) {
        return false;
    }/*from ww  w .  j  a va  2  s .c o m*/
    boolean success = true;
    ;
    // Tell the validator which bean to validate against.
    validator.setFormName(name);
    validator.setParameter(Validator.BEAN_PARAM, bean);

    ValidatorResults results = null;

    // Run the validation actions against the bean.
    log.info("Validating " + name); // can't print the bean... it put plain credit card numbers in the logs
    results = validator.validate();
    Form form = resources.getForm(Locale.getDefault(), name);
    // Iterate over each of the properties of the Bean which had messages.
    Iterator propertyNames = results.getPropertyNames().iterator();
    while (propertyNames.hasNext()) {
        String propertyName = (String) propertyNames.next();
        // log.debug("name " + propertyName);

        // Get the Field associated with that property in the Form
        Field field = form.getField(propertyName);

        // Look up the formatted name of the field from the Field arg0
        // String prettyFieldName =
        // apps.getString(field.getArg(0).getKey());

        // Get the result of validating the property.
        ValidatorResult result = results.getValidatorResult(propertyName);

        // Get all the actions run against the property, and iterate over
        // their names.
        Map actionMap = result.getActionMap();
        Iterator keys = actionMap.keySet().iterator();
        while (keys.hasNext()) {
            String actName = (String) keys.next();

            // Get the Action for that name.
            ValidatorAction action = resources.getValidatorAction(actName);

            /*
             * If the result is valid, print PASSED, otherwise print FAILED
             * log.debug(propertyName + "[" + actName + "] (" +
             * (result.isValid(actName) ? "PASSED" : "FAILED") + ")");
             */

            // If the result failed, format the Action's message against the
            // formatted field name
            if (!result.isValid(actName)) {
                if (!success) {
                    text += "-";
                } else {
                    success = false;
                    code = RES_CODE_ERROR;
                    subCode = RES_SUB_CODE_ERR_VALIDATION;
                }
                String message = apps.getString(action.getMsg());
                // read the variables
                Map vars = field.getVars();
                // will need some changes to accomodate 'range'
                Object[] args = new Object[2];
                args[0] = propertyName;
                Var var = field.getVar(actName);
                if (var != null) {
                    args[1] = var.getValue();
                }
                text += "Object type " + name + ":" + MessageFormat.format(message, args);
            }
        }
    }
    log.debug("Done. result " + success + " message:" + text);
    return success;
}

From source file:org.agnitas.util.ImportUtils.java

public static Boolean checkIsCurrentFieldValid(ValidatorResults results, String propertyName) {
    // Get the result of validating the property.
    final ValidatorResult result = results.getValidatorResult(propertyName);
    // Get all the actions run against the property, and iterate over their names.
    if (result == null) {
        return true;
    }//from   w w w  .  j a v a  2 s. c om
    Map actionMap = result.getActionMap();
    for (Object action : actionMap.keySet()) {
        String actName = (String) action;

        if (!result.isValid(actName)) {
            return false;
        }

    }
    return true;
}

From source file:org.agnitas.util.ImportUtils.java

public static Boolean checkIsCurrentFieldValid(ValidatorResults results, String propertyName,
        String actionName) {//from w w w  . java2  s  .co  m
    // Get the result of validating the property.
    final ValidatorResult result = results.getValidatorResult(propertyName);
    // Get all the actions run against the property, and iterate over their names.
    if (result == null) {
        return true;
    }
    Map actionMap = result.getActionMap();
    for (Object action : actionMap.keySet()) {
        String actName = (String) action;

        if (!result.isValid(actName) && actName.equals(actionName)) {
            return false;
        }

    }
    return true;
}

From source file:org.apache.commons.validator.example.ValidateExample.java

/**
 * Dumps out the Bean in question and the results of validating it.
 *///w w w . jav  a  2  s  . com
public static void printResults(Object bean, ValidatorResults results, ValidatorResources resources) {

    boolean success = true;

    // Start by getting the form for the current locale and Bean.
    Form form = resources.getForm(Locale.getDefault(), "ValidateBean");

    System.out.println("\n\nValidating:");
    System.out.println(bean);

    // Iterate over each of the properties of the Bean which had messages.
    Iterator propertyNames = results.getPropertyNames().iterator();
    while (propertyNames.hasNext()) {
        String propertyName = (String) propertyNames.next();

        // Get the Field associated with that property in the Form
        Field field = form.getField(propertyName);

        // Look up the formatted name of the field from the Field arg0
        String prettyFieldName = apps.getString(field.getArg(0).getKey());

        // Get the result of validating the property.
        ValidatorResult result = results.getValidatorResult(propertyName);

        // Get all the actions run against the property, and iterate over their names.
        Map actionMap = result.getActionMap();
        Iterator keys = actionMap.keySet().iterator();
        while (keys.hasNext()) {
            String actName = (String) keys.next();

            // Get the Action for that name.
            ValidatorAction action = resources.getValidatorAction(actName);

            // If the result is valid, print PASSED, otherwise print FAILED
            System.out.println(propertyName + "[" + actName + "] ("
                    + (result.isValid(actName) ? "PASSED" : "FAILED") + ")");

            //If the result failed, format the Action's message against the formatted field name
            if (!result.isValid(actName)) {
                success = false;
                String message = apps.getString(action.getMsg());
                Object[] args = { prettyFieldName };
                System.out.println("     Error message will be: " + MessageFormat.format(message, args));

            }
        }
    }
    if (success) {
        System.out.println("FORM VALIDATION PASSED");
    } else {
        System.out.println("FORM VALIDATION FAILED");
    }

}

From source file:org.hyperic.util.validator.common.CommonValidator.java

/**
 * Perform all form-specific the validation. All form specific validation
 * is performed and then finally a CommonValidatorException object will be
 * thrown if (AND ONLY IF) validation errors were detected. The exception
 * object will contain a collection of error Strings see
 * {@ref CommonValidatorException}./*ww w . ja  v a 2 s  .  c o  m*/
 *
 * @param validationMappingRes A String containing the name of the
 * mapping resource file.
 * @param formName A String containing the name of the form containing
 * the validation action references.
 * @param beanInstance An instance of the bean to apply validation on.
 * @throws CommonValidatorException - Exception containing collection of
 * messages.
 **/
public void validate(String validationMappingRes, String formName, Object beanInstance)
        throws CommonValidatorException, SAXException {
    InputStream xmlStream = null;
    CommonValidatorException cve = null;
    ValidatorResults results;

    try {
        // Start by setting the "ValidatorResources" object. Its only
        // created if necessary. Contains FormSets stored against locale.
        setValidatorResources(validationMappingRes, beanInstance, xmlStream);

        // Get the form for the current locale and Bean.
        Form form = _validatorResources.getForm(Locale.getDefault(), formName);

        // Instantiate the validator (coordinates the validation
        // while the ValidatorResources implements the validation)
        Validator validator = new Validator(_validatorResources, formName);

        // Tell the validator which bean to validate against.
        validator.setParameter(Validator.BEAN_PARAM, beanInstance);

        // Get the results
        results = validator.validate();

        // Localize a reference for future access.
        setValidatorResults(results);

        // Iterate over each of the properties of the Bean which had messages.
        Iterator propertyNames = results.getPropertyNames().iterator();

        while (propertyNames.hasNext()) {
            // There were errors. Instantiate CVE

            String propertyName = (String) propertyNames.next();

            // Get the Field associated with that property in the Form
            Field field = (Field) form.getField(propertyName);

            // Look up the formatted name of the field from the Field arg0
            String prettyFieldName = _properties.getString(field.getArg(0).getKey());

            // Get the result of validating the property.
            ValidatorResult result = results.getValidatorResult(propertyName);

            // Get all the actions run against the property, and iterate
            // over their names. Check for invalid results.
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            while (keys.hasNext()) {
                String actName = (String) keys.next();
                // Get the Action for that name.
                ValidatorAction action = _validatorResources.getValidatorAction(actName);
                if (!result.isValid(actName)) {
                    String message = _properties.getString(action.getMsg());
                    Object[] args = { prettyFieldName };
                    if (cve == null) {
                        cve = new CommonValidatorException();
                    }
                    cve.addMessage(MessageFormat.format(message, args));
                }
            }
        }
    } catch (IOException ex) {
        // Note: This exception shouldn't be reported to user since it
        // wasn't likely caused by user input.
        ex.printStackTrace(); //log this
    } catch (ValidatorException ex) {
        // Note: This exception shouldn't bubble up to user since it
        // wasn't likely caused by user input.
        ex.printStackTrace(); //log this
    } finally {
        // Make sure we close the input stream.
        if (xmlStream != null)
            try {
                xmlStream.close();
            } catch (Exception e) {
            }
        // Lastly, if we had any invalid fields, throw CVE.
        if (cve != null)
            throw cve;
    }
}

From source file:org.openmobster.core.common.validation.ObjectValidator.java

/**
 * Validates the value of the specified Field on the target Object
 * // w  ww  .  jav  a  2 s  . c om
 * @param object
 * @param fieldName
 * @return a Set of Validation Error Keys
 * @throws ValidationException
 */
public Set<String> validate(Object object, String fieldName) throws ValidationException {
    try {
        Set<String> errorKeys = new HashSet<String>();
        String objectId = object.getClass().getName();

        //Setup the Validator
        Validator validator = new Validator(this.validatorResources, objectId);
        validator.setParameter(Validator.BEAN_PARAM, object);
        validator.setFieldName(fieldName);

        ValidatorResults results = validator.validate();

        Form form = this.validatorResources.getForm(Locale.getDefault(), objectId);
        Iterator propertyNames = results.getPropertyNames().iterator();
        while (propertyNames.hasNext()) {
            String property = (String) propertyNames.next();
            ValidatorResult result = results.getValidatorResult(property);
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            while (keys.hasNext()) {
                String actionName = (String) keys.next();
                if (!result.isValid(actionName)) {
                    Field field = form.getField(property);
                    Arg[] args = field.getArgs(actionName);
                    if (args != null) {
                        for (int i = 0; i < args.length; i++) {
                            errorKeys.add(args[i].getKey());
                        }
                    }
                }
            }
        }

        return errorKeys;
    } catch (Exception e) {
        log.error(this, e);
        throw new ValidationException(e);
    }
}

From source file:org.openmobster.core.common.validation.ObjectValidator.java

/**
 * Fully validates all the Fields of the specified Object
 * //from   www .ja  v a  2  s  .c  o m
 * @param object
 * @return a Map of Fields to their corresponding Validation Error Keys
 * 
 * @throws ValidationException
 */
public Map<String, String[]> validate(Object object) throws ValidationException {
    try {
        Map<String, String[]> errorKeys = new HashMap<String, String[]>();
        String objectId = object.getClass().getName();

        //Setup the Validator
        Validator validator = new Validator(this.validatorResources, objectId);
        validator.setParameter(Validator.BEAN_PARAM, object);

        ValidatorResults results = validator.validate();

        Form form = this.validatorResources.getForm(Locale.getDefault(), objectId);
        Iterator propertyNames = results.getPropertyNames().iterator();
        while (propertyNames.hasNext()) {
            Set<String> cour = new HashSet<String>();
            String property = (String) propertyNames.next();
            ValidatorResult result = results.getValidatorResult(property);
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            boolean errorFound = false;
            while (keys.hasNext()) {
                String actionName = (String) keys.next();
                if (!result.isValid(actionName)) {
                    Field field = form.getField(property);
                    Arg[] args = field.getArgs(actionName);
                    if (args != null) {
                        for (int i = 0; i < args.length; i++) {
                            cour.add(args[i].getKey());
                            errorFound = true;
                        }
                    }
                }
            }
            if (errorFound) {
                errorKeys.put(property, cour.toArray(new String[cour.size()]));
            }
        }

        return errorKeys;
    } catch (Exception e) {
        log.error(this, e);
        throw new ValidationException(e);
    }
}