From 15c845c98af5264b960982e6b04448703800bd12 Mon Sep 17 00:00:00 2001 From: ojwanganto Date: Wed, 23 Oct 2024 17:27:33 +0300 Subject: [PATCH] Close only outpatient visits which have been active for more than 8 hours --- .../task/AutoCloseActiveVisitsTask.java | 83 ++++++++++--------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/kenyaemr/task/AutoCloseActiveVisitsTask.java b/api/src/main/java/org/openmrs/module/kenyaemr/task/AutoCloseActiveVisitsTask.java index aacd47367..2e8e53139 100644 --- a/api/src/main/java/org/openmrs/module/kenyaemr/task/AutoCloseActiveVisitsTask.java +++ b/api/src/main/java/org/openmrs/module/kenyaemr/task/AutoCloseActiveVisitsTask.java @@ -9,7 +9,6 @@ */ package org.openmrs.module.kenyaemr.task; -import org.openmrs.Patient; import org.openmrs.Visit; import org.openmrs.api.VisitService; import org.openmrs.api.context.Context; @@ -18,53 +17,63 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; import java.util.List; /** * A scheduled task that automatically closes all unvoided active visits - * The focus should only be on the OUTPATIENT visit type + * The focus should only be on the OUTPATIENT visit type with visit duration (checkin to now) > 8 hours * Set date_stopped to 11 23:59:59 of the date_started */ public class AutoCloseActiveVisitsTask extends AbstractTask { - - private static final Logger log = LoggerFactory.getLogger(AutoCloseActiveVisitsTask.class); - - /** - * @see AbstractTask#execute() - */ - @Override - public void execute() { - if (!isExecuting) { - if (log.isDebugEnabled()) { - log.debug("Starting Auto Close Visits Task..."); - } - String openVisitsQuery = "select visit_id from visit v\n" + + private static final Logger log = LoggerFactory.getLogger(AutoCloseActiveVisitsTask.class); + + /** + * @see AbstractTask#execute() + */ + @Override + public void execute() { + if (!isExecuting) { + if (log.isDebugEnabled()) { + log.debug("Starting Auto Close Visits Task..."); + } + System.out.println("Starting the close visits task"); + + String openVisitsQuery = "select visit_id, TIMESTAMPDIFF(HOUR, v.date_started, now()) as visit_duration from visit v\n" + "inner join visit_type vt on vt.visit_type_id = v.visit_type_id and vt.uuid ='3371a4d4-f66f-4454-a86d-92c7b3da990c'\n" + - "where v.date_stopped is null"; + "where v.date_stopped is null having visit_duration > 8;"; + + startExecuting(); + VisitService visitService = Context.getVisitService(); + List> visits = Context.getAdministrationService().executeSQL(openVisitsQuery, true); - startExecuting(); - VisitService visitService = Context.getVisitService(); + if (visits.size() > 0) { + try { + int counter = 0; + for (List ls : visits) { + if (ls.get(0) != null) { + Integer visitId = (Integer) ls.get(0); + Visit visit = visitService.getVisit(visitId); + visit.setStopDatetime(OpenmrsUtil.getLastMomentOfDay(visit.getStartDatetime())); + visitService.saveVisit(visit); + counter++; - List> visits = Context.getAdministrationService().executeSQL(openVisitsQuery, true); - if (visits.size() > 0) { - for (List ls : visits) { - try { - if (ls.get(0) != null) { - Integer visitId = (Integer) ls.get(0); - Visit visit = visitService.getVisit(visitId); - visit.setStopDatetime(OpenmrsUtil.getLastMomentOfDay(visit.getStartDatetime())); - visitService.saveVisit(visit); - } + if ((counter % 400) == 0) { + Context.flushSession(); + Context.clearSession(); + counter = 0; + System.out.println("AutoCloseVisitTask: Flushing session...."); + } + } + } + } catch (Exception e) { + log.error("Error while auto closing visits:", e); + } finally { + stopExecuting(); + System.out.println("Completed the close visits task"); - } catch (Exception e) { - log.error("Error while auto closing visits:", e); - } finally { - stopExecuting(); - } } - } - } - } + } + } + } }