Search in sources :

Example 11 with PersonAddress

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

the class PersonSaveHandler method handle.

/**
 * @see org.openmrs.api.handler.SaveHandler#handle(org.openmrs.OpenmrsObject, org.openmrs.User,
 *      java.util.Date, java.lang.String)
 */
@Override
public void handle(Person person, User creator, Date dateCreated, String other) {
    // address collection
    if (person.getAddresses() != null && !person.getAddresses().isEmpty()) {
        for (PersonAddress pAddress : person.getAddresses()) {
            if (pAddress.isBlank()) {
                person.removeAddress(pAddress);
                continue;
            }
            pAddress.setPerson(person);
        }
    }
    // name collection
    if (person.getNames() != null && !person.getNames().isEmpty()) {
        for (PersonName pName : person.getNames()) {
            pName.setPerson(person);
        }
    }
    // attribute collection
    if (person.getAttributes() != null && !person.getAttributes().isEmpty()) {
        for (PersonAttribute pAttr : person.getAttributes()) {
            pAttr.setPerson(person);
        }
    }
    // if the patient was marked as dead and reversed, drop the cause of death
    if (!person.getDead() && person.getCauseOfDeath() != null) {
        person.setCauseOfDeath(null);
    }
    // do the checks for voided attributes (also in PersonVoidHandler)
    if (person.getPersonVoided()) {
        if (!StringUtils.hasLength(person.getPersonVoidReason())) {
            throw new APIException("Person.voided.bit", new Object[] { person });
        }
        if (person.getPersonVoidedBy() == null) {
            person.setPersonVoidedBy(creator);
        }
        if (person.getPersonDateVoided() == null) {
            person.setPersonDateVoided(dateCreated);
        }
    } else {
        // voided is set to false
        person.setPersonVoidedBy(null);
        person.setPersonDateVoided(null);
        person.setPersonVoidReason(null);
    }
}
Also used : PersonName(org.openmrs.PersonName) APIException(org.openmrs.api.APIException) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

Example 12 with PersonAddress

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

the class PersonAddressValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see PersonAddressValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    PersonAddress personAddress = new PersonAddress();
    personAddress.setStartDate(null);
    personAddress.setEndDate(null);
    String longString = "too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text";
    personAddress.setAddress1(longString);
    personAddress.setAddress2(longString);
    personAddress.setCityVillage(longString);
    personAddress.setStateProvince(longString);
    personAddress.setPostalCode(longString);
    personAddress.setCountry(longString);
    personAddress.setLatitude(longString);
    personAddress.setLongitude(longString);
    personAddress.setVoidReason(longString);
    personAddress.setCountyDistrict(longString);
    personAddress.setAddress3(longString);
    personAddress.setAddress4(longString);
    personAddress.setAddress5(longString);
    personAddress.setAddress6(longString);
    personAddress.setAddress7(longString);
    personAddress.setAddress8(longString);
    personAddress.setAddress9(longString);
    personAddress.setAddress10(longString);
    personAddress.setAddress11(longString);
    personAddress.setAddress12(longString);
    personAddress.setAddress13(longString);
    personAddress.setAddress14(longString);
    personAddress.setAddress15(longString);
    Errors errors = new BindException(personAddress, "personAddress");
    validator.validate(personAddress, errors);
    Assert.assertEquals(true, errors.hasFieldErrors("address1"));
    Assert.assertEquals(true, errors.hasFieldErrors("address2"));
    Assert.assertEquals(true, errors.hasFieldErrors("cityVillage"));
    Assert.assertEquals(true, errors.hasFieldErrors("stateProvince"));
    Assert.assertEquals(true, errors.hasFieldErrors("postalCode"));
    Assert.assertEquals(true, errors.hasFieldErrors("country"));
    Assert.assertEquals(true, errors.hasFieldErrors("latitude"));
    Assert.assertEquals(true, errors.hasFieldErrors("longitude"));
    Assert.assertEquals(true, errors.hasFieldErrors("voidReason"));
    Assert.assertEquals(true, errors.hasFieldErrors("countyDistrict"));
    Assert.assertEquals(true, errors.hasFieldErrors("address3"));
    Assert.assertEquals(true, errors.hasFieldErrors("address4"));
    Assert.assertEquals(true, errors.hasFieldErrors("address5"));
    Assert.assertEquals("address6 missing in errors", true, errors.hasFieldErrors("address6"));
    Assert.assertEquals("address7 missing in errors", true, errors.hasFieldErrors("address7"));
    Assert.assertEquals("address8 missing in errors", true, errors.hasFieldErrors("address8"));
    Assert.assertEquals("address9 missing in errors", true, errors.hasFieldErrors("address9"));
    Assert.assertEquals("address10 missing in errors", true, errors.hasFieldErrors("address10"));
    Assert.assertEquals("address11 missing in errors", true, errors.hasFieldErrors("address11"));
    Assert.assertEquals("address12 missing in errors", true, errors.hasFieldErrors("address12"));
    Assert.assertEquals("address13 missing in errors", true, errors.hasFieldErrors("address13"));
    Assert.assertEquals("address14 missing in errors", true, errors.hasFieldErrors("address14"));
    Assert.assertEquals("address15 missing in errors", true, errors.hasFieldErrors("address15"));
}
Also used : Errors(org.springframework.validation.Errors) PersonAddress(org.openmrs.PersonAddress) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 13 with PersonAddress

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

the class PersonAddressValidatorTest method validate_shouldFailIfTheEndDateIsBeforeTheStartDate.

/**
 * @see PersonAddressValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailIfTheEndDateIsBeforeTheStartDate() {
    PersonAddress personAddress = new PersonAddress();
    Calendar c = Calendar.getInstance();
    personAddress.setStartDate(c.getTime());
    // set to an older date
    c.set(2010, 3, 15);
    personAddress.setEndDate(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 14 with PersonAddress

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

the class PersonAddressValidatorTest method validate_shouldPassValidationIfFieldLengthsAreCorrect.

/**
 * @see PersonAddressValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
    PersonAddress personAddress = new PersonAddress();
    personAddress.setStartDate(null);
    personAddress.setEndDate(null);
    personAddress.setAddress1("address1");
    personAddress.setAddress2("address2");
    personAddress.setCityVillage("cityVillage");
    personAddress.setStateProvince("stateProvince");
    personAddress.setPostalCode("postalCode");
    personAddress.setCountry("country");
    personAddress.setLatitude("latitude");
    personAddress.setLongitude("longitude");
    personAddress.setVoidReason("voidReason");
    personAddress.setCountyDistrict("countyDistrict");
    personAddress.setAddress3("address3");
    personAddress.setAddress4("address4");
    personAddress.setAddress5("address5");
    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) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 15 with PersonAddress

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

the class PersonAddressValidatorTest method validate_shouldFailIfRequiredFieldsAreEmpty.

/**
 * @see PersonAddressValidator#validate(Object, org.springframework.validation.Errors)
 */
@Test
public void validate_shouldFailIfRequiredFieldsAreEmpty() {
    executeDataSet(PERSON_ADDRESS_VALIDATOR_DATASET_PACKAGE_PATH);
    Address personAddress = new PersonAddress();
    Errors errors = new BindException(personAddress, "personAddress");
    validator.validate(personAddress, errors);
    Assert.assertEquals(true, 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