Example usage for org.springframework.validation ValidationUtils rejectIfEmpty

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

Introduction

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

Prototype

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

Source Link

Document

Reject the given field with the given error code if the value is empty.

Usage

From source file:org.openmrs.module.jsslab.validator.LabPreconditionValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * //from  ww  w. ja v a 2  s .  com
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if required fields are missing
 */
public void validate(Object obj, Errors errors) {

    LabPrecondition precondition = (LabPrecondition) obj;
    if (precondition == null) {
        errors.rejectValue("precondition", "jsslab.validation.error.null");
    }

    // for the following elements LabPrecondition.hbm.xml says: not-null="true"
    ValidationUtils.rejectIfEmpty(errors, "testPanel", "jsslab.validation.NullTestPanel");
    ValidationUtils.rejectIfEmpty(errors, "preconditionQuestionConcept", "jsslab.validation.NullConcept");
    ValidationUtils.rejectIfEmpty(errors, "preconditionAnswerConcept", "jsslab.validation.NullConcept");
    ValidationUtils.rejectIfEmpty(errors, "creator", "jsslab.validation.NullField");
    ValidationUtils.rejectIfEmpty(errors, "dateCreated", "jsslab.validation.NullField");
    ValidationUtils.rejectIfEmpty(errors, "voided", "jsslab.validation.NullField");
}

From source file:org.openmrs.module.appointmentscheduling.validator.TimeSlotValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * /*from w ww .ja va  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 or whitespace
 * @should pass validation if all required fields have proper values
 */

public void validate(Object obj, Errors errors) {
    TimeSlot timeSlot = (TimeSlot) obj;
    if (timeSlot == null) {
        errors.rejectValue("timeSlot", "error.general");
    } else {
        ValidationUtils.rejectIfEmpty(errors, "startDate", "appointmentscheduling.TimeSlot.emptyStartDate");
        ValidationUtils.rejectIfEmpty(errors, "endDate", "appointmentscheduling.TimeSlot.emptyEndDate");
        ValidationUtils.rejectIfEmpty(errors, "appointmentBlock", "appointmentscheduling.TimeSlot.emptyBlock");
    }
}

From source file:org.openmrs.module.allergyapi.AllergyValidator.java

/**
 * @see Validator#validate(Object, org.springframework.validation.Errors)
 * @param target//  w w w. j a v  a  2s  .com
 * @param errors
 * @should fail for a null value
 * @should fail if patient is null
 * @should fail id allergenType is null
 * @should fail if allergen is null
 * @should fail if codedAllergen is null
 * @should fail if nonCodedAllergen is null and allergen is set to other non coded
 * @should reject a duplicate allergen
 * @should reject a duplicate non coded allergen
 * @should pass for a valid allergy
 */
@Override
public void validate(Object target, Errors errors) {

    if (target == null) {
        throw new IllegalArgumentException("Allergy should not be null");
    }

    ValidationUtils.rejectIfEmpty(errors, "patient", "allergyapi.patient.required");

    Allergy allergy = (Allergy) target;

    if (allergy.getAllergen() == null) {
        errors.rejectValue("allergen", "allergyapi.allergen.required");
    } else {
        Allergen allergen = allergy.getAllergen();
        if (allergen.getAllergenType() == null) {
            errors.rejectValue("allergen", "allergyapi.allergenType.required");
        }

        if (allergen.getCodedAllergen() == null && StringUtils.isBlank(allergen.getNonCodedAllergen())) {
            errors.rejectValue("allergen", "allergyapi.allergen.codedOrNonCodedAllergen.required");
        } else if (!allergen.isCoded() && StringUtils.isBlank(allergen.getNonCodedAllergen())) {
            errors.rejectValue("allergen", "allergyapi.allergen.nonCodedAllergen.required");
        }

        if (allergy.getAllergyId() == null && allergy.getPatient() != null) {
            Allergies existingAllergies = patientService.getAllergies(allergy.getPatient());
            if (existingAllergies.containsAllergen(allergy)) {
                String key = "ui.i18n.Concept.name." + allergen.getCodedAllergen().getUuid();
                String name = Context.getMessageSourceService().getMessage(key);
                if (key.equals(name)) {
                    name = allergen.toString();
                }
                errors.rejectValue("allergen", "allergyapi.message.duplicateAllergen", new Object[] { name },
                        null);
            }
        }
    }
}

From source file:org.openmrs.module.casereport.CaseReportTriggerValidator.java

/**
 * @see Validator#validate(Object, Errors)
 * @should fail if the case report trigger object is null
 * @should fail if the trigger name is null
 * @should fail if the trigger name is an empty string
 * @should fail if the trigger name is a white space character
 * @should fail if the case report field is null
 * @should fail if a case report with the same trigger already exists for the patient
 * @should pass for a valid case report trigger
 *///from w w w  .ja  v a 2s.com
@Override
public void validate(Object target, Errors errors) {
    if (target == null || !(target instanceof CaseReportTrigger)) {
        throw new IllegalArgumentException(
                "The parameter obj should not be null and must be of type" + getClass());
    }

    CaseReportTrigger trigger = (CaseReportTrigger) target;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "casereports.error.trigger.name.required");
    ValidationUtils.rejectIfEmpty(errors, "caseReport", "casereports.error.trigger.casereport.required");

    if (errors.hasErrors()) {
        return;
    }

    CaseReportService service = Context.getService(CaseReportService.class);
    CaseReport duplicate = service.getCaseReportByPatient(trigger.getCaseReport().getPatient());
    if (duplicate != null) {
        CaseReportTrigger crt = duplicate.getCaseReportTriggerByName(trigger.getName());
        if (crt != null && !crt.equals(trigger)) {
            errors.rejectValue("name", "casereport.error.trigger.duplicate", new Object[] { trigger.getName() },
                    null);
        }
    }
}

From source file:org.openmrs.module.casereport.CaseReportValidator.java

/**
 * @see Validator#validate(Object, Errors)
 * @should fail if the case report object is null
 * @should fail if the patient is null//from  w  ww .  ja v a2 s .com
 * @should fail if the report has no trigger added
 * @should fail for a new item if the patient already has an existing report item
 * @should pass for auto submit item with unique trigger and patient has existing item
 * @should pass for a valid case report
 */
@Override
public void validate(Object target, Errors errors) {
    if (target == null || !(target instanceof CaseReport)) {
        throw new IllegalArgumentException(
                "The parameter obj should not be null and must be of type" + getClass());
    }

    CaseReport caseReport = (CaseReport) target;
    ValidationUtils.rejectIfEmpty(errors, "patient", "casereports.error.patient.required");
    if (!errors.hasErrors()) {
        CaseReportService service = Context.getService(CaseReportService.class);
        if (caseReport.getId() == null && !caseReport.getAutoSubmitted()
                && service.getCaseReportByPatient(caseReport.getPatient()) != null) {
            errors.reject("casereports.error.patient.alreadyHasQueueItem");
        }
    }

    if (CollectionUtils.isEmpty(caseReport.getReportTriggers())) {
        errors.rejectValue("reportTriggers", "casereports.error.atleast.one.trigger.required");
    }

    if (!errors.hasErrors()) {
        int index = 0;
        for (CaseReportTrigger trigger : caseReport.getReportTriggers()) {
            try {
                errors.pushNestedPath("reportTriggers[" + index + "]");
                ValidationUtils.invokeValidator(triggerValidator, trigger, errors);
            } finally {
                errors.popNestedPath();
                index++;
            }
        }
    }

}

From source file:org.tonguetied.web.ImportValidator.java

/**
 * Validate the {@link ImportParameters} of the {@link ImportBean} for
 * erroneous input.//from www.  j av a 2s .  c  o m
 * 
 * @param parameters the import parameters to validate
 * @param errors the errors object
 */
private void validate(ImportParameters parameters, Errors errors) {
    ValidationUtils.rejectIfEmpty(errors, FIELD_FORMAT_TYPE, "error.import.type.required");
    ValidationUtils.rejectIfEmpty(errors, FIELD_TRANSLATION_STATE, "error.import.state.required");
}

From source file:org.openmrs.module.appointmentscheduling.validator.AppointmentValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * //from   w  w  w.ja v a 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 all required fields have proper values
 */
public void validate(Object obj, Errors errors) {
    Appointment appointment = (Appointment) obj;
    if (appointment == null) {
        errors.rejectValue("appointment", "error.general");
    } else {
        ValidationUtils.rejectIfEmpty(errors, "timeSlot", "appointmentscheduling.Appointment.emptyTimeSlot");
        ValidationUtils.rejectIfEmpty(errors, "patient", "appointmentscheduling.Appointment.emptyPatient");
        ValidationUtils.rejectIfEmpty(errors, "appointmentType", "appointmentscheduling.Appointment.emptyType");

        //Check whether the slot supports this appointment type
        AppointmentType type = appointment.getAppointmentType();
        if (type == null)
            errors.rejectValue("appointmentType", "appointmentscheduling.Appointment.emptyType");
        if (appointment.getTimeSlot() == null)
            errors.rejectValue("timeSlot", "appointmentscheduling.Appointment.emptyTimeSlot");
        else if (type != null && !appointment.getTimeSlot().getAppointmentBlock().getTypes().contains(type))
            errors.rejectValue("appointmentType", "appointmentscheduling.Appointment.notSupportedType");
    }
}

From source file:org.openmrs.api.validator.ConditionValidator.java

@Override
public void validate(Object obj, Errors errors) {

    Condition condition = (Condition) obj;
    if (condition == null) {
        errors.reject("error.general");
    } else {//from www .j  a v a 2  s .c om
        ValidationUtils.rejectIfEmpty(errors, "patient", "error.null");
        ValidationUtils.rejectIfEmpty(errors, "status", "error.null");
        ValidationUtils.rejectIfEmpty(errors, "creator", "error.null");
        ValidationUtils.rejectIfEmpty(errors, "concept", "error.null");
        ValidationUtils.rejectIfEmpty(errors, "voided", "error.null");
        ValidationUtils.rejectIfEmpty(errors, "dateCreated", "error.null");
        ValidationUtils.rejectIfEmpty(errors, "uuid", "error.null");

        validateNonCodedCondition(condition, errors);
        validateDuplicateConditions(condition, errors);
        validateEndReasonConcept(condition, errors);
    }

}

From source file:org.openmrs.module.appointmentscheduling.validator.AppointmentTypeValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * //from w  w  w . j ava  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
 */
public void validate(Object obj, Errors errors) {
    AppointmentType appointmentType = (AppointmentType) obj;
    if (appointmentType == null) {
        errors.rejectValue("appointmentType", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidationUtils.rejectIfEmpty(errors, "duration",
                "appointmentscheduling.AppointmentType.durationEmpty");
    }
}

From source file:org.openmrs.module.appointmentscheduling.validator.AppointmentBlockValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 * /*from  ww  w. java2s. c  o m*/
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should pass validation if all required fields have proper values
 * @should fail validation if start date is not before end date
 */

public void validate(Object obj, Errors errors) {
    AppointmentBlock appointmentBlock = (AppointmentBlock) obj;
    if (appointmentBlock == null) {
        errors.rejectValue("appointmentBlock", "error.general");
    } else {
        ValidationUtils.rejectIfEmpty(errors, "startDate",
                "appointmentscheduling.AppointmentBlock.emptyStartDate");
        ValidationUtils.rejectIfEmpty(errors, "endDate", "appointmentscheduling.AppointmentBlock.emptyEndDate");
        ValidationUtils.rejectIfEmpty(errors, "provider",
                "appointmentscheduling.AppointmentBlock.emptyProvider");
        ValidationUtils.rejectIfEmpty(errors, "location",
                "appointmentscheduling.AppointmentBlock.emptyLocation");
        if (appointmentBlock.getTypes() == null) {
            ValidationUtils.rejectIfEmpty(errors, "types", "appointmentscheduling.AppointmentBlock.emptyTypes");
        }
    }
}