From 7f8fe94a37156059bad1f8e5a63147c674da48de Mon Sep 17 00:00:00 2001 From: jayasanka-sack <33048395+jayasanka-sack@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:39:24 +0530 Subject: [PATCH] Merge DAOs --- .../openmrs/module/emrapi/db/EmrApiDAO.java | 14 ++++ .../module/emrapi/db/EmrApiDAOImpl.java | 59 +++++++++++++++ .../module/emrapi/db/HibernateVisitDAO.java | 74 ------------------- .../openmrs/module/emrapi/db/VisitDAO.java | 11 --- .../emrapi/visit/VisitWithDiagnoses.java | 25 ++----- .../visit/VisitWithDiagnosesServiceImpl.java | 6 +- .../{VisitDAOTest.java => EmrApiDaoTest.java} | 32 ++++---- .../web/controller/VisitController.java | 16 ++-- .../web/controller/VisitControllerTest.java | 36 +++++---- 9 files changed, 128 insertions(+), 145 deletions(-) delete mode 100644 api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java delete mode 100644 api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java rename api/src/test/java/org/openmrs/module/emrapi/db/{VisitDAOTest.java => EmrApiDaoTest.java} (65%) diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAO.java b/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAO.java index 7ab3fbd8..8f8907b9 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAO.java +++ b/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAO.java @@ -1,5 +1,8 @@ package org.openmrs.module.emrapi.db; +import org.openmrs.Patient; +import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; + import java.util.List; import java.util.Map; @@ -8,4 +11,15 @@ public interface EmrApiDAO { List executeHql(String queryString, Map parameters, Class clazz); List executeHqlFromResource(String resource, Map parameters, Class clazz); + + /** + * Fetches visits with note encounters and diagnoses of a patient. + * + * @param patient the patient + * @param startIndex the start index of the number of visits to fetch + * @param limit the limit of the number of visits to fetch + * + * @return a list of visits that has note encounters and diagnoses + */ + List getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit); } diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAOImpl.java b/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAOImpl.java index 908bbb8f..ea5baec4 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAOImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAOImpl.java @@ -4,14 +4,23 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Query; +import org.openmrs.Diagnosis; +import org.openmrs.Patient; +import org.openmrs.Visit; import org.openmrs.api.db.hibernate.DbSessionFactory; +import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; public class EmrApiDAOImpl implements EmrApiDAO { @@ -55,4 +64,54 @@ public List executeHqlFromResource(String resource, Map p } return executeHql(hql, parameters, clazz); } + + public List getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit) { + + String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; + + String hqlVisit="SELECT DISTINCT v FROM Visit v " + + "LEFT JOIN FETCH v.encounters enc " + + "LEFT JOIN enc.encounterType et " + + "WHERE v.patient.id = :patientId " + + "AND (et.uuid = :encounterTypeUuid OR enc IS NULL) " + + "ORDER BY v.startDatetime DESC"; + + Query visitQuery = sessionFactory.getCurrentSession().createQuery(hqlVisit); + + visitQuery.setParameter("patientId", patient.getId()); + visitQuery.setParameter("encounterTypeUuid", visitNoteEncounterTypeUuid); + visitQuery.setFirstResult(startIndex); + visitQuery.setMaxResults(limit); + + List visits = visitQuery.list(); + + String hqlDiagnosis = "SELECT DISTINCT diag FROM Diagnosis diag " + + "JOIN diag.encounter e " + + "WHERE e.visit.id IN :visitIds"; + + List visitIds = visits.stream() + .map(Visit::getId) + .collect(Collectors.toList()); + + List diagnoses = sessionFactory.getCurrentSession() + .createQuery(hqlDiagnosis) + .setParameterList("visitIds", visitIds) + .list(); + + Map> visitToDiagnosesMap = new HashMap<>(); + for (Diagnosis diagnosis : diagnoses) { + Visit visit = diagnosis.getEncounter().getVisit(); + visitToDiagnosesMap + .computeIfAbsent(visit, v -> new HashSet<>()) + .add(diagnosis); + } + + List visitWithDiagnoses = visits.stream() + .sorted(Comparator.comparing(Visit::getStartDatetime).reversed()) + .map(visit -> new VisitWithDiagnoses(visit, visitToDiagnosesMap.getOrDefault(visit, new HashSet<>()))) + .collect(Collectors.toList()); + + return visitWithDiagnoses; + } + } diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java b/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java deleted file mode 100644 index d8df56cb..00000000 --- a/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.openmrs.module.emrapi.db; - -import org.hibernate.Query; -import org.hibernate.SessionFactory; -import org.openmrs.Diagnosis; -import org.openmrs.Patient; -import org.openmrs.Visit; -import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; - -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -@Repository -public class HibernateVisitDAO implements VisitDAO { - - @Autowired - private SessionFactory sessionFactory; - - public List getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit) { - - String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; - - String hqlVisit="SELECT DISTINCT v FROM Visit v " + - "LEFT JOIN FETCH v.encounters enc " + - "LEFT JOIN enc.encounterType et " + - "WHERE v.patient.id = :patientId " + - "AND (et.uuid = :encounterTypeUuid OR enc IS NULL) " + - "ORDER BY v.startDatetime DESC"; - - Query visitQuery = sessionFactory.getCurrentSession().createQuery(hqlVisit); - - visitQuery.setParameter("patientId", patient.getId()); - visitQuery.setParameter("encounterTypeUuid", visitNoteEncounterTypeUuid); - visitQuery.setFirstResult(startIndex); - visitQuery.setMaxResults(limit); - - List visits = visitQuery.list(); - - String hqlDiagnosis = "SELECT DISTINCT diag FROM Diagnosis diag " + - "JOIN diag.encounter e " + - "WHERE e.visit.id IN :visitIds"; - - List visitIds = visits.stream() - .map(Visit::getId) - .collect(Collectors.toList()); - - List diagnoses = sessionFactory.getCurrentSession() - .createQuery(hqlDiagnosis) - .setParameterList("visitIds", visitIds) - .list(); - - Map> visitToDiagnosesMap = new HashMap<>(); - for (Diagnosis diagnosis : diagnoses) { - Visit visit = diagnosis.getEncounter().getVisit(); - visitToDiagnosesMap - .computeIfAbsent(visit, v -> new HashSet<>()) - .add(diagnosis); - } - - List visitWithDiagnoses = visits.stream() - .sorted(Comparator.comparing(Visit::getStartDatetime).reversed()) - .map(visit -> new VisitWithDiagnoses(visit, visitToDiagnosesMap.getOrDefault(visit, new HashSet<>()))) - .collect(Collectors.toList()); - - return visitWithDiagnoses; - } -} diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java b/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java deleted file mode 100644 index c90acec3..00000000 --- a/api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.openmrs.module.emrapi.db; - -import org.openmrs.Patient; -import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; - -import java.util.List; - -public interface VisitDAO { - - List getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit); -} diff --git a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnoses.java b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnoses.java index 188fac7c..c17b45e6 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnoses.java +++ b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnoses.java @@ -9,25 +9,14 @@ @Setter @Getter -public class VisitWithDiagnoses extends Visit { - +public class VisitWithDiagnoses { + + private Visit visit; + private Set diagnoses; public VisitWithDiagnoses(Visit visit, Set diagnoses) { - super(); - this.setVisitId(visit.getVisitId()); - this.setPatient(visit.getPatient()); - this.setVisitType(visit.getVisitType()); - this.setIndication(visit.getIndication()); - this.setLocation(visit.getLocation()); - this.setStartDatetime(visit.getStartDatetime()); - this.setStopDatetime(visit.getStopDatetime()); - this.setEncounters(visit.getEncounters()); - this.uuid = visit.getUuid(); - this.diagnoses = diagnoses; + this.visit = visit; + this.diagnoses = diagnoses; } - - private String uuid; - private Set diagnoses; - - + } diff --git a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java index 4c3e9abb..acb051ea 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java @@ -4,7 +4,7 @@ import org.openmrs.Patient; import org.openmrs.api.PatientService; import org.openmrs.api.impl.BaseOpenmrsService; -import org.openmrs.module.emrapi.db.VisitDAO; +import org.openmrs.module.emrapi.db.EmrApiDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -17,7 +17,7 @@ public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements PatientService patientService; @Autowired - VisitDAO visitDAO; + EmrApiDAO emrApiDAO; @Override public List getVisitsByPatientId(String patientUuid, int startIndex, int limit) { @@ -28,6 +28,6 @@ public List getVisitsByPatientId(String patientUuid, int sta throw new ObjectNotFoundException("No patient found with uuid " + patientUuid, Patient.class.getName()); } - return visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient, startIndex, limit); + return emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient, startIndex, limit); } } diff --git a/api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java b/api/src/test/java/org/openmrs/module/emrapi/db/EmrApiDaoTest.java similarity index 65% rename from api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java rename to api/src/test/java/org/openmrs/module/emrapi/db/EmrApiDaoTest.java index 3ee39573..0919a5eb 100644 --- a/api/src/test/java/org/openmrs/module/emrapi/db/VisitDAOTest.java +++ b/api/src/test/java/org/openmrs/module/emrapi/db/EmrApiDaoTest.java @@ -15,10 +15,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -public class VisitDAOTest extends EmrApiContextSensitiveTest { +public class EmrApiDaoTest extends EmrApiContextSensitiveTest { @Autowired - private VisitDAO visitDAO; + private EmrApiDAO emrApiDAO; @Before public void setup() { @@ -27,22 +27,22 @@ public void setup() { } @Test - public void shouldFetchVisitsByPatientId() { + public void shouldFetchVisitsWithNotesAndDiagnosesByPatientId() { String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; Patient patient = new Patient(); patient.setPatientId(109); - List visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,10); + List visits = emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,10); assertNotNull(visits); assert visits.size() == 3; VisitWithDiagnoses firstVisit = visits.get(2); - Set firstVisitEncounters = firstVisit.getEncounters(); + Set firstVisitEncounters = firstVisit.getVisit().getEncounters(); Set firstVisitDiagnoses = firstVisit.getDiagnoses(); - assert firstVisit.getId() == 1014; - assert firstVisit.getPatient().getPatientId() == 109; + assert firstVisit.getVisit().getId() == 1014; + assert firstVisit.getVisit().getPatient().getPatientId() == 109; assert firstVisitEncounters.size() == 2; assert firstVisitDiagnoses.size() == 3; @@ -51,11 +51,11 @@ public void shouldFetchVisitsByPatientId() { } VisitWithDiagnoses secondVisit = visits.get(1); - Set secondVisitEncounters = secondVisit.getEncounters(); + Set secondVisitEncounters = secondVisit.getVisit().getEncounters(); Set secondVisitDiagnoses = secondVisit.getDiagnoses(); - assert secondVisit.getId() == 1015; - assert secondVisit.getPatient().getPatientId() == 109; + assert secondVisit.getVisit().getId() == 1015; + assert secondVisit.getVisit().getPatient().getPatientId() == 109; assert secondVisitEncounters.size() == 1; assert secondVisitDiagnoses.size() == 2; @@ -64,27 +64,27 @@ public void shouldFetchVisitsByPatientId() { } VisitWithDiagnoses thirdVisit = visits.get(0); - Set thirdVisitEncounters = thirdVisit.getEncounters(); + Set thirdVisitEncounters = thirdVisit.getVisit().getEncounters(); Set thirdVisitDiagnoses = thirdVisit.getDiagnoses(); - assert thirdVisit.getId() == 1017; - assert thirdVisit.getPatient().getPatientId() == 109; + assert thirdVisit.getVisit().getId() == 1017; + assert thirdVisit.getVisit().getPatient().getPatientId() == 109; assert thirdVisitEncounters.isEmpty(); assert thirdVisitDiagnoses.isEmpty(); } @Test - public void shouldFetchVisitsByPatientIdWithPagination() { + public void shouldFetchVisitsWithNotesAndDiagnosesWithPagination() { Patient patient = new Patient(); patient.setPatientId(109); - List visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,1); + List visits = emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,1); assertNotNull(visits); assert visits.size() == 1; VisitWithDiagnoses mostRecentVisit = visits.get(0); - assert mostRecentVisit.getId() == 1017; + assert mostRecentVisit.getVisit().getId() == 1017; } } diff --git a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java index 409a67af..2b3f3662 100644 --- a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java @@ -36,21 +36,23 @@ public ResponseEntity getVisitsByPatientId( HttpServletResponse response, @PathVariable("patientUuid") String patientUuid) { RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT); - List visits; - visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit()); + List visitsEntries; + visitsEntries = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit()); // Convert the visits and diagnoses to SimpleObjects List convertedVisits = new ArrayList<>(); - for (VisitWithDiagnoses visit : visits) { - SimpleObject visitObject = (SimpleObject) ConversionUtil.convertToRepresentation(visit, context.getRepresentation()); + for (VisitWithDiagnoses visitEntry : visitsEntries) { + SimpleObject visitEntryObject = new SimpleObject(); + SimpleObject visitObject = (SimpleObject) ConversionUtil.convertToRepresentation(visitEntry.getVisit(), context.getRepresentation()); List convertedDiagnoses = new ArrayList<>(); - for (Diagnosis diagnosis : visit.getDiagnoses()) { + for (Diagnosis diagnosis : visitEntry.getDiagnoses()) { convertedDiagnoses.add((SimpleObject) ConversionUtil.convertToRepresentation(diagnosis, context.getRepresentation())); } - visitObject.put("diagnoses", convertedDiagnoses); + visitEntryObject.put("visit", visitObject); + visitEntryObject.put("diagnoses", convertedDiagnoses); - convertedVisits.add(visitObject); + convertedVisits.add(visitEntryObject); } return new ResponseEntity<>(new NeedsPaging<>(convertedVisits, context), HttpStatus.OK); diff --git a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java index fc579590..eb413011 100644 --- a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java @@ -26,22 +26,21 @@ public class VisitControllerTest extends BaseModuleWebContextSensitiveTest { private MockMvc mockMvc; @Before - public void setUp() throws Exception { + public void setUp() { executeDataSet("baseMetaData.xml"); executeDataSet("pastVisitSetup.xml"); mockMvc = MockMvcBuilders.standaloneSetup(controllerFactory.getObject()).build(); } @Test - public void shouldGetVisitsByPatientId() throws Exception { + public void shouldGetVisitsWithNotesAndDiagnosesByPatient() throws Exception { - String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; String patientUuid = "8604d42e-3ca8-11e3-bf2b-0d0c09861e97"; String firstVisitUuid = "1esd5218-6b78-11e0-93c3-18a905e044dc"; String secondVisitUuid = "1c72e1ac-9b18-11e0-93c3-18a905e044dc"; String thirdVisitUuid = "3c72f2bc-9b18-11e0-93c3-18a905e044ec"; - MvcResult response = mockMvc.perform(get("/rest/v1/emrapi/patient/" + patientUuid+"/visitWithNotesAndDiagnoses") + MvcResult response = mockMvc.perform(get("/rest/v1/emrapi/patient/" + patientUuid + "/visitWithNotesAndDiagnoses") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn(); @@ -53,13 +52,14 @@ public void shouldGetVisitsByPatientId() throws Exception { assertNotNull(result); assert result.get("totalCount").equals(3); - List> visits = (List>) result.get("pageOfResults"); - assert visits.size() == 3; + List> visitEntries = (List>) result.get("pageOfResults"); + assert visitEntries.size() == 3; // extract the first visit and check its properties - Map firstVisit = visits.get(2); + Map firstVisitEntry = visitEntries.get(2); + Map firstVisit = (Map) firstVisitEntry.get("visit"); List> firstVisitEncounters = (List>) firstVisit.get("encounters"); - List> firstVisitDiagnoses = (List>) firstVisit.get("diagnoses"); + List> firstVisitDiagnoses = (List>) firstVisitEntry.get("diagnoses"); assert firstVisit.get("uuid").equals(firstVisitUuid); assert firstVisitEncounters.size() == 2; @@ -70,9 +70,10 @@ public void shouldGetVisitsByPatientId() throws Exception { } // extract the second visit and check its properties - Map secondVisit = visits.get(1); + Map secondVisitEntry = visitEntries.get(1); + Map secondVisit = (Map) secondVisitEntry.get("visit"); List> secondVisitEncounters = (List>) secondVisit.get("encounters"); - List> secondVisitDiagnoses = (List>) secondVisit.get("diagnoses"); + List> secondVisitDiagnoses = (List>) secondVisitEntry.get("diagnoses"); assert secondVisit.get("uuid").equals(secondVisitUuid); assert secondVisitEncounters.size() == 1; @@ -83,9 +84,10 @@ public void shouldGetVisitsByPatientId() throws Exception { } // extract the third visit and check its properties (no notes or diagnoses) - Map thirdVisit = visits.get(0); + Map thirdVisitEntry = visitEntries.get(0); + Map thirdVisit = (Map) thirdVisitEntry.get("visit"); List> thirdVisitEncounters = (List>) thirdVisit.get("encounters"); - List> thirdVisitDiagnoses = (List>) thirdVisit.get("diagnoses"); + List> thirdVisitDiagnoses = (List>) thirdVisitEntry.get("diagnoses"); assert thirdVisit.get("uuid").equals(thirdVisitUuid); assert thirdVisitEncounters.isEmpty(); @@ -93,7 +95,7 @@ public void shouldGetVisitsByPatientId() throws Exception { } @Test - public void shouldGetVisitsByPatientIdWithPagination() throws Exception { + public void shouldGetVisitsWithNotesAndDiagnosesByPatientWithPagination() throws Exception { String patientUuid = "8604d42e-3ca8-11e3-bf2b-0d0c09861e97"; String mostRecentVisitUuid = "3c72f2bc-9b18-11e0-93c3-18a905e044ec"; @@ -112,10 +114,12 @@ public void shouldGetVisitsByPatientIdWithPagination() throws Exception { assertNotNull(result); assert result.get("totalCount").equals(1); - List> visits = (List>) result.get("pageOfResults"); - assert visits.size() == 1; + List> visitsEntries = (List>) result.get("pageOfResults"); + assert visitsEntries.size() == 1; - Map recentVisit = visits.get(0); + Map recentVisitEntry = visitsEntries.get(0); + Map recentVisit = (Map) recentVisitEntry.get("visit"); assert recentVisit.get("uuid").equals(mostRecentVisitUuid); + assert recentVisit.get("encounters").equals(0); } }