Search in sources :

Example 21 with VisitDomainWrapper

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Location(org.openmrs.Location) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with VisitDomainWrapper

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Location(org.openmrs.Location) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with VisitDomainWrapper

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Location(org.openmrs.Location) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with VisitDomainWrapper

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));
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) AppContextModel(org.openmrs.module.appframework.context.AppContextModel) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Location(org.openmrs.Location) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with VisitDomainWrapper

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);
            }
        }
    }
}
Also used : AdtService(org.openmrs.module.emrapi.adt.AdtService) VisitService(org.openmrs.api.VisitService) Visit(org.openmrs.Visit) Duration(org.joda.time.Duration) DateTime(org.joda.time.DateTime) LocationTag(org.openmrs.LocationTag) LocationService(org.openmrs.api.LocationService) Disposition(org.openmrs.module.emrapi.disposition.Disposition) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Location(org.openmrs.Location)

Aggregations

VisitDomainWrapper (org.openmrs.module.emrapi.visit.VisitDomainWrapper)47 Location (org.openmrs.Location)31 Test (org.junit.Test)28 Visit (org.openmrs.Visit)20 Date (java.util.Date)19 Patient (org.openmrs.Patient)17 SimpleObject (org.openmrs.ui.framework.SimpleObject)17 AppContextModel (org.openmrs.module.appframework.context.AppContextModel)16 Encounter (org.openmrs.Encounter)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 PihCoreContextSensitiveTest (org.openmrs.module.pihcore.PihCoreContextSensitiveTest)8 DateTime (org.joda.time.DateTime)6 ArrayList (java.util.ArrayList)5 Obs (org.openmrs.Obs)4 PatientContextModel (org.openmrs.module.coreapps.contextmodel.PatientContextModel)4 VisitContextModel (org.openmrs.module.coreapps.contextmodel.VisitContextModel)4 Redirect (org.openmrs.ui.framework.page.Redirect)4 Extension (org.openmrs.module.appframework.domain.Extension)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2