Skip to content

Commit

Permalink
Search functionality API added & Refactor get patients API (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalai-tw authored Feb 22, 2024
1 parent ab4e348 commit b7d9507
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

public interface WardDAO {

List<AdmittedPatient> getAdmittedPatients(Location location);
List<AdmittedPatient> searchAdmittedPatients(Location location,List<String> searchKeys,String searchValue,Integer offset,Integer limit);

List<AdmittedPatient> getAdmittedPatientsByLocation(Location location,Integer offset,Integer limit);

WardPatientsSummary getWardPatientSummary(Location location);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public HibernateWardDAO(SessionFactory sessionFactory) {
}

@Override
public List<AdmittedPatient> getAdmittedPatients(Location location) {
public List<AdmittedPatient> getAdmittedPatientsByLocation(Location location,Integer offset,Integer limit) {
Session session = this.sessionFactory.getCurrentSession();
try {
Query query = session.createQuery("select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment," +
Expand All @@ -46,6 +46,7 @@ public List<AdmittedPatient> getAdmittedPatients(Location location) {
"ORDER BY assignment.startDatetime desc");

query.setParameter("location", location);
query.setFirstResult(offset).setMaxResults(limit);
return query.getResultList();
}
catch (Exception ex){
Expand Down Expand Up @@ -73,4 +74,93 @@ public WardPatientsSummary getWardPatientSummary(Location location) {
return new WardPatientsSummary();
}

@Override
public List<AdmittedPatient> searchAdmittedPatients(Location location, List<String> searchKeys, String searchValue,Integer offset,Integer limit) {
try {
Session session = sessionFactory.getCurrentSession();

String selectQuery = "select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment," +
"(COUNT(DISTINCT o.orderId) - COUNT (DISTINCT s.order.orderId))) " +
"from org.openmrs.module.bedmanagement.entity.BedPatientAssignment assignment " +
"JOIN org.openmrs.Visit v on v.patient = assignment.patient " +
"JOIN org.openmrs.Patient p on assignment.patient = p " +
"JOIN org.openmrs.Person pr on pr.personId=p.patientId " +
"JOIN org.openmrs.Encounter e on e.visit = v " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap on locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l on locmap.location = l " +
"LEFT JOIN org.openmrs.Order o on o.encounter = e " +
"LEFT JOIN Slot s on s.order = o ";

// Construct additional joins and where clause based on search keys
StringBuilder additionalJoins = new StringBuilder("");
StringBuilder whereClause = new StringBuilder("");
generateSQLSearchConditions(searchKeys,additionalJoins,whereClause);

// Construct group by clause
String groupBy = " GROUP BY assignment.patient, v ORDER BY assignment.startDatetime desc ";

// Create query
Query query = session.createQuery(selectQuery + additionalJoins + whereClause + groupBy);

// Set parameters
query.setParameter("location", location);
setQueryParameters(query, searchKeys, searchValue);
query.setFirstResult(offset).setMaxResults(limit);

return query.getResultList();
} catch (Exception ex) {
log.error("Exception at WardDAO searching ", ex.getMessage());
return new ArrayList<>();
}
}

private void generateSQLSearchConditions(List<String> searchKeys,StringBuilder additionalJoins,StringBuilder whereClause) {
whereClause.append("where (assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location)");
if (searchKeys != null && !searchKeys.isEmpty()) {
whereClause.append(" and (");
for (int i = 0; i < searchKeys.size(); i++) {
switch (searchKeys.get(i)) {
case "bedNumber":
whereClause.append(" assignment.bed.bedNumber LIKE :bedNumber ");
break;
case "patientIdentifier":
additionalJoins.append(" JOIN p.identifiers pi ");
whereClause.append(" pi.identifier LIKE :patientIdentifier ");
break;
case "patientName":
additionalJoins.append(" JOIN pr.names prn ");
whereClause.append(" (prn.givenName LIKE :patientName or prn.middleName LIKE :patientName or prn.familyName LIKE :patientName) ");
break;
}
if (i < searchKeys.size() - 1) {
whereClause.append(" or ");
}
}
whereClause.append(" ) ");
}
}

private void setQueryParameters(Query query, List<String> searchKeys, String searchValue) {
if (searchKeys != null && searchValue != null && !searchValue.isEmpty()) {
if (searchKeys.contains("bedNumber")) {
query.setParameter("bedNumber", "%" + searchValue + "%");
}
if (searchKeys.contains("patientIdentifier")) {
query.setParameter("patientIdentifier", "%" + searchValue + "%");
}
if (searchKeys.contains("patientName")) {
query.setParameter("patientName", "%" + searchValue + "%");
}
}
}

private StringBuilder appendORIfMoreSearchKeysPresent(int i,int size,StringBuilder whereClause){
if (size==(i+1)){
return whereClause;
}
return whereClause.append(" or ");
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@

import org.openmrs.module.bedmanagement.entity.BedPatientAssignment;

public class AdmittedPatient extends BedPatientAssignment {
public class AdmittedPatient {

private BedPatientAssignment bedPatientAssignment;
private Long newTreatments;

public AdmittedPatient(BedPatientAssignment bedPatientAssignment, Long newTreatments){
super.setId(bedPatientAssignment.getId());
super.setBed(bedPatientAssignment.getBed());
super.setPatient(bedPatientAssignment.getPatient());
super.setEncounter(bedPatientAssignment.getEncounter());
super.setStartDatetime(bedPatientAssignment.getStartDatetime());
super.setEndDatetime(bedPatientAssignment.getEndDatetime());
super.setUuid(bedPatientAssignment.getUuid());
super.setVoided(bedPatientAssignment.getVoided());
super.setVoidReason(bedPatientAssignment.getVoidReason());
this.bedPatientAssignment=bedPatientAssignment;
this.newTreatments=newTreatments;
}

public void setNewTreatments(Long newTreatments) {
this.newTreatments = newTreatments;
public BedPatientAssignment getBedPatientAssignment() {
return bedPatientAssignment;
}

public Long getNewTreatments() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package org.openmrs.module.ipd.api.service;

import org.openmrs.module.ipd.api.model.IPDWardPatientDetails;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;

import java.util.List;


public interface WardService {

WardPatientsSummary getIPDWardPatientSummary(String wardUuid);

IPDWardPatientDetails getWardPatientsByUuid(String wardUuid, Integer offset, Integer limit);
List<AdmittedPatient> getWardPatientsByUuid(String wardUuid, Integer offset, Integer limit);

List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys,String searchValue, Integer offset, Integer limit);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.openmrs.api.context.Context;
import org.openmrs.module.ipd.api.dao.WardDAO;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.IPDWardPatientDetails;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;
import org.openmrs.module.ipd.api.service.WardService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -34,22 +33,15 @@ public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
}

@Override
public IPDWardPatientDetails getWardPatientsByUuid(String wardUuid, Integer offset, Integer limit) {
public List<AdmittedPatient> getWardPatientsByUuid(String wardUuid, Integer offset, Integer limit) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
List<AdmittedPatient> activePatients= wardDAO.getAdmittedPatients(location);
if (activePatients==null){
return new IPDWardPatientDetails(new ArrayList<>(),new WardPatientsSummary());
}
offset = Math.min(offset, activePatients.size());
limit = Math.min(limit, activePatients.size() - offset);

WardPatientsSummary wardPatientsSummary = computePatientStats(activePatients);
return new IPDWardPatientDetails(activePatients.subList(offset, offset + limit), wardPatientsSummary);
return wardDAO.getAdmittedPatientsByLocation(location,offset,limit);
}

private WardPatientsSummary computePatientStats(List<AdmittedPatient> activePatients){
WardPatientsSummary wardPatientsSummary =new WardPatientsSummary();
wardPatientsSummary.setTotalPatients(activePatients !=null ? activePatients.size() : 0L);
return wardPatientsSummary;
@Override
public List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue, Integer offset, Integer limit) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
return wardDAO.searchAdmittedPatients(location,searchKeys,searchValue,offset,limit);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.openmrs.Location;
import org.openmrs.api.LocationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.ipd.api.BaseIntegrationTest;
import org.openmrs.module.ipd.api.dao.WardDAO;
import org.openmrs.module.ipd.api.model.AdmittedPatient;
Expand All @@ -19,8 +23,12 @@ public class HibernateWardDAOIntegrationTest extends BaseIntegrationTest {


@Test
public void shouldGetAdmittedPatients() {
List<AdmittedPatient> assignments= wardDAO.getAdmittedPatients(null);
public void shouldGetAdmittedPatientsByLocation() {

Location location= Context.getService(LocationService.class).getLocationByUuid("7779d653-393b-4118-9c83-a3715b82d4ac");
List<AdmittedPatient> admittedPatients= wardDAO.getAdmittedPatientsByLocation(location,0,10);

Assertions.assertEquals(0, admittedPatients.size());

}
}
Loading

0 comments on commit b7d9507

Please sign in to comment.