From c73a4fd0f8e2f16e96f038775c9e26b7ec773813 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Thu, 22 Aug 2024 16:42:59 +0200 Subject: [PATCH] fix: return timespan of all partial radiations (#80) * fix: return timespan of all partial radiations fixes #79 * feat: return early on null or empty list * chore: replace lombok val with final var --------- Co-authored-by: Marcel Erpenbeck --- .../mapper/ObdsProcedureMapper.java | 51 ++++++++++--------- .../mapper/ObdsProcedureMapperTest.java | 47 +++++++++++++++++ 2 files changed, 75 insertions(+), 23 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 9a6a685e..d8053b33 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 @@ -481,31 +481,36 @@ 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); + if (null == partialRadiations || partialRadiations.isEmpty()) { + return new Tupel<>(null, null); } - return new Tupel<>(minDate, maxDate); + final var minDates = + partialRadiations.stream() + .map(radio -> convertObdsDateToDateTimeType(radio.getST_Beginn_Datum())) + .filter(Objects::nonNull) + .map(PrimitiveType::getValue) + .toList(); + + final var 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..3f2e6640 --- /dev/null +++ b/src/test/java/org/miracum/streams/ume/obdstofhir/mapper/ObdsProcedureMapperTest.java @@ -0,0 +1,47 @@ +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 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() { + final var partialRadiations = + List.of( + getTestPartialRadiationForTimespand("01.01.2024", "31.01.2024"), + getTestPartialRadiationForTimespand("01.03.2024", "30.04.2024"), + getTestPartialRadiationForTimespand("10.05.2024", "31.05.2024")); + + final var 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; + } +}