Example usage for org.apache.commons.validator.util ValidatorUtils getValueAsString

List of usage examples for org.apache.commons.validator.util ValidatorUtils getValueAsString

Introduction

In this page you can find the example usage for org.apache.commons.validator.util ValidatorUtils getValueAsString.

Prototype

public static String getValueAsString(Object bean, String property) 

Source Link

Document

Convenience method for getting a value from a bean property as a String.

Usage

From source file:org.megatome.frame2.validator.CommonsFieldValidator.java

/**
 * Validate a value that is required if a certain circumstance is met.
 * /*from  www.ja  v a2 s  . c om*/
 * @param bean
 *            The event containing the field to validate
 * @param va
 *            The validator action
 * @param errors
 *            Errors object to populate
 * @param field
 *            The field to validate
 * @return True if validation passes
 */
public static boolean validateRequiredIf(Object bean, ValidatorAction va, Errors errors, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    boolean required = false;
    int i = 0;
    String fieldJoin = "AND"; //$NON-NLS-1$
    if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) { //$NON-NLS-1$
        fieldJoin = field.getVarValue("fieldJoin"); //$NON-NLS-1$
    }
    if (fieldJoin.equalsIgnoreCase("AND")) { //$NON-NLS-1$
        required = true;
    }
    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i //$NON-NLS-1$
            + "]"))) { //$NON-NLS-1$
        String dependProp = field.getVarValue("field[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        String dependTest = field.getVarValue("fieldTest[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        String dependTestValue = field.getVarValue("fieldValue[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
        if (dependIndexed == null) {
            dependIndexed = "false"; //$NON-NLS-1$
        }
        String dependVal = null;
        boolean thisRequired = false;
        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) { //$NON-NLS-1$
            String key = field.getKey();
            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) { //$NON-NLS-1$ //$NON-NLS-2$
                String ind = key.substring(0, key.indexOf(".") + 1); //$NON-NLS-1$
                dependProp = ind + dependProp;
            }
        }
        dependVal = ValidatorUtils.getValueAsString(bean, dependProp);
        if (dependTest.equals(FIELD_TEST_NULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = false;
            } else {
                thisRequired = true;
            }
        }
        if (dependTest.equals(FIELD_TEST_NOTNULL)) {
            if ((dependVal != null) && (dependVal.length() > 0)) {
                thisRequired = true;
            } else {
                thisRequired = false;
            }
        }
        if (dependTest.equals(FIELD_TEST_EQUAL)) {
            thisRequired = dependTestValue.equalsIgnoreCase(dependVal);
        }
        if (fieldJoin.equalsIgnoreCase("AND")) { //$NON-NLS-1$
            required = required && thisRequired;
        } else {
            required = required || thisRequired;
        }
        i++;
    }
    if (required) {
        if (GenericValidator.isBlankOrNull(value)) {
            addError(va, errors, field, null);
            return false;
        }

        return true;
    }
    return true;
}

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

/**
 * Checks if the field is required.//www  .j  a  va2  s.  c  om
 *
 * @return boolean If the field isn't <code>null</code> and
 * has a length greater than zero, <code>true</code> is returned.  
 * Otherwise <code>false</code>.
 */
public static boolean validateRequired(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    return !GenericValidator.isBlankOrNull(value);
}

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

/**
 * /*from   www.  ja  v  a2  s. c  o  m*/
 * @param bean
 * @param field
 * @return
 */
public static boolean minimumLength(Object bean, Field field) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String varValue = field.getVarValue("minimumLength");
    return GenericValidator.minLength(value, Integer.parseInt(varValue));
}

From source file:org.openmrs.contrib.metadatarepository.webapp.util.ValidationUtil.java

/**
 * Validates that two fields match.//from   w  ww  . ja v a  2 s .c om
 * @param bean
 * @param va
 * @param field
 * @param errors
 */
public static boolean validateTwoFields(Object bean, ValidatorAction va, Field field, Errors errors) {
    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (!value.equals(value2)) {
                FieldChecks.rejectValue(errors, field, va);
                return false;
            }
        } catch (Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }

    return true;
}

From source file:org.patientview.patientview.patiententry.SysGreaterThanDiaValidator.java

public static boolean sysGreaterThanDia(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request) {//  w w  w  . j  a  va 2s .c  o  m

    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
    String sProperty2 = field.getVarValue("secondProperty");
    String value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            int intValue = Integer.decode(value);
            int intValue2 = Integer.decode(value2);
            if (intValue2 <= intValue) {
                errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
                return false;
            }
        } catch (Exception e) {
            errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
            return false;
        }
    }

    return true;
}

From source file:org.patientview.patientview.utils.NhsnoChecksum.java

public static boolean nhsnoChecksum(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request) {/*from  w w w .  ja  va2s  .  co m*/

    String value = ValidatorUtils.getValueAsString(bean, field.getProperty());

    boolean checksumValid = UserUtils.isNhsNumberValid(value);

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

    return checksumValid;
}

From source file:org.pentaho.di.job.entry.validator.FileDoesNotExistValidator.java

public boolean validate(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks, ValidatorContext context) {

    String filename = ValidatorUtils.getValueAsString(source, propertyName);
    VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context);
    boolean failIfExists = getFailIfExists(source, propertyName, remarks, context);

    if (null == variableSpace) {
        return false;
    }//w  w w.jav a  2  s.c  o  m

    String realFileName = variableSpace.environmentSubstitute(filename);
    FileObject fileObject = null;
    try {
        fileObject = KettleVFS.getFileObject(realFileName, variableSpace);

        if (fileObject.exists() && failIfExists) {
            JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks,
                    JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
            return false;
        }
        try {
            fileObject.close(); // Just being paranoid
        } catch (IOException ignored) {
            // Ignore close errors
        }
    } catch (Exception e) {
        JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
        return false;
    }
    return true;
}

From source file:org.pentaho.di.job.entry.validator.FileExistsValidator.java

public boolean validate(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks, ValidatorContext context) {

    String filename = ValidatorUtils.getValueAsString(source, propertyName);
    VariableSpace variableSpace = getVariableSpace(source, propertyName, remarks, context);
    boolean failIfDoesNotExist = getFailIfDoesNotExist(source, propertyName, remarks, context);

    if (null == variableSpace) {
        return false;
    }/*  ww  w. j  a v  a  2s . com*/

    String realFileName = variableSpace.environmentSubstitute(filename);
    FileObject fileObject = null;
    try {
        fileObject = KettleVFS.getFileObject(realFileName, variableSpace);
        if (fileObject == null || (fileObject != null && !fileObject.exists() && failIfDoesNotExist)) {
            JobEntryValidatorUtils.addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks,
                    JobEntryValidatorUtils.getLevelOnFail(context, VALIDATOR_NAME));
            return false;
        }
        try {
            fileObject.close(); // Just being paranoid
        } catch (IOException ignored) {
            // Ignore close errors
        }
    } catch (Exception e) {
        JobEntryValidatorUtils.addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
        return false;
    }
    return true;
}

From source file:org.pentaho.di.job.entry.validator.JobEntryValidatorUtils.java

/**
 * Fails if a field's value does not match the given mask.
 *//*  w w w .  j  a v a  2 s .  c o  m*/
public static boolean validateMask(CheckResultSourceInterface source, String propertyName, int levelOnFail,
        List<CheckResultInterface> remarks, String mask) {
    final String VALIDATOR_NAME = "matches";
    String value = null;

    value = ValidatorUtils.getValueAsString(source, propertyName);

    try {
        if (null == mask) {
            addGeneralRemark(source, propertyName, VALIDATOR_NAME, remarks, "errors.missingVar",
                    CheckResultInterface.TYPE_RESULT_ERROR);
            return false;
        }

        if (StringUtils.isNotBlank(value) && !GenericValidator.matchRegexp(value, mask)) {
            addFailureRemark(source, propertyName, VALIDATOR_NAME, remarks, levelOnFail);
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        addExceptionRemark(source, propertyName, VALIDATOR_NAME, remarks, e);
        return false;
    }
}

From source file:org.pentaho.di.job.entry.validator.JobEntryValidatorUtils.java

public static void addOkRemark(CheckResultSourceInterface source, String propertyName,
        List<CheckResultInterface> remarks) {
    final int SUBSTRING_LENGTH = 20;
    String value = ValidatorUtils.getValueAsString(source, propertyName);
    String substr = null;/*from   w  w w . j  a  v  a2 s .  c o m*/
    if (value != null) {
        substr = value.substring(0, Math.min(SUBSTRING_LENGTH, value.length()));
        if (value.length() > SUBSTRING_LENGTH) {
            substr += "...";
        }
    }
    remarks.add(new CheckResult(CheckResultInterface.TYPE_RESULT_OK,
            ValidatorMessages.getString("messages.passed", propertyName, substr), source));
}