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.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if the field can safely be converted to a double primitive.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.//from  w w  w. j  a v  a 2s. com
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Double if valid, a null otherwise.
 */
public static Double validateDouble(Object bean, ValidatorAction va, Field field, Errors errors) {
    Double result = null;
    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatDouble(value);

        if (result == null) {
            FieldChecks.rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if the field is a valid date. If the field has a datePattern
 * variable, that will be used to format
 * <code>java.text.SimpleDateFormat</code>. If the field has a
 * datePatternStrict variable, that will be used to format
 * <code>java.text.SimpleDateFormat</code> and the length will be checked
 * so '2/12/1999' will not pass validation with the format 'MM/dd/yyyy'
 * because the month isn't two digits. If no datePattern variable is
 * specified, then the field gets the DateFormat.SHORT format for the
 * locale. The setLenient method is set to <code>false</code> for all
 * variations.//from ww w.  j  a  v  a  2  s.com
 *
 * @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>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return A Date if valid, a null if blank or invalid.
 */
public static Date validateDate(Object bean, ValidatorAction va, Field field, Errors errors) {

    Date result = null;
    String value = FieldChecks.extractValue(bean, field);
    String datePattern = field.getVarValue("datePattern");
    String datePatternStrict = field.getVarValue("datePatternStrict");

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            if (datePattern != null && datePattern.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePattern, false);
            } else if (datePatternStrict != null && datePatternStrict.length() > 0) {
                result = GenericTypeValidator.formatDate(value, datePatternStrict, true);
            }
        } catch (Exception e) {
            FieldChecks.log.error(e.getMessage(), e);
        }

        if (result == null) {
            FieldChecks.rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute)./* w ww .jav a 2s  . c  om*/
 *
 * @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>Errors</code> object to add errors to if any
 * validation errors occur.
 * @return <code>true</code> if in range, <code>false</code> otherwise.
 */
public static boolean validateIntRange(Object bean, ValidatorAction va, Field field, Errors errors) {
    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            int intValue = Integer.parseInt(value);
            int min = Integer.parseInt(field.getVarValue("min"));
            int max = Integer.parseInt(field.getVarValue("max"));

            if (!GenericValidator.isInRange(intValue, min, max)) {
                FieldChecks.rejectValue(errors, field, va);

                return false;
            }
        } catch (Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }
    return true;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute).//ww w. jav  a  2  s .  co 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>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateDoubleRange(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            double doubleValue = Double.parseDouble(value);
            double min = Double.parseDouble(field.getVarValue("min"));
            double max = Double.parseDouble(field.getVarValue("max"));

            if (!GenericValidator.isInRange(doubleValue, min, max)) {
                FieldChecks.rejectValue(errors, field, va);

                return false;
            }
        } catch (Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }
    return true;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if a fields value is within a range (min &amp; max specified in
 * the vars attribute).// w  w w  .j  a v a2  s .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>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return True if in range, false otherwise.
 */
public static boolean validateFloatRange(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            float floatValue = Float.parseFloat(value);
            float min = Float.parseFloat(field.getVarValue("min"));
            float max = Float.parseFloat(field.getVarValue("max"));
            if (!GenericValidator.isInRange(floatValue, min, max)) {
                FieldChecks.rejectValue(errors, field, va);
                return false;
            }
        } catch (Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }

    return true;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if the field is a valid credit card number.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.//from w  w w  .  j  av  a2 s  .  co  m
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return The credit card as a Long, a null if invalid, blank, or null.
 */
public static Long validateCreditCard(Object bean, ValidatorAction va, Field field, Errors errors) {

    Long result = null;
    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        result = GenericTypeValidator.formatCreditCard(value);

        if (result == null) {
            FieldChecks.rejectValue(errors, field, va);
        }
    }

    return result;
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if a field has a valid e-mail address.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed.//w  ww.  j a  v a 2s.c om
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return True if valid, false otherwise.
 */
public static boolean validateEmail(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value) && !GenericValidator.isEmail(value)) {
        FieldChecks.rejectValue(errors, field, va);
        return false;
    } else {
        return true;
    }
}

From source file:org.springmodules.validation.commons.FieldChecks.java

/**
 * Checks if the field's length is greater than or equal to the minimum
 * value. A <code>Null</code> will be considered an error.
 *
 * @param bean The bean validation is being performed on.
 * @param va The <code>ValidatorAction</code> that is currently being
 * performed./*w  w  w .  j ava  2  s  .c om*/
 * @param field The <code>Field</code> object associated with the current
 * field being validated.
 * @param errors The <code>Errors</code> object to add errors to if any
 * validation errors occur.
 * -param request
 * Current request object.
 * @return True if stated conditions met.
 */
public static boolean validateMinLength(Object bean, ValidatorAction va, Field field, Errors errors) {

    String value = FieldChecks.extractValue(bean, field);

    if (!GenericValidator.isBlankOrNull(value)) {
        try {
            int min = Integer.parseInt(field.getVarValue("minlength"));

            if (!GenericValidator.minLength(value, min)) {
                FieldChecks.rejectValue(errors, field, va);

                return false;
            }
        } catch (Exception e) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        }
    }

    return true;
}

From source file:org.squale.welcom.addons.access.bean.ProfileBean.java

/**
 * Met a jour un Profile en fonction de idProfile
 * /*from ww  w  .  j  a v  a2 s . c o m*/
 * @param profil : Profile
 * @throws ServletException : Probleme SQL
 */
public static void getProfileBean(final ProfileBean profil) throws ServletException {
    WStatement sta = null;
    java.sql.ResultSet rs = null;
    WJdbc jdbc = null;
    try {
        jdbc = new WJdbcMagic();

        if (!GenericValidator.isBlankOrNull(profil.getIdProfile())) {
            // Chargement du profil
            sta = jdbc.getWStatement();
            sta.add("select * from");
            sta.add(AddonsConfig.WEL_PROFILE);
            sta.addParameter("where idprofile=?", profil.getIdProfile());
            rs = sta.executeQuery();
            if ((rs != null) && rs.next()) {
                ResultSetUtils.populate(profil, rs);
            } else {
                new ServletException("idProfile not found");
            }
            sta.close();

            // Liste de reference
            final ArrayList listAccessInt = new ArrayList();
            final HashMap hashAccessInt = new HashMap();

            sta = jdbc.getWStatement();
            sta.add("select * from ");
            sta.add(AddonsConfig.WEL_PROFILE_ACCESSKEY_INT + " A,");
            sta.add(AddonsConfig.WEL_ACCESSKEY + " B where");
            sta.add("A.accesskey=B.accesskey and ");
            sta.addParameter("A.idprofile=?", profil.getIdProfile());
            sta.add("order by B.idAccessKey ");
            rs = sta.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    final AccessBean droits = new AccessBean();
                    ResultSetUtils.populate(droits, rs);
                    hashAccessInt.put(droits.getAccesskey(), droits);
                    listAccessInt.add(droits);
                }
            }
            sta.close();

            // Recupere son profil modifi via l'IHM de l'appli
            sta = jdbc.getWStatement();
            sta.add("select * from ");
            sta.add(AddonsConfig.WEL_PROFILE_ACCESSKEY);
            sta.add("where");
            sta.addParameter("idprofile=?", profil.getIdProfile());
            rs = sta.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    final AccessBean droits = new AccessBean();
                    ResultSetUtils.populate(droits, rs);
                    if (hashAccessInt.containsKey(droits.getAccesskey())) {
                        final AccessBean dr = (AccessBean) hashAccessInt.get(droits.getAccesskey());
                        dr.setValue(droits.getValue());
                    }
                }
            }
            profil.setAccessList(listAccessInt);
            sta.close();
        }

    } catch (final SQLException e) {
        throw new ServletException(e.getMessage());
    } finally {
        if (jdbc != null) {
            jdbc.close();
        }
    }

}

From source file:org.squale.welcom.addons.access.excel.filereader.AccessKeyReader10.java

/**
 * Lecture des droits de la pages//from w  ww  . ja  va  2 s. co  m
 * 
 * @throws AccessKeyReaderException : probleme de lecture
 */
public void readProfile() throws AccessKeyReaderException {
    final ArrayList arrayList = new ArrayList();
    final Cell cProfile = workbook.findCellByName("PROFILE");
    final Cell cProfileID = workbook.findCellByName("PROFILEID");

    // Parse en execption si on ne trouve pas de cellule contenant PROFILE / PROFILEID
    if ((cProfile == null) || (cProfileID == null)) {
        throw new AccessKeyReaderException("La cellule de profil est introuvable");
    }

    // Recupere l'ID de demarrage
    final int idStart = cProfile.getColumn();

    // Pour chaque cellule recupere l'id ...
    for (int i = idStart; i < sheet.getColumns(); i++) {

        // Si la cellule n'est pas vide
        if (!GenericValidator.isBlankOrNull(sheet.getCell(i, cProfileID.getRow()).getContents())
                && !GenericValidator.isBlankOrNull(sheet.getCell(i, cProfile.getRow()).getContents())) {
            final Profile profiles = new Profile();
            profiles.setIdProfile(sheet.getCell(i, cProfileID.getRow()).getContents().toUpperCase());
            profiles.setName(sheet.getCell(i, cProfile.getRow()).getContents());

            arrayList.add(profiles);

        }
    }

    // Stocke en cache
    cacheProfileList = arrayList;
}