use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-pihcore by PIH.
the class RequireUtilTest method shouldReturnFalseIfLocationDOesNotHaveTagAndVisitNotActive.
@Test
public void shouldReturnFalseIfLocationDOesNotHaveTagAndVisitNotActive() {
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", "Check-In Location");
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
VisitDomainWrapper visit = mock(VisitDomainWrapper.class);
when(visit.isActive()).thenReturn(false);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
appContextModel.put("visit", visit);
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(and(sessionLocationHasTag("Admission Location"), patientHasActiveVisit())), appContextModel), is(false));
}
use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-pihcore by PIH.
the class RequireUtilTest method shouldReturnFalseIfLocationDoesNotHasTagAndVisitActive.
@Test
public void shouldReturnFalseIfLocationDoesNotHasTagAndVisitActive() {
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", "Check-In Location");
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
VisitDomainWrapper visit = mock(VisitDomainWrapper.class);
when(visit.isActive()).thenReturn(true);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
appContextModel.put("visit", visit);
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(and(sessionLocationHasTag("Admission Location"), patientHasActiveVisit())), appContextModel), is(false));
}
use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-pihcore by PIH.
the class RequireUtilTest method shouldReturnFalseIfLocationHasTagButVisitNotActive.
@Test
public void shouldReturnFalseIfLocationHasTagButVisitNotActive() {
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", "Admission Location");
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
VisitDomainWrapper visit = mock(VisitDomainWrapper.class);
when(visit.isActive()).thenReturn(false);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
appContextModel.put("visit", visit);
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(and(sessionLocationHasTag("Admission Location"), patientHasActiveVisit())), appContextModel), is(false));
}
use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-pihcore by PIH.
the class RequireUtilTest method shouldReturnTrueIfLocationHasTagAndVisitActive.
@Test
public void shouldReturnTrueIfLocationHasTagAndVisitActive() {
Location sessionLocation = new Location();
SimpleObject sessionLocationRestRep = new SimpleObject();
sessionLocationRestRep.put("uuid", "123abc");
SimpleObject admitTag = new SimpleObject();
admitTag.put("display", "Admission Location");
sessionLocationRestRep.put("tags", Arrays.asList(admitTag));
PowerMockito.mockStatic(ConversionUtil.class);
when(ConversionUtil.convertToRepresentation(sessionLocation, Representation.DEFAULT)).thenReturn(sessionLocationRestRep);
VisitDomainWrapper visit = mock(VisitDomainWrapper.class);
when(visit.isActive()).thenReturn(true);
uiSessionContext.setSessionLocation(sessionLocation);
AppContextModel appContextModel = uiSessionContext.generateAppContextModel();
appContextModel.put("visit", visit);
assertThat(appFrameworkService.checkRequireExpression(extensionRequiring(and(sessionLocationHasTag("Admission Location"), patientHasActiveVisit())), appContextModel), is(true));
}
use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-pihcore by PIH.
the class PihCloseStaleVisitsTask method run.
@Override
public void run() {
log.info("Executing " + getClass());
AdtService adtService = Context.getService(AdtService.class);
VisitService visitService = Context.getVisitService();
LocationService locationService = Context.getLocationService();
LocationTag visitLocationTag = locationService.getLocationTagByName(EmrApiConstants.LOCATION_TAG_SUPPORTS_VISITS);
List<Location> locations = locationService.getLocationsByTag(visitLocationTag);
List<Visit> openVisits = visitService.getVisits(null, null, locations, null, null, null, null, null, null, false, false);
for (Visit visit : openVisits) {
VisitDomainWrapper wrappedVisit = adtService.wrap(visit);
Boolean changedOrUpdatedRecently = changedOrUpdatedRecently(visit, REGULAR_VISIT_EXPIRE_TIME_IN_HOURS);
Disposition mostRecentDisposition = wrappedVisit.getMostRecentDisposition();
Long hoursSinceLastEncounter = wrappedVisit.getMostRecentEncounter() != null ? new Duration(new DateTime(wrappedVisit.getMostRecentEncounter().getEncounterDatetime()), new DateTime()).getStandardHours() : null;
// **this logic is intentionally verbose, since it can be difficult to follow**
Boolean closeVisit = false;
// if the patient has been discharged, close visit
if (wrappedVisit.hasBeenDischarged()) {
closeVisit = true;
} else // if the patient has been admitted, or is awaiting admission, don't close the visit
if (wrappedVisit.isAdmitted() || wrappedVisit.isAwaitingAdmission()) {
closeVisit = false;
} else // otherwise, branch based on whether this is a "regular" visits, or a "ED Visit", with our special logic
if (!isEDVisit(wrappedVisit)) {
// if the disposition is one that "keeps a visit open" ("ED Observation" and "Still hospitalized") keep the visit open
if (mostRecentDisposition != null && mostRecentDisposition.getKeepsVisitOpen() != null && mostRecentDisposition.getKeepsVisitOpen()) {
closeVisit = false;
} else // otherwise, close the visit if there are no encounter, or no encounters in the last 12 hours, and the visit hasn't been updated in the last 12 hours
if ((hoursSinceLastEncounter == null || hoursSinceLastEncounter > REGULAR_VISIT_EXPIRE_TIME_IN_HOURS) && !changedOrUpdatedRecently) {
closeVisit = true;
} else // otherwise, don't close
{
closeVisit = false;
}
} else {
// if the most recent disposition is "discharge" and there are no encouters in the last 12 hours, and visit hasn't been updated in the last 12 hours, close
if (mostRecentDisposition != null && mostRecentDisposition.getType() != null && mostRecentDisposition.getType().equals(DispositionType.DISCHARGE) && (hoursSinceLastEncounter == null || hoursSinceLastEncounter > REGULAR_VISIT_EXPIRE_TIME_IN_HOURS) && !changedOrUpdatedRecently) {
closeVisit = true;
} else // if the most recent disposition is one that "keeps a visit open" ("ED Observation" and "Still hospitalized") and there are no encounters in the last 7 days, and visit hasn't been updated in the last 12 hours, close
if (mostRecentDisposition != null && mostRecentDisposition.getKeepsVisitOpen() != null && mostRecentDisposition.getKeepsVisitOpen() && (hoursSinceLastEncounter == null || hoursSinceLastEncounter > ED_VISIT_EXPIRE_TIME_IN_HOURS) && !changedOrUpdatedRecently) {
closeVisit = true;
} else // otherwise, if there are no encounters in the last 30 days, and visit hasn't been updated in the last 12 hours, close
if ((hoursSinceLastEncounter == null || hoursSinceLastEncounter > ED_VISIT_EXPIRE_VERY_OLD_TIME_IN_HOURS) && !changedOrUpdatedRecently) {
closeVisit = true;
} else // otherwise, don't close
{
closeVisit = false;
}
}
if (closeVisit) {
try {
adtService.closeAndSaveVisit(visit);
} catch (Exception ex) {
log.warn("Failed to close inactive visit " + visit, ex);
}
}
}
}
Aggregations