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 72c67a96..2491f595 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 @@ -12,6 +12,7 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; +import java.util.stream.Collectors; /** * Represents a hospital Admission @@ -103,4 +104,18 @@ private Comparator getEncounterComparator() { .thenComparing(Encounter::getDateCreated) .thenComparing(Encounter::getEncounterId); } + + public List getAllEncounters() { + return Collections.unmodifiableList(visit.getEncounters().stream().filter(e -> !e.getVoided()).sorted(getEncounterComparator()).collect(Collectors.toList())); + } + + public Encounter getFirstEncounter() { + List encounters = getAllEncounters(); + return encounters.isEmpty() ? null : encounters.get(0); + } + + public Encounter getLatestEncounter() { + List encounters = getAllEncounters(); + return encounters.isEmpty() ? null : encounters.get(encounters.size() - 1); + } } diff --git a/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java b/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java index 11db3d0f..4d9e2142 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java +++ b/api/src/main/java/org/openmrs/module/emrapi/adt/InpatientRequest.java @@ -9,7 +9,11 @@ import org.openmrs.Visit; import org.openmrs.module.emrapi.disposition.DispositionType; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; /** * Represents and Admission, Discharge, or Transfer request @@ -23,4 +27,24 @@ public class InpatientRequest { private Obs dispositionObsGroup; private Concept disposition; private Location dispositionLocation; + + public List getAllEncounters() { + return Collections.unmodifiableList(visit.getEncounters().stream().filter(e -> !e.getVoided()).sorted(getEncounterComparator()).collect(Collectors.toList())); + } + + public Encounter getFirstEncounter() { + List encounters = getAllEncounters(); + return encounters.isEmpty() ? null : encounters.get(0); + } + + public Encounter getLatestEncounter() { + List encounters = getAllEncounters(); + return encounters.isEmpty() ? null : encounters.get(encounters.size() - 1); + } + + private Comparator getEncounterComparator() { + return Comparator.comparing(Encounter::getEncounterDatetime) + .thenComparing(Encounter::getDateCreated) + .thenComparing(Encounter::getEncounterId); + } }