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;
}
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());
}
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"));
}
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()));
}
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);
}
Aggregations