Skip to content

Commit

Permalink
A-1207053820490648 | Fix. Patients are not arranged in ascending orde…
Browse files Browse the repository at this point in the history
…r of bed number

* Kavitha| refactored search to support multiple words (#64)

* A-1207053820490648 | [CURE IPD Bug] - Patients are not arranged in ascending order of bed number (#62)

* add. sort logic to handle sorting of bed numbers that are purely integers

* update. sort logic for numeric bed numbers

* Remove IPD branch publish (#65)

* add. check to filter out orders that are inactive and not scheduled (#66)

---------

Co-authored-by: kavitha-sundararajan <[email protected]>
Co-authored-by: Arjun G <[email protected]>
  • Loading branch information
3 people authored Jul 2, 2024
1 parent b60d9a9 commit 08afb1c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,24 @@ public List<AdmittedPatient> searchAdmittedPatients(Location location, List<Stri
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)),careTeam) " +
String selectQuery = "select NEW org.openmrs.module.ipd.api.model.AdmittedPatient(assignment, " +
"(COUNT(DISTINCT o.orderId) - COUNT(DISTINCT s.order.orderId)), careTeam) " +
"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.Person pr on pr.personId = p.patientId " +
"JOIN org.openmrs.Encounter e on e.visit = v " +
"LEFT JOIN CareTeam careTeam on careTeam.visit = v "+
"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 " +
"LEFT JOIN org.openmrs.Order o on o.encounter = e " +
"LEFT JOIN org.openmrs.Order o on o.encounter = e and o.dateStopped is null and o.action!='DISCONTINUE' and o.careSetting.careSettingId = 2 " +
"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);
StringBuilder additionalJoins = new StringBuilder();
StringBuilder whereClause = new StringBuilder();
generateSQLSearchConditions(searchKeys, additionalJoins, whereClause, searchValue);

// Construct group by clause
String groupBy = " GROUP BY assignment.patient, v ";
Expand All @@ -188,7 +189,8 @@ public List<AdmittedPatient> searchAdmittedPatients(Location location, List<Stri
}
}

private void generateSQLSearchConditions(List<String> searchKeys,StringBuilder additionalJoins,StringBuilder whereClause) {

private void generateSQLSearchConditions(List<String> searchKeys, StringBuilder additionalJoins, StringBuilder whereClause, String searchValue) {
whereClause.append("where (assignment.endDatetime is null and v.stopDatetime is null and l.parentLocation = :location)");
if (searchKeys != null && !searchKeys.isEmpty()) {
whereClause.append(" and (");
Expand All @@ -203,7 +205,16 @@ private void generateSQLSearchConditions(List<String> searchKeys,StringBuilder a
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) ");
whereClause.append(" (");

String[] nameParts = searchValue.split("\\s+");
for (int j = 0; j < nameParts.length; j++) {
if (j > 0) whereClause.append(" and ");
whereClause.append(" (prn.givenName LIKE :namePart" + j +
" or prn.middleName LIKE :namePart" + j +
" or prn.familyName LIKE :namePart" + j + ") ");
}
whereClause.append(" )");
break;
}
if (i < searchKeys.size() - 1) {
Expand All @@ -223,7 +234,10 @@ private void setQueryParameters(Query query, List<String> searchKeys, String sea
query.setParameter("patientIdentifier", "%" + searchValue + "%");
}
if (searchKeys.contains("patientName")) {
query.setParameter("patientName", "%" + searchValue + "%");
String[] nameParts = searchValue.split("\\s+");
for (int i = 0; i < nameParts.length; i++) {
query.setParameter("namePart" + i, "%" + nameParts[i] + "%");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openmrs.module.ipd.service.impl;


import org.openmrs.module.ipd.api.model.AdmittedPatient;
import org.openmrs.module.ipd.api.model.IPDPatientDetails;
import org.openmrs.module.ipd.api.model.WardPatientsSummary;
Expand All @@ -12,6 +11,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

@Service
@Transactional
Expand All @@ -24,6 +25,7 @@ public IPDWardServiceImpl(WardService wardService) {
this.wardService = wardService;
}

private static final Pattern numericPattern = Pattern.compile("^\\d+$");

@Override
public WardPatientsSummary getIPDWardPatientSummary(String wardUuid, String providerUuid) {
Expand All @@ -38,11 +40,12 @@ public IPDPatientDetails getIPDPatientByWard(String wardUuid, Integer offset, In
if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}
List<AdmittedPatient> admittedPatientsSortedList = Objects.equals(sortBy, "bedNumber") ? sortNumericBedNumbers(admittedPatients) : admittedPatients;

offset = Math.min(offset, admittedPatients.size());
limit = Math.min(limit, admittedPatients.size() - offset);
offset = Math.min(offset, admittedPatientsSortedList.size());
limit = Math.min(limit, admittedPatientsSortedList.size() - offset);

return new IPDPatientDetails(admittedPatients.subList(offset, offset + limit), admittedPatients.size());
return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatientsSortedList.size());
}

@Override
Expand All @@ -53,25 +56,46 @@ public IPDPatientDetails getIPDPatientsByWardAndProvider(String wardUuid, String
if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}
List<AdmittedPatient> admittedPatientsSortedList = Objects.equals(sortBy, "bedNumber") ? sortNumericBedNumbers(admittedPatients) : admittedPatients;

offset = Math.min(offset, admittedPatients.size());
limit = Math.min(limit, admittedPatients.size() - offset);
offset = Math.min(offset, admittedPatientsSortedList.size());
limit = Math.min(limit, admittedPatientsSortedList.size() - offset);

return new IPDPatientDetails(admittedPatients.subList(offset, offset + limit), admittedPatients.size());
return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatientsSortedList.size());
}

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

List<AdmittedPatient> admittedPatients = wardService.searchWardPatients(wardUuid,searchKeys,searchValue,sortBy);

if (admittedPatients ==null ){
return new IPDPatientDetails(new ArrayList<>(),0);
}

offset = Math.min(offset, admittedPatients.size());
limit = Math.min(limit, admittedPatients.size() - offset);
List<AdmittedPatient> admittedPatientsSortedList = Objects.equals(sortBy, "bedNumber") ? sortNumericBedNumbers(admittedPatients) : admittedPatients;

offset = Math.min(offset, admittedPatientsSortedList.size());
limit = Math.min(limit, admittedPatientsSortedList.size() - offset);

return new IPDPatientDetails(admittedPatientsSortedList.subList(offset, offset + limit), admittedPatientsSortedList.size());
}

return new IPDPatientDetails(admittedPatients.subList(offset, offset + limit), admittedPatients.size());
private List<AdmittedPatient> sortNumericBedNumbers(List<AdmittedPatient> admittedPatients) {

boolean allNumeric = admittedPatients.stream()
.map(patient -> patient.getBedPatientAssignment().getBed().getBedNumber())
.allMatch(bedNumber -> numericPattern.matcher(bedNumber).matches());

if(allNumeric) {
admittedPatients.sort((patientA, patientB) -> {
String bedNumberA = patientA.getBedPatientAssignment().getBed().getBedNumber();
String bedNumberB = patientB.getBedPatientAssignment().getBed().getBedNumber();

return Integer.compare(Integer.parseInt(bedNumberA), Integer.parseInt(bedNumberB));
});
}
return admittedPatients;
}
}
2 changes: 1 addition & 1 deletion omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>1.0.0-SNAPSHOT</version>
<package>${MODULE_PACKAGE}</package>
<author>Bahmni</author>
<description>${project.parent.description}</description>
<description>Provides APIs for IPD features</description>

<require_modules>
<require_module>org.openmrs.module.webservices.rest</require_module>
Expand Down

0 comments on commit 08afb1c

Please sign in to comment.