Example usage for org.apache.commons.validator GenericValidator isBlankOrNull

List of usage examples for org.apache.commons.validator GenericValidator isBlankOrNull

Introduction

In this page you can find the example usage for org.apache.commons.validator GenericValidator isBlankOrNull.

Prototype

public static boolean isBlankOrNull(String value) 

Source Link

Document

Checks if the field isn't null and length of the field is greater than zero not including whitespace.

Usage

From source file:org.agnitas.service.csv.validator.FieldsValidator.java

/**
 * Checks if the field can be successfully converted to a <code>date</code>.
 *
 * @param value The value validation is being performed on.
 * @return boolean If the field can be successfully converted
 *         to a <code>date</code> <code>true</code> is returned.
 *         Otherwise <code>false</code>.
 */// w  w  w.  jav  a 2 s  . c o  m
public static boolean validateDate(Object bean, Field field, Validator validator) {
    String value = getValueAsString(bean, field);
    final ImportProfile profile = (ImportProfile) validator
            .getParameterValue("org.agnitas.beans.ImportProfile");
    final ColumnMapping currentColumn = getColumnMappingForCurentField(field, profile);
    if (currentColumn != null && currentColumn.getMandatory()) {
        return !GenericValidator.isBlankOrNull(value)
                && GenericValidator.isDate(value, DateFormat.getValue(profile.getDateFormat()), true);
    } else {
        return currentColumn == null
                || GenericValidator.isDate(value, DateFormat.getValue(profile.getDateFormat()), true);
    }
}

From source file:org.agnitas.web.forms.ImportProfileForm.java

public ActionErrors formSpecificValidate(ActionMapping actionMapping, HttpServletRequest request) {
    ActionErrors actionErrors = new ActionErrors();

    if (action == ImportProfileAction.ACTION_SAVE) {
        if (AgnUtils.parameterNotEmpty(request, "save")) {
            if (profile.getName().length() < 3) {
                actionErrors.add("shortname", new ActionMessage("error.nameToShort"));
            }//from   w ww .ja  v  a 2  s .c  o m
            if (!GenericValidator.isBlankOrNull(profile.getMailForReport())
                    && !GenericValidator.isEmail(profile.getMailForReport())) {
                actionErrors.add("mailForReport", new ActionMessage("error.invalid.email"));
            }
        }
    }
    return actionErrors;
}

From source file:org.agnitas.web.NewImportWizardAction.java

/**
 * Sends import report if profile has emailForReport
 *
 * @param request request/*from w  w w  .j  av  a  2s . c o  m*/
 * @param aForm   a form
 */
private void sendReportEmail(HttpServletRequest request, NewImportWizardForm aForm) {
    ImportProfileDao profileDao = (ImportProfileDao) getWebApplicationContext().getBean("ImportProfileDao");
    ImportProfile profile = profileDao.getImportProfileById(aForm.getDefaultProfileId());
    String address = profile.getMailForReport();
    if (!GenericValidator.isBlankOrNull(address) && GenericValidator.isEmail(address)) {
        Locale locale = (Locale) request.getSession().getAttribute(org.apache.struts.Globals.LOCALE_KEY);
        ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
        Collection<EmailAttachment> attachments = new ArrayList<EmailAttachment>();
        File invalidRecipientsFile = aForm.getInvalidRecipientsFile();
        File fixedRecipientsFile = aForm.getFixedRecipientsFile();
        EmailAttachment invalidRecipients = createZipAttachment(invalidRecipientsFile, "invalid_recipients.zip",
                bundle.getString("import.recipients.invalid"));
        if (invalidRecipients != null) {
            attachments.add(invalidRecipients);
        }
        EmailAttachment fixedRecipients = createZipAttachment(fixedRecipientsFile, "fixed_recipients.zip",
                bundle.getString("import.recipients.fixed"));
        if (fixedRecipients != null) {
            attachments.add(fixedRecipients);
        }
        EmailAttachment[] attArray = attachments.toArray(new EmailAttachment[] {});
        String subject = bundle.getString("import.recipients.report");
        String message = generateReportEmailBody(bundle, aForm);
        message = subject + ":\n" + message;
        ImportUtils.sendEmailWithAttachments(AgnUtils.getDefaultValue("import.report.from.address"),
                AgnUtils.getDefaultValue("import.report.from.name"), address, subject, message, attArray);
    }
}

From source file:org.apache.beehive.netui.pageflow.validation.ValidatorRules.java

/**
 * Check if a given expression evaluates to <code>true</code>.
 * /*from   ww  w .  j  av a 2  s. c  om*/
 * @param bean the bean that 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 the current request object.
 * @return <code>true</code> if the given expression evaluates to <code>true</code>
 */
