Skip to content

Commit 451074f

Browse files
modified date validator to convert any date value=1 to a default date - 1970-01-01
1 parent d9b8eb0 commit 451074f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

docker/service/src/main/java/uk/nhs/adaptors/scr/mappings/from/hl7/XmlToFhirMapper.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package uk.nhs.adaptors.scr.mappings.from.hl7;
22

33
import lombok.SneakyThrows;
4+
5+
import org.hibernate.validator.internal.util.logging.LoggerFactory;
46
import org.hl7.fhir.r4.model.BaseDateTimeType;
57
import org.hl7.fhir.r4.model.DateTimeType;
68
import org.hl7.fhir.r4.model.InstantType;
@@ -13,6 +15,7 @@
1315
import java.time.LocalDate;
1416
import java.time.LocalDateTime;
1517
import java.time.format.DateTimeFormatter;
18+
import java.util.Date;
1619
import java.util.List;
1720
import java.util.TimeZone;
1821

@@ -23,6 +26,10 @@
2326
import static ca.uhn.fhir.model.api.TemporalPrecisionEnum.MILLI;
2427
import static java.lang.Integer.parseInt;
2528

29+
import java.lang.System.Logger;
30+
import java.lang.System.Logger.Level;
31+
32+
2633
public interface XmlToFhirMapper {
2734
// List of known date/time formats which may be present in HL7 based SCR.
2835
String DATE_TIME_SECONDS_PATTERN = "yyyyMMddHHmmss";
@@ -34,9 +41,16 @@ public interface XmlToFhirMapper {
3441
String SNOMED_SYSTEM = "http://snomed.info/sct";
3542
String TIMEZONE = "Europe/London";
3643

44+
// private static Logger logger() {
45+
// final class LogHolder {
46+
// private static final Logger LOGGER = LoggerFactory.getLogger(XmlToFhirMapper.class);
47+
// }
48+
// return LogHolder.LOGGER;
49+
// }
50+
3751
List<Resource> map(Node document);
3852

39-
/**
53+
/**
4054
* Takes a date, and parses it into a BaseDateTimeObject.
4155
* Dates may be received in partial format, e.g. YYYY, YYYY-mm, which are invalid, but we still need to preserve
4256
* them as-is for clinical safety. Due to limitations on the third party FHIR parser code, we instead allocate a
@@ -61,6 +75,8 @@ static <T extends BaseDateTimeType> T parseDate(String date, Class<T> clazz) {
6175
int monthPrecision = 2;
6276
int yearPrecision = 1;
6377

78+
Date defaultDate;
79+
defaultDate = new Date(0);
6480

6581
if (isValidDate(date, DATE_TIME_SECONDS_PATTERN)) {
6682
LocalDateTime parsed = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(DATE_TIME_SECONDS_PATTERN));
@@ -106,6 +122,11 @@ static <T extends BaseDateTimeType> T parseDate(String date, Class<T> clazz) {
106122
baseDateTimeType.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
107123
baseDateTimeType.setYear(parseInt(date) + 1);
108124
baseDateTimeType.setMillis(yearPrecision);
125+
} else if (date.equals("1")) {
126+
baseDateTimeType.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
127+
baseDateTimeType.setValue(defaultDate);
128+
// logger().log(Level.ERROR, "Invalid date value:1 updated");
129+
109130
} else {
110131
throw new ScrBaseException("Unsupported date format: " + date);
111132
}
@@ -141,4 +162,5 @@ static void setDatePart(BaseDateTimeType baseDateTimeType, int dayOfMonth, int m
141162
baseDateTimeType.setMonth(monthValue - 1);
142163
baseDateTimeType.setYear(year);
143164
}
165+
144166
}

docker/service/src/test/java/uk/nhs/adaptors/scr/mappings/from/hl7/XmlToFhirMapperTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public void When_ParsingDate_Expect_CorrectDateFormat() {
3737
date = parseDate("2023", InstantType.class);
3838
assertThat(date.getValueAsString()).isEqualTo("2023");
3939

40+
// FLAGSAPI-806
41+
date = parseDate("1", InstantType.class);
42+
// System.out.println(date.getValueAsString());
43+
assertThat(date.getValueAsString()).isEqualTo("1970-01-01T01:00:00.000+01:00");
44+
4045
assertThrows(ScrBaseException.class, () -> {
4146
parseDate("-2023", InstantType.class);
4247
});

0 commit comments

Comments
 (0)