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

Ea 184 #228

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
Expand Up @@ -22,6 +22,7 @@
import org.openmrs.Provider;
import org.openmrs.Visit;
import org.openmrs.api.OpenmrsService;
import org.openmrs.module.emrapi.adt.domain.AdtVisit;
import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException;
import org.openmrs.module.emrapi.merge.PatientMergeAction;
import org.openmrs.module.emrapi.merge.VisitMergeAction;
Expand Down Expand Up @@ -158,6 +159,8 @@ Encounter checkInPatient(Patient patient, Location where, Provider checkInClerk,
*/
List<VisitDomainWrapper> getActiveVisits(Location location);

List<AdtVisit> getAdtEventsForActiveVisits(Location location);

/**
* If any currently-open visits are now inactive per our business logic, close them
*/
Expand Down
103 changes: 103 additions & 0 deletions api/src/main/java/org/openmrs/module/emrapi/adt/AdtServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

package org.openmrs.module.emrapi.adt;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.time.DateUtils;
import org.joda.time.DateTime;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.EncounterRole;
import org.openmrs.EncounterType;
Expand All @@ -40,8 +42,14 @@
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.emrapi.EmrApiConstants;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.adt.domain.AdtEvent;
import org.openmrs.module.emrapi.adt.domain.AdtEventType;
import org.openmrs.module.emrapi.adt.domain.AdtVisit;
import org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException;
import org.openmrs.module.emrapi.disposition.Disposition;
import org.openmrs.module.emrapi.disposition.DispositionDescriptor;
import org.openmrs.module.emrapi.disposition.DispositionService;
import org.openmrs.module.emrapi.disposition.DispositionType;
import org.openmrs.module.emrapi.domainwrapper.DomainWrapperFactory;
import org.openmrs.module.emrapi.merge.PatientMergeAction;
import org.openmrs.module.emrapi.merge.VisitMergeAction;
Expand Down Expand Up @@ -81,6 +89,8 @@ public class AdtServiceImpl extends BaseOpenmrsService implements AdtService {

private LocationService locationService;

private DispositionService dispositionService;

private DomainWrapperFactory domainWrapperFactory;

private List<PatientMergeAction> patientMergeActions;
Expand Down Expand Up @@ -111,6 +121,10 @@ public void setProviderService(ProviderService providerService) {
this.providerService = providerService;
}

public void setDispositionService(DispositionService dispositionService) {
this.dispositionService = dispositionService;
}

public void setDomainWrapperFactory(DomainWrapperFactory domainWrapperFactory) {
this.domainWrapperFactory = domainWrapperFactory;
}
Expand Down Expand Up @@ -492,6 +506,95 @@ public List<VisitDomainWrapper> getActiveVisits(Location location) {
return active;
}

@Override
@Transactional(readOnly = true)
public List<AdtVisit> getAdtEventsForActiveVisits(Location location) {
List<AdtVisit> ret = new ArrayList<>();
Set<Location> locations = null;
if (location != null) {
locations = getChildLocationsRecursively(location, null);
}
List<Visit> visits = visitService.getVisits(null, null, locations, null, null, null, null, null, null, false, false);
for (Visit visit : visits) {
if (itBelongsToARealPatient(visit)) {
if (BooleanUtils.isNotTrue(visit.getPatient().getDead())) {
ret.add(getAdtVisitEvents(visit));
}
}
}
return ret;
}

public boolean isAdmissionEncounter(Encounter encounter) {
return encounter.getEncounterType().equals(emrApiProperties.getAdmissionEncounterType());
}

public boolean isTransferEncounter(Encounter encounter) {
return encounter.getEncounterType().equals(emrApiProperties.getTransferWithinHospitalEncounterType());
}

public boolean isDischargeEncounter(Encounter encounter) {
return encounter.getEncounterType().equals(emrApiProperties.getExitFromInpatientEncounterType());
}

public AdtVisit getAdtVisitEvents(Visit visit) {
AdtVisit adtVisit = new AdtVisit(visit);
DispositionDescriptor dispositionDescriptor = null;
if (dispositionService.dispositionsSupported()) {
dispositionDescriptor = dispositionService.getDispositionDescriptor();
}
Concept admissionDecision = emrApiProperties.getAdmissionDecisionConcept();
Concept admissionDeny = emrApiProperties.getDenyAdmissionConcept();

for (Encounter e : visit.getNonVoidedEncounters()) {
if (isAdmissionEncounter(e)) {
adtVisit.addEvent(new AdtEvent(AdtEventType.ADMISSION, e));
}
else if (isTransferEncounter(e)) {
adtVisit.addEvent(new AdtEvent(AdtEventType.TRANSFER, e));
}
else if (isDischargeEncounter(e)) {
adtVisit.addEvent(new AdtEvent(AdtEventType.DISCHARGE, e));
}
else {
for (Obs obs : e.getAllObs(false)) {
if (dispositionDescriptor != null) {
if (dispositionDescriptor.isDisposition(obs)) {
Disposition disposition = dispositionService.getDispositionFromObsGroup(obs);
if (disposition.getType() == DispositionType.ADMIT) {
Location location = dispositionDescriptor.getAdmissionLocation(obs, locationService);
adtVisit.addEvent(new AdtEvent(AdtEventType.ADMISSION_REQUEST, e, obs, location));
} else if (disposition.getType() == DispositionType.TRANSFER) {
Location location = dispositionDescriptor.getInternalTransferLocation(obs, locationService);
adtVisit.addEvent(new AdtEvent(AdtEventType.TRANSFER_REQUEST, e, obs, location));
} else if (disposition.getType() == DispositionType.DISCHARGE) {
Location location = e.getLocation();
adtVisit.addEvent(new AdtEvent(AdtEventType.DISCHARGE_REQUEST, e, obs, location));
}
}
}
if (obs.getConcept().equals(admissionDecision)) {
if (obs.getValueCoded().equals(admissionDeny)) {
adtVisit.addEvent(new AdtEvent(AdtEventType.ADMISSION_DECISION_DENY, e));
}
else {
if (dispositionDescriptor != null) {
Disposition disposition = dispositionService.getDispositionFromObs(obs);
if (disposition != null) {
if (disposition.getType() == DispositionType.ADMIT) {
adtVisit.addEvent(new AdtEvent(AdtEventType.ADMISSION_DECISION_ADMIT, e));
}
}
}
}
}
}
}
}
adtVisit.getAdtEvents().sort(Comparator.comparing(e -> e.getEventEncounter().getEncounterDatetime()));
return adtVisit;
}

@Override
public List<VisitDomainWrapper> getInpatientVisits(Location visitLocation, Location ward) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.emrapi.adt.domain;

import org.openmrs.Encounter;
import org.openmrs.Location;
import org.openmrs.Obs;

public class AdtEvent {

private AdtEventType eventType;
private Encounter eventEncounter;
private Obs dispositionObsGroup;
private Location ward;

public AdtEvent() {}

public AdtEvent(AdtEventType eventType, Encounter encounter) {
this.eventType = eventType;
this.eventEncounter = encounter;
this.ward = encounter.getLocation();
}

public AdtEvent(AdtEventType eventType, Encounter encounter, Obs dispositionObsGroup, Location ward) {
this.eventType = eventType;
this.eventEncounter = encounter;
this.dispositionObsGroup = dispositionObsGroup;
this.ward = ward;
}

public AdtEventType getEventType() {
return eventType;
}

public void setEventType(AdtEventType eventType) {
this.eventType = eventType;
}

public Encounter getEventEncounter() {
return eventEncounter;
}

public void setEventEncounter(Encounter eventEncounter) {
this.eventEncounter = eventEncounter;
}

public Obs getDispositionObsGroup() {
return dispositionObsGroup;
}

public void setDispositionObsGroup(Obs dispositionObsGroup) {
this.dispositionObsGroup = dispositionObsGroup;
}

public Location getWard() {
return ward;
}

public void setWard(Location ward) {
this.ward = ward;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.emrapi.adt.domain;

public enum AdtEventType {
ADMISSION_REQUEST,
ADMISSION_DECISION_DENY,
ADMISSION_DECISION_ADMIT,
ADMISSION,
TRANSFER_REQUEST,
TRANSFER,
DISCHARGE_REQUEST,
DISCHARGE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.emrapi.adt.domain;

import org.openmrs.Visit;

import java.util.ArrayList;
import java.util.List;

public class AdtVisit {

private final Visit visit;
private final List<AdtEvent> adtEvents = new ArrayList<>();

public AdtVisit(Visit visit) {
this.visit = visit;
}

public AdtEvent getLastEvent() {
return (!adtEvents.isEmpty() ? adtEvents.get(adtEvents.size() - 1) : null);
}

public boolean hasLatestEventWithType(AdtEventType type) {
AdtEvent lastEvent = getLastEvent();
return lastEvent != null && lastEvent.getEventType() == type;
}

public Visit getVisit() {
return visit;
}

public List<AdtEvent> getAdtEvents() {
return adtEvents;
}

public void addEvent(AdtEvent event) {
getAdtEvents().add(event);
}
}
Loading