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 f147768b4c..33aa32d5f4 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 @@ -23,6 +23,7 @@ import org.openmrs.PatientIdentifier; import org.openmrs.PatientIdentifierType; import org.openmrs.RelationshipType; +import org.openmrs.api.PatientService; import org.openmrs.api.context.Context; import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl; import org.springframework.beans.factory.annotation.Autowired; @@ -35,17 +36,20 @@ import java.util.Objects; import java.util.Set; -import static java.util.stream.Collectors.reducing; import static java.util.stream.Collectors.toList; @Repository public class PatientDaoImpl implements PatientDao { private SessionFactory sessionFactory; + private PatientService patientService; + private BahmniProgramWorkflowService bahmniProgramWorkflowService; @Autowired - public PatientDaoImpl(SessionFactory sessionFactory) { + public PatientDaoImpl(SessionFactory sessionFactory, PatientService patientService, BahmniProgramWorkflowService bahmniProgramWorkflowService) { this.sessionFactory = sessionFactory; + this.patientService = patientService; + this.bahmniProgramWorkflowService = bahmniProgramWorkflowService; } @Override @@ -77,24 +81,35 @@ public List getPatientsUsingLuceneSearch(String identifier, Str String programAttributeFieldName, String[] addressSearchResultFields, String[] patientSearchResultFields, String loginLocationUuid, Boolean filterPatientsByLocation, Boolean filterOnAllIdentifiers) { - validateSearchParams(customAttributeFields, programAttributeFieldName, addressFieldName); - List patientIdentifiers = getPatientIdentifiers(identifier, filterOnAllIdentifiers, offset, length); - List patientIds = patientIdentifiers.stream().map(patientIdentifier -> patientIdentifier.getPatient().getPatientId()).collect(toList()); - Map programAttributes = Context.getService(BahmniProgramWorkflowService.class).getPatientProgramAttributeByAttributeName(patientIds, programAttributeFieldName); + Set patients = new HashSet<>(); + + if(identifier != null && !identifier.isEmpty()){ + patients.addAll(patientService.getPatients(identifier, false, offset, length)); + if(name != null && !name.isEmpty()){ + patients.retainAll( patientService.getPatients(name, false, offset, length)); + } + if(customAttribute != null && !customAttribute.isEmpty()){ + patients.retainAll( patientService.getPatients(customAttribute, false, offset, length)); + } + }else if(name != null && !name.isEmpty()){ + patients.addAll(patientService.getPatients(name, false, offset, length)); + if(customAttribute!=null && !customAttribute.isEmpty()){ + patients.retainAll(patientService.getPatients(customAttribute, false, offset, length)); + } + }else if((customAttribute != null && !customAttribute.isEmpty())){ + patients.addAll(patientService.getPatients(customAttribute, false, offset, length)); + } + + List patientIds = patients.stream().map(Patient::getPatientId).collect(toList()); PatientResponseMapper patientResponseMapper = new PatientResponseMapper(Context.getVisitService(),new BahmniVisitLocationServiceImpl(Context.getLocationService())); - Set uniquePatientIds = new HashSet<>(); - List patientResponses = patientIdentifiers.stream() - .map(patientIdentifier -> { - Patient patient = patientIdentifier.getPatient(); - if(!uniquePatientIds.contains(patient.getPatientId())) { - PatientResponse patientResponse = patientResponseMapper.map(patient, loginLocationUuid, patientSearchResultFields, addressSearchResultFields, - programAttributes.get(patient.getPatientId())); - uniquePatientIds.add(patient.getPatientId()); - return patientResponse; - }else - return null; + Map programAttributes = bahmniProgramWorkflowService.getPatientProgramAttributeByAttributeName(patientIds, programAttributeFieldName); + + List patientResponses = patients.stream() + .map(patient -> { + PatientResponse patientResponse = patientResponseMapper.map(patient,loginLocationUuid,patientSearchResultFields,addressSearchResultFields,programAttributes.get(patient.getPatientId())); + return patientResponse; }).filter(Objects::nonNull) .collect(toList()); return patientResponses; diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/model/bahmniPatientProgram/PatientProgramAttribute.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/model/bahmniPatientProgram/PatientProgramAttribute.java index ff418975f6..360cede090 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/model/bahmniPatientProgram/PatientProgramAttribute.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/model/bahmniPatientProgram/PatientProgramAttribute.java @@ -1,9 +1,14 @@ package org.bahmni.module.bahmnicore.model.bahmniPatientProgram; +import org.hibernate.search.annotations.DocumentId; +import org.hibernate.search.annotations.Indexed; import org.openmrs.attribute.Attribute; import org.openmrs.attribute.BaseAttribute; +@Indexed public class PatientProgramAttribute extends BaseAttribute implements Attribute { + + @DocumentId private Integer patientProgramAttributeId; @Override 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..11315304cf 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 @@ -18,4 +18,13 @@ public interface BahmniPatientService { public List get(String partialIdentifier, boolean shouldMatchExactPatientId); public List getByAIsToB(String aIsToB); + + /** + * Intersection of lucene and hibernate search in one query + * First take advantage of lucene and fall back to hibernate + * @param searchParameters + * @return List of PatientResponse + */ + List luceneHibernateSearch(PatientSearchParameters searchParameters); + } 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..617824b497 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,5 +1,6 @@ package org.bahmni.module.bahmnicore.service.impl; +import org.apache.commons.lang3.StringUtils; import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; import org.bahmni.module.bahmnicore.contract.patient.response.PatientConfigResponse; import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; @@ -10,11 +11,14 @@ import org.openmrs.PersonAttributeType; import org.openmrs.RelationshipType; import org.openmrs.api.ConceptService; +import org.openmrs.api.PatientService; import org.openmrs.api.PersonService; +import org.openmrs.api.context.Context; 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; @Service @@ -23,13 +27,15 @@ public class BahmniPatientServiceImpl implements BahmniPatientService { private PersonService personService; private ConceptService conceptService; private PatientDao patientDao; + private PatientService patientService; @Autowired public BahmniPatientServiceImpl(PersonService personService, ConceptService conceptService, - PatientDao patientDao) { + PatientDao patientDao, PatientService patientService) { this.personService = personService; this.conceptService = conceptService; this.patientDao = patientDao; + this.patientService = patientService; } @Override @@ -93,4 +99,30 @@ public List getByAIsToB(String aIsToB) { return patientDao.getByAIsToB(aIsToB); } + @Override + public List luceneHibernateSearch(PatientSearchParameters searchParameters) { + List luceneHibernateResponse = new ArrayList<>(); + if(StringUtils.isNotEmpty(searchParameters.getIdentifier()) || StringUtils.isNotEmpty(searchParameters.getName()) || StringUtils.isNotEmpty(searchParameters.getCustomAttribute())){ + List luceneResponse = luceneSearch(searchParameters); + + for(PatientResponse patientResponse: luceneResponse){ + Patient patient = patientService.getPatient(patientResponse.getPersonId()); + searchParameters.setIdentifier(patient.getPatientIdentifier().getIdentifier()); + if(StringUtils.isNotEmpty(searchParameters.getName())){ + searchParameters.setName(""); + } + if( StringUtils.isNotEmpty(searchParameters.getCustomAttribute())){ + searchParameters.setCustomAttribute(""); + } + luceneHibernateResponse.addAll(search(searchParameters)); + } + }else{ + luceneHibernateResponse = search(searchParameters); + } + + + + return luceneHibernateResponse; + } + } 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 c02cfc7bc0..c88652facc 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 @@ -11,6 +11,8 @@ import org.springframework.beans.factory.annotation.Autowired; import java.util.List; + +import static java.util.Arrays.asList; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNull; @@ -21,13 +23,13 @@ public class BahmniPatientDaoImplLuceneIT extends BaseIntegrationTest { private PatientDao patientDao; @Rule public ExpectedException expectedEx = ExpectedException.none(); - + @Before public void setUp() throws Exception { executeDataSet("apiTestData.xml"); updateSearchIndex(); } - + @Test public void shouldSearchByPatientPrimaryIdentifier() { String[] addressResultFields = {"city_village"}; @@ -45,7 +47,7 @@ public void shouldSearchByPatientPrimaryIdentifier() { assertEquals(null, patient.getDeathDate()); assertEquals("{\"National ID\" : \"NAT100010\"}", patient.getExtraIdentifiers()); } - + @Test public void shouldSearchByPatientExtraIdentifier() { String[] addressResultFields = {"city_village"}; @@ -63,35 +65,35 @@ public void shouldSearchByPatientExtraIdentifier() { assertEquals(null, patient.getDeathDate()); assertEquals("{\"National ID\" : \"NAT100010\"}", patient.getExtraIdentifiers()); } - + @Test public void shouldSearchByPartialPatientIdentifier() { List patients = patientDao.getPatientsUsingLuceneSearch("02", "", null, "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); assertEquals(1, patients.size()); PatientResponse patient = patients.get(0); - + assertEquals("GAN200002", patient.getIdentifier()); assertNull(patient.getExtraIdentifiers()); } - + @Test public void shouldReturnResultAfterGivenOffset() throws Exception { List patients = patientDao.getPatientsUsingLuceneSearch("300001", "", null, "city_village", "", 100, 1, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); assertEquals(1, patients.size()); - + patients = patientDao.getPatientsUsingLuceneSearch("300001", "", null, "city_village", "", 100, 2, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); assertEquals(0, patients.size()); } - + @Test public void shouldThrowErrorWhenPatientAttributesIsNotPresent() throws Exception { String[] patientAttributes = {"caste","nonExistingAttribute"}; expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("Invalid Attribute In Patient Attributes [caste, nonExistingAttribute]"); List patients = patientDao.getPatientsUsingLuceneSearch("", "", "testCaste1", "city_village", null, 100, 0, patientAttributes, "", null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + } - + @Test public void shouldThrowErrorWhenPatientAddressIsNotPresent() throws Exception { String[] patientAttributes = {"caste"}; @@ -99,31 +101,31 @@ public void shouldThrowErrorWhenPatientAddressIsNotPresent() throws Exception { expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("Invalid Address Filed nonExistingAddressFiled"); List patients = patientDao.getPatientsUsingLuceneSearch("", "", "testCaste1", addressField, null, 100, 0, patientAttributes, "", null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + } - + @Test public void shouldThrowErrorWhenProgramAttributesIsNotPresent() { String nonExistingAttribute = "nonExistingAttribute"; expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("Invalid Program Attribute nonExistingAttribute"); patientDao.getPatientsUsingLuceneSearch("", "", "", "city_village", null, 100, 0, null, "Stage1",nonExistingAttribute, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + } - + @Test public void shouldReturnAdmissionStatus() throws Exception{ List patients = patientDao.getPatientsUsingLuceneSearch("200000", null, null, "city_village", null, 10, 0, null, null, null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); assertEquals(1, patients.size()); PatientResponse patient200000 = patients.get(0); assertFalse(patient200000.getHasBeenAdmitted()); - + patients = patientDao.getPatientsUsingLuceneSearch("200002", null, null, "city_village", null, 10, 0, null, null, null,null,null, "8d6c993e-c2cc-11de-8d13-0040c6dffd0f", false, false); assertEquals(1, patients.size()); PatientResponse patient200003 = patients.get(0); assertTrue(patient200003.getHasBeenAdmitted()); } - + @Test public void shouldReturnAddressAndPatientAttributes() throws Exception{ String[] addressResultFields = {"address3"}; @@ -134,83 +136,180 @@ public void shouldReturnAddressAndPatientAttributes() throws Exception{ assertTrue("{\"middleNameLocal\" : \"singh\",\"familyNameLocal\" : \"gond\",\"givenNameLocal\" : \"ram\"}".equals(patient200002.getCustomAttribute())); assertTrue("{\"address3\" : \"Dindori\"}".equals(patient200002.getAddressFieldValue())); } - + @Test public void shouldGiveAllThePatientsIfWeSearchWithPercentileAsIdentifier() throws Exception { List patients = patientDao.getPatientsUsingLuceneSearch("%", null, null, null, null, 10, 0, null, null, null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + assertEquals(10, patients.size()); } - + @Test public void shouldFetchPatientsByPatientIdentifierWhenThereIsSingleQuoteInPatientIdentifier(){ List patients = patientDao.getPatientsUsingLuceneSearch("51'0003", "", "", null, null, 100, 0, null,null, null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + PatientResponse response = patients.get(0); - + assertEquals(1, patients.size()); assertEquals("SEV51'0003", response.getIdentifier()); } - + @Test public void shouldFetchPatientsByPatientIdentifierWhenThereIsJustOneSingleQuoteInPatientIdentifier() throws Exception { List patients = patientDao.getPatientsUsingLuceneSearch("'", "", "", null, null, 100, 0, null,null, null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + PatientResponse response = patients.get(0); - + assertEquals(1, patients.size()); assertEquals("SEV51'0003", response.getIdentifier()); } - + @Test public void shouldSearchPatientsByPatientIdentifierWhenThereAreMultipleSinglesInSearchString() throws Exception { - + List patients = patientDao.getPatientsUsingLuceneSearch("'''", "", "", null, null, 100, 0, null,null, null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + assertEquals(0, patients.size()); } - + @Test public void shouldNotReturnDuplicatePatientsEvenIfThereAreMultipleVisitsForThePatients() { List patients = patientDao.getPatientsUsingLuceneSearch("HOS1225", "", null, "city_village", "", 100, 0, null,"",null,null,null, "8d6c993e-c2cc-11de-8d34-0010c6affd0f", false, false); - + assertEquals(1, patients.size()); PatientResponse patient1 = patients.get(0); - + assertEquals("1058GivenName", patient1.getGivenName()); } - + @Test public void shouldNotSearchExtraIdentifiersIfFilterOnAllIdenfiersIsFalse() { List patients = patientDao.getPatientsUsingLuceneSearch("100010", "", null, "city_village", "", 100, 0, null,"", null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); - + assertEquals(0, patients.size()); } - + @Test public void shouldSearchAllIdentifiersIfFilterOnAllIdentifiersIsTrue() { List patients = patientDao.getPatientsUsingLuceneSearch("0001", "", null, "city_village", "", 100, 0, null,"", null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, true); - + assertEquals(3, patients.size()); assertEquals("{\"National ID\" : \"NAT100010\"}", patients.get(0).getExtraIdentifiers()); assertEquals("GAN300001",patients.get(1).getIdentifier()); } - + @Test public void shouldNotReturnPatientsIfFilterOnAllIdenfiersIsTrueButNotAnExtraIdentifier() { List patients = patientDao.getPatientsUsingLuceneSearch("DLF200001", "", null, "city_village", "", 100, 0, null,"", null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, true); - + assertEquals(0, patients.size()); } - + @Test public void shouldNotReturnDuplicatePatientsEvenIfTwoIdentifiersMatches() { List patients = patientDao.getPatientsUsingLuceneSearch("200006", "", null, "city_village", "", 100, 0, null,"", null, null, null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, true); - + assertEquals(1, patients.size()); PatientResponse patient = patients.get(0); assertTrue(patient.getIdentifier().contains("200006")); assertTrue(patient.getExtraIdentifiers().contains("200006")); } -} + @Test + public void shouldSearchByName() { + + List patients = patientDao.getPatientsUsingLuceneSearch("", "Horatio", null, "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + + assertEquals(3, patients.size()); + PatientResponse patient1 = patients.get(0); + PatientResponse patient2 = patients.get(1); + List uuids = asList("341b4e41-790c-484f-b6ed-71dc8da222db", "86526ed5-3c11-11de-a0ba-001e378eb67a"); + + assertTrue(uuids.contains(patient1.getUuid())); + assertTrue(uuids.contains(patient2.getUuid())); + + assertEquals("Horatio", patient1.getGivenName()); + assertEquals("Horatio", patient2.getGivenName()); + } + + @Test + public void shouldSearchAcrossFirstNameAndLastName() { + List patients = patientDao.getPatientsUsingLuceneSearch("", "Horati Sinha", null, "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + + assertEquals(1, patients.size()); + PatientResponse patient1 = patients.get(0); + assertEquals("341b4e41-790c-484f-b6ed-71dc8da222db", patient1.getUuid()); + assertEquals("Horatio", patient1.getGivenName()); + assertEquals("Sinha", patient1.getFamilyName()); + } + + @Test + public void shouldFetchBasedOnPatientAttributeTypes() throws Exception { + String[] patientAttributes = { "caste"}; + String[] patientResultFields = {"caste"}; + List patients = patientDao.getPatients("", "", "testCaste1", "city_village", null, 100, 0, patientAttributes,"",null,null,patientResultFields, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + + assertEquals(1, patients.size()); + } + + @Test + public void shouldReturnPatientsWhenMatchingIdentifierAndName(){ + List patients = patientDao.getPatientsUsingLuceneSearch("GAN200001", "Horatio Peeter Sinha", "", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + assertEquals(1, patients.size()); + PatientResponse patient = patients.get(0); + assertEquals("GAN200001", patient.getIdentifier()); + assertEquals("Horatio", patient.getGivenName()); + assertEquals("Sinha", patient.getFamilyName()); + } + + @Test + public void shouldNotReturnPatientsWhenNotMatchingNameAndIdentifier(){ + List patients = patientDao.getPatientsUsingLuceneSearch("GAN200001", "Not Name", "city_village", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + assertEquals(0, patients.size()); + } + + @Test + public void shouldSearchWhenMatchingIdentifierAndPersonAttribute(){ + List patients = patientDao.getPatientsUsingLuceneSearch("GAN200002", "Horati Sinha", "testCaste1", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + assertEquals(1, patients.size()); + PatientResponse patient = patients.get(0); + assertEquals("GAN200002", patient.getIdentifier()); + //assert for patient attribute + assertEquals("Horatio", patient.getGivenName()); + } + + @Test + public void shouldSearchWhenMatchingNameAndPersonAttribute(){ + List patients = patientDao.getPatientsUsingLuceneSearch("", "John Peeter Sinha", "testCaste1testCaste1", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + + assertEquals(1, patients.size()); + PatientResponse patient = patients.get(0); + assertEquals("df8ae447-6745-45be-b859-403241d9913d", patient.getUuid()); + assertEquals("GAN200002", patient.getIdentifier()); + assertEquals("John", patient.getGivenName()); + assertEquals("Sinha", patient.getFamilyName()); + assertEquals("F", patient.getGender()); + } + + @Test + public void shouldNotSearchWhenNotMatchingNameAndPersonAttribute(){ + List patients = patientDao.getPatientsUsingLuceneSearch("", "John Peeter Sinha", "testCaste1", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + assertEquals(0, patients.size()); + + } + + @Test + public void shouldReturnPatientsWhenMatchingIdentifierNameAndPersonAttribute(){ + List patients = patientDao.getPatientsUsingLuceneSearch("GAN200001", "Horatio Peeter Sinha", "caste1", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + assertEquals(1, patients.size()); + PatientResponse patient = patients.get(0); + assertEquals("GAN200001", patient.getIdentifier()); + } + + @Test + public void shouldNotReturnPatientsWhenNotMatchingIdentifierNameAndPersonAttribute(){ + List patients = patientDao.getPatientsUsingLuceneSearch("GAN200002", "Horatio Peeter Sinha", "caste1", "city_village", "", 100, 0, null,"",null,null,null, "c36006e5-9fbb-4f20-866b-0ece245615a1", false, false); + assertEquals(0, patients.size()); + } + +} \ No newline at end of file diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplIT.java new file mode 100644 index 0000000000..8d74a0c461 --- /dev/null +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplIT.java @@ -0,0 +1,130 @@ +package org.bahmni.module.bahmnicore.service.impl; + +import org.bahmni.module.bahmnicore.BaseIntegrationTest; +import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; +import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; +import org.bahmni.module.bahmnicore.service.BahmniPatientService; +import org.hibernate.SessionFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.LocationService; +import org.openmrs.api.db.hibernate.PersonAttributeHelper; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestUtil; +import org.openmrs.util.GlobalPropertiesTestHelper; +import org.openmrs.util.OpenmrsConstants; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +public class BahmniPatientServiceImplIT extends BaseIntegrationTest { + + @Autowired + private BahmniPatientService bahmniPatientService; + + @Autowired + private LocationService locationService; + + @Autowired + private AdministrationService adminService; + + private GlobalPropertiesTestHelper globalPropertiesTestHelper; + + private PersonAttributeHelper personAttributeHelper; + + private RequestContext requestContext; + + @Autowired + private SessionFactory sessionFactory; + + @Before + public void setUp() throws Exception { + HttpServletRequest mockedRequest = Mockito.mock(HttpServletRequest.class); + HttpServletResponse mockedRsponse = Mockito.mock(HttpServletResponse.class); + executeDataSet("apiTestData.xml"); + updateSearchIndex(); + + requestContext = RestUtil.getRequestContext(mockedRequest, mockedRsponse); + personAttributeHelper = new PersonAttributeHelper(sessionFactory); + globalPropertiesTestHelper = new GlobalPropertiesTestHelper(adminService); + + globalPropertiesTestHelper.setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_MODE, + OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_ANYWHERE); + globalPropertiesTestHelper.setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PERSON_ATTRIBUTE_SEARCH_MATCH_MODE, + OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_ANYWHERE); + globalPropertiesTestHelper.setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SEARCH_MATCH_MODE, + OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_SEARCH_MATCH_ANYWHERE); + + } + + @Test + public void shouldIntersectBetweenLuceneAndHibernateWithIdentifier(){ + PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); + searchParameters.setIdentifier("GAN"); + searchParameters.setLoginLocationUuid("c36006e5-9fbb-4f20-866b-0ece245615a1"); + searchParameters.setLength(100); + searchParameters.setStart(0); + List patientResponses = bahmniPatientService.luceneHibernateSearch(searchParameters); + Assert.assertEquals(5, patientResponses.size()); + } + + @Test + public void shouldIntersectBetweenLuceneAndHibernateWithName(){ + PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); + searchParameters.setName("Sin"); + searchParameters.setLoginLocationUuid("c36006e5-9fbb-4f20-866b-0ece245615a1"); + searchParameters.setLength(100); + searchParameters.setStart(0); + List patientResponses = bahmniPatientService.luceneHibernateSearch(searchParameters); + Assert.assertEquals(2, patientResponses.size()); + } + + @Test + public void shouldIntersectBetweenLuceneAndHibernateWithAttribute(){ + PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); + searchParameters.setLoginLocationUuid("c36006e5-9fbb-4f20-866b-0ece245615a1"); + searchParameters.setCustomAttribute("testCaste1"); + searchParameters.setLength(100); + searchParameters.setStart(0); + List patientResponses = bahmniPatientService.luceneHibernateSearch(searchParameters); + Assert.assertEquals(1, patientResponses.size()); + } + + + @Test + public void shouldFetchPatientsByPatientAddress(){ + PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); + searchParameters.setLoginLocationUuid("c36006e5-9fbb-4f20-866b-0ece245615a1"); + String[] addressResultFields = {"city_village"}; + String[] patientResultFields = {"caste"}; + searchParameters.setAddressSearchResultFields(addressResultFields); + searchParameters.setAddressFieldValue("Bilaspur"); + List patientResponses = bahmniPatientService.luceneHibernateSearch(searchParameters); + Assert.assertEquals(2, patientResponses.size()); + + } + + @Test + public void shouldIntersectBetweenLuceneAndHibernateWithIdentifierAndPatientAddress(){ + PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); + searchParameters.setLoginLocationUuid("c36006e5-9fbb-4f20-866b-0ece245615a1"); + String[] addressResultFields = {"city_village"}; + searchParameters.setAddressSearchResultFields(addressResultFields); + searchParameters.setIdentifier("GAN200002"); + searchParameters.setAddressFieldValue("Bilaspur"); + + List patientResponses = bahmniPatientService.luceneHibernateSearch(searchParameters); + PatientResponse response = patientResponses.get(0); + Assert.assertEquals(1, patientResponses.size()); + Assert.assertEquals("GAN200002",response.getIdentifier()); + Assert.assertEquals(1026,response.getPersonId()); + Assert.assertEquals("John",response.getGivenName()); + Assert.assertEquals("Peeter",response.getMiddleName()); + Assert.assertEquals("Sinha",response.getFamilyName()); + } +} 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..68e7c6e179 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 @@ -8,6 +8,7 @@ import org.openmrs.Concept; import org.openmrs.PersonAttributeType; import org.openmrs.api.ConceptService; +import org.openmrs.api.PatientService; import org.openmrs.api.PersonService; import java.util.ArrayList; @@ -26,13 +27,15 @@ public class BahmniPatientServiceImplTest { private ConceptService conceptService; @Mock private PatientDao patientDao; + @Mock + private PatientService patientService; private BahmniPatientServiceImpl bahmniPatientService; @Before public void setup() { initMocks(this); - bahmniPatientService = new BahmniPatientServiceImpl(personService, conceptService, patientDao); + bahmniPatientService = new BahmniPatientServiceImpl(personService, conceptService, patientDao, patientService); } @Test @@ -67,4 +70,5 @@ public void shouldGetPatientByPartialIdentifier() throws Exception { bahmniPatientService.get("partial_identifier", shouldMatchExactPatientId); verify(patientDao).getPatients("partial_identifier", shouldMatchExactPatientId); } + } 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..a17707ca94 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 @@ -44,7 +44,7 @@ public ResponseEntity> search(HttpServletRequest r RequestContext requestContext = RestUtil.getRequestContext(request, response); PatientSearchParameters searchParameters = new PatientSearchParameters(requestContext); try { - List patients = bahmniPatientService.search(searchParameters); + List patients = bahmniPatientService.luceneHibernateSearch(searchParameters); AlreadyPaged alreadyPaged = new AlreadyPaged(requestContext, patients, false); return new ResponseEntity(alreadyPaged,HttpStatus.OK); }catch (IllegalArgumentException e){ diff --git a/bahmnicore-omod/src/main/resources/liquibase.xml b/bahmnicore-omod/src/main/resources/liquibase.xml index be46369d52..a49fa9a474 100644 --- a/bahmnicore-omod/src/main/resources/liquibase.xml +++ b/bahmnicore-omod/src/main/resources/liquibase.xml @@ -4254,6 +4254,40 @@ INSERT INTO role_role(child_role, parent_role) VALUES ('Appointments:FullAccess', 'Appointments:ManageAppointments'); + + + + SELECT COUNT(*) FROM global_property where property = 'patientIdentifierSearch.matchMode' AND property_value != 'ANYWHERE'; + + + Specify how patient identifiers are matched while searching for a patient + + UPDATE global_property SET property_value = 'ANYWHERE' WHERE property = 'patientIdentifierSearch.matchMode'; + + + + + + + SELECT COUNT(*) FROM global_property where property = 'patientSearch.matchMode' AND property_value != 'ANYWHERE'; + + + Specify how patient names are matched while searching for a patient + + UPDATE global_property SET property_value = 'ANYWHERE' WHERE property = 'patientSearch.matchMode'; + + + + + + SELECT COUNT(*) FROM global_property where property = 'person.attributeSearchMatchMode' AND property_value != 'ANYWHERE'; + + + Specify how person attributes are matched while searching for a patients + + UPDATE global_property SET property_value = 'ANYWHERE' WHERE property = 'person.attributeSearchMatchMode'; + + diff --git a/pom.xml b/pom.xml index 8da708a2a0..798a6a501c 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ UTF-8 - 2.1.1 + 2.1.3 2.17 3.2.7.RELEASE 1.9.4