From eb7b8e816bb421f9145d52307c529187c10d8ca6 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Thu, 4 Jun 2020 16:07:50 -0400 Subject: [PATCH] Empi 58 no match create person (#1898) --- .../ca/uhn/fhir/jpa/dao/EmpiLinkDaoSvc.java | 13 ++++- .../jpa/empi/svc/EmpiLinkUpdaterSvcImpl.java | 49 ++++++++++++------- .../jpa/empi/provider/BaseLinkR4Test.java | 10 +++- .../EmpiProviderUpdateLinkR4Test.java | 28 +++++++++-- .../empi/svc/EmpiLinkUpdaterSvcImplTest.java | 10 ++++ 5 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImplTest.java diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/EmpiLinkDaoSvc.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/EmpiLinkDaoSvc.java index ca57593dd61..8cf57e23bf3 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/EmpiLinkDaoSvc.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/EmpiLinkDaoSvc.java @@ -28,6 +28,7 @@ import ca.uhn.fhir.jpa.dao.data.IEmpiLinkDao; import ca.uhn.fhir.jpa.dao.index.IdHelperService; import ca.uhn.fhir.jpa.entity.EmpiLink; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.Patient; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; @@ -172,7 +173,7 @@ public class EmpiLinkDaoSvc { } public List findEmpiLinksByPersonId(IBaseResource thePersonResource) { - @Nullable Long pid = myIdHelperService.getPidOrNull(thePersonResource); + Long pid = myIdHelperService.getPidOrNull(thePersonResource); if (pid == null) { return Collections.emptyList(); } @@ -192,4 +193,14 @@ public class EmpiLinkDaoSvc { public List findEmpiLinkByExample(Example theExampleLink) { return myEmpiLinkDao.findAll(theExampleLink); } + + public List findEmpiLinksByTarget(Patient theTargetResource) { + Long pid = myIdHelperService.getPidOrNull(theTargetResource); + if (pid == null) { + return Collections.emptyList(); + } + EmpiLink empiLink = new EmpiLink().setTargetPid(pid); + Example example = Example.of(empiLink); + return myEmpiLinkDao.findAll(example); + } } diff --git a/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImpl.java b/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImpl.java index df5d73d1dac..459425c712e 100644 --- a/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImpl.java +++ b/hapi-fhir-jpaserver-empi/src/main/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImpl.java @@ -53,31 +53,15 @@ public class EmpiLinkUpdaterSvcImpl implements IEmpiLinkUpdaterSvc { IEmpiLinkSvc myEmpiLinkSvc; @Autowired EmpiResourceDaoSvc myEmpiResourceDaoSvc; + @Autowired + EmpiMatchLinkSvc myEmpiMatchLinkSvc; @Transactional @Override public IAnyResource updateLink(IAnyResource thePerson, IAnyResource theTarget, EmpiMatchResultEnum theMatchResult, EmpiTransactionContext theEmpiContext) { - if (theMatchResult != EmpiMatchResultEnum.NO_MATCH && - theMatchResult != EmpiMatchResultEnum.MATCH) { - throw new InvalidRequestException("Match Result may only be set to " + EmpiMatchResultEnum.NO_MATCH + " or " + EmpiMatchResultEnum.MATCH); - } - - String personType = myFhirContext.getResourceType(thePerson); - if (!"Person".equals(personType)) { - throw new InvalidRequestException("First argument to updateLink must be a Person. Was " + personType); - } String targetType = myFhirContext.getResourceType(theTarget); - if (!EmpiUtil.supportedTargetType(targetType)) { - throw new InvalidRequestException("Second argument to updateLink must be a Patient or Practitioner. Was " + targetType); - } - if (!EmpiUtil.isEmpiManaged(thePerson)) { - throw new InvalidRequestException("Only EMPI Managed Person resources may be updated via this operation. The Person resource provided is not tagged as managed by hapi-empi"); - } - - if (!EmpiUtil.isEmpiAccessible(theTarget)) { - throw new InvalidRequestException("The target is marked with the " + EmpiConstants.CODE_NO_EMPI_MANAGED + " tag which means it may not be EMPI linked."); - } + validateUpdateLinkRequest(thePerson, theTarget, theMatchResult, targetType); Long personId = myIdHelperService.getPidOrThrowException(thePerson); Long targetId = myIdHelperService.getPidOrThrowException(theTarget); @@ -98,6 +82,33 @@ public class EmpiLinkUpdaterSvcImpl implements IEmpiLinkUpdaterSvc { myEmpiLinkDaoSvc.save(empiLink); myEmpiLinkSvc.syncEmpiLinksToPersonLinks(thePerson, theEmpiContext); myEmpiResourceDaoSvc.updatePerson(thePerson); + if (theMatchResult == EmpiMatchResultEnum.NO_MATCH) { + // Need to find a new Person to link this target to + myEmpiMatchLinkSvc.updateEmpiLinksForEmpiTarget(theTarget, theEmpiContext); + } return thePerson; } + + private void validateUpdateLinkRequest(IAnyResource thePerson, IAnyResource theTarget, EmpiMatchResultEnum theMatchResult, String theTargetType) { + String personType = myFhirContext.getResourceType(thePerson); + if (theMatchResult != EmpiMatchResultEnum.NO_MATCH && + theMatchResult != EmpiMatchResultEnum.MATCH) { + throw new InvalidRequestException("Match Result may only be set to " + EmpiMatchResultEnum.NO_MATCH + " or " + EmpiMatchResultEnum.MATCH); + } + + if (!"Person".equals(personType)) { + throw new InvalidRequestException("First argument to updateLink must be a Person. Was " + personType); + } + if (!EmpiUtil.supportedTargetType(theTargetType)) { + throw new InvalidRequestException("Second argument to updateLink must be a Patient or Practitioner. Was " + theTargetType); + } + + if (!EmpiUtil.isEmpiManaged(thePerson)) { + throw new InvalidRequestException("Only EMPI Managed Person resources may be updated via this operation. The Person resource provided is not tagged as managed by hapi-empi"); + } + + if (!EmpiUtil.isEmpiAccessible(theTarget)) { + throw new InvalidRequestException("The target is marked with the " + EmpiConstants.CODE_NO_EMPI_MANAGED + " tag which means it may not be EMPI linked."); + } + } } diff --git a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/BaseLinkR4Test.java b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/BaseLinkR4Test.java index c0be68494a5..6687d91d7c0 100644 --- a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/BaseLinkR4Test.java +++ b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/BaseLinkR4Test.java @@ -9,6 +9,7 @@ import org.hl7.fhir.r4.model.StringType; import org.junit.Before; import javax.annotation.Nonnull; +import java.util.List; import static org.junit.Assert.assertEquals; @@ -37,7 +38,7 @@ public abstract class BaseLinkR4Test extends BaseProviderR4Test { myPersonId = new StringType(myPerson.getIdElement().getValue()); myVersionlessPersonId = new StringType(myPerson.getIdElement().toVersionless().getValue()); - myLink = getLink(); + myLink = getOnlyPatientLink(); // Tests require our initial link to be a POSSIBLE_MATCH myLink.setMatchResult(EmpiMatchResultEnum.POSSIBLE_MATCH); myEmpiLinkDao.save(myLink); @@ -45,7 +46,12 @@ public abstract class BaseLinkR4Test extends BaseProviderR4Test { } @Nonnull - protected EmpiLink getLink() { + protected EmpiLink getOnlyPatientLink() { return myEmpiLinkDaoSvc.findEmpiLinkByTarget(myPatient).get(); } + + @Nonnull + protected List getPatientLinks() { + return myEmpiLinkDaoSvc.findEmpiLinksByTarget(myPatient); + } } diff --git a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/EmpiProviderUpdateLinkR4Test.java b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/EmpiProviderUpdateLinkR4Test.java index 1fad4f502d9..83fbc3d2456 100644 --- a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/EmpiProviderUpdateLinkR4Test.java +++ b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/provider/EmpiProviderUpdateLinkR4Test.java @@ -3,6 +3,7 @@ package ca.uhn.fhir.jpa.empi.provider; import ca.uhn.fhir.empi.api.EmpiConstants; import ca.uhn.fhir.empi.api.EmpiLinkSourceEnum; import ca.uhn.fhir.empi.api.EmpiMatchResultEnum; +import ca.uhn.fhir.jpa.entity.EmpiLink; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException; import org.hl7.fhir.r4.model.Patient; @@ -10,23 +11,40 @@ import org.hl7.fhir.r4.model.Person; import org.hl7.fhir.r4.model.StringType; import org.junit.Test; +import java.util.List; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.matchesPattern; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.fail; public class EmpiProviderUpdateLinkR4Test extends BaseLinkR4Test { + @Test + public void testUpdateLinkNoMatch() { + assertLinkCount(1); + myEmpiProviderR4.updateLink(myPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails); + assertLinkCount(2); + List links = getPatientLinks(); + assertEquals(EmpiLinkSourceEnum.MANUAL, links.get(0).getLinkSource()); + assertEquals(EmpiMatchResultEnum.NO_MATCH, links.get(0).getMatchResult()); + assertEquals(EmpiLinkSourceEnum.AUTO, links.get(1).getLinkSource()); + assertEquals(EmpiMatchResultEnum.MATCH, links.get(1).getMatchResult()); + assertNotEquals(links.get(0).getPersonPid(), links.get(1).getPersonPid()); + } @Test - public void testUpdateLinkHappyPath() { - myEmpiProviderR4.updateLink(myPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails); + public void testUpdateLinkMatch() { + assertLinkCount(1); + myEmpiProviderR4.updateLink(myPersonId, myPatientId, MATCH_RESULT, myRequestDetails); + assertLinkCount(1); - myLink = getLink(); - assertEquals(EmpiLinkSourceEnum.MANUAL, myLink.getLinkSource()); - assertEquals(EmpiMatchResultEnum.NO_MATCH, myLink.getMatchResult()); + List links = getPatientLinks(); + assertEquals(EmpiLinkSourceEnum.MANUAL, links.get(0).getLinkSource()); + assertEquals(EmpiMatchResultEnum.MATCH, links.get(0).getMatchResult()); } @Test diff --git a/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImplTest.java b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImplTest.java new file mode 100644 index 00000000000..3e812f48de4 --- /dev/null +++ b/hapi-fhir-jpaserver-empi/src/test/java/ca/uhn/fhir/jpa/empi/svc/EmpiLinkUpdaterSvcImplTest.java @@ -0,0 +1,10 @@ +package ca.uhn.fhir.jpa.empi.svc; + +import ca.uhn.fhir.jpa.empi.provider.EmpiProviderUpdateLinkR4Test; + +/** + * Tests for this service are in the test for the provider that wraps this service: + * @see EmpiProviderUpdateLinkR4Test + */ +public class EmpiLinkUpdaterSvcImplTest { +}