public static boolean validateValidWhen(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request, ServletContext servletContext) {

    String value;

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

    if (!GenericValidator.isBlankOrNull(value)) {
        String condition = field.getVarValue("netui_validwhen");

        try {
            if (!InternalExpressionUtils.evaluateCondition(condition, bean, request, servletContext)) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
                return false;
            }
        } catch (Exception e) {
            _log.error("Error evaluating expression " + condition + " for ValidWhen rule on field "
                    + field.getProperty() + " on bean of type "
                    + (bean != null ? bean.getClass().getName() : null));

            errors.add(field.getKey(), Resources.getActionError(request, va, field));
            return false;
        }
    }

    return true;
}

From source file:org.apache.beehive.netui.pageflow.validation.ValidatorRules.java

/**
 * Check if a field's value is within a range ("min" and "max" Long variables on the passed-in Field).
 *
 * @param bean the bean that 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 the current request object.
 * @return <code>true</code> if in range, false otherwise.
 *//* w w w  . j  a  va2 s .com*/
public static boolean validateLongRange(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        HttpServletRequest request) {

    String value;

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

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            long longValue = Long.parseLong(value);
            long min = Long.parseLong(field.getVarValue("min"));
            long max = Long.parseLong(field.getVarValue("max"));

            if (longValue < min || longValue > max) {
                errors.add(field.getKey(), Resources.getActionError(request, va, field));
                return false;
            }
        } catch (Exception e) {
            errors.add(field.getKey(), Resources.getActionError(request, va, field));
            return false;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field isn't null and length of the field is greater than
 * zero not including whitespace./*w w w.  j  a  va 2s .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 validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if meets stated requirements, false otherwise.
 */
public static boolean validateRequired(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

        return false;
    } else {
        return true;
    }
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field isn't null based on the values of other fields.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed./* w  ww  . jav  a 2s .  c o m*/
 * @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 validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if meets stated requirements, false otherwise.
 */
public static boolean validateRequiredIf(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object form = validator.getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM);
    String value = null;
    boolean required = false;

    value = evaluateBean(bean, field);

    int i = 0;
    String fieldJoin = "AND";

    if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
        fieldJoin = field.getVarValue("fieldJoin");
    }

    if (fieldJoin.equalsIgnoreCase("AND")) {
        required = true;
    }

    while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
        String dependProp = field.getVarValue("field[" + i + "]");
        String dependTest = field.getVarValue("fieldTest[" + i + "]");
        String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
        String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");

        if (dependIndexed == null) {
            dependIndexed = "false";
        }

        String dependVal = null;
        boolean thisRequired = false;

        if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
            String key = field.getKey();

            if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
                String ind = key.substring(0, key.indexOf(".") + 1);

                dependProp = ind + dependProp;
            }
        }

        dependVal = ValidatorUtils.getValueAsString(form, 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")) {
            required = required && thisRequired;
        } else {
            required = required || thisRequired;
        }

        i++;
    }

    if (required) {
        if (GenericValidator.isBlankOrNull(value)) {
            errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));

            return false;
        } else {
            return true;
        }
    }

    return true;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a byte primitive.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//  ww  w.  ja va  2s . co m
 * @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 validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if valid, false otherwise.
 */
public static Object validateByte(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object result = null;
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return Boolean.TRUE;
    }

    result = GenericTypeValidator.formatByte(value);

    if (result == null) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
    }

    return (result == null) ? Boolean.FALSE : result;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a byte primitive.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//w  w  w.  java  2  s . c  o  m
 * @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 validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if valid, false otherwise.
 */
public static Object validateByteLocale(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object result = null;
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return Boolean.TRUE;
    }

    Locale locale = RequestUtils.getUserLocale(request, null);

    result = GenericTypeValidator.formatByte(value, locale);

    if (result == null) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
    }

    return (result == null) ? Boolean.FALSE : result;
}

From source file:org.apache.struts.validator.FieldChecks.java

/**
 * Checks if the field can safely be converted to a short primitive.
 *
 * @param bean      The bean validation is being performed on.
 * @param va        The <code>ValidatorAction</code> that is currently
 *                  being performed.//  w  w w  .  j a v  a 2s . c o m
 * @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 validator The <code>Validator</code> instance, used to access
 *                  other field values.
 * @param request   Current request object.
 * @return true if valid, false otherwise.
 */
public static Object validateShort(Object bean, ValidatorAction va, Field field, ActionMessages errors,
        Validator validator, HttpServletRequest request) {
    Object result = null;
    String value = null;

    value = evaluateBean(bean, field);

    if (GenericValidator.isBlankOrNull(value)) {
        return Boolean.TRUE;
    }

    result = GenericTypeValidator.formatShort(value);

    if (result == null) {
        errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field));
    }

    return (result == null) ? Boolean.FALSE : result;
}