Example usage for org.apache.commons.validator Validator BEAN_PARAM

List of usage examples for org.apache.commons.validator Validator BEAN_PARAM

Introduction

In this page you can find the example usage for org.apache.commons.validator Validator BEAN_PARAM.

Prototype

String BEAN_PARAM

To view the source code for org.apache.commons.validator Validator BEAN_PARAM.

Click Source Link

Document

Resources key the JavaBean is stored to perform validation on.

Usage

From source file:us.mn.state.health.lims.common.util.validator.ValidWhen.java

/**
 * Checks if the field matches the boolean expression specified in
 * <code>test</code> parameter.
 * //from  w ww  . j a v a2s.  c  o  m
 * @param bean
 *            The bean validation is being performed on.
 * 
 * @param va
 *            The <code>ValidatorAction</code> that is currently being
 *            performed.
 * 
 * @param field
 *            The <code>Field</code> object associated with the current
 *            field being validated.
 * 
 * @param errors
 *            The <code>ActionMessages</code> object to add errors to if
 *            any validation errors occur.
 * 
 * @param request
 *            Current request object.
 * 
 * @return <code>true</code> if meets stated requirements,
 *         <code>false</code> otherwise.
 */
public static boolean validateValidWhen(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {

    Object form = validator.getParameterValue(Validator.BEAN_PARAM);
    String value = null;
    boolean valid = false;
    int index = -1;

    if (field.isIndexed()) {
        String key = field.getKey();

        final int leftBracket = key.indexOf("[");
        final int rightBracket = key.indexOf("]");

        if ((leftBracket > -1) && (rightBracket > -1)) {
            index = Integer.parseInt(key.substring(leftBracket + 1, rightBracket));
        }
    }

    if (isString(bean)) {
        value = (String) bean;
    } else {
        value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    }

    String test = field.getVarValue("test");
    if (test == null) {
        String msg = "ValidWhen Error 'test' parameter is missing for field ' " + field.getKey() + "'";
        errors.add(field.getKey(), new ActionMessage(msg, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", msg);
        return false;
    }

    // Create the Lexer
    ValidWhenLexer lexer = null;
    try {
        lexer = new ValidWhenLexer(new StringReader(test));
    } catch (Exception ex) {
        String msg = "ValidWhenLexer Error for field ' " + field.getKey() + "' - " + ex;
        errors.add(field.getKey(), new ActionMessage(msg + " - " + ex, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", ex.toString());
        LogEvent.logDebug("ValidWhen", "validateValidWhen", msg);
        return false;
    }

    // Create the Parser
    ValidWhenParser parser = null;
    try {
        parser = new ValidWhenParser(lexer);
    } catch (Exception ex) {
        String msg = "ValidWhenParser Error for field ' " + field.getKey() + "' - " + ex;
        errors.add(field.getKey(), new ActionMessage(msg, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", ex.toString());
        LogEvent.logDebug("ValidWhen", "validateValidWhen", msg);
        return false;
    }
    parser.setForm(form);
    parser.setIndex(index);
    parser.setValue(value);

    try {
        parser.expression();
        valid = parser.getResult();

    } catch (Exception ex) {

        // errors.add(
        // field.getKey(),
        // Resources.getActionMessage(validator, request, va, field));

        String msg = "ValidWhen Error for field ' " + field.getKey() + "' - " + ex;
        errors.add(field.getKey(), new ActionMessage(msg, false));
        //bugzilla 2154
        LogEvent.logError("ValidWhen", "validateValidWhen()", ex.toString());
        LogEvent.logDebug("ValidWhen", "validateValidWhen", msg);

        return false;
    }

    if (!valid) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    }

    return true;
}