From ff7e89e6eef5a6d05f4f573f06394561b10f9091 Mon Sep 17 00:00:00 2001 From: Pawel Mankowski Date: Sat, 18 Feb 2023 18:19:44 -0500 Subject: [PATCH] [HIE-2] make CR request for patients matching identifiers returned by ihe-pix CR endpoint --- .../api/impl/FhirCRPatientServiceImpl.java | 54 +++++++++++-------- .../providers/FhirCRConstants.java | 1 + 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/clientregistry/api/impl/FhirCRPatientServiceImpl.java b/api/src/main/java/org/openmrs/module/clientregistry/api/impl/FhirCRPatientServiceImpl.java index c8f64ac..6f91773 100644 --- a/api/src/main/java/org/openmrs/module/clientregistry/api/impl/FhirCRPatientServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/clientregistry/api/impl/FhirCRPatientServiceImpl.java @@ -1,11 +1,15 @@ package org.openmrs.module.clientregistry.api.impl; import ca.uhn.fhir.rest.client.api.IGenericClient; +import ca.uhn.fhir.rest.gclient.IOperationUntypedWithInputAndPartialOutput; import ca.uhn.fhir.rest.gclient.IQuery; +import ca.uhn.fhir.rest.gclient.StringClientParam; +import ca.uhn.fhir.rest.param.StringOrListParam; +import ca.uhn.fhir.rest.param.StringParam; +import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.param.UriOrListParam; import org.hl7.fhir.instance.model.api.IBaseBundle; -import org.hl7.fhir.r4.model.Bundle; -import org.hl7.fhir.r4.model.Parameters; -import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.*; import org.openmrs.module.clientregistry.ClientRegistryConfig; import org.openmrs.module.clientregistry.api.CRPatientService; import org.openmrs.module.clientregistry.providers.FhirCRConstants; @@ -14,7 +18,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; @Component @@ -27,34 +33,40 @@ public class FhirCRPatientServiceImpl implements CRPatientService { private ClientRegistryConfig config; /** - * Constructs a $ihe-pix fhir client call to an external Client Registry returning any patients - * that match the given source identifier/system and target systems. + * Get patient identifiers from an external client registry's $ihe-pix implementation. Use the + * returned identifiers to then request a matching Patient bundle from the client registry. */ @Override public List getCRPatients(String sourceIdentifier, String sourceIdentifierSystem, List targetSystems) { - - - Parameters betterCrRequest = fhirClient + // construct request to external FHIR $ihe-pix endpoint + IOperationUntypedWithInputAndPartialOutput identifiersRequest = fhirClient .operation() .onType(FhirConstants.PATIENT) .named(FhirCRConstants.IHE_PIX_OPERATION) - .withNoParameters(Parameters.class) - .execute(); - - // construct and send request to external client registry - IQuery crRequest = fhirClient - .search() - .byUrl( - String.format("%s/%s", config.getClientRegistryServerUrl(), config.getClientRegistryGetPatientEndpoint())) - .where( - FhirCRConstants.SOURCE_IDENTIFIER_PARAM.exactly().systemAndIdentifier(sourceIdentifierSystem, - sourceIdentifier)); + .withSearchParameter(Parameters.class, FhirCRConstants.SOURCE_IDENTIFIER, new TokenParam(sourceIdentifierSystem, sourceIdentifier)); if (!targetSystems.isEmpty()) { - crRequest.and(FhirCRConstants.TARGET_SYSTEM_PARAM.matches().values(targetSystems)); + identifiersRequest.andSearchParameter(FhirCRConstants.TARGET_SYSTEM, new StringParam(String.join(",", targetSystems))); + } + + Parameters crMatchingParams = identifiersRequest.useHttpGet().execute(); + List crIdentifiers = crMatchingParams.getParameter().stream() + .filter(param -> Objects.equals(param.getName(), "targetId")) + .map(param -> param.getValue().toString()) + .collect(Collectors.toList()); + + if (crIdentifiers.isEmpty()) { + return Collections.emptyList(); } + + // construct and send request to external client registry + Bundle patientBundle = fhirClient + .search() + .forResource(Patient.class) + .where(new StringClientParam(Patient.SP_RES_ID).matches().values(crIdentifiers)) + .returnBundle(Bundle.class) + .execute(); - Bundle patientBundle = crRequest.returnBundle(Bundle.class).execute(); return parseCRPatientSearchResults(patientBundle); } diff --git a/api/src/main/java/org/openmrs/module/clientregistry/providers/FhirCRConstants.java b/api/src/main/java/org/openmrs/module/clientregistry/providers/FhirCRConstants.java index 3ec4eea..66c7286 100644 --- a/api/src/main/java/org/openmrs/module/clientregistry/providers/FhirCRConstants.java +++ b/api/src/main/java/org/openmrs/module/clientregistry/providers/FhirCRConstants.java @@ -4,6 +4,7 @@ import ca.uhn.fhir.rest.gclient.StringClientParam; import ca.uhn.fhir.rest.gclient.TokenClientParam; import ca.uhn.fhir.rest.gclient.UriClientParam; +import ca.uhn.fhir.rest.param.StringOrListParam; public class FhirCRConstants {