Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading