|
1 | 1 | package de.samply.directory_sync_service.fhir; |
2 | 2 |
|
3 | 3 | import de.samply.directory_sync_service.Util; |
| 4 | +import org.hl7.fhir.r4.model.Bundle; |
4 | 5 | import org.hl7.fhir.r4.model.CodeableConcept; |
5 | 6 | import org.hl7.fhir.r4.model.Coding; |
6 | 7 | import org.hl7.fhir.r4.model.Extension; |
7 | 8 | import org.hl7.fhir.r4.model.Identifier; |
| 9 | +import org.hl7.fhir.r4.model.Observation; |
8 | 10 | import org.hl7.fhir.r4.model.Specimen; |
9 | 11 | import org.slf4j.Logger; |
10 | 12 | import org.slf4j.LoggerFactory; |
@@ -152,4 +154,77 @@ protected List<String> extractStorageTemperaturesFromSpecimenList(List<Specimen> |
152 | 154 |
|
153 | 155 | return storageTemperatures; |
154 | 156 | } |
| 157 | + |
| 158 | + /** |
| 159 | + * Extracts and returns any diagnoses associated with the Specimen. |
| 160 | + * |
| 161 | + * @param specimen |
| 162 | + * @return |
| 163 | + */ |
| 164 | +// public List<String> extractDiagnosesFromSpecimen(Specimen specimen) { |
| 165 | +// return extractExtensionElementValuesFromSpecimen(specimen, getSampleDiagnosisUri()); |
| 166 | +// } |
| 167 | + public List<String> extractDiagnosesFromSpecimen(Specimen specimen) { |
| 168 | + List<String> diagnoses = new ArrayList<>(); |
| 169 | + |
| 170 | + // Sample-diagnosis extension |
| 171 | + List<String> extensionDiagnoses = |
| 172 | + extractExtensionElementValuesFromSpecimen( |
| 173 | + specimen, |
| 174 | + getSampleDiagnosisUri()); |
| 175 | + |
| 176 | + if (extensionDiagnoses != null) { |
| 177 | + diagnoses.addAll(extensionDiagnoses); |
| 178 | + } |
| 179 | + |
| 180 | + // Diagnoses represented as Observations referring to this Specimen |
| 181 | + diagnoses.addAll(extractDiagnosesFromObservations(specimen)); |
| 182 | + |
| 183 | + return diagnoses.stream() |
| 184 | + .filter(Objects::nonNull) |
| 185 | + .distinct() |
| 186 | + .toList(); |
| 187 | + } |
| 188 | + |
| 189 | + private List<String> extractDiagnosesFromObservations(Specimen specimen) { |
| 190 | + if (specimen == null || specimen.getIdElement().isEmpty()) { |
| 191 | + return List.of(); |
| 192 | + } |
| 193 | + |
| 194 | + String specimenReference = |
| 195 | + "Specimen/" + specimen.getIdElement().getIdPart(); |
| 196 | + |
| 197 | + try { |
| 198 | + Bundle bundle = fhirClient |
| 199 | + .search() |
| 200 | + .forResource(Observation.class) |
| 201 | + .where(Observation.SPECIMEN.hasId(specimenReference)) |
| 202 | + .returnBundle(Bundle.class) |
| 203 | + .execute(); |
| 204 | + |
| 205 | + return bundle.getEntry().stream() |
| 206 | + .map(Bundle.BundleEntryComponent::getResource) |
| 207 | + .filter(Observation.class::isInstance) |
| 208 | + .map(Observation.class::cast) |
| 209 | + |
| 210 | + // Optional, but useful if other kinds of Observations |
| 211 | + // also point to specimens: |
| 212 | + .filter(Observation::hasValueCodeableConcept) |
| 213 | + |
| 214 | + .map(Observation::getValueCodeableConcept) |
| 215 | + .flatMap(cc -> cc.getCoding().stream()) |
| 216 | + .filter(coding -> |
| 217 | + ICD10_SYSTEM.equals(coding.getSystem())) |
| 218 | + .map(Coding::getCode) |
| 219 | + .filter(Objects::nonNull) |
| 220 | + .distinct() |
| 221 | + .toList(); |
| 222 | + |
| 223 | + } catch (Exception e) { |
| 224 | + logger.warn( |
| 225 | + "extractDiagnosesFromObservations: exception: " |
| 226 | + + Util.traceFromException(e)); |
| 227 | + return List.of(); |
| 228 | + } |
| 229 | + } |
155 | 230 | } |
0 commit comments