Search in sources :

Example 6 with PersonAddress

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

the class PersonServiceTest method savePerson_shouldNotSetThePreferredNameAndAddressIfTheyAlreadyExist.

/**
 * @see PersonService#savePerson(Person)
 */
@Test
public void savePerson_shouldNotSetThePreferredNameAndAddressIfTheyAlreadyExist() throws Exception {
    Person person = new Person();
    person.setGender("M");
    PersonName name = new PersonName("givenName", "middleName", "familyName");
    PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
    preferredName.setPreferred(true);
    person.addName(name);
    person.addName(preferredName);
    PersonAddress address = new PersonAddress();
    address.setAddress1("some address");
    PersonAddress preferredAddress = new PersonAddress();
    preferredAddress.setAddress1("another address");
    preferredAddress.setPreferred(true);
    person.addAddress(address);
    person.addAddress(preferredAddress);
    personService.savePerson(person);
    assertTrue(preferredName.getPreferred());
    assertTrue(preferredAddress.getPreferred());
    Assert.assertFalse(name.getPreferred());
    Assert.assertFalse(address.getPreferred());
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Person(org.openmrs.Person) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 7 with PersonAddress

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

the class PersonValidator method validate.

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @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 deathdate is a future date
 * @should fail validation if birthdate is after death date
 * @should fail validation if voidReason is blank when patient is voided
 * @should fail validation if causeOfDeath is blank when patient is dead
 * @should pass validation if gender is blank for Persons
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object target, Errors errors) {
    log.debug("{}.validate...", this.getClass().getName());
    if (target == null) {
        return;
    }
    Person person = (Person) target;
    int index = 0;
    boolean atLeastOneNonVoidPersonNameLeft = false;
    for (PersonName personName : person.getNames()) {
        errors.pushNestedPath("names[" + index + "]");
        personNameValidator.validate(personName, errors);
        if (!personName.getVoided()) {
            atLeastOneNonVoidPersonNameLeft = true;
        }
        errors.popNestedPath();
        index++;
    }
    if (!person.getVoided() && !atLeastOneNonVoidPersonNameLeft) {
        errors.rejectValue("names", "Person.shouldHaveAtleastOneNonVoidedName");
    }
    // validate the personAddress
    index = 0;
    for (PersonAddress address : person.getAddresses()) {
        try {
            errors.pushNestedPath("addresses[" + index + "]");
            ValidationUtils.invokeValidator(personAddressValidator, address, errors);
        } finally {
            errors.popNestedPath();
            index++;
        }
    }
    validateBirthDate(errors, person.getBirthdate());
    validateDeathDate(errors, person.getDeathDate(), person.getBirthdate());
    if (person.getVoided()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
    }
    if (person.getDead()) {
        ValidationUtils.rejectIfEmpty(errors, "causeOfDeath", "Person.dead.causeOfDeathNull");
    }
    ValidateUtil.validateFieldLengths(errors, Person.class, "gender", "personVoidReason");
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Person(org.openmrs.Person)

Example 8 with PersonAddress

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

the class PatientValidatorTest method validate_shouldFailValidationIfFieldLengthsAreNotCorrect.

/**
 * @see PatientValidator#validate(Object,Errors)
 */
@Override
@Test
public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() {
    PatientIdentifierType patientIdentifierType = Context.getPatientService().getAllPatientIdentifierTypes(false).get(0);
    Patient patient = new Patient();
    PersonName pName = new PersonName();
    pName.setGivenName("Tom");
    pName.setMiddleName("E.");
    pName.setFamilyName("Patient");
    patient.addName(pName);
    patient.setGender("male");
    PersonAddress pAddress = new PersonAddress();
    pAddress.setAddress1("123 My street");
    pAddress.setAddress2("Apt 402");
    pAddress.setCityVillage("Anywhere city");
    pAddress.setCountry("Some Country");
    Set<PersonAddress> pAddressList = patient.getAddresses();
    pAddressList.add(pAddress);
    patient.setAddresses(pAddressList);
    patient.addAddress(pAddress);
    PatientIdentifier patientIdentifier1 = new PatientIdentifier();
    patientIdentifier1.setLocation(new Location(1));
    patientIdentifier1.setIdentifier("012345678");
    patientIdentifier1.setDateCreated(new Date());
    patientIdentifier1.setIdentifierType(patientIdentifierType);
    patient.addIdentifier(patientIdentifier1);
    patient.setVoidReason("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");
    Errors errors = new BindException(patient, "patient");
    validator.validate(patient, errors);
    Assert.assertTrue(errors.hasFieldErrors("voidReason"));
}
Also used : Errors(org.springframework.validation.Errors) PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) Date(java.util.Date) Location(org.openmrs.Location) Test(org.junit.Test)

Example 9 with PersonAddress

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

the class PatientValidatorTest method validate_shouldNotFailWhenPatientHasOnlyOneIdentifierAndItsNotPreferred.

@Test
public void validate_shouldNotFailWhenPatientHasOnlyOneIdentifierAndItsNotPreferred() {
    PatientIdentifierType patientIdentifierType = Context.getPatientService().getAllPatientIdentifierTypes(false).get(0);
    Patient patient = new Patient();
    PersonName pName = new PersonName();
    pName.setGivenName("Tom");
    pName.setMiddleName("E.");
    pName.setFamilyName("Patient");
    patient.addName(pName);
    patient.setGender("male");
    PersonAddress pAddress = new PersonAddress();
    pAddress.setAddress1("123 My street");
    pAddress.setAddress2("Apt 402");
    pAddress.setCityVillage("Anywhere city");
    pAddress.setCountry("Some Country");
    Set<PersonAddress> pAddressList = patient.getAddresses();
    pAddressList.add(pAddress);
    patient.setAddresses(pAddressList);
    patient.addAddress(pAddress);
    PatientIdentifier patientIdentifier1 = new PatientIdentifier();
    patientIdentifier1.setLocation(new Location(1));
    patientIdentifier1.setIdentifier("012345678");
    patientIdentifier1.setDateCreated(new Date());
    patientIdentifier1.setIdentifierType(patientIdentifierType);
    patient.addIdentifier(patientIdentifier1);
    Errors errors = new BindException(patient, "patient");
    validator.validate(patient, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Errors(org.springframework.validation.Errors) PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) BindException(org.springframework.validation.BindException) PatientIdentifierType(org.openmrs.PatientIdentifierType) PatientIdentifier(org.openmrs.PatientIdentifier) Date(java.util.Date) Location(org.openmrs.Location) Test(org.junit.Test)

Example 10 with PersonAddress

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

the class HibernatePersonDAO method deletePersonAndAttributes.

/**
 * Used by deletePerson, deletePatient, and deleteUser to remove all properties of a person
 * before deleting them.
 *
 * @param sessionFactory the session factory from which to pull the current session
 * @param person the person to delete
 */
public static void deletePersonAndAttributes(SessionFactory sessionFactory, Person person) {
    // delete properties and fields so hibernate can't complain
    for (PersonAddress address : person.getAddresses()) {
        if (address.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(address);
        } else {
            sessionFactory.getCurrentSession().delete(address);
        }
    }
    person.setAddresses(null);
    for (PersonAttribute attribute : person.getAttributes()) {
        if (attribute.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(attribute);
        } else {
            sessionFactory.getCurrentSession().delete(attribute);
        }
    }
    person.setAttributes(null);
    for (PersonName name : person.getNames()) {
        if (name.getDateCreated() == null) {
            sessionFactory.getCurrentSession().evict(name);
        } else {
            sessionFactory.getCurrentSession().delete(name);
        }
    }
    person.setNames(null);
    // finally, just tell hibernate to delete our object
    sessionFactory.getCurrentSession().delete(person);
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

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