Search in sources :

Example 36 with VisitDomainWrapper

use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-coreapps by openmrs.

the class VisitDetailsFragmentControllerTest method shouldReturnEncounterWithinBoundaries.

@Test
public void shouldReturnEncounterWithinBoundaries() {
    ParseEncounterToJson parseEncounterToJson = mock(ParseEncounterToJson.class);
    when(parseEncounterToJson.createEncounterJSON(any(User.class), any(Encounter.class))).thenReturn(new SimpleObject());
    VisitDomainWrapper visitWrapper = mock(VisitDomainWrapper.class);
    when(visitWrapper.getSortedEncounters()).thenReturn(getTestEncounterList(100));
    VisitDetailsFragmentController controller = new VisitDetailsFragmentController();
    List<SimpleObject> encounters;
    encounters = controller.getEncounterListAsJson(parseEncounterToJson, visitWrapper, new User(), 0, 50);
    assertThat(encounters.size(), is(50));
    encounters = controller.getEncounterListAsJson(parseEncounterToJson, visitWrapper, new User(), 50, 50);
    assertThat(encounters.size(), is(50));
    encounters = controller.getEncounterListAsJson(parseEncounterToJson, visitWrapper, new User(), 0, 200);
    assertThat(encounters.size(), is(100));
    encounters = controller.getEncounterListAsJson(parseEncounterToJson, visitWrapper, new User(), 99, 25);
    assertThat(encounters.size(), is(1));
    encounters = controller.getEncounterListAsJson(parseEncounterToJson, visitWrapper, new User(), 100, 25);
    assertThat(encounters.isEmpty(), is(true));
}
Also used : ParseEncounterToJson(org.openmrs.module.coreapps.parser.ParseEncounterToJson) User(org.openmrs.User) SimpleObject(org.openmrs.ui.framework.SimpleObject) Encounter(org.openmrs.Encounter) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Test(org.junit.Test)

Example 37 with VisitDomainWrapper

use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-coreapps by openmrs.

the class RetrospectiveVisitFragmentController method create.

public Object create(@SpringBean("adtService") AdtService adtService, @RequestParam("patientId") Patient patient, @RequestParam("locationId") Location location, @RequestParam("startDate") Date startDate, @RequestParam(value = "stopDate", required = false) Date stopDate, HttpServletRequest request, UiUtils ui) {
    // if no stop date, set it to start date
    if (stopDate == null) {
        stopDate = startDate;
    }
    // set the startDate time component to the start of day
    startDate = new DateTime(startDate).withTime(0, 0, 0, 0).toDate();
    // if stopDate is today, set stopDate to current datetime, otherwise set time component to end of date
    if (new DateTime().withTime(0, 0, 0, 0).equals(new DateTime(stopDate).withTime(0, 0, 0, 0))) {
        stopDate = new Date();
    } else {
        stopDate = new DateTime(stopDate).withTime(23, 59, 59, 999).toDate();
    }
    try {
        VisitDomainWrapper createdVisit = adtService.createRetrospectiveVisit(patient, location, startDate, stopDate);
        request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("coreapps.retrospectiveVisit.addedVisitMessage"));
        request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
        return SimpleObject.create("success", true, "id", createdVisit.getVisit().getId().toString(), "uuid", createdVisit.getVisit().getUuid());
    } catch (ExistingVisitDuringTimePeriodException e) {
        // if there are existing visit(s), return these existing visits
        List<SimpleObject> simpleVisits = new ArrayList<SimpleObject>();
        List<VisitDomainWrapper> visits = adtService.getVisits(patient, location, startDate, stopDate);
        if (visits != null) {
            for (VisitDomainWrapper visit : visits) {
                simpleVisits.add(SimpleObject.create("startDate", ui.format(new DateTime(visit.getVisit().getStartDatetime()).toDateMidnight().toDate()), "stopDate", ui.format(new DateTime(visit.getVisit().getStopDatetime()).toDateMidnight().toDate()), "id", visit.getVisit().getId(), "uuid", visit.getVisit().getUuid()));
            }
        }
        return simpleVisits;
    } catch (Exception e) {
        log.error("Unable to add retrospective visit", e);
        return new FailureResult(ui.message(e.getMessage()));
    }
}
Also used : SimpleObject(org.openmrs.ui.framework.SimpleObject) FailureResult(org.openmrs.ui.framework.fragment.action.FailureResult) ExistingVisitDuringTimePeriodException(org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException) ArrayList(java.util.ArrayList) List(java.util.List) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) DateTime(org.joda.time.DateTime) Date(java.util.Date) ExistingVisitDuringTimePeriodException(org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException)

Example 38 with VisitDomainWrapper

use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-coreapps by openmrs.

the class VisitDetailsFragmentController method verifyIfUserHasPermissionToDeleteVisit.

private boolean verifyIfUserHasPermissionToDeleteVisit(Visit visit, User authenticatedUser, boolean canDeleteVisit) {
    VisitDomainWrapper visitDomainWrapper = new VisitDomainWrapper(visit);
    if (visitDomainWrapper.hasEncounters()) {
        // allowed to delete only empty visits
        return false;
    }
    boolean userParticipatedInVisit = visitDomainWrapper.verifyIfUserIsTheCreatorOfVisit(authenticatedUser);
    return canDeleteVisit || userParticipatedInVisit;
}
Also used : VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper)

Example 39 with VisitDomainWrapper

use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-coreapps by openmrs.

the class QuickVisitFragmentController method create.

@Transactional
public FragmentActionResult create(@SpringBean("adtService") AdtService adtService, @SpringBean("visitService") VisitService visitService, @RequestParam("patientId") Patient patient, @RequestParam("locationId") Location location, UiUtils uiUtils, @RequestParam(value = "selectedTypeId", required = false) VisitType selectedType, UiSessionContext emrContext, HttpServletRequest request) {
    VisitDomainWrapper activeVisit = adtService.getActiveVisit(patient, location);
    BindingResult bindingResult = null;
    if (activeVisit != null) {
        return new FailureResult(uiUtils.message("coreapps.activeVisits.alreadyExists"));
    }
    Visit visit = adtService.ensureVisit(patient, new Date(), location);
    if (selectedType != null) {
        // set visit type
        visit.setVisitType(selectedType);
        // manually handle the attribute parameters
        List<VisitAttributeType> attributeTypes = visitService.getAllVisitAttributeTypes();
        WebAttributeUtil.handleSubmittedAttributesForType(visit, bindingResult, VisitAttribute.class, request, attributeTypes);
        try {
            visitService.saveVisit(visit);
        } catch (APIException e) {
            return new FailureResult((uiUtils.message("Visit.save.error")));
        }
    }
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, uiUtils.message("coreapps.visit.createQuickVisit.successMessage", uiUtils.encodeHtml(uiUtils.format(patient))));
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
    SimpleObject result = SimpleObject.create("id", visit.getId().toString(), "uuid", visit.getUuid());
    return new ObjectResult(result);
}
Also used : BindingResult(org.springframework.validation.BindingResult) APIException(org.openmrs.api.APIException) SimpleObject(org.openmrs.ui.framework.SimpleObject) FailureResult(org.openmrs.ui.framework.fragment.action.FailureResult) Visit(org.openmrs.Visit) VisitAttributeType(org.openmrs.VisitAttributeType) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) ObjectResult(org.openmrs.ui.framework.fragment.action.ObjectResult) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 40 with VisitDomainWrapper

use of org.openmrs.module.emrapi.visit.VisitDomainWrapper in project openmrs-module-coreapps by openmrs.

the class VisitFragmentController method start.

@Transactional
public FragmentActionResult start(@SpringBean("adtService") AdtService adtService, @RequestParam("patientId") Patient patient, @RequestParam("locationId") Location location, @RequestParam(value = "stopActiveVisit", required = false) Boolean stopActive, UiUtils uiUtils, HttpServletRequest request) {
    VisitDomainWrapper activeVisit = adtService.getActiveVisit(patient, location);
    // if patient has an active visit close it
    if (patient != null && (activeVisit != null)) {
        Visit visit = activeVisit.getVisit();
        if (visit != null && stopActive) {
            adtService.closeAndSaveVisit(visit);
        }
    }
    // create new visit and save it
    adtService.ensureVisit(patient, new Date(), location);
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, uiUtils.message("emr.visit.createQuickVisit.successMessage", uiUtils.format(patient)));
    request.getSession().setAttribute(AppUiConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
    return new SuccessResult();
}
Also used : Visit(org.openmrs.Visit) SuccessResult(org.openmrs.ui.framework.fragment.action.SuccessResult) VisitDomainWrapper(org.openmrs.module.emrapi.visit.VisitDomainWrapper) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

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