diff --git a/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java index dc269b3a..6140c32f 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java @@ -1061,14 +1061,32 @@ else if (encounter.getEncounterType().equals(dischargeEncounterType)) { } } - List ret = new ArrayList<>(); - for (InpatientAdmission admission : m.values()) { - if (criteria.getCurrentInpatientLocations() == null || criteria.getCurrentInpatientLocations().contains(admission.getCurrentInpatientLocation())) { - if (criteria.isIncludeDischarged() || !admission.isDischarged()) { - ret.add(admission); - } + // Filter out any admissions that do not match the search criteria + List visitIds = new ArrayList<>(); + for (Iterator> i = m.entrySet().iterator(); i.hasNext(); ) { + InpatientAdmission admission = i.next().getValue(); + if (criteria.getCurrentInpatientLocations() != null && !criteria.getCurrentInpatientLocations().contains(admission.getCurrentInpatientLocation())) { + i.remove(); + } + else if (!criteria.isIncludeDischarged() && admission.isDischarged()) { + i.remove(); + } + else { + visitIds.add(admission.getVisit().getVisitId()); } } - return ret; + + // Retrieve InpatientRequests associated with these admissions prior to returning them + InpatientRequestSearchCriteria requestCriteria = new InpatientRequestSearchCriteria(); + requestCriteria.setVisitIds(visitIds); + List requests = getInpatientRequests(requestCriteria); + for (InpatientRequest r : requests) { + InpatientAdmission admission = m.get(r.getVisit()); + if (admission != null) { + admission.setCurrentInpatientRequest(r); + } + } + + return new ArrayList<>(m.values()); } } diff --git a/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientAdmission.java b/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientAdmission.java index 30276241..72c67a96 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientAdmission.java +++ b/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientAdmission.java @@ -21,6 +21,7 @@ public class InpatientAdmission { private Visit visit; private Patient patient; + private InpatientRequest currentInpatientRequest; private Set admissionEncounters = new TreeSet<>(getEncounterComparator()); private Set transferEncounters = new TreeSet<>(getEncounterComparator()); private Set dischargeEncounters = new TreeSet<>(getEncounterComparator()); diff --git a/api/src/test/java/org/openmrs/module/emrapi/adt/AdtServiceImplTest.java b/api/src/test/java/org/openmrs/module/emrapi/adt/AdtServiceImplTest.java index a3b499f0..b0944a4e 100644 --- a/api/src/test/java/org/openmrs/module/emrapi/adt/AdtServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/emrapi/adt/AdtServiceImplTest.java @@ -28,6 +28,8 @@ import java.util.List; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; public class AdtServiceImplTest extends EmrApiContextSensitiveTest { @@ -595,4 +597,24 @@ public void shouldNotGetAdmissionsForEndedVisits() { assertNumAdmissions(admissionCriteria, 0); } + @Test + public void shouldGetInpatientRequestsAssociatedWithAdmission() { + assertNumAdmissions(admissionCriteria, 0); + createAdmissionRequest(DateUtils.addHours(visitDate, 2)); + createAdmissionEncounter(DateUtils.addHours(visitDate, 3)); + List l = assertNumAdmissions(admissionCriteria, 1); + assertNull(l.get(0).getCurrentInpatientRequest()); + Obs transferRequest = createTransferRequest(DateUtils.addHours(visitDate, 4)); + l = assertNumAdmissions(admissionCriteria, 1); + assertNotNull(l.get(0).getCurrentInpatientRequest()); + assertThat(l.get(0).getCurrentInpatientRequest().getDispositionObsGroup(), equalTo(transferRequest)); + Obs dischargeRequest = createDischargeRequest(DateUtils.addHours(visitDate, 5), admissionLocation); + l = assertNumAdmissions(admissionCriteria, 1); + assertNotNull(l.get(0).getCurrentInpatientRequest()); + assertThat(l.get(0).getCurrentInpatientRequest().getDispositionObsGroup(), equalTo(dischargeRequest)); + createTransferEncounter(DateUtils.addHours(visitDate, 6)); + l = assertNumAdmissions(admissionCriteria, 1); + assertNull(l.get(0).getCurrentInpatientRequest()); + } + } diff --git a/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/InpatientAdmissionConverter.java b/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/InpatientAdmissionConverter.java index ef0db2af..e6dbfa2a 100644 --- a/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/InpatientAdmissionConverter.java +++ b/omod/src/main/java/org/openmrs/module/emrapi/rest/converter/InpatientAdmissionConverter.java @@ -1,7 +1,5 @@ package org.openmrs.module.emrapi.rest.converter; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.openmrs.annotation.Handler; import org.openmrs.module.emrapi.adt.InpatientAdmission; import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; @@ -13,8 +11,6 @@ @Handler(supports = InpatientAdmission.class, order = 0) public class InpatientAdmissionConverter extends SimpleBeanConverter { - private final Log log = LogFactory.getLog(getClass()); - @Override public DelegatingResourceDescription getResourceDescription(InpatientAdmission req, Representation representation) { DelegatingResourceDescription ret = super.getResourceDescription(req, representation); @@ -25,6 +21,7 @@ public DelegatingResourceDescription getResourceDescription(InpatientAdmission r rep.addProperty("firstAdmissionOrTransferEncounter", getEncounterRepresentation()); rep.addProperty("latestAdmissionOrTransferEncounter", getEncounterRepresentation()); rep.addProperty("encounterAssigningToCurrentInpatientLocation", getEncounterRepresentation()); + rep.addProperty("currentInpatientRequest", getInpatientRequestRepresentation()); rep.addProperty("discharged"); return rep; } @@ -41,4 +38,8 @@ else if (representation instanceof FullRepresentation) { public Representation getEncounterRepresentation() { return new CustomRepresentation("uuid,display,encounterDatetime,location:ref,encounterType:ref"); } + + public Representation getInpatientRequestRepresentation() { + return new CustomRepresentation("dispositionType,dispositionEncounter:(uuid,display,encounterDatetime),dispositionLocation:ref"); + } }