Skip to content

Commit

Permalink
Merge pull request #46 from Bahmni/sort-by-bed-number
Browse files Browse the repository at this point in the history
Sort patient list by BedNumber in Search & Patient APIs
  • Loading branch information
umair-fayaz authored Mar 20, 2024
2 parents 5762c79 + 1206481 commit 21930b1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 36 deletions.
4 changes: 2 additions & 2 deletions api/src/main/java/org/openmrs/module/ipd/api/dao/WardDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

public interface WardDAO {

List<AdmittedPatient> searchAdmittedPatients(Location location,List<String> searchKeys,String searchValue);
List<AdmittedPatient> searchAdmittedPatients(Location location, List<String> searchKeys, String searchValue, String sortBy);

List<AdmittedPatient> getAdmittedPatients(Location location, Provider provider, Date currentDateTime);
List<AdmittedPatient> getAdmittedPatients(Location location, Provider provider, Date currentDateTime, String sortBy);

WardPatientsSummary getWardPatientSummary(Location location);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,24 @@ public HibernateWardDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

private static String generateOrderByClauseForSorting(String sortBy) {
String orderBy = " ";

sortBy = sortBy != null ? sortBy : "default";

switch (sortBy) {
case "bedNumber":
orderBy += "ORDER BY assignment.bed.bedNumber ";
break;
default:
orderBy += "ORDER BY assignment.startDatetime desc ";
break;
}
return orderBy;
}

@Override
public List<AdmittedPatient> getAdmittedPatients(Location location, Provider provider, Date dateTime) {
public List<AdmittedPatient> getAdmittedPatients(Location location, Provider provider, Date dateTime, String sortBy) {
Session session = this.sessionFactory.getCurrentSession();
try {
String queryString = "select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment," +
Expand All @@ -42,11 +58,11 @@ public List<AdmittedPatient> getAdmittedPatients(Location location, Provider pro
"LEFT JOIN CareTeam careTeam on careTeam.visit = v " +
"JOIN org.openmrs.module.bedmanagement.entity.BedLocationMapping locmap on locmap.bed = assignment.bed " +
"JOIN org.openmrs.Location l on locmap.location = l " +
//"JOIN careTeam.participants ctp " +
"LEFT JOIN careTeam.participants ctp " +
"LEFT JOIN org.openmrs.Order o on o.encounter = e " +
"LEFT JOIN Slot s on s.order = o " +
"where assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location ";

if (provider != null) {
queryString += "and ctp.provider = :provider ";
}
Expand All @@ -55,11 +71,13 @@ public List<AdmittedPatient> getAdmittedPatients(Location location, Provider pro
queryString += "and :dateTime between ctp.startTime and ctp.endTime ";
}

String groupBy = " GROUP BY assignment.patient, v ";

String orderBy = generateOrderByClauseForSorting(sortBy);

queryString += "GROUP BY assignment.patient, v " +
"ORDER BY assignment.startDatetime desc";
String finalQuery = queryString + groupBy + orderBy;

Query query = session.createQuery(queryString);
Query query = session.createQuery(finalQuery);

query.setParameter("location", location);

Expand Down Expand Up @@ -99,7 +117,7 @@ public WardPatientsSummary getWardPatientSummary(Location location) {
}

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

Expand All @@ -122,10 +140,12 @@ public List<AdmittedPatient> searchAdmittedPatients(Location location, List<Stri
generateSQLSearchConditions(searchKeys,additionalJoins,whereClause);

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

String orderBy = generateOrderByClauseForSorting(sortBy);

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

// Set parameters
query.setParameter("location", location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public interface WardService {

WardPatientsSummary getIPDWardPatientSummary(String wardUuid);

List<AdmittedPatient> getWardPatientsByUuid(String wardUuid);
List<AdmittedPatient> getWardPatientsByUuid(String wardUuid, String sortBy);

List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue);
List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue, String sortBy);

List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid);
List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid, String sortBy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
}

@Override
public List<AdmittedPatient> getWardPatientsByUuid(String wardUuid) {
public List<AdmittedPatient> getWardPatientsByUuid(String wardUuid, String sortBy) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
return wardDAO.getAdmittedPatients(location,null, null);
return wardDAO.getAdmittedPatients(location,null, null, sortBy);
}

@Override
public List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid) {
public List<AdmittedPatient> getPatientsByWardAndProvider(String wardUuid, String providerUuid, String sortBy) {
Location location = Context.getService(LocationService.class).getLocationByUuid(wardUuid);
Provider provider = Context.getProviderService().getProviderByUuid(providerUuid);
Date currentDateTime = new Date();
return wardDAO.getAdmittedPatients(location, provider, currentDateTime);
return wardDAO.getAdmittedPatients(location, provider, currentDateTime, sortBy);
}

@Override
public List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue) {
public List<AdmittedPatient> searchWardPatients(String wardUuid, List<String> searchKeys, String searchValue, String sortBy) {
Location location= Context.getService(LocationService.class).getLocationByUuid(wardUuid);
return wardDAO.searchAdmittedPatients(location,searchKeys,searchValue);
return wardDAO.searchAdmittedPatients(location,searchKeys,searchValue,sortBy);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HibernateWardDAOIntegrationTest extends BaseIntegrationTest {
public void shouldGetAdmittedPatientsByLocation() {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public ResponseEntity<Object> getIPDWardPatientStats (@PathVariable("wardUuid")
@ResponseBody
public ResponseEntity<Object> getIPDWardPatient(@PathVariable("wardUuid") String wardUuid,
@RequestParam(value = "offset") Integer offset,
@RequestParam (value = "limit") Integer limit) throws ParseException {
@RequestParam (value = "limit") Integer limit,
@RequestParam(value = "sortBy", required = false) String sortBy) throws ParseException {
try {
IPDPatientDetails ipdPatientDetails = ipdWardService.getIPDPatientByWard(wardUuid,offset,limit);
IPDPatientDetails ipdPatientDetails = ipdWardService.getIPDPatientByWard(wardUuid,offset,limit,sortBy);
return new ResponseEntity<>(IPDPatientDetailsResponse.createFrom(ipdPatientDetails), OK);
} catch (Exception e) {
log.error("Runtime error while trying to create new schedule", e);
Expand All @@ -56,9 +57,10 @@ public ResponseEntity<Object> getIPDWardPatient(@PathVariable("wardUuid") String
public ResponseEntity<Object> getIPDWardPatientsForProvider(@PathVariable("wardUuid") String wardUuid,
@RequestParam(value = "providerUuid") String providerUuid,
@RequestParam(value = "offset") Integer offset,
@RequestParam (value = "limit") Integer limit) throws ParseException {
@RequestParam (value = "limit") Integer limit,
@RequestParam(value = "sortBy", required = false) String sortBy) throws ParseException {
try {
IPDPatientDetails ipdPatientDetails = ipdWardService.getIPDPatientsByWardAndProvider(wardUuid, providerUuid, offset, limit);
IPDPatientDetails ipdPatientDetails = ipdWardService.getIPDPatientsByWardAndProvider(wardUuid, providerUuid, offset, limit, sortBy);
return new ResponseEntity<>(IPDPatientDetailsResponse.createFrom(ipdPatientDetails), OK);
} catch (Exception e) {
log.error("Runtime error while trying to create new schedule", e);
Expand All @@ -72,9 +74,10 @@ public ResponseEntity<Object> searchIPDWardPatient(@PathVariable("wardUuid") Str
@RequestParam(value = "offset") Integer offset,
@RequestParam (value = "limit") Integer limit,
@RequestParam(value = "searchKeys") List<String> searchKeys,
@RequestParam(value = "searchValue") String searchValue) throws ParseException {
@RequestParam(value = "searchValue") String searchValue,
@RequestParam(value = "sortBy", required = false) String sortBy) throws ParseException {
try {
IPDPatientDetails ipdPatientDetails = ipdWardService.searchIPDPatientsInWard(wardUuid,searchKeys,searchValue,offset,limit);
IPDPatientDetails ipdPatientDetails = ipdWardService.searchIPDPatientsInWard(wardUuid,searchKeys,searchValue,offset,limit,sortBy);
return new ResponseEntity<>(IPDPatientDetailsResponse.createFrom(ipdPatientDetails), OK);
} catch (Exception e) {
log.error("Runtime error while trying to create new schedule", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public interface IPDWardService {

WardPatientsSummary getIPDWardPatientSummary(String wardUuid);

IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, Integer limit);
IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, Integer limit, String sortBy);

IPDPatientDetails searchIPDPatientsInWard(String wardUuid, List<String> searchKeys, String searchValue, Integer offset, Integer limit);
IPDPatientDetails searchIPDPatientsInWard(String wardUuid, List<String> searchKeys, String searchValue, Integer offset, Integer limit, String sortBy);

IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String providerUuid, Integer offset, Integer limit);
IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String providerUuid, Integer offset, Integer limit, String sortBy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public WardPatientsSummary getIPDWardPatientSummary(String wardUuid) {
}

@Override
public IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, Integer limit) {
public IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, Integer limit, String sortBy) {

List<AdmittedPatient> admittedPatients = wardService.getWardPatientsByUuid(wardUuid);
List<AdmittedPatient> admittedPatients = wardService.getWardPatientsByUuid(wardUuid,sortBy);

if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
Expand All @@ -46,9 +46,9 @@ public IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, In
}

@Override
public IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String providerUuid, Integer offset, Integer limit) {
public IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String providerUuid, Integer offset, Integer limit, String sortBy) {

List<AdmittedPatient> admittedPatients = wardService.getPatientsByWardAndProvider(wardUuid, providerUuid);
List<AdmittedPatient> admittedPatients = wardService.getPatientsByWardAndProvider(wardUuid, providerUuid, sortBy);

if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
Expand All @@ -62,9 +62,9 @@ public IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String

@Override
public IPDPatientDetails searchIPDPatientsInWard(String wardUuid, List<String> searchKeys, String searchValue,
Integer offset, Integer limit) {
Integer offset, Integer limit, String sortBy) {

List<AdmittedPatient> admittedPatients = wardService.searchWardPatients(wardUuid,searchKeys,searchValue);
List<AdmittedPatient> admittedPatients = wardService.searchWardPatients(wardUuid,searchKeys,searchValue,sortBy);
if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}
Expand Down

0 comments on commit 21930b1

Please sign in to comment.