Search in sources :

Example 1 with ExistingVisitDuringTimePeriodException

use of org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException 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)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 ExistingVisitDuringTimePeriodException (org.openmrs.module.emrapi.adt.exception.ExistingVisitDuringTimePeriodException)1 VisitDomainWrapper (org.openmrs.module.emrapi.visit.VisitDomainWrapper)1 SimpleObject (org.openmrs.ui.framework.SimpleObject)1 FailureResult (org.openmrs.ui.framework.fragment.action.FailureResult)1