Skip to content

Commit

Permalink
Hardened patient-from-specimen
Browse files Browse the repository at this point in the history
The Cyprus instance was breaking because they had a weird value for sex
that HAPI FHIR didn't like, so I now catch thst exception and skip
the affected patients.
  • Loading branch information
DavidCroftDKFZ committed Jan 17, 2025
1 parent f5ffba2 commit dd04ee0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/main/java/de/samply/directory_sync_service/fhir/FhirApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ public static <T> Predicate<T> distinctBy(Function<? super T, ?> keyExtractor) {
* @return
*/
private List<Patient> extractPatientListFromSpecimenList(List<Specimen> specimens) {
List<Patient> patients = specimens.stream()
// filter out specimens without a patient reference
.filter(specimen -> specimen.hasSubject())
// Find a Patient object corresponding to the specimen's subject
.map(specimen -> extractPatientFromSpecimen(specimen))
// Skip null results from the mapping step
.filter(Objects::nonNull)
// Avoid duplicating the same patient
.filter(distinctBy(Patient::getId))
// collect the patients into a new list
.collect(Collectors.toList());

return patients;
}
private List<Patient> extractPatientListFromSpecimenList_old(List<Specimen> specimens) {
List<Patient> patients = specimens.stream()
// filter out specimens without a patient reference
.filter(specimen -> specimen.hasSubject())
Expand All @@ -355,16 +370,22 @@ private List<Patient> extractPatientListFromSpecimenList(List<Specimen> specimen
*
* @param specimen a Specimen resource that contains a reference to a Patient resource
* @return a Patient resource that matches the reference in the Specimen resource, or null if not found
* @throws ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException if the FHIR server cannot find the Patient resource
*/
public Patient extractPatientFromSpecimen(Specimen specimen) {
return fhirClient
.read()
.resource(Patient.class)
.withId(specimen.getSubject()
.getReference()
.replaceFirst("Patient/", ""))
.execute();
Patient patient = null;
try {
patient = fhirClient
.read()
.resource(Patient.class)
.withId(specimen.getSubject()
.getReference()
.replaceFirst("Patient/", ""))
.execute();
} catch (Exception e) {
logger.warn("extractPatientFromSpecimen: caught exception", Util.traceFromException(e));
}

return patient;
}

Boolean conditionsPresentInFhirStore = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ private void populateSpecimen(StarModelData starModelInputData, String collectio
// Get the Patient who donated the sample
Patient patient = fhirApi.extractPatientFromSpecimen(specimen);

if (patient == null) {
logger.warn("populateSpecimen: patient is null, skipping specimen: " + specimen.getIdElement().getIdPart());
return;
}

String material = extractMaterialFromSpecimen(specimen);
String patientId = patient.getIdElement().getIdPart();
String sex = patient.getGender().getDisplay();
Expand Down

0 comments on commit dd04ee0

Please sign in to comment.