Empi 58 no match create person (#1898)
This commit is contained in:
parent
c91e7b8b41
commit
eb7b8e816b
|
@ -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<EmpiLink> 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<EmpiLink> findEmpiLinkByExample(Example<EmpiLink> theExampleLink) {
|
||||
return myEmpiLinkDao.findAll(theExampleLink);
|
||||
}
|
||||
|
||||
public List<EmpiLink> findEmpiLinksByTarget(Patient theTargetResource) {
|
||||
Long pid = myIdHelperService.getPidOrNull(theTargetResource);
|
||||
if (pid == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
EmpiLink empiLink = new EmpiLink().setTargetPid(pid);
|
||||
Example<EmpiLink> example = Example.of(empiLink);
|
||||
return myEmpiLinkDao.findAll(example);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<EmpiLink> getPatientLinks() {
|
||||
return myEmpiLinkDaoSvc.findEmpiLinksByTarget(myPatient);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<EmpiLink> 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<EmpiLink> links = getPatientLinks();
|
||||
assertEquals(EmpiLinkSourceEnum.MANUAL, links.get(0).getLinkSource());
|
||||
assertEquals(EmpiMatchResultEnum.MATCH, links.get(0).getMatchResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -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 {
|
||||
}
|
Loading…
Reference in New Issue