From 170944c1ca08e1ed9385c784a2039ded88f010e7 Mon Sep 17 00:00:00 2001 From: Martina Duemcke Date: Thu, 9 Aug 2018 11:57:46 +0700 Subject: [PATCH] Martina | BAH-460 | set default time for birthdate to be parsable by sql Timestamp functionality --- .../contract/patient/PatientSearchParameters.java | 14 ++++++++++++-- .../patient/PatientSearchParametersTest.java | 12 ++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParameters.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParameters.java index eab26ef8f..1694f4f8a 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParameters.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParameters.java @@ -8,6 +8,8 @@ import java.util.Map; public class PatientSearchParameters { + private final String MIDNIGHT = "00:00:00"; + private Boolean filterPatientsByLocation; private String identifier; private String name; @@ -86,9 +88,17 @@ public void setGender(String gender) { } public void setBirthdate(String birthdate) { - if(StringUtils.isNotEmpty(birthdate) && birthdate.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) { - this.birthdate = Timestamp.valueOf(birthdate); + if(StringUtils.isEmpty(birthdate)) { + return; } + + this.birthdate = Timestamp.valueOf(addTimeIfMissing(birthdate)); + } + + private String addTimeIfMissing(String birthdate) { + if(StringUtils.isNotEmpty(birthdate) && birthdate.matches("\\d{4}-\\d{2}-\\d{2}")) { + return birthdate + " " + MIDNIGHT; + } else return birthdate; } public Date getBirthdate() { diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParametersTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParametersTest.java index 6dd1f9f80..50a599457 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParametersTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/contract/patient/PatientSearchParametersTest.java @@ -37,12 +37,20 @@ public void shouldParseTheBirtdateFromString () { } @Test - public void shouldIgnoreWronglyFormattedBirthdate () { - when(requestContext.getParameter("birthdate")).thenReturn("1983-01-30"); + public void shouldIgnoreEmptyBirthdate () { + when(requestContext.getParameter("birthdate")).thenReturn(""); PatientSearchParameters patientSearchParameters; patientSearchParameters = new PatientSearchParameters(requestContext); assertNull(patientSearchParameters.getBirthdate()); } + @Test + public void shouldSetToMidnightForBirthdateIfTimeIsMissing () { + when(requestContext.getParameter("birthdate")).thenReturn("1983-01-30"); + PatientSearchParameters patientSearchParameters; + patientSearchParameters = new PatientSearchParameters(requestContext); + + assertEquals(Timestamp.valueOf("1983-01-30 00:00:00"), patientSearchParameters.getBirthdate()); + } } \ No newline at end of file