Skip to content

Commit

Permalink
A-1207053820490648 | [CURE IPD Bug] - Patients are not arranged in as…
Browse files Browse the repository at this point in the history
…cending 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

(cherry picked from commit abcdb78)
  • Loading branch information
Arjun-Go authored and rahu1ramesh committed Jul 4, 2024
1 parent 1d13991 commit f72d644
Showing 1 changed file with 34 additions and 10 deletions.
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;
}
}

0 comments on commit f72d644

Please sign in to comment.