Skip to content

Commit

Permalink
Merge DAOs
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasanka-sack committed Dec 12, 2024
1 parent 8b20d2d commit 7f8fe94
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 145 deletions.
14 changes: 14 additions & 0 deletions api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAO.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,4 +11,15 @@ public interface EmrApiDAO {
<T> List<T> executeHql(String queryString, Map<String, Object> parameters, Class<T> clazz);

<T> List<T> executeHqlFromResource(String resource, Map<String, Object> parameters, Class<T> 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<VisitWithDiagnoses> getVisitsWithNotesAndDiagnosesByPatient(Patient patient, int startIndex, int limit);
}
59 changes: 59 additions & 0 deletions api/src/main/java/org/openmrs/module/emrapi/db/EmrApiDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -55,4 +64,54 @@ public <T> List<T> executeHqlFromResource(String resource, Map<String, Object> p
}
return executeHql(hql, parameters, clazz);
}

public List<VisitWithDiagnoses> 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<Visit> visits = visitQuery.list();

String hqlDiagnosis = "SELECT DISTINCT diag FROM Diagnosis diag " +
"JOIN diag.encounter e " +
"WHERE e.visit.id IN :visitIds";

List<Integer> visitIds = visits.stream()
.map(Visit::getId)
.collect(Collectors.toList());

List<Diagnosis> diagnoses = sessionFactory.getCurrentSession()
.createQuery(hqlDiagnosis)
.setParameterList("visitIds", visitIds)
.list();

Map<Visit, Set<Diagnosis>> visitToDiagnosesMap = new HashMap<>();
for (Diagnosis diagnosis : diagnoses) {
Visit visit = diagnosis.getEncounter().getVisit();
visitToDiagnosesMap
.computeIfAbsent(visit, v -> new HashSet<>())
.add(diagnosis);
}

List<VisitWithDiagnoses> visitWithDiagnoses = visits.stream()
.sorted(Comparator.comparing(Visit::getStartDatetime).reversed())
.map(visit -> new VisitWithDiagnoses(visit, visitToDiagnosesMap.getOrDefault(visit, new HashSet<>())))
.collect(Collectors.toList());

return visitWithDiagnoses;
}

}

This file was deleted.

11 changes: 0 additions & 11 deletions api/src/main/java/org/openmrs/module/emrapi/db/VisitDAO.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,14 @@

@Setter
@Getter
public class VisitWithDiagnoses extends Visit {

public class VisitWithDiagnoses {

private Visit visit;
private Set<Diagnosis> diagnoses;

public VisitWithDiagnoses(Visit visit, Set<Diagnosis> 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<Diagnosis> diagnoses;



}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +17,7 @@ public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements
PatientService patientService;

@Autowired
VisitDAO visitDAO;
EmrApiDAO emrApiDAO;

@Override
public List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid, int startIndex, int limit) {
Expand All @@ -28,6 +28,6 @@ public List<VisitWithDiagnoses> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<VisitWithDiagnoses> visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,10);
List<VisitWithDiagnoses> visits = emrApiDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,10);
assertNotNull(visits);
assert visits.size() == 3;

VisitWithDiagnoses firstVisit = visits.get(2);
Set<Encounter> firstVisitEncounters = firstVisit.getEncounters();
Set<Encounter> firstVisitEncounters = firstVisit.getVisit().getEncounters();
Set<Diagnosis> 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;

Expand All @@ -51,11 +51,11 @@ public void shouldFetchVisitsByPatientId() {
}

VisitWithDiagnoses secondVisit = visits.get(1);
Set<Encounter> secondVisitEncounters = secondVisit.getEncounters();
Set<Encounter> secondVisitEncounters = secondVisit.getVisit().getEncounters();
Set<Diagnosis> 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;

Expand All @@ -64,27 +64,27 @@ public void shouldFetchVisitsByPatientId() {
}

VisitWithDiagnoses thirdVisit = visits.get(0);
Set<Encounter> thirdVisitEncounters = thirdVisit.getEncounters();
Set<Encounter> thirdVisitEncounters = thirdVisit.getVisit().getEncounters();
Set<Diagnosis> 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<VisitWithDiagnoses> visits = visitDAO.getVisitsWithNotesAndDiagnosesByPatient(patient,0,1);
List<VisitWithDiagnoses> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ public ResponseEntity<?> getVisitsByPatientId(
HttpServletResponse response,
@PathVariable("patientUuid") String patientUuid) {
RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT);
List<VisitWithDiagnoses> visits;
visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit());
List<VisitWithDiagnoses> visitsEntries;
visitsEntries = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit());

// Convert the visits and diagnoses to SimpleObjects
List<SimpleObject> 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<SimpleObject> 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);
Expand Down
Loading

0 comments on commit 7f8fe94

Please sign in to comment.