From a28e9e736acbfabec2051e67676619db40ee3434 Mon Sep 17 00:00:00 2001 From: Nick Goupinets Date: Wed, 11 Nov 2020 17:25:44 -0500 Subject: [PATCH] WIP --- .../jpa/empi/svc/EmpiPersonMergerSvcImpl.java | 10 +++---- .../jpa/empi/svc/EmpiPersonMergerSvcTest.java | 26 ++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcImpl.java b/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcImpl.java index a95289dcb5d..66292439a07 100644 --- a/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcImpl.java +++ b/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiPersonMergerSvcImpl.java @@ -64,7 +64,7 @@ public class EmpiPersonMergerSvcImpl implements IEmpiPersonMergerSvc { // myPersonHelper.mergeFields(theFrom, theTo); mergeSourceResourceLinks(theFrom, theTo, toPid, theEmpiTransactionContext); - //removeTargetLinks(theFrom); + removeTargetLinks(theFrom, theTo, theEmpiTransactionContext); refreshLinksAndUpdatePerson(theTo, theEmpiTransactionContext); @@ -86,10 +86,10 @@ public class EmpiPersonMergerSvcImpl implements IEmpiPersonMergerSvc { * @param theEmpiTransactionContext Context to keep track of the deletions */ private void removeTargetLinks(IAnyResource theFrom, IAnyResource theTo, EmpiTransactionContext theEmpiTransactionContext) { - List empiLinksByTargetAndSource = myEmpiLinkDaoSvc.findEmpiLinksByTarget(theFrom); - empiLinksByTargetAndSource + List allLinksWithTheFromAsTarget = myEmpiLinkDaoSvc.findEmpiLinksBySourceResource(theFrom); + allLinksWithTheFromAsTarget .stream() - .filter(EmpiLink::isAuto) + .filter(EmpiLink::isAuto) // only keep manual links .forEach(l -> { theEmpiTransactionContext.addTransactionLogMessage(String.format("Deleting link %s", l)); myEmpiLinkDaoSvc.deleteLink(l); @@ -120,7 +120,7 @@ public class EmpiPersonMergerSvcImpl implements IEmpiPersonMergerSvc { for (EmpiLink fromLink : fromLinks) { Optional optionalToLink = findFirstLinkWithMatchingTarget(toLinks, fromLink); if (optionalToLink.isPresent()) { - toLinks.remove(optionalToLink); + // toLinks.remove(optionalToLink); // The original links already contain this target, so move it over to the toPerson EmpiLink toLink = optionalToLink.get(); 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 6be8301a59f..3536fc582e3 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 @@ -12,6 +12,7 @@ import ca.uhn.fhir.jpa.empi.interceptor.IEmpiStorageInterceptor; import ca.uhn.fhir.jpa.entity.EmpiLink; import ca.uhn.fhir.rest.server.TransactionLogMessages; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Address; import org.hl7.fhir.r4.model.DateType; import org.hl7.fhir.r4.model.Enumerations; @@ -28,6 +29,7 @@ import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.UUID; +import java.util.function.Predicate; import java.util.stream.Collectors; import static org.hamcrest.MatcherAssert.assertThat; @@ -296,7 +298,8 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test { saveLink(toLink); mergeSourcePatients(); - assertEquals(1, myToSourcePatient.getLink().size()); + assertResourceHasLinkCount(myToSourcePatient, 1, (e) -> false); +// assertEquals(1, myToSourcePatient.getLink().size()); assertEquals(3, myEmpiLinkDao.count()); } @@ -311,7 +314,17 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test { myEmpiLinkHelper.logEmpiLinks(); assertThat(myToSourcePatient, is(possibleLinkedTo(myTargetPatient1, myTargetPatient2, myTargetPatient3))); - assertEquals(3, myToSourcePatient.getLink().size()); +// assertEquals(3, myToSourcePatient.getLink().size()); + assertResourceHasAutoLinkCount(myToSourcePatient, 3); + } + + private void assertResourceHasAutoLinkCount(IBaseResource theResource, int theCount) { + assertResourceHasLinkCount(theResource, theCount, EmpiLink::isAuto); + } + + private void assertResourceHasLinkCount(IBaseResource theResource, int theCount, Predicate thePredicate) { + List links = myEmpiLinkDaoSvc.findEmpiLinksBySourceResource(theResource); + assertEquals(theCount, links.stream().filter(thePredicate).count()); } @Test @@ -325,7 +338,7 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test { myEmpiLinkHelper.logEmpiLinks(); assertThat(myToSourcePatient, is(possibleLinkedTo(myTargetPatient1, myTargetPatient2, myTargetPatient3))); - assertEquals(3, myToSourcePatient.getLink().size()); + assertResourceHasAutoLinkCount(myToSourcePatient, 3); } @Test @@ -341,9 +354,8 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test { assertThat(myToSourcePatient, is(possibleLinkedTo(myTargetPatient1, myTargetPatient2, myTargetPatient3))); - List sourcePatientLinks = myEmpiLinkDaoSvc.findEmpiLinksBySourceResource(myToSourcePatient); // assertEquals(3, myToSourcePatient.getLink().size()); - assertEquals(3, sourcePatientLinks.stream().filter(EmpiLink::isAuto).count()); + assertResourceHasAutoLinkCount(myToSourcePatient, 3); } @Test @@ -357,7 +369,9 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test { myEmpiLinkHelper.logEmpiLinks(); assertThat(myToSourcePatient, is(possibleLinkedTo(myTargetPatient1, myTargetPatient2, myTargetPatient3))); - assertEquals(3, myToSourcePatient.getLink().size()); + +// assertEquals(3, myToSourcePatient.getLink().size()); + assertResourceHasAutoLinkCount(myToSourcePatient, 3); } @Test