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 496dc54c4c..222bc955cf 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 @@ -9,6 +9,7 @@ public class PatientSearchParameters { private Boolean filterPatientsByLocation; private String identifier; private String name; + private String gender; private String addressFieldName; private String addressFieldValue; private Integer start; @@ -43,6 +44,7 @@ public PatientSearchParameters(RequestContext context) { } else { this.setAddressFieldName("city_village"); } + this.setGender(context.getParameter("gender")); this.setAddressFieldValue(context.getParameter("addressFieldValue")); Map parameterMap = context.getRequest().getParameterMap(); this.setAddressSearchResultFields((String[]) parameterMap.get("addressSearchResultsConfig")); @@ -71,6 +73,14 @@ public void setName(String name) { this.name = name; } + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + public String getAddressFieldName() { return addressFieldName; } diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/mapper/PatientResponseMapper.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/mapper/PatientResponseMapper.java index b5a24f279c..17d9b6b768 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/mapper/PatientResponseMapper.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/contract/patient/mapper/PatientResponseMapper.java @@ -51,7 +51,9 @@ public PatientResponse map(Patient patient, String loginLocationUuid, String[] s patientResponse.setFamilyName(patient.getFamilyName()); patientResponse.setGender(patient.getGender()); PatientIdentifier primaryIdentifier = patient.getPatientIdentifier(); - patientResponse.setIdentifier(primaryIdentifier.getIdentifier()); + if(primaryIdentifier != null) { + patientResponse.setIdentifier(primaryIdentifier.getIdentifier()); + } patientResponse.setPatientProgramAttributeValue(programAttributeValue); mapExtraIdentifiers(patient, primaryIdentifier); diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniPatientService.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniPatientService.java index 5a3905ba5f..dc17ca3b6c 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniPatientService.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/BahmniPatientService.java @@ -1,9 +1,11 @@ package org.bahmni.module.bahmnicore.service; import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; +import org.bahmni.module.bahmnicore.contract.patient.mapper.PatientResponseMapper; import org.bahmni.module.bahmnicore.contract.patient.response.PatientConfigResponse; import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; import org.openmrs.Patient; +import org.openmrs.Person; import org.openmrs.RelationshipType; import java.util.List; @@ -15,6 +17,8 @@ public interface BahmniPatientService { List luceneSearch(PatientSearchParameters searchParameters); + List searchSimilarPatients(PatientSearchParameters searchParameters, PatientResponseMapper patientResponseMapper); + public List get(String partialIdentifier, boolean shouldMatchExactPatientId); public List getByAIsToB(String aIsToB); diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImpl.java index 0c50fae0a8..ad2eb52a0a 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImpl.java @@ -1,21 +1,27 @@ package org.bahmni.module.bahmnicore.service.impl; import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; +import org.bahmni.module.bahmnicore.contract.patient.mapper.PatientResponseMapper; import org.bahmni.module.bahmnicore.contract.patient.response.PatientConfigResponse; import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; import org.bahmni.module.bahmnicore.dao.PatientDao; import org.bahmni.module.bahmnicore.service.BahmniPatientService; import org.openmrs.Concept; import org.openmrs.Patient; +import org.openmrs.Person; import org.openmrs.PersonAttributeType; import org.openmrs.RelationshipType; import org.openmrs.api.ConceptService; import org.openmrs.api.PersonService; +import org.openmrs.api.context.Context; +import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; +import java.util.Set; @Service @Lazy //to get rid of cyclic dependencies @@ -83,6 +89,23 @@ public List luceneSearch(PatientSearchParameters searchParamete searchParameters.getFilterPatientsByLocation(), searchParameters.getFilterOnAllIdentifiers()); } + @Override + public List searchSimilarPatients(PatientSearchParameters searchParameters, PatientResponseMapper patientResponseMapper) { + List patients = new ArrayList(); + Set persons = personService.getSimilarPeople(searchParameters.getName(), null, searchParameters.getGender()); + for (Person person : persons) { + Patient patient = new Patient(person); + PatientResponse patientResponse = patientResponseMapper.map( + patient, + searchParameters.getLoginLocationUuid(), + searchParameters.getPatientSearchResultFields(), + searchParameters.getAddressSearchResultFields(), + patient.getPatientId()); + patients.add(patientResponse); + } + return patients; + } + @Override public List get(String partialIdentifier, boolean shouldMatchExactPatientId) { return patientDao.getPatients(partialIdentifier, shouldMatchExactPatientId); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java index 5c77844fdb..84d45a10ac 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java @@ -1,17 +1,23 @@ package org.bahmni.module.bahmnicore.service.impl; +import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; +import org.bahmni.module.bahmnicore.contract.patient.mapper.PatientResponseMapper; import org.bahmni.module.bahmnicore.contract.patient.response.PatientConfigResponse; +import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; import org.bahmni.module.bahmnicore.dao.PatientDao; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.openmrs.Concept; +import org.openmrs.Person; import org.openmrs.PersonAttributeType; import org.openmrs.api.ConceptService; import org.openmrs.api.PersonService; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static junit.framework.Assert.assertEquals; import static org.mockito.Mockito.anyInt; @@ -26,6 +32,10 @@ public class BahmniPatientServiceImplTest { private ConceptService conceptService; @Mock private PatientDao patientDao; + @Mock + private PatientSearchParameters searchParameters; + @Mock + private PatientResponseMapper patientResponseMapper; private BahmniPatientServiceImpl bahmniPatientService; @@ -67,4 +77,17 @@ public void shouldGetPatientByPartialIdentifier() throws Exception { bahmniPatientService.get("partial_identifier", shouldMatchExactPatientId); verify(patientDao).getPatients("partial_identifier", shouldMatchExactPatientId); } + + @Test + public void shouldGetSimiliarPatientResponseFromPersonSet() throws Exception { + Set persons = new HashSet(); + persons.add(new Person()); + persons.add(new Person()); + when(personService.getSimilarPeople(searchParameters.getName(), null, searchParameters.getGender())).thenReturn(persons); + + List response = bahmniPatientService.searchSimilarPatients(searchParameters, patientResponseMapper); + + assertEquals(response.size(), 2); + + } } diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java index a3d71c703a..ab443f7b25 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java @@ -1,8 +1,14 @@ package org.bahmni.module.bahmnicore.web.v1_0.controller.search; import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; +import org.bahmni.module.bahmnicore.contract.patient.mapper.PatientResponseMapper; import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; import org.bahmni.module.bahmnicore.service.BahmniPatientService; +import org.openmrs.Patient; +import org.openmrs.PatientIdentifier; +import org.openmrs.Person; +import org.openmrs.api.context.Context; +import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl; import org.openmrs.module.webservices.rest.web.RequestContext; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.RestUtil; @@ -15,6 +21,7 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; @@ -66,4 +73,20 @@ public ResponseEntity> luceneSearch(HttpServletReq return new ResponseEntity(RestUtil.wrapErrorResponse(e, e.getMessage()), HttpStatus.BAD_REQUEST); } } + + @RequestMapping(value="similar", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity> searchSimilarPerson(HttpServletRequest request, + HttpServletResponse response) throws ResponseException{ + RequestContext requestContext = RestUtil.getRequestContext(request, response); + PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); + try { + PatientResponseMapper patientResponseMapper = new PatientResponseMapper(Context.getVisitService(),new BahmniVisitLocationServiceImpl(Context.getLocationService())); + List patients = bahmniPatientService.searchSimilarPatients(searchParameters, patientResponseMapper); + AlreadyPaged alreadyPaged = new AlreadyPaged(requestContext, patients, false); + return new ResponseEntity(alreadyPaged, HttpStatus.OK); + }catch (IllegalArgumentException e){ + return new ResponseEntity(RestUtil.wrapErrorResponse(e, e.getMessage()), HttpStatus.BAD_REQUEST); + } + } }