-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EA-190 Improve API for retrieving inpatient visits (#235)
- Loading branch information
Showing
13 changed files
with
754 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
api/src/main/java/org/openmrs/module/emrapi/adt/InpatientAdmission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package org.openmrs.module.emrapi.adt; | ||
|
||
import lombok.Data; | ||
import org.openmrs.Encounter; | ||
import org.openmrs.Location; | ||
import org.openmrs.Patient; | ||
import org.openmrs.Visit; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* Represents a hospital Admission | ||
*/ | ||
@Data | ||
public class InpatientAdmission { | ||
|
||
private Visit visit; | ||
private Patient patient; | ||
private Set<Encounter> admissionEncounters = new TreeSet<>(getEncounterComparator()); | ||
private Set<Encounter> transferEncounters = new TreeSet<>(getEncounterComparator()); | ||
private Set<Encounter> dischargeEncounters = new TreeSet<>(getEncounterComparator()); | ||
|
||
public List<Encounter> getAdmissionAndTransferEncounters() { | ||
List<Encounter> encounters = new ArrayList<>(); | ||
encounters.addAll(admissionEncounters); | ||
encounters.addAll(transferEncounters); | ||
encounters.sort(getEncounterComparator()); | ||
return Collections.unmodifiableList(encounters); | ||
} | ||
|
||
public List<Encounter> getAdtEncounters() { | ||
List<Encounter> encounters = new ArrayList<>(); | ||
encounters.addAll(admissionEncounters); | ||
encounters.addAll(transferEncounters); | ||
encounters.addAll(dischargeEncounters); | ||
encounters.sort(getEncounterComparator()); | ||
return Collections.unmodifiableList(encounters); | ||
} | ||
|
||
public Encounter getFirstAdmissionOrTransferEncounter() { | ||
List<Encounter> encounters = getAdmissionAndTransferEncounters(); | ||
return encounters.isEmpty() ? null : encounters.get(0); | ||
} | ||
|
||
public Encounter getLatestAdmissionOrTransferEncounter() { | ||
List<Encounter> encounters = getAdmissionAndTransferEncounters(); | ||
return encounters.isEmpty() ? null : encounters.get(encounters.size() - 1); | ||
} | ||
|
||
public Encounter getLatestAdtEncounter() { | ||
List<Encounter> encounters = getAdtEncounters(); | ||
return encounters.isEmpty() ? null : encounters.get(encounters.size() - 1); | ||
} | ||
|
||
public Location getCurrentInpatientLocation() { | ||
if (isDischarged()) { | ||
return null; | ||
} | ||
Encounter encounter = getLatestAdmissionOrTransferEncounter(); | ||
return encounter == null ? null : encounter.getLocation(); | ||
} | ||
|
||
public Encounter getEncounterAssigningToCurrentInpatientLocation() { | ||
Location location = getCurrentInpatientLocation(); | ||
if (location == null) { | ||
return null; | ||
} | ||
List<Encounter> encounters = getAdmissionAndTransferEncounters(); | ||
if (encounters.isEmpty()) { | ||
return null; | ||
} | ||
Encounter ret = encounters.get(encounters.size() - 1); | ||
if (!ret.getLocation().equals(location)) { // Sanity check, this should not happen | ||
return null; | ||
} | ||
for (int i=encounters.size()-2; i>=0; i--) { | ||
Encounter e = encounters.get(i); | ||
if (e.getLocation().equals(location)) { | ||
ret = e; | ||
} | ||
else { | ||
return ret; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
public boolean isDischarged() { | ||
if (dischargeEncounters.isEmpty()) { | ||
return false; | ||
} | ||
return dischargeEncounters.contains(getLatestAdtEncounter()); | ||
} | ||
|
||
private Comparator<Encounter> getEncounterComparator() { | ||
return Comparator.comparing(Encounter::getEncounterDatetime) | ||
.thenComparing(Encounter::getDateCreated) | ||
.thenComparing(Encounter::getEncounterId); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/src/main/java/org/openmrs/module/emrapi/adt/InpatientAdmissionSearchCriteria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.openmrs.module.emrapi.adt; | ||
|
||
import lombok.Data; | ||
import org.openmrs.Location; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Represents criteria for searching for InpatientAdmissions | ||
*/ | ||
@Data | ||
public class InpatientAdmissionSearchCriteria { | ||
|
||
private Location visitLocation; | ||
private List<Location> currentInpatientLocations; | ||
private boolean includeDischarged = false; | ||
private List<Integer> patientIds; | ||
private List<Integer> visitIds; | ||
|
||
public void addCurrentInpatientLocation(Location location) { | ||
if (currentInpatientLocations == null) { | ||
currentInpatientLocations = new ArrayList<>(); | ||
} | ||
currentInpatientLocations.add(location); | ||
} | ||
|
||
public void addPatientId(Integer patientId) { | ||
if (patientIds == null) { | ||
patientIds = new ArrayList<>(); | ||
} | ||
patientIds.add(patientId); | ||
} | ||
|
||
public void addVisitId(Integer visitId) { | ||
if (visitIds == null) { | ||
visitIds = new ArrayList<>(); | ||
} | ||
visitIds.add(visitId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
select | ||
visit, | ||
patient, | ||
encounter | ||
from | ||
Encounter as encounter | ||
inner join encounter.visit as visit | ||
inner join encounter.patient as patient | ||
where encounter.voided = false | ||
and visit.voided = false | ||
and patient.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)) | ||
order by visit.visitId, encounter.encounterDatetime, encounter.encounterId |
File renamed without changes.
Oops, something went wrong.