Search in sources :

Example 41 with PersonAddress

use of org.openmrs.PersonAddress in project openmrs-core by openmrs.

the class PersonAddressValidator method validate.

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should pass if all the dates are valid
 * @should fail if the startDate is in the future
 * @should fail if the endDate is before the startDate
 * @should pass if startDate and endDate are both null
 * @should pass if startDate is null
 * @should pass if endDate is null
 * @should fail if required fields are empty
 * @should pass if required fields are not empty
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object object, Errors errors) {
    // TODO Validate other aspects of the personAddress object
    if (log.isDebugEnabled()) {
        log.debug(this.getClass().getName() + ".validate...");
    }
    if (object == null) {
        throw new IllegalArgumentException("The personAddress object should not be null");
    }
    PersonAddress personAddress = (PersonAddress) object;
    // resolve a shorter name to display along with the error message
    String addressString;
    if (StringUtils.isNotBlank(personAddress.getAddress1())) {
        addressString = personAddress.getAddress1();
    } else if (StringUtils.isNotBlank(personAddress.getAddress2())) {
        addressString = personAddress.getAddress2();
    } else if (StringUtils.isNotBlank(personAddress.getCityVillage())) {
        addressString = personAddress.getCityVillage();
    } else {
        addressString = personAddress.toString();
    }
    if (OpenmrsUtil.compareWithNullAsEarliest(personAddress.getStartDate(), new Date()) > 0) {
        errors.rejectValue("startDate", "PersonAddress.error.startDateInFuture", new Object[] { "'" + addressString + "'" }, "The Start Date for address '" + addressString + "' shouldn't be in the future");
    }
    if (personAddress.getStartDate() != null && OpenmrsUtil.compareWithNullAsLatest(personAddress.getStartDate(), personAddress.getEndDate()) > 0) {
        errors.rejectValue("endDate", "PersonAddress.error.endDateBeforeStartDate", new Object[] { "'" + addressString + "'" }, "The End Date for address '" + addressString + "' shouldn't be earlier than the Start Date");
    }
    String xml = Context.getLocationService().getAddressTemplate();
    List<String> requiredElements;
    try {
        AddressTemplate addressTemplate = Context.getSerializationService().getDefaultSerializer().deserialize(xml, AddressTemplate.class);
        requiredElements = addressTemplate.getRequiredElements();
    } catch (Exception e) {
        errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error"));
        return;
    }
    if (requiredElements != null) {
        for (String fieldName : requiredElements) {
            try {
                Object value = PropertyUtils.getProperty(personAddress, fieldName);
                if (StringUtils.isBlank((String) value)) {
                    // required field not found
                    errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error.requiredAddressFieldIsBlank", new Object[] { fieldName }, Context.getLocale()));
                }
            } catch (Exception e) {
                // wrong field declared in template
                errors.reject(Context.getMessageSourceService().getMessage("AddressTemplate.error.fieldNotDeclaredInTemplate", new Object[] { fieldName }, Context.getLocale()));
            }
        }
    }
    ValidateUtil.validateFieldLengths(errors, object.getClass(), "address1", "address2", "cityVillage", "stateProvince", "postalCode", "country", "latitude", "longitude", "voidReason", "countyDistrict", "address3", "address4", "address5", "address6", "address7", "address8", "address9", "address10", "address11", "address12", "address13", "address14", "address15");
}
Also used : AddressTemplate(org.openmrs.layout.address.AddressTemplate) PersonAddress(org.openmrs.PersonAddress) Date(java.util.Date)

Example 42 with PersonAddress

use of org.openmrs.PersonAddress in project openmrs-core by openmrs.

the class PersonAddressValidatorTest method validate_shouldFailIfTheStartDateIsInTheFuture.

/**
 * @see PersonAddressValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfTheStartDateIsInTheFuture() {
    PersonAddress personAddress = new PersonAddress();
    Calendar c = Calendar.getInstance();
    // put the time into the future by a minute
    c.add(Calendar.MINUTE, 1);
    personAddress.setStartDate(c.getTime());
    Errors errors = new BindException(personAddress, "personAddress");
    validator.validate(personAddress, errors);
    Assert.assertEquals(true, errors.hasFieldErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonAddress(org.openmrs.PersonAddress) Calendar(java.util.Calendar) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 43 with PersonAddress

use of org.openmrs.PersonAddress in project openmrs-core by openmrs.

the class PersonAddressValidatorTest method validate_shouldPassIfAllTheDatesAreValid.

/**
 * @see PersonAddressValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassIfAllTheDatesAreValid() {
    PersonAddress personAddress = new PersonAddress();
    Calendar c = Calendar.getInstance();
    personAddress.setStartDate(c.getTime());
    personAddress.setEndDate(c.getTime());
    Errors errors = new BindException(personAddress, "personAddress");
    validator.validate(personAddress, errors);
    Assert.assertEquals(false, errors.hasFieldErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonAddress(org.openmrs.PersonAddress) Calendar(java.util.Calendar) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 44 with PersonAddress

use of org.openmrs.PersonAddress in project openmrs-core by openmrs.

the class PersonAddressValidatorTest method validate_shouldPassIfStartDateIsNull.

/**
 * @see PersonAddressValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassIfStartDateIsNull() {
    PersonAddress personAddress = new PersonAddress();
    Calendar c = Calendar.getInstance();
    personAddress.setStartDate(null);
    personAddress.setEndDate(c.getTime());
    Errors errors = new BindException(personAddress, "personAddress");
    validator.validate(personAddress, errors);
    Assert.assertEquals(false, errors.hasFieldErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonAddress(org.openmrs.PersonAddress) Calendar(java.util.Calendar) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 45 with PersonAddress

use of org.openmrs.PersonAddress in project openmrs-core by openmrs.

the class PersonAddressValidatorTest method validate_shouldPassIfRequiredFieldsAreNotEmpty.

/**
 * @see PersonAddressValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldPassIfRequiredFieldsAreNotEmpty() {
    executeDataSet(PERSON_ADDRESS_VALIDATOR_DATASET_PACKAGE_PATH);
    Address personAddress = new PersonAddress();
    personAddress.setAddress1("Address1");
    Errors errors = new BindException(personAddress, "personAddress");
    validator.validate(personAddress, errors);
    Assert.assertEquals(false, errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonAddress(org.openmrs.PersonAddress) Address(org.openmrs.Address) PersonAddress(org.openmrs.PersonAddress) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

PersonAddress (org.openmrs.PersonAddress)49 Test (org.junit.Test)32 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)26 PersonName (org.openmrs.PersonName)20 Patient (org.openmrs.Patient)18 BindException (org.springframework.validation.BindException)13 Errors (org.springframework.validation.Errors)13 Date (java.util.Date)12 PatientIdentifier (org.openmrs.PatientIdentifier)12 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)9 Person (org.openmrs.Person)6 Calendar (java.util.Calendar)5 Location (org.openmrs.Location)5 PatientIdentifierType (org.openmrs.PatientIdentifierType)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)3 DateTime (org.joda.time.DateTime)3 Concept (org.openmrs.Concept)3 PersonAttribute (org.openmrs.PersonAttribute)3 DateFormat (java.text.DateFormat)2