Skip to content

Commit

Permalink
Martina, Roni, Eyal, Klaus | BAH-460 | add api to search patients wit…
Browse files Browse the repository at this point in the history
…h similar name, gender and birthdate/age
  • Loading branch information
mduemcke committed Aug 20, 2018
1 parent 46c1e58 commit da52d51
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,27 @@ private PatientResponse toPatientResponse(PatientResponseMapper patientResponseM
}

private List<Patient> getPatientsByNameGenderAndBirthdate(String name, String gender, Date birthdate, Integer length) {
if(isNullOrEmpty(name, gender)) {
if(isAllNullOrEmpty(name, gender, birthdate)) {
return new ArrayList<>();
}

HibernatePatientDAO patientDAO = new HibernatePatientDAO();
patientDAO.setSessionFactory(sessionFactory);
String query = LuceneQuery.escapeQuery(name);
PersonLuceneQuery personLuceneQuery = new PersonLuceneQuery(sessionFactory);
LuceneQuery<PersonName> nameQuery = personLuceneQuery.getPatientNameQueryWithOrParser(query, false);
LuceneQuery<PersonName> nameQuery = personLuceneQuery.getPatientNameQuery(query, false);
List<Patient> 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;
}


Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ public void shouldSearchSimilarPatientByNameAndBirthdate() {
assertEquals(patient.getBirthDate(), birthdate);
}

@Test
public void shouldMatchAllNamesInSearchSimilarPatient() {
List<PatientResponse> 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<PatientResponse> patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "", null, "c36006e5-9fbb-4f20-866b-0ece245615a1", 5);
Expand All @@ -271,9 +278,16 @@ public void shouldReturnEmptyListIfAllSearchTermsAreEmpty() {
}

@Test
public void shouldReturnResultsIfOneFieldIsSet() {
public void shouldReturnResultsIfOnlyGenderIsSet() {
List<PatientResponse> patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "F", null, "c36006e5-9fbb-4f20-866b-0ece245615a1", 5);

assertEquals(5, patients.size());
}

@Test
public void shouldReturnResultsIfOnlyBirthdateIsSet() {
List<PatientResponse> patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "", Timestamp.valueOf("1983-01-30 00:00:00"), "c36006e5-9fbb-4f20-866b-0ece245615a1", 5);

assertEquals(1, patients.size());
}
}

0 comments on commit da52d51

Please sign in to comment.