Search in sources :

Example 1 with PersonMergeLogData

use of org.openmrs.person.PersonMergeLogData in project openmrs-core by openmrs.

the class PersonServiceTest method getTestPersonMergeLog.

private PersonMergeLog getTestPersonMergeLog() {
    PersonMergeLog personMergeLog = new PersonMergeLog();
    personMergeLog.setLoser(new Person(1));
    personMergeLog.setWinner(new Person(2));
    PersonMergeLogData data = new PersonMergeLogData();
    data.addCreatedAddress("1");
    data.addCreatedAttribute("2");
    data.addCreatedIdentifier("3");
    data.addCreatedName("4");
    data.addCreatedOrder("5");
    data.addCreatedProgram("6");
    data.addCreatedRelationship("7");
    data.addMovedEncounter("8");
    data.addMovedIndependentObservation("9");
    data.addMovedUser("10");
    data.addVoidedRelationship("11");
    data.setPriorCauseOfDeath("test");
    data.setPriorDateOfBirth(new Date());
    data.setPriorDateOfBirthEstimated(true);
    data.setPriorDateOfDeath(new Date());
    data.setPriorGender("F");
    personMergeLog.setPersonMergeLogData(data);
    return personMergeLog;
}
Also used : PersonMergeLog(org.openmrs.person.PersonMergeLog) Person(org.openmrs.Person) Date(java.util.Date) PersonMergeLogData(org.openmrs.person.PersonMergeLogData)

Example 2 with PersonMergeLogData

use of org.openmrs.person.PersonMergeLogData in project openmrs-core by openmrs.

the class PersonMergeLogValidatorTest method validate_shouldPassValidationIfAllFieldsAreCorrect.

/**
 * @see PersonMergeLogValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllFieldsAreCorrect() {
    PersonMergeLog personMergeLog = new PersonMergeLog();
    personMergeLog.setWinner(new Person());
    personMergeLog.setLoser(new Person());
    personMergeLog.setPersonMergeLogData(new PersonMergeLogData());
    PersonMergeLogValidator validator = new PersonMergeLogValidator();
    Errors errors = new BindException(personMergeLog, "personMergeLog");
    validator.validate(personMergeLog, errors);
    Assert.assertFalse(errors.hasFieldErrors());
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) PersonMergeLog(org.openmrs.person.PersonMergeLog) Person(org.openmrs.Person) PersonMergeLogData(org.openmrs.person.PersonMergeLogData) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with PersonMergeLogData

use of org.openmrs.person.PersonMergeLogData in project openmrs-core by openmrs.

the class PersonMergeLogValidatorTest method validate_shouldFailValidationIfLoserIsNull.

/**
 * @see PersonMergeLogValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfLoserIsNull() {
    PersonMergeLog personMergeLog = new PersonMergeLog();
    personMergeLog.setWinner(new Person());
    personMergeLog.setPersonMergeLogData(new PersonMergeLogData());
    PersonMergeLogValidator validator = new PersonMergeLogValidator();
    Errors errors = new BindException(personMergeLog, "personMergeLog");
    validator.validate(personMergeLog, errors);
    Assert.assertTrue(errors.hasFieldErrors("loser"));
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) PersonMergeLog(org.openmrs.person.PersonMergeLog) Person(org.openmrs.Person) PersonMergeLogData(org.openmrs.person.PersonMergeLogData) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 4 with PersonMergeLogData

use of org.openmrs.person.PersonMergeLogData in project openmrs-core by openmrs.

the class PatientServiceTest method mergePatients_shouldMergeVisitsFromNonPreferredToPreferredPatient.

/**
 * @see PatientService#mergePatients(Patient,Patient)
 */
@Test
public void mergePatients_shouldMergeVisitsFromNonPreferredToPreferredPatient() throws Exception {
    executeDataSet(ENCOUNTERS_FOR_VISITS_XML);
    VisitService visitService = Context.getVisitService();
    Patient notPreferred = patientService.getPatient(2);
    voidOrders(Collections.singleton(notPreferred));
    Patient preferred = patientService.getPatient(6);
    // patient 2 (not preferred) has 3 unvoided visits (id = 1, 2, 3) and 1 voided visit (id = 6)
    Visit visit1 = visitService.getVisit(1);
    Visit visit2 = visitService.getVisit(2);
    Visit visit3 = visitService.getVisit(3);
    Visit visit6 = visitService.getVisit(6);
    // patient 6 (preferred) has 2 unvoided visits (id = 4, 5) and no voided visits
    Visit visit4 = visitService.getVisit(4);
    Visit visit5 = visitService.getVisit(5);
    List<String> encounterUuidsThatShouldBeMoved = new ArrayList<>();
    encounterUuidsThatShouldBeMoved.add(Context.getEncounterService().getEncounter(6).getUuid());
    for (Visit v : Arrays.asList(visit1, visit2, visit3)) {
        for (Encounter e : v.getEncounters()) {
            encounterUuidsThatShouldBeMoved.add(e.getUuid());
        }
    }
    List<Obs> originalUnvoidedObs = Context.getObsService().getObservationsByPerson(notPreferred);
    PersonMergeLog mergeLog = mergeAndRetrieveAudit(preferred, notPreferred);
    Patient merged = patientService.getPatient(preferred.getId());
    List<Visit> mergedVisits = visitService.getVisitsByPatient(merged, true, true);
    assertThat(mergedVisits.size(), is(6));
    // in order to keep this test passing when (someday?) we copy visits instead of moving them, use matchers here:
    assertThat(mergedVisits, containsInAnyOrder(matchingVisit(visit1), matchingVisit(visit2), matchingVisit(visit3), matchingVisit(visit4), matchingVisit(visit5), matchingVisit(visit6)));
    // be sure nothing slipped through without being assigned to the right patient (probably not necessary)
    for (Visit v : mergedVisits) {
        for (Encounter e : v.getEncounters()) {
            assertThat(e.getPatient(), is(v.getPatient()));
            for (Obs o : e.getAllObs(true)) {
                if (!originalUnvoidedObs.contains(o)) {
                    assertThat(o.getPerson().getId(), is(v.getPatient().getId()));
                }
            }
        }
    }
    // now check that moving visits and their contained encounters was audited correctly
    PersonMergeLogData mergeLogData = mergeLog.getPersonMergeLogData();
    assertThat(mergeLogData.getMovedVisits().size(), is(4));
    assertThat(mergeLogData.getMovedVisits(), containsInAnyOrder(visit1.getUuid(), visit2.getUuid(), visit3.getUuid(), visit6.getUuid()));
    assertThat(mergeLogData.getMovedEncounters().size(), is(encounterUuidsThatShouldBeMoved.size()));
    assertThat(mergeLogData.getMovedEncounters(), containsInAnyOrder(encounterUuidsThatShouldBeMoved.toArray()));
}
Also used : Obs(org.openmrs.Obs) Visit(org.openmrs.Visit) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) Encounter(org.openmrs.Encounter) PersonMergeLog(org.openmrs.person.PersonMergeLog) PersonMergeLogData(org.openmrs.person.PersonMergeLogData) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) PatientServiceImplTest(org.openmrs.api.impl.PatientServiceImplTest) Test(org.junit.Test)

Example 5 with PersonMergeLogData

use of org.openmrs.person.PersonMergeLogData in project openmrs-core by openmrs.

the class PatientServiceImpl method mergePatients.

/**
 * 1) Moves object (encounters/obs) pointing to <code>nonPreferred</code> to
 * <code>preferred</code> 2) Copies data (gender/birthdate/names/ids/etc) from
 * <code>nonPreferred</code> to <code>preferred</code> iff the data is missing or null in
 * <code>preferred</code> 3) <code>notPreferred</code> is marked as voided
 *
 * @param preferred
 * @param notPreferred
 * @throws APIException
 * @see org.openmrs.api.PatientService#mergePatients(org.openmrs.Patient, org.openmrs.Patient)
 */
@Override
public void mergePatients(Patient preferred, Patient notPreferred) throws APIException, SerializationException {
    log.debug("Merging patients: (preferred)" + preferred.getPatientId() + ", (notPreferred) " + notPreferred.getPatientId());
    if (preferred.getPatientId().equals(notPreferred.getPatientId())) {
        log.debug("Merge operation cancelled: Cannot merge user" + preferred.getPatientId() + " to self");
        throw new APIException("Patient.merge.cancelled", new Object[] { preferred.getPatientId() });
    }
    requireNoActiveOrderOfSameType(preferred, notPreferred);
    PersonMergeLogData mergedData = new PersonMergeLogData();
    mergeVisits(preferred, notPreferred, mergedData);
    mergeEncounters(preferred, notPreferred, mergedData);
    mergeProgramEnrolments(preferred, notPreferred, mergedData);
    mergeRelationships(preferred, notPreferred, mergedData);
    mergeObservationsNotContainedInEncounters(preferred, notPreferred, mergedData);
    mergeIdentifiers(preferred, notPreferred, mergedData);
    mergeNames(preferred, notPreferred, mergedData);
    mergeAddresses(preferred, notPreferred, mergedData);
    mergePersonAttributes(preferred, notPreferred, mergedData);
    mergeGenderInformation(preferred, notPreferred, mergedData);
    mergeDateOfBirth(preferred, notPreferred, mergedData);
    mergeDateOfDeath(preferred, notPreferred, mergedData);
    // 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 : APIException(org.openmrs.api.APIException) PersonMergeLog(org.openmrs.person.PersonMergeLog) PersonMergeLogData(org.openmrs.person.PersonMergeLogData)

Aggregations

PersonMergeLogData (org.openmrs.person.PersonMergeLogData)9 PersonMergeLog (org.openmrs.person.PersonMergeLog)8 Test (org.junit.Test)6 Person (org.openmrs.Person)6 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)6 BindException (org.springframework.validation.BindException)5 Errors (org.springframework.validation.Errors)5 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Encounter (org.openmrs.Encounter)1 Obs (org.openmrs.Obs)1 Patient (org.openmrs.Patient)1 Visit (org.openmrs.Visit)1 APIException (org.openmrs.api.APIException)1 PatientServiceImplTest (org.openmrs.api.impl.PatientServiceImplTest)1