Skip to content

Commit

Permalink
[HIE-2] make CR request for patients matching identifiers returned by…
Browse files Browse the repository at this point in the history
… ihe-pix CR endpoint
  • Loading branch information
pmanko2 authored and samuelmale committed Mar 19, 2024
1 parent 41427b5 commit ff7e89e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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<Patient> getCRPatients(String sourceIdentifier, String sourceIdentifierSystem, List<String> targetSystems) {


Parameters betterCrRequest = fhirClient
// construct request to external FHIR $ihe-pix endpoint
IOperationUntypedWithInputAndPartialOutput<Parameters> identifiersRequest = fhirClient
.operation()
.onType(FhirConstants.PATIENT)
.named(FhirCRConstants.IHE_PIX_OPERATION)
.withNoParameters(Parameters.class)
.execute();

// construct and send request to external client registry
IQuery<IBaseBundle> 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<String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit ff7e89e

Please sign in to comment.