Search in sources :

Example 31 with PersonAddress

use of org.openmrs.PersonAddress in project openmrs-module-coreapps by openmrs.

the class FormatAddressFragmentController method controller.

public void controller(FragmentConfiguration config, FragmentModel model) {
    config.require("address");
    PersonAddress address = (PersonAddress) config.getAttribute("address");
    AddressSupportCompatibility addressSupport = Context.getRegisteredComponent("coreapps.AddressSupportCompatibility", AddressSupportCompatibility.class);
    Set<String> tokens = addressSupport.getNameMappings().keySet();
    List<String> formattedLines = new ArrayList<String>();
    for (String lineFormat : addressSupport.getLineByLineFormat()) {
        formattedLines.add(replaceWithProperties(lineFormat, tokens, address));
    }
    model.addAttribute("lines", formattedLines);
}
Also used : PersonAddress(org.openmrs.PersonAddress) AddressSupportCompatibility(org.openmrs.module.coreapps.AddressSupportCompatibility) ArrayList(java.util.ArrayList)

Example 32 with PersonAddress

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

the class PatientServiceImpl method setPreferredPatientAddress.

private void setPreferredPatientAddress(Patient patient) {
    PersonAddress preferredAddress = null;
    PersonAddress possiblePreferredAddress = patient.getPersonAddress();
    if (possiblePreferredAddress != null && possiblePreferredAddress.getPreferred() && !possiblePreferredAddress.getVoided()) {
        preferredAddress = possiblePreferredAddress;
    }
    for (PersonAddress address : patient.getAddresses()) {
        if (preferredAddress == null && !address.getVoided()) {
            address.setPreferred(true);
            preferredAddress = address;
            continue;
        }
        if (!address.equals(preferredAddress)) {
            address.setPreferred(false);
        }
    }
}
Also used : PersonAddress(org.openmrs.PersonAddress)

Example 33 with PersonAddress

use of org.openmrs.PersonAddress 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 34 with PersonAddress

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

the class PatientServiceTest method mergePatients_shouldCopyNonvoidedAddressesToPreferredPatient.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldCopyNonvoidedAddressesToPreferredPatient() throws Exception {
    Patient preferred = patientService.getPatient(7);
    Patient notPreferred = patientService.getPatient(8);
    patientService.mergePatients(preferred, notPreferred);
    // make sure one of their addresses has the city of "Jabali"
    boolean found = false;
    for (PersonAddress pa : preferred.getAddresses()) {
        if (pa.getCityVillage().equals("Jabali")) {
            found = true;
        }
    }
    Assert.assertTrue("odd, user 7 didn't get user 8's address", found);
}
Also used : PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 35 with PersonAddress

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

the class PatientServiceTest method savePatient_shouldNotSetThePreferredNameAddressAndIdentifierIfTheyAlreadyExist.

/**
 * @see PatientService#savePatient(Patient)
 */
@Test
public void savePatient_shouldNotSetThePreferredNameAddressAndIdentifierIfTheyAlreadyExist() throws Exception {
    Patient patient = new Patient();
    patient.setGender("M");
    PatientIdentifier identifier = new PatientIdentifier("QWERTY", patientService.getPatientIdentifierType(5), locationService.getLocation(1));
    PatientIdentifier preferredIdentifier = new PatientIdentifier("QWERTY2", patientService.getPatientIdentifierType(2), locationService.getLocation(1));
    preferredIdentifier.setPreferred(true);
    patient.addIdentifier(identifier);
    patient.addIdentifier(preferredIdentifier);
    PersonName name = new PersonName("givenName", "middleName", "familyName");
    PersonName preferredName = new PersonName("givenName", "middleName", "familyName");
    preferredName.setPreferred(true);
    patient.addName(name);
    patient.addName(preferredName);
    PersonAddress address = new PersonAddress();
    address.setAddress1("some address");
    PersonAddress preferredAddress = new PersonAddress();
    preferredAddress.setAddress1("another address");
    preferredAddress.setPreferred(true);
    patient.addAddress(address);
    patient.addAddress(preferredAddress);
    patientService.savePatient(patient);
    Assert.assertTrue(preferredIdentifier.getPreferred());
    Assert.assertTrue(preferredName.getPreferred());
    Assert.assertTrue(preferredAddress.getPreferred());
    Assert.assertFalse(identifier.getPreferred());
    Assert.assertFalse(name.getPreferred());
    Assert.assertFalse(address.getPreferred());
}
Also used : PersonName(org.openmrs.PersonName) PersonAddress(org.openmrs.PersonAddress) Patient(org.openmrs.Patient) PatientIdentifier(org.openmrs.PatientIdentifier) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

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