Example usage for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace

List of usage examples for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace

Introduction

In this page you can find the example usage for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace.

Prototype

public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode) 

Source Link

Document

Reject the given field with the given error code if the value is empty or just contains whitespace.

Usage

From source file:org.openmrs.validator.ConceptSourceValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * //w w  w.j  a  va  2 s  .co  m
 *     * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation if HL7 Code is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) throws IllegalArgumentException {
    if (obj == null || !(obj instanceof ConceptSource)) {
        throw new IllegalArgumentException(
                "The parameter obj should not be null and must be of type " + ConceptSource.class);
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "hl7Code", "description",
                "retireReason");
    }

}

From source file:org.openmrs.validator.EncounterTypeValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * //ww  w.java2  s .c om
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should fail validation if name is duplicate
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation for an existing EncounterType
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    EncounterType encounterType = (EncounterType) obj;
    if (encounterType == null) {
        errors.rejectValue("encounterType", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");

        if (!errors.hasErrors()) {
            EncounterType duplicate = Context.getEncounterService()
                    .getEncounterType(encounterType.getName().trim());
            if (duplicate != null && !OpenmrsUtil.nullSafeEquals(encounterType.getUuid(), duplicate.getUuid())
                    && !duplicate.isRetired()) {
                errors.rejectValue("name", "EncounterType.error.duplicateEncounterTypeNameSpecified",
                        "Specified Encounter Type name already exists, please specify another ");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "retireReason");
    }
}

From source file:org.openmrs.validator.EncounterValidator.java

/**
 * Validates the given Encounter. Currently checks if the patient has been set and also ensures
 * that the patient for an encounter and the visit it is associated to if any, are the same.
 *
 * @param obj The encounter to validate.
 * @param errors Errors//from w w  w  .j a va2s .co m
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if the patients for the visit and the encounter dont match
 * @should fail if patient is not set
 * @should fail if encounter type is not set
 * @should fail if encounter dateTime is not set
 * @should fail if encounter dateTime is after current dateTime
 * @should fail if encounter dateTime is before visit startDateTime
 * @should fail if encounter dateTime is after visit stopDateTime
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) throws APIException {
    if (log.isDebugEnabled()) {
        log.debug(this.getClass().getName() + ".validate...");
    }

    if (obj == null || !(obj instanceof Encounter)) {
        throw new IllegalArgumentException(
                "The parameter obj should not be null and must be of type " + Encounter.class);
    }

    Encounter encounter = (Encounter) obj;

    ValidationUtils.rejectIfEmpty(errors, "encounterType", "Encounter.error.encounterType.required",
            "Encounter type is Required");

    ValidationUtils.rejectIfEmpty(errors, "patient", "Encounter.error.patient.required", "Patient is required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "encounterDatetime", "Encounter.datetime.required");
    if (encounter.getVisit() != null
            && !ObjectUtils.equals(encounter.getVisit().getPatient(), encounter.getPatient())) {
        errors.rejectValue("visit", "Encounter.visit.patients.dontMatch",
                "The patient for the encounter and visit should be the same");
    }

    Date encounterDateTime = encounter.getEncounterDatetime();

    if (encounterDateTime != null && encounterDateTime.after(new Date())) {
        errors.rejectValue("encounterDatetime", "Encounter.datetimeShouldBeBeforeCurrent",
                "The encounter datetime should be before the current date.");
    }

    Visit visit = encounter.getVisit();
    if (visit != null && encounterDateTime != null) {
        if (visit.getStartDatetime() != null && encounterDateTime.before(visit.getStartDatetime())) {
            errors.rejectValue("encounterDatetime", "Encounter.datetimeShouldBeInVisitDatesRange",
                    "The encounter datetime should be between the visit start and stop dates.");
        }

        if (visit.getStopDatetime() != null && encounterDateTime.after(visit.getStopDatetime())) {
            errors.rejectValue("encounterDatetime", "Encounter.datetimeShouldBeInVisitDatesRange",
                    "The encounter datetime should be between the visit start and stop dates.");
        }
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "voidReason");
}

From source file:org.openmrs.validator.FieldTypeValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * /*w ww.  jav a  2 s.c  o m*/
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should fail validation if field type name already exist in none retired filed types
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    FieldType fieldType = (FieldType) obj;
    if (fieldType == null) {
        errors.rejectValue("fieldType", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        if (!errors.hasErrors()) {
            FieldType exist = Context.getFormService().getFieldTypeByName(fieldType.getName());
            if (exist != null && !exist.isRetired()
                    && !OpenmrsUtil.nullSafeEquals(fieldType.getUuid(), exist.getUuid())) {
                errors.rejectValue("name", "fieldtype.duplicate.name");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name");
    }
}

From source file:org.openmrs.validator.FormValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null
 * @should fail validation if version is null
 * @should fail validation if version does not match regex
 * @should fail validation if retiredReason is null
 * @should fail validation if retiredReason is empty
 * @should pass validation if all fields are correct
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 *///from   w  ww. java  2  s.c  o m
public void validate(Object obj, Errors errors) {
    Form form = (Form) obj;
    if (form == null) {
        errors.rejectValue("form", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "version", "error.null");

        if (form.getVersion() != null && !form.getVersion().matches("^\\d.*$")) {
            errors.rejectValue("version", "Form.version.invalid");
        }

        if (form.isRetired()) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "retireReason", "general.retiredReason.empty");
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "version", "description",
                "retireReason");
    }
}

From source file:org.openmrs.validator.HL7SourceValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * /*from  ww  w  . j  a va2s . co  m*/
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    HL7Source hl7Source = (HL7Source) obj;
    if (hl7Source == null) {
        errors.rejectValue("hl7Source", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name");
    }
}

From source file:org.openmrs.validator.ImplementationIdValidator.java

@Override
public void validate(Object obj, Errors errors) throws APIException {
    ImplementationId implId = (ImplementationId) obj;
    char[] illegalChars = { '^', '|' };
    if (implId == null) {
        throw new APIException("ImplementationId.null", (Object[]) null);
    } else {//  ww  w  .  j a  v  a 2s .  co m
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "ImplementationId.name.empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "implementationId",
                "ImplementationId.implementationId.empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passphrase", "ImplementationId.passphrase.empty");
        if (implId.getImplementationId() != null
                && StringUtils.containsAny(implId.getImplementationId(), illegalChars)) {
            errors.rejectValue("implementationId", "ImplementationId.implementationId.invalidCharacter");
        }
    }
}

From source file:org.openmrs.validator.LocationValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * /*w  w w .ja v  a 2s  .  c o m*/
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty
 * @should fail validation if retired and retireReason is null or empty
 * @should set retired to false if retireReason is null or empty
 * @should pass validation if all fields are correct
 * @should pass validation if retired location is given retired reason
 * @should fail validation if parent location creates a loop
 * @should fail validation if name is exist in non retired locations
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    Location location = (Location) obj;
    if (location == null) {
        errors.rejectValue("location", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");

        if (location.isRetired() && !StringUtils.hasLength(location.getRetireReason())) {
            location.setRetired(false); // so that the jsp page displays
            // properly again
            errors.rejectValue("retireReason", "error.null");
        }

        Location exist = Context.getLocationService().getLocation(location.getName());
        if (exist != null && !exist.isRetired()
                && !OpenmrsUtil.nullSafeEquals(location.getUuid(), exist.getUuid())) {
            errors.rejectValue("name", "location.duplicate.name");
        }

        // Traverse all the way up (down?) to the root and check if it
        // equals the root.
        Location root = location;
        while (root.getParentLocation() != null) {
            root = root.getParentLocation();
            if (root.equals(location)) { // Have gone in a circle
                errors.rejectValue("parentLocation", "Location.parentLocation.error");
                break;
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "address1", "address2",
                "cityVillage", "stateProvince", "country", "postalCode", "latitude", "longitude",
                "countyDistrict", "address3", "address4", "address5", "address6", "retireReason");
        super.validateAttributes(location, errors, Context.getLocationService().getAllLocationAttributeTypes());
    }

}

From source file:org.openmrs.validator.PatientIdentifierTypeValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * //from   w w w . jav a 2 s .  c  o m
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation if regEx field length is not too long
 * @should fail validation if regEx field length is too long
 * @should fail validation if name field length is too long
 * @should fail validation if name is already exist in non retired identifier types
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    PatientIdentifierType identifierType = (PatientIdentifierType) obj;
    if (identifierType == null) {
        errors.rejectValue("identifierType", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidateUtil.validateFieldLengths(errors, identifierType.getClass(), "name", "format",
                "formatDescription", "validator", "retireReason");
        PatientIdentifierType exist = Context.getPatientService()
                .getPatientIdentifierTypeByName(identifierType.getName());
        if (exist != null && !exist.isRetired()
                && !OpenmrsUtil.nullSafeEquals(identifierType.getUuid(), exist.getUuid())) {
            errors.rejectValue("name", "identifierType.duplicate.name");
        }
    }
}

From source file:org.openmrs.validator.PatientValidator.java

/**
 * Validates the given Patient. Currently just checks for errors in identifiers. TODO: Check for
 * errors in all Patient fields.//from  w  w w.  j  ava 2 s  .c o m
 * 
 * @param obj The patient to validate.
 * @param errors Errors
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if gender is blank
 * @should fail validation if birthdate makes patient older that 120 years old
 * @should fail validation if birthdate is a future date
 * @should fail validation if a preferred patient identifier is not chosen
 * @should fail validation if voidReason is blank when patient is voided
 * @should fail validation if causeOfDeath is blank when patient is dead
 * @should fail validation if a preferred patient identifier is not chosen for voided patients
 * @should not fail when patient has only one identifier and its not preferred
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    if (log.isDebugEnabled()) {
        log.debug(this.getClass().getName() + ".validate...");
    }

    if (obj == null) {
        return;
    }

    super.validate(obj, errors);

    Patient patient = (Patient) obj;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", "Person.gender.required");

    // Make sure they chose a preferred ID
    Boolean preferredIdentifierChosen = false;
    //Voided patients have only voided identifiers since they were voided with the patient, 
    //so get all otherwise get the active ones
    Collection<PatientIdentifier> identifiers = patient.isVoided() ? patient.getIdentifiers()
            : patient.getActiveIdentifiers();
    for (PatientIdentifier pi : identifiers) {
        if (pi.isPreferred()) {
            preferredIdentifierChosen = true;
        }
    }
    if (!preferredIdentifierChosen && identifiers.size() != 1) {
        errors.reject("error.preferredIdentifier");
    }
    int index = 0;
    if (!errors.hasErrors()) {
        // Validate PatientIdentifers
        if (patient.getIdentifiers() != null) {
            for (PatientIdentifier identifier : patient.getIdentifiers()) {
                errors.pushNestedPath("identifiers[" + index + "]");
                patientIdentifierValidator.validate(identifier, errors);
                errors.popNestedPath();
                index++;
            }
        }
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "voidReason");
}