Skip to content

Commit

Permalink
EA-208: Change Inpatient Admission and Inaptient Request endpoints to…
Browse files Browse the repository at this point in the history
… support searching by patient and visits
  • Loading branch information
chibongho committed Oct 31, 2024
1 parent 8b657bf commit da797b7
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 51 deletions.
22 changes: 11 additions & 11 deletions api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,10 +989,10 @@ public List<InpatientRequest> getInpatientRequests(InpatientRequestSearchCriteri
parameters.put("limitByDispositionLocation", dispositionLocationIds != null);
parameters.put("admitLocationConcept", descriptor.getAdmissionLocationConcept());
parameters.put("transferLocationConcept", descriptor.getInternalTransferLocationConcept());
parameters.put("patientIds", criteria.getPatientIds());
parameters.put("limitByPatient", criteria.getPatientIds() != null);
parameters.put("visitIds", criteria.getVisitIds());
parameters.put("limitByVisit", criteria.getVisitIds() != null);
parameters.put("patients", criteria.getPatients());
parameters.put("limitByPatient", criteria.getPatients() != null);
parameters.put("visits", criteria.getVisits());
parameters.put("limitByVisit", criteria.getVisits() != null);

List<?> reqs = emrApiDAO.executeHqlFromResource("hql/inpatient_requests.hql", parameters, List.class);
List<InpatientRequest> ret = new ArrayList<>();
Expand Down Expand Up @@ -1036,10 +1036,10 @@ public List<InpatientAdmission> getInpatientAdmissions(InpatientAdmissionSearchC
parameters.put("admissionEncounterType", emrApiProperties.getAdmissionEncounterType());
parameters.put("transferEncounterType", emrApiProperties.getTransferWithinHospitalEncounterType());
parameters.put("dischargeEncounterType", emrApiProperties.getExitFromInpatientEncounterType());
parameters.put("patientIds", criteria.getPatientIds());
parameters.put("limitByPatient", criteria.getPatientIds() != null);
parameters.put("visitIds", criteria.getVisitIds());
parameters.put("limitByVisit", criteria.getVisitIds() != null);
parameters.put("patients", criteria.getPatients());
parameters.put("limitByPatient", criteria.getPatients() != null);
parameters.put("visits", criteria.getVisits());
parameters.put("limitByVisit", criteria.getVisits() != null);

List<?> l = emrApiDAO.executeHqlFromResource("hql/inpatient_admissions.hql", parameters, List.class);
Map<Visit, InpatientAdmission> m = new LinkedHashMap<>();
Expand Down Expand Up @@ -1067,7 +1067,7 @@ else if (encounter.getEncounterType().equals(dischargeEncounterType)) {
}

// Filter out any admissions that do not match the search criteria
List<Integer> visitIds = new ArrayList<>();
List<String> visits = new ArrayList<>();
for (Iterator<Map.Entry<Visit, InpatientAdmission>> i = m.entrySet().iterator(); i.hasNext(); ) {
InpatientAdmission admission = i.next().getValue();
if (criteria.getCurrentInpatientLocations() != null && !criteria.getCurrentInpatientLocations().contains(admission.getCurrentInpatientLocation())) {
Expand All @@ -1077,13 +1077,13 @@ else if (!criteria.isIncludeDischarged() && admission.isDischarged()) {
i.remove();
}
else {
visitIds.add(admission.getVisit().getVisitId());
visits.add(admission.getVisit().getUuid());
}
}

// Retrieve InpatientRequests associated with these admissions prior to returning them
InpatientRequestSearchCriteria requestCriteria = new InpatientRequestSearchCriteria();
requestCriteria.setVisitIds(visitIds);
requestCriteria.setVisits(visits);
List<InpatientRequest> requests = getInpatientRequests(requestCriteria);
for (InpatientRequest r : requests) {
InpatientAdmission admission = m.get(r.getVisit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class InpatientAdmissionSearchCriteria {
private Location visitLocation;
private List<Location> currentInpatientLocations;
private boolean includeDischarged = false;
private List<Integer> patientIds;
private List<Integer> visitIds;
private List<String> patients;
private List<String> visits;

public void addCurrentInpatientLocation(Location location) {
if (currentInpatientLocations == null) {
Expand All @@ -25,17 +25,17 @@ public void addCurrentInpatientLocation(Location location) {
currentInpatientLocations.add(location);
}

public void addPatientId(Integer patientId) {
if (patientIds == null) {
patientIds = new ArrayList<>();
public void addPatientUuid(String patientUuid) {
if (patients == null) {
patients = new ArrayList<>();
}
patientIds.add(patientId);
patients.add(patientUuid);
}

public void addVisitId(Integer visitId) {
if (visitIds == null) {
visitIds = new ArrayList<>();
public void addVisitUuid(String visitUuid) {
if (visits == null) {
visits = new ArrayList<>();
}
visitIds.add(visitId);
visits.add(visitUuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class InpatientRequestSearchCriteria {
private Location visitLocation;
private List<Location> dispositionLocations;
private List<DispositionType> dispositionTypes;
private List<Integer> patientIds;
private List<Integer> visitIds;
private List<String> patients;
private List<String> visits;

public void addDispositionLocation(Location location) {
if (dispositionLocations == null) {
Expand All @@ -34,17 +34,17 @@ public void addDispositionType(DispositionType dispositionType) {
dispositionTypes.add(dispositionType);
}

public void addPatientId(Integer patientId) {
if (patientIds == null) {
patientIds = new ArrayList<>();
public void addPatientUuid(String patientUuid) {
if (patients == null) {
patients = new ArrayList<>();
}
patientIds.add(patientId);
patients.add(patientUuid);
}

public void addVisitId(Integer visitId) {
if (visitIds == null) {
visitIds = new ArrayList<>();
public void addVisitUuid(String visitUuid) {
if (visits == null) {
visits = new ArrayList<>();
}
visitIds.add(visitId);
visits.add(visitUuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.openmrs.Patient;
import org.openmrs.RelationshipType;
Expand Down Expand Up @@ -69,9 +70,8 @@ public List<MotherAndChild> getMothersAndChildren(MothersAndChildrenSearchCriter

// now fetch all the admissions for children in the result set
InpatientAdmissionSearchCriteria inpatientAdmissionSearchCriteria = new InpatientAdmissionSearchCriteria();
Set<Integer> patientIds = ret.stream().map(MotherAndChild::getChild).map(Patient::getId).collect(Collectors.toSet());
patientIds.addAll(ret.stream().map(MotherAndChild::getMother).map(Patient::getId).collect(Collectors.toSet()));
inpatientAdmissionSearchCriteria.setPatientIds(new ArrayList<>(patientIds));
Set<String> patients = ret.stream().flatMap(mc -> Stream.of(mc.getChild(),mc.getMother())).map(Patient::getUuid).collect(Collectors.toSet());
inpatientAdmissionSearchCriteria.setPatients(new ArrayList<>(patients));
List<InpatientAdmission> admissions = adtService.getInpatientAdmissions(inpatientAdmissionSearchCriteria);
Map<Patient, InpatientAdmission> admissionsByPatient = new HashMap<>();
for (InpatientAdmission admission : admissions) {
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/resources/hql/inpatient_admissions.hql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ where encounter.voided = false
and (:visitLocation is null or visit.location = :visitLocation)
and visit.stopDatetime is null
and encounter.encounterType in (:admissionEncounterType, :transferEncounterType, :dischargeEncounterType)
and (:limitByPatient is false or patient.patientId in (:patientIds))
and (:limitByVisit is false or visit.visitId in (:visitIds))
and (:limitByPatient is false or patient.uuid in (:patients))
and (:limitByVisit is false or visit.uuid in (:visits))
order by visit.visitId, encounter.encounterDatetime, encounter.encounterId
4 changes: 2 additions & 2 deletions api/src/main/resources/hql/inpatient_requests.hql
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ where
and locationObs.valueText in (:dispositionLocationIds)
) > 0
)
and (:limitByPatient is false or dispoEncounter.patient.patientId in (:patientIds))
and (:limitByVisit is false or visit.visitId in (:visitIds))
and (:limitByPatient is false or person.uuid in (:patients))
and (:limitByVisit is false or visit.uuid in (:visits))
order by dispo.obsId
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ public void shouldGetInpatientRequestsBasedOnPatient() {
assertNumRequests(requestCriteria, 0);
createAdmissionRequest(DateUtils.addHours(visitDate, 2));
assertNumRequests(requestCriteria, 1);
requestCriteria.addPatientId(patient.getPatientId() + 1);
requestCriteria.addPatientUuid("fakePatientUuid1");
assertNumRequests(requestCriteria, 0);
requestCriteria.addPatientId(patient.getPatientId() + 2);
requestCriteria.addPatientUuid("fakePatientUuid2");
assertNumRequests(requestCriteria, 0);
requestCriteria.addPatientId(patient.getPatientId());
requestCriteria.addPatientUuid(patient.getUuid());
assertNumRequests(requestCriteria, 1);
}

Expand All @@ -287,11 +287,11 @@ public void shouldGetInpatientRequestsBasedOnVisit() {
assertNumRequests(requestCriteria, 0);
createAdmissionRequest(DateUtils.addHours(visitDate, 2));
assertNumRequests(requestCriteria, 1);
requestCriteria.addVisitId(visit.getVisitId() + 1);
requestCriteria.addVisitUuid("fakeVisitUuid1");
assertNumRequests(requestCriteria, 0);
requestCriteria.addVisitId(visit.getVisitId() + 2);
requestCriteria.addVisitUuid("fakeVisitUuid2");
assertNumRequests(requestCriteria, 0);
requestCriteria.addVisitId(visit.getVisitId());
requestCriteria.addVisitUuid(visit.getUuid());
assertNumRequests(requestCriteria, 1);
}

Expand Down Expand Up @@ -559,27 +559,27 @@ public void shouldGetAdmissionForInpatientLocation() {

@Test
public void shouldGetAdmissionForPatient() {
admissionCriteria.addPatientId(patient.getPatientId());
admissionCriteria.addPatientUuid(patient.getUuid());
assertNumAdmissions(admissionCriteria, 0);
createAdmissionEncounter(DateUtils.addHours(visitDate, 2));
assertNumAdmissions(admissionCriteria, 1);
admissionCriteria.setPatientIds(Collections.singletonList(patient.getPatientId() + 1));
admissionCriteria.setPatients(Collections.singletonList("fakePatientUuid"));
assertNumAdmissions(admissionCriteria, 0);
}

@Test
public void shouldGetAdmissionForVisit() {
admissionCriteria.addVisitId(visit.getVisitId());
admissionCriteria.addVisitUuid(visit.getUuid());
assertNumAdmissions(admissionCriteria, 0);
createAdmissionEncounter(DateUtils.addHours(visitDate, 2));
assertNumAdmissions(admissionCriteria, 1);
admissionCriteria.setVisitIds(Collections.singletonList(visit.getVisitId() + 1));
admissionCriteria.setVisits(Collections.singletonList("fakeVisitUuid"));
assertNumAdmissions(admissionCriteria, 0);
}

@Test
public void shouldGetAdmissionThatHasBeenDischarged() {
admissionCriteria.addVisitId(visit.getVisitId());
admissionCriteria.addVisitUuid(visit.getUuid());
assertNumAdmissions(admissionCriteria, 0);
createAdmissionEncounter(DateUtils.addHours(visitDate, 2));
assertNumAdmissions(admissionCriteria, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ public SimpleObject getInpatientAdmissions(
HttpServletResponse response,
@RequestParam(required = false, value = "visitLocation") Location visitLocation,
@RequestParam(required = false, value = "currentInpatientLocation") List<Location> currentInpatientLocations,
@RequestParam(required = false, value = "includeDischarged") boolean includeDischarged
@RequestParam(required = false, value = "includeDischarged") boolean includeDischarged,
@RequestParam(required = false, value = "patients") List<String> patients,
@RequestParam(required = false, value = "visits") List<String> visits
) {
RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT);
InpatientAdmissionSearchCriteria criteria = new InpatientAdmissionSearchCriteria();
criteria.setVisitLocation(visitLocation);
criteria.setCurrentInpatientLocations(currentInpatientLocations);
criteria.setIncludeDischarged(includeDischarged);
criteria.setPatients(patients);
criteria.setVisits(visits);
List<InpatientAdmission> requests = adtService.getInpatientAdmissions(criteria);
return new NeedsPaging<>(requests, context).toSimpleObject(new InpatientAdmissionConverter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ public SimpleObject getInpatientRequests(
HttpServletResponse response,
@RequestParam(required = false, value = "visitLocation") Location visitLocation,
@RequestParam(required = false, value = "dispositionLocation") List<Location> dispositionLocations,
@RequestParam(required = false, value = "dispositionType") List<DispositionType> dispositionTypes
@RequestParam(required = false, value = "dispositionType") List<DispositionType> dispositionTypes,
@RequestParam(required = false, value = "patients") List<String> patients,
@RequestParam(required = false, value = "visits") List<String> visits
) {
RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT);
InpatientRequestSearchCriteria criteria = new InpatientRequestSearchCriteria();
criteria.setVisitLocation(visitLocation);
criteria.setDispositionLocations(dispositionLocations);
criteria.setDispositionTypes(dispositionTypes);
criteria.setPatients(patients);
criteria.setVisits(visits);
List<InpatientRequest> requests = adtService.getInpatientRequests(criteria);
return new NeedsPaging<>(requests, context).toSimpleObject(new InpatientRequestConverter());
}
Expand Down

0 comments on commit da797b7

Please sign in to comment.