From 7e61a960117fbc09bc8625c9ab068eefa443f027 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Mon, 19 Aug 2024 11:31:18 +0200 Subject: [PATCH] fix: return timespan of all partial radiations fixes #79 --- .../mapper/ObdsProcedureMapper.java | 52 ++++++++++--------- .../mapper/ObdsProcedureMapperTest.java | 48 +++++++++++++++++ 2 files changed, 75 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapperTest.java diff --git a/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapper.java b/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapper.java index 025d8b20..df4282e6 100644 --- a/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapper.java +++ b/src/main/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapper.java @@ -1,6 +1,7 @@ package org.miracum.streams.ume.obdstofhir.mapper; import java.util.*; +import lombok.val; import org.hl7.fhir.r4.model.*; import org.miracum.streams.ume.obdstofhir.FhirProperties; import org.miracum.streams.ume.obdstofhir.lookup.*; @@ -500,31 +501,32 @@ public Procedure createRadiotherapyProcedure( return stProcedure; } + /** + * This will return the whole timespan of completed partial radiations as part of a therapy. If no + * usable start or end dates have been found, this method will return a tuple containing `null` + * values. + * + * @param partialRadiations The partial radiations + * @return A tuple with start date and end date of all given partial radiations + */ public Tupel getTimeSpanFromPartialRadiations(List partialRadiations) { - // min Beginndatum - List minDates = new ArrayList<>(); - // maxBeginndatum - List maxDates = new ArrayList<>(); - - for (var radio : partialRadiations) { - if (radio.getST_Beginn_Datum() != null) { - minDates.add(convertObdsDateToDateTimeType(radio.getST_Beginn_Datum()).getValue()); - } - if (radio.getST_Ende_Datum() != null) { - maxDates.add(convertObdsDateToDateTimeType(radio.getST_Ende_Datum()).getValue()); - } - } - - Date minDate = null; - Date maxDate = null; - - if (!minDates.isEmpty()) { - minDate = Collections.min(minDates); - } - if (!maxDates.isEmpty()) { - maxDate = Collections.min(maxDates); - } - - return new Tupel<>(minDate, maxDate); + val minDates = + partialRadiations.stream() + .map(radio -> convertObdsDateToDateTimeType(radio.getST_Beginn_Datum())) + .filter(Objects::nonNull) + .map(PrimitiveType::getValue) + .toList(); + + val maxDates = + partialRadiations.stream() + .map(radio -> convertObdsDateToDateTimeType(radio.getST_Ende_Datum())) + .filter(Objects::nonNull) + .map(PrimitiveType::getValue) + .toList(); + + return Tupel.builder() + .first(minDates.isEmpty() ? null : Collections.min(minDates)) + .second(maxDates.isEmpty() ? null : Collections.max(maxDates)) + .build(); } } diff --git a/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapperTest.java b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapperTest.java new file mode 100644 index 00000000..3bb77a1d --- /dev/null +++ b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapperTest.java @@ -0,0 +1,48 @@ +package org.miracum.streams.ume.obdstofhir.mapper; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Instant; +import java.util.Date; +import java.util.List; +import lombok.val; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.miracum.streams.ume.obdstofhir.FhirProperties; +import org.miracum.streams.ume.obdstofhir.model.ADT_GEKID.Menge_Patient.Patient.Menge_Meldung.Meldung.Menge_ST.ST.Menge_Bestrahlung.Bestrahlung; +import org.miracum.streams.ume.obdstofhir.model.Tupel; + +public class ObdsProcedureMapperTest { + + private ObdsProcedureMapper mapper; + + @BeforeEach + void setup() { + this.mapper = new ObdsProcedureMapper(new FhirProperties()); + } + + @Test + void shouldGetWholeTimeSpanFromPartialRadiations() { + val partialRadiations = + List.of( + getTestPartialRadiationForTimespand("01.01.2024", "31.01.2024"), + getTestPartialRadiationForTimespand("01.03.2024", "30.04.2024"), + getTestPartialRadiationForTimespand("10.05.2024", "31.05.2024")); + + val actual = this.mapper.getTimeSpanFromPartialRadiations(partialRadiations); + + assertThat(actual) + .isEqualTo( + Tupel.builder() + .first(Date.from(Instant.parse("2024-01-01T00:00:00Z"))) + .second(Date.from(Instant.parse("2024-05-31T00:00:00Z"))) + .build()); + } + + private static Bestrahlung getTestPartialRadiationForTimespand(String start, String end) { + Bestrahlung bestrahlung = new Bestrahlung(); + bestrahlung.setST_Beginn_Datum(start); + bestrahlung.setST_Ende_Datum(end); + return bestrahlung; + } +}