diff --git a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcTest.java b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcTest.java index 8c512b43940..bd1189e66bf 100644 --- a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcTest.java +++ b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcTest.java @@ -28,6 +28,7 @@ import java.util.Collections; import java.util.List; import java.util.UUID; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -315,6 +316,55 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test { assertEquals(3, myKeepPerson.getLink().size()); } + @Test + public void testMergeNames() { + myDeletePerson.addName().addGiven("Jim"); + myDeletePerson.getNameFirstRep().addGiven("George"); + assertThat(myDeletePerson.getName(), hasSize(1)); + assertThat(myDeletePerson.getName().get(0).getGiven(), hasSize(2)); + + myKeepPerson.addName().addGiven("Jeff"); + myKeepPerson.getNameFirstRep().addGiven("George"); + assertThat(myKeepPerson.getName(), hasSize(1)); + assertThat(myKeepPerson.getName().get(0).getGiven(), hasSize(2)); + + mergePersons(); + assertThat(myKeepPerson.getName(), hasSize(2)); + assertThat(myKeepPerson.getName().get(0).getGiven(), hasSize(2)); + assertThat(myKeepPerson.getName().get(1).getGiven(), hasSize(2)); + } + + @Test + public void testMergeNamesAllSame() { + myDeletePerson.addName().addGiven("Jim"); + myDeletePerson.getNameFirstRep().addGiven("George"); + assertThat(myDeletePerson.getName(), hasSize(1)); + assertThat(myDeletePerson.getName().get(0).getGiven(), hasSize(2)); + + myKeepPerson.addName().addGiven("Jim"); + myKeepPerson.getNameFirstRep().addGiven("George"); + assertThat(myKeepPerson.getName(), hasSize(1)); + assertThat(myKeepPerson.getName().get(0).getGiven(), hasSize(2)); + + mergePersons(); + assertThat(myKeepPerson.getName(), hasSize(1)); + assertThat(myKeepPerson.getName().get(0).getGiven(), hasSize(2)); + } + + @Test + public void testMergeIdentities() { + myDeletePerson.addIdentifier().setValue("aaa"); + myDeletePerson.addIdentifier().setValue("bbb"); + assertThat(myDeletePerson.getIdentifier(), hasSize(2)); + + myKeepPerson.addIdentifier().setValue("aaa"); + myKeepPerson.addIdentifier().setValue("ccc"); + assertThat(myKeepPerson.getIdentifier(), hasSize(2)); + + mergePersons(); + assertThat(myKeepPerson.getIdentifier(), hasSize(3)); + } + private EmpiLink createEmpiLink(Person thePerson, Patient theTargetPatient) { thePerson.addLink().setTarget(new Reference(theTargetPatient)); return myEmpiLinkDaoSvc.createOrUpdateLinkEntity(thePerson, theTargetPatient, EmpiMatchResultEnum.POSSIBLE_MATCH, EmpiLinkSourceEnum.AUTO, createContextForCreate()); diff --git a/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/util/PersonHelper.java b/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/util/PersonHelper.java index c782533da39..d0fe6a946b4 100644 --- a/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/util/PersonHelper.java +++ b/hapi-fhir-server-empi/src/main/java/ca/uhn/fhir/empi/util/PersonHelper.java @@ -34,7 +34,10 @@ import org.hl7.fhir.instance.model.api.IBaseCoding; import org.hl7.fhir.instance.model.api.IBaseReference; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.Address; import org.hl7.fhir.r4.model.Coding; +import org.hl7.fhir.r4.model.ContactPoint; +import org.hl7.fhir.r4.model.HumanName; import org.hl7.fhir.r4.model.Identifier; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Person; @@ -45,8 +48,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Nonnull; +import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.function.BiPredicate; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -476,15 +482,11 @@ public class PersonHelper { private void mergeR4PersonFields(IBaseResource thePersonToDelete, IBaseResource thePersonToKeep) { Person fromPerson = (Person) thePersonToDelete; Person toPerson = (Person) thePersonToKeep; - if (!toPerson.hasName()) { - toPerson.setName(fromPerson.getName()); - } - if (!toPerson.hasAddress()) { - toPerson.setAddress(fromPerson.getAddress()); - } - if (!toPerson.hasTelecom()) { - toPerson.setTelecom(fromPerson.getTelecom()); - } + + mergeElementList(fromPerson, toPerson, HumanName.class, Person::getName, HumanName::equalsDeep); + mergeElementList(fromPerson, toPerson, Identifier.class, Person::getIdentifier, Identifier::equalsDeep); + mergeElementList(fromPerson, toPerson, Address.class, Person::getAddress, Address::equalsDeep); + mergeElementList(fromPerson, toPerson, ContactPoint.class, Person::getTelecom, ContactPoint::equalsDeep); if (!toPerson.hasBirthDate()) { toPerson.setBirthDate(fromPerson.getBirthDate()); } @@ -496,19 +498,28 @@ public class PersonHelper { } } + private
void mergeElementList(P fromPerson, P toPerson, Class > theGetList, BiPredicate