Search in sources :

Example 16 with PersonAttribute

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

the class PatientServiceImpl method mergeAddresses.

private void mergeAddresses(Patient preferred, Patient notPreferred, PersonMergeLogData mergedData) throws SerializationException {
    // (must be done after all calls to services above so hbm doesn't try to save things prematurely (hacky)
    for (PersonAddress newAddress : notPreferred.getAddresses()) {
        boolean containsAddress = false;
        for (PersonAddress currentAddress : preferred.getAddresses()) {
            containsAddress = currentAddress.equalsContent(newAddress);
            if (containsAddress) {
                break;
            }
        }
        if (!containsAddress) {
            PersonAddress tmpAddress = (PersonAddress) newAddress.clone();
            tmpAddress.setPersonAddressId(null);
            tmpAddress.setVoided(false);
            tmpAddress.setVoidedBy(null);
            tmpAddress.setVoidReason(null);
            // addresses from non-preferred patient shouldn't be marked as preferred
            tmpAddress.setPreferred(false);
            tmpAddress.setUuid(UUID.randomUUID().toString());
            preferred.addAddress(tmpAddress);
            mergedData.addCreatedAddress(tmpAddress.getUuid());
            log.debug("Merging address " + newAddress.getPersonAddressId() + " to " + preferred.getPatientId());
        }
    }
    // copy person attributes
    for (PersonAttribute attr : notPreferred.getAttributes()) {
        if (!attr.getVoided()) {
            PersonAttribute tmpAttr = attr.copy();
            tmpAttr.setPerson(null);
            tmpAttr.setUuid(UUID.randomUUID().toString());
            preferred.addAttribute(tmpAttr);
            mergedData.addCreatedAttribute(tmpAttr.getUuid());
        }
    }
    // move all other patient info
    mergedData.setPriorGender(preferred.getGender());
    if (!"M".equals(preferred.getGender()) && !"F".equals(preferred.getGender())) {
        preferred.setGender(notPreferred.getGender());
    }
    mergedData.setPriorDateOfBirth(preferred.getBirthdate());
    mergedData.setPriorDateOfBirthEstimated(preferred.getBirthdateEstimated());
    if (preferred.getBirthdate() == null || (preferred.getBirthdateEstimated() && !notPreferred.getBirthdateEstimated())) {
        preferred.setBirthdate(notPreferred.getBirthdate());
        preferred.setBirthdateEstimated(notPreferred.getBirthdateEstimated());
    }
    mergedData.setPriorDateOfDeathEstimated(preferred.getDeathdateEstimated());
    if (preferred.getDeathdateEstimated() == null) {
        preferred.setDeathdateEstimated(notPreferred.getDeathdateEstimated());
    }
    mergedData.setPriorDateOfDeath(preferred.getDeathDate());
    if (preferred.getDeathDate() == null) {
        preferred.setDeathDate(notPreferred.getDeathDate());
    }
    if (preferred.getCauseOfDeath() != null) {
        mergedData.setPriorCauseOfDeath(preferred.getCauseOfDeath().getUuid());
    }
    if (preferred.getCauseOfDeath() == null) {
        preferred.setCauseOfDeath(notPreferred.getCauseOfDeath());
    }
    // void the non preferred patient
    Context.getPatientService().voidPatient(notPreferred, "Merged with patient #" + preferred.getPatientId());
    // void the person associated with not preferred patient
    Context.getPersonService().voidPerson(notPreferred, "The patient corresponding to this person has been voided and Merged with patient #" + preferred.getPatientId());
    // associate the Users associated with the not preferred person, to the preferred person.
    changeUserAssociations(preferred, notPreferred, mergedData);
    // Save the newly update preferred patient
    // This must be called _after_ voiding the nonPreferred patient so that
    // a "Duplicate Identifier" error doesn't pop up.
    savePatient(preferred);
    // save the person merge log
    PersonMergeLog personMergeLog = new PersonMergeLog();
    personMergeLog.setWinner(preferred);
    personMergeLog.setLoser(notPreferred);
    personMergeLog.setPersonMergeLogData(mergedData);
    Context.getPersonService().savePersonMergeLog(personMergeLog);
}
Also used : PersonAddress(org.openmrs.PersonAddress) PersonMergeLog(org.openmrs.person.PersonMergeLog) PersonAttribute(org.openmrs.PersonAttribute)

Example 17 with PersonAttribute

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

the class ORUR01Handler method updateHealthCenter.

private void updateHealthCenter(Patient patient, PV1 pv1) {
    // Update patient's location if it has changed
    if (log.isDebugEnabled()) {
        log.debug("Checking for discharge to location");
    }
    DLD dld = pv1.getDischargedToLocation();
    log.debug("DLD = " + dld);
    if (dld == null) {
        return;
    }
    IS hl7DischargeToLocation = dld.getDischargeLocation();
    log.debug("is = " + hl7DischargeToLocation);
    if (hl7DischargeToLocation == null) {
        return;
    }
    String dischargeToLocation = hl7DischargeToLocation.getValue();
    log.debug("dischargeToLocation = " + dischargeToLocation);
    if (dischargeToLocation != null && dischargeToLocation.length() > 0) {
        if (log.isDebugEnabled()) {
            log.debug("Patient discharged to " + dischargeToLocation);
        }
        // delimiter
        for (int i = 0; i < dischargeToLocation.length(); i++) {
            char ch = dischargeToLocation.charAt(i);
            if (ch == '&' || ch == '^') {
                dischargeToLocation = dischargeToLocation.substring(0, i);
                break;
            }
        }
        Integer newLocationId = Integer.parseInt(dischargeToLocation);
        // Hydrate a full patient object from patient object containing only
        // identifier
        patient = Context.getPatientService().getPatient(patient.getPatientId());
        PersonAttributeType healthCenterAttrType = Context.getPersonService().getPersonAttributeTypeByName("Health Center");
        if (healthCenterAttrType == null) {
            log.error("A person attribute type with name 'Health Center' is not defined but patient " + patient.getPatientId() + " is trying to change their health center to " + newLocationId);
            return;
        }
        PersonAttribute currentHealthCenter = patient.getAttribute("Health Center");
        if (currentHealthCenter == null || !newLocationId.toString().equals(currentHealthCenter.getValue())) {
            PersonAttribute newHealthCenter = new PersonAttribute(healthCenterAttrType, newLocationId.toString());
            log.debug("Updating patient's location from " + currentHealthCenter + " to " + newLocationId);
            // add attribute (and void old if there is one)
            patient.addAttribute(newHealthCenter);
            // save the patient and their new attribute
            Context.getPatientService().savePatient(patient);
        }
    }
    log.debug("finished discharge to location method");
}
Also used : PersonAttributeType(org.openmrs.PersonAttributeType) IS(ca.uhn.hl7v2.model.v25.datatype.IS) DLD(ca.uhn.hl7v2.model.v25.datatype.DLD) PersonAttribute(org.openmrs.PersonAttribute)

Example 18 with PersonAttribute

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

the class PersonServiceTest method getPersonAttributeByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see PersonService#getPersonAttributeByUuid(String)
 */
@Test
public void getPersonAttributeByUuid_shouldFindObjectGivenValidUuid() throws Exception {
    String uuid = "0768f3da-b692-44b7-a33f-abf2c450474e";
    PersonAttribute person = Context.getPersonService().getPersonAttributeByUuid(uuid);
    Assert.assertEquals(1, (int) person.getPersonAttributeId());
}
Also used : PersonAttribute(org.openmrs.PersonAttribute) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 19 with PersonAttribute

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

the class PersonServiceTest method getPersonAttribute_shouldReturnPersonAttributeWhenGivenIdDoesExist.

/**
 * @see PersonService#getPersonAttribute(Integer)
 */
@Test
public void getPersonAttribute_shouldReturnPersonAttributeWhenGivenIdDoesExist() throws Exception {
    PersonAttribute personAttribute = Context.getPersonService().getPersonAttribute(17);
    Assert.assertNotNull(personAttribute);
    assertTrue("Expecting the return is of a person attribute", personAttribute.getClass().equals(PersonAttribute.class));
}
Also used : PersonAttribute(org.openmrs.PersonAttribute) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 20 with PersonAttribute

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

the class PersonServiceTest method getPersonAttribute_shouldReturnNullWhenGivenIdDoesNotExist.

/**
 * @see PersonService#getPersonAttribute(Integer)
 */
@Test
public void getPersonAttribute_shouldReturnNullWhenGivenIdDoesNotExist() throws Exception {
    PersonAttribute personAttribute = Context.getPersonService().getPersonAttribute(10000);
    Assert.assertNull(personAttribute);
}
Also used : PersonAttribute(org.openmrs.PersonAttribute) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

PersonAttribute (org.openmrs.PersonAttribute)20 Test (org.junit.Test)12 Patient (org.openmrs.Patient)9 PersonName (org.openmrs.PersonName)7 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)5 Date (java.util.Date)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 PersonAddress (org.openmrs.PersonAddress)3 PersonAttributeType (org.openmrs.PersonAttributeType)3 PatientAndMatchQuality (org.openmrs.module.registrationcore.api.search.PatientAndMatchQuality)3 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)3 ArrayList (java.util.ArrayList)2 PersonMergeLog (org.openmrs.person.PersonMergeLog)2 DLD (ca.uhn.hl7v2.model.v25.datatype.DLD)1 IS (ca.uhn.hl7v2.model.v25.datatype.IS)1 File (java.io.File)1 URL (java.net.URL)1 Random (java.util.Random)1 Criteria (org.hibernate.Criteria)1 Ignore (org.junit.Ignore)1