Skip to content

Commit

Permalink
ensure search by other attributes aswell
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Sep 2, 2024
1 parent e04b7b4 commit 0520aa7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public PatientSearchResultsForm getPatientResults(HttpServletRequest request,
}

if (request.getParameter("crResult") != null && request.getParameter("crResult").contains("true")) {
List<PatientSearchResults> fhirResults = searchPatientInClientRegistry(nationalID);
List<PatientSearchResults> fhirResults = searchPatientInClientRegistry(lastName, firstName, STNumber,
subjectNumber, nationalID, null, guid, dateOfBirth, gender);
results.addAll(fhirResults);
}

Expand Down Expand Up @@ -171,52 +172,69 @@ private List<org.hl7.fhir.r4.model.Patient> parseCRPatientSearchResults(Bundle p
.map(entry -> (org.hl7.fhir.r4.model.Patient) entry.getResource()).collect(Collectors.toList());
}

private List<PatientSearchResults> searchPatientInClientRegistry(String nationalID) {
private List<PatientSearchResults> searchPatientInClientRegistry(String lastName, String firstName, String STNumber,
String subjectNumber, String nationalID, String patientID, String guid, String dateOfBirth, String gender) {
if (isClientRegistryConfigInvalid()) {
return new ArrayList<>();
}

IGenericClient clientRegistry = fhirUtil.getFhirClient(fhirConfig.getClientRegistryServerUrl(),
fhirConfig.getClientRegistryUserName(), fhirConfig.getClientRegistryPassword());

Patient patient = patientService.getPatientByNationalId(nationalID);
List<PatientSearchResults> patientSearchResults = searchResultsService.getSearchResults(lastName, firstName,
STNumber, subjectNumber, nationalID, null, patientID, guid, dateOfBirth, gender);

List<PatientSearchResults> finalResults = new ArrayList<>();

for (PatientSearchResults patientSearchResult : patientSearchResults) {
List<String> crIdentifiers = fetchCRIdentifiers(clientRegistry, patientSearchResult);

if (!crIdentifiers.isEmpty()) {
List<org.hl7.fhir.r4.model.Patient> externalPatients = fetchExternalPatients(clientRegistry,
crIdentifiers);

Patient openelis = patientService.getPatientByNationalId(patientSearchResult.getNationalId());
for (org.hl7.fhir.r4.model.Patient externalPatient : externalPatients) {
Patient openElisPatient = SpringContext.getBean(FhirTransformService.class)
.transformToOpenElisPatient(openelis, externalPatient);
PatientSearchResults searchResult = getSearchResultsForPatient(openElisPatient, null);
searchResult.setDataSourceName(MessageUtil.getMessage("patient.cr.source"));
finalResults.add(searchResult);
}
}
}

return finalResults;
}

private List<String> fetchCRIdentifiers(IGenericClient clientRegistry, PatientSearchResults patientSearchResult) {
List<String> targetSystems = targetSystemsParam == null ? Collections.emptyList()
: targetSystemsParam.getValuesAsQueryTokens().stream().filter(Objects::nonNull)
.map(StringParam::getValue).collect(Collectors.toList());

// construct request to external fhir client
// Construct request to external FHIR client
IOperationUntypedWithInputAndPartialOutput<Parameters> identifiersRequest = clientRegistry.operation()
.onType("Patient").named("$ihe-pix").withSearchParameter(Parameters.class, "sourceIdentifier",
new TokenParam("http://openelis-global.org/pat_nationalId", patient.getNationalId()));
new TokenParam("http://openelis-global.org/pat_nationalId",
patientSearchResult.getNationalId()));

if (!targetSystems.isEmpty()) {
identifiersRequest.andSearchParameter("targetSystem", 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 -> ((Reference) param.getValue()).getReference()).collect(Collectors.toList());

if (crIdentifiers.isEmpty()) {
return new ArrayList<>();
}
return crMatchingParams.getParameter().stream().filter(param -> Objects.equals(param.getName(), "targetId"))
.map(param -> ((Reference) param.getValue()).getReference()).collect(Collectors.toList());
}

private List<org.hl7.fhir.r4.model.Patient> fetchExternalPatients(IGenericClient clientRegistry,
List<String> crIdentifiers) {
Bundle patientBundle = clientRegistry.search().forResource(org.hl7.fhir.r4.model.Patient.class)
.where(new StringClientParam(org.hl7.fhir.r4.model.Patient.SP_RES_ID).matches().values(crIdentifiers))
.returnBundle(Bundle.class).execute();

List<org.hl7.fhir.r4.model.Patient> externalPatients = parseCRPatientSearchResults(patientBundle);
List<PatientSearchResults> results = new ArrayList<>();

for (org.hl7.fhir.r4.model.Patient externalPatient : externalPatients) {
Patient openElisPatient = SpringContext.getBean(FhirTransformService.class).transformToOpenElisPatient(patient, externalPatient);
PatientSearchResults searchResult = getSearchResultsForPatient(openElisPatient, null);
searchResult.setDataSourceName(MessageUtil.getMessage("patient.cr.source"));
results.add(searchResult);
}

return results;
return parseCRPatientSearchResults(patientBundle);
}

private boolean isClientRegistryConfigInvalid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,15 +721,15 @@ public Patient transformToOpenElisPatient(Patient openELISPatient, org.hl7.fhir.
}

switch (fhirPatient.getGender()) {
case MALE:
openELISPatient.setGender("M");
break;
case FEMALE:
openELISPatient.setGender("F");
break;
default:
openELISPatient.setGender(null);
break;
case MALE:
openELISPatient.setGender("M");
break;
case FEMALE:
openELISPatient.setGender("F");
break;
default:
openELISPatient.setGender(null);
break;
}

if (fhirPatient.getBirthDate() != null) {
Expand Down

0 comments on commit 0520aa7

Please sign in to comment.