From da52d517162e447b369b144c3263f02d40c499a8 Mon Sep 17 00:00:00 2001 From: Martina Duemcke Date: Thu, 16 Aug 2018 15:45:41 +0700 Subject: [PATCH] Martina, Roni, Eyal, Klaus | BAH-460 | add api to search patients with similar name, gender and birthdate/age --- .../patient/PatientSearchParameters.java | 16 +++++++++------- .../bahmnicore/dao/impl/PatientDaoImpl.java | 16 +++++++--------- .../patient/PatientSearchParametersTest.java | 17 +++-------------- .../dao/impl/BahmniPatientDaoImplLuceneIT.java | 16 +++++++++++++++- 4 files changed, 34 insertions(+), 31 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 1694f4f8ac..815bce20c5 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 @@ -92,13 +92,7 @@ public void setBirthdate(String 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; + this.birthdate = Timestamp.valueOf(setToMidnight(birthdate)); } public Date getBirthdate() { @@ -208,4 +202,12 @@ public void setFilterOnAllIdentifiers(Boolean filterOnAllIdentifiers) { public Boolean getFilterOnAllIdentifiers() { return filterOnAllIdentifiers; } + + private String setToMidnight(String birthdate) { + if(StringUtils.isNotEmpty(birthdate) && birthdate.matches("\\d{4}-\\d{2}-\\d{2}")) { + return birthdate + " " + MIDNIGHT; + } else { + return birthdate; + } + } } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java index 4fe8e11d56..c315780d7b 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java @@ -122,7 +122,7 @@ private PatientResponse toPatientResponse(PatientResponseMapper patientResponseM } private List getPatientsByNameGenderAndBirthdate(String name, String gender, Date birthdate, Integer length) { - if(isNullOrEmpty(name, gender)) { + if(isAllNullOrEmpty(name, gender, birthdate)) { return new ArrayList<>(); } @@ -130,20 +130,19 @@ private List getPatientsByNameGenderAndBirthdate(String name, String ge patientDAO.setSessionFactory(sessionFactory); String query = LuceneQuery.escapeQuery(name); PersonLuceneQuery personLuceneQuery = new PersonLuceneQuery(sessionFactory); - LuceneQuery nameQuery = personLuceneQuery.getPatientNameQueryWithOrParser(query, false); + LuceneQuery nameQuery = personLuceneQuery.getPatientNameQuery(query, false); List patients = nameQuery.list().stream() - .filter(personName -> - personName.getPreferred() && checkGender(personName.getPerson(), gender) - && checkBirthdate(personName.getPerson(), birthdate) + .filter(patient -> patient.getPreferred() && checkGender(patient.getPerson(), gender) + && checkBirthdate(patient.getPerson(), birthdate) ) .limit(length) - .map(personName -> new Patient(personName.getPerson())) + .map(patient -> new Patient(patient.getPerson())) .collect(toList()); return patients; } - private Boolean isNullOrEmpty(String name, String gender) { - return (name == null || name.trim().isEmpty()) && (gender == null || gender.isEmpty()); + private Boolean isAllNullOrEmpty(String name, String gender, Date birthdate) { + return (name == null || name.trim().isEmpty()) && (gender == null || gender.isEmpty()) && birthdate == null; } @@ -157,7 +156,6 @@ private Boolean checkGender(Person person, String gender) { private Boolean checkBirthdate(Person person, Date birthdate) { if(birthdate != null) { - Date extractedBirthdate = person.getBirthdate(); return birthdate.equals(person.getBirthdate()); } else { return true; 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 c686c10c30..e51848f193 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 @@ -9,7 +9,6 @@ import java.sql.Timestamp; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -29,28 +28,18 @@ public void setup() { when(requestContext.getRequest()).thenReturn(request); } - @Test - public void shouldParseTheBirtdateFromString () { - when(requestContext.getParameter("birthdate")).thenReturn("1983-01-30 00:00:00"); - PatientSearchParameters patientSearchParameters = new PatientSearchParameters(requestContext); - - assertEquals(Timestamp.valueOf("1983-01-30 00:00:00"), patientSearchParameters.getBirthdate()); - } - @Test public void shouldIgnoreEmptyBirthdate () { when(requestContext.getParameter("birthdate")).thenReturn(""); - PatientSearchParameters patientSearchParameters; - patientSearchParameters = new PatientSearchParameters(requestContext); + PatientSearchParameters patientSearchParameters = new PatientSearchParameters(requestContext); assertNull(patientSearchParameters.getBirthdate()); } @Test - public void shouldSetToMidnightForBirthdateIfTimeIsMissing () { + public void shouldParseBirthdateFromStringAndSetToMidnight () { when(requestContext.getParameter("birthdate")).thenReturn("1983-01-30"); - PatientSearchParameters patientSearchParameters; - patientSearchParameters = new PatientSearchParameters(requestContext); + PatientSearchParameters patientSearchParameters = new PatientSearchParameters(requestContext); assertEquals(Timestamp.valueOf("1983-01-30 00:00:00"), patientSearchParameters.getBirthdate()); } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java index 067af7312c..bc949f5e1e 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java @@ -263,6 +263,13 @@ public void shouldSearchSimilarPatientByNameAndBirthdate() { assertEquals(patient.getBirthDate(), birthdate); } + @Test + public void shouldMatchAllNamesInSearchSimilarPatient() { + List patients = patientDao.getSimilarPatientsUsingLuceneSearch("Horatio Peet", "", null, "c36006e5-9fbb-4f20-866b-0ece245615a1", 5); + + assertEquals("Should use an AND query on names",1, patients.size()); + } + @Test public void shouldReturnEmptyListIfAllSearchTermsAreEmpty() { List patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "", null, "c36006e5-9fbb-4f20-866b-0ece245615a1", 5); @@ -271,9 +278,16 @@ public void shouldReturnEmptyListIfAllSearchTermsAreEmpty() { } @Test - public void shouldReturnResultsIfOneFieldIsSet() { + public void shouldReturnResultsIfOnlyGenderIsSet() { List patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "F", null, "c36006e5-9fbb-4f20-866b-0ece245615a1", 5); assertEquals(5, patients.size()); } + + @Test + public void shouldReturnResultsIfOnlyBirthdateIsSet() { + List patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "", Timestamp.valueOf("1983-01-30 00:00:00"), "c36006e5-9fbb-4f20-866b-0ece245615a1", 5); + + assertEquals(1, patients.size()); + } }