diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/api/pom.xml b/api/pom.xml old mode 100644 new mode 100755 index 49410d3..dd882a5 --- a/api/pom.xml +++ b/api/pom.xml @@ -15,7 +15,7 @@ org.openmrs.module labonfhir - 1.4.0-SNAPSHOT + 1.5.3-SNAPSHOT labonfhir-api diff --git a/api/src/main/java/org/openmrs/module/labonfhir/FhirConfig.java b/api/src/main/java/org/openmrs/module/labonfhir/FhirConfig.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/LabOnFhirActivator.java b/api/src/main/java/org/openmrs/module/labonfhir/LabOnFhirActivator.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/LabOnFhirConfig.java b/api/src/main/java/org/openmrs/module/labonfhir/LabOnFhirConfig.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderHandler.java b/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderHandler.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderManager.java b/api/src/main/java/org/openmrs/module/labonfhir/api/LabOrderManager.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/dao/LabOnFhirDao.java b/api/src/main/java/org/openmrs/module/labonfhir/api/dao/LabOnFhirDao.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/event/EncounterCreationListener.java b/api/src/main/java/org/openmrs/module/labonfhir/api/event/EncounterCreationListener.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java b/api/src/main/java/org/openmrs/module/labonfhir/api/event/LabCreationListener.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/event/OrderCreationListener.java b/api/src/main/java/org/openmrs/module/labonfhir/api/event/OrderCreationListener.java old mode 100644 new mode 100755 index e5ce142..00fbc59 --- a/api/src/main/java/org/openmrs/module/labonfhir/api/event/OrderCreationListener.java +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/event/OrderCreationListener.java @@ -49,6 +49,7 @@ public void processMessage(Message message) { Order order; try { order = orderService.getOrderByUuid(uuid); + log.trace("Fetched order {}", order); } catch (APIException e) { log.error("Exception caught while trying to load order {}", uuid, e); diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/fhir/OrderCreationException.java b/api/src/main/java/org/openmrs/module/labonfhir/api/fhir/OrderCreationException.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/model/FailedTask.java b/api/src/main/java/org/openmrs/module/labonfhir/api/model/FailedTask.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/model/TaskRequest.java b/api/src/main/java/org/openmrs/module/labonfhir/api/model/TaskRequest.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskRejected.java b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskRejected.java new file mode 100755 index 0000000..17dee98 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskRejected.java @@ -0,0 +1,215 @@ +package org.openmrs.module.labonfhir.api.scheduler; + +import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +import ca.uhn.fhir.rest.client.api.IGenericClient; +import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import lombok.AccessLevel; +import lombok.Setter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.hibernate.SessionFactory; +import org.hl7.fhir.instance.model.api.IBaseBundle; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.Task; +import org.hl7.fhir.r4.model.codesystems.TaskStatus; +import org.openmrs.Order; +import org.openmrs.api.OrderService; +import org.openmrs.module.fhir2.FhirConstants; +import org.openmrs.module.fhir2.api.FhirObservationService; +import org.openmrs.module.fhir2.api.FhirTaskService; +import org.openmrs.module.fhir2.api.dao.FhirObservationDao; +import org.openmrs.module.fhir2.api.translators.ObservationReferenceTranslator; +import org.openmrs.module.labonfhir.LabOnFhirConfig; +import org.openmrs.module.labonfhir.api.model.TaskRequest; +import org.openmrs.module.labonfhir.api.service.LabOnFhirService; +import org.openmrs.scheduler.tasks.AbstractTask; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.text.SimpleDateFormat; + +@Component +@Setter(AccessLevel.PACKAGE) +public class FetchTaskRejected extends AbstractTask implements ApplicationContextAware { + + private static Log log = LogFactory.getLog(FetchTaskUpdates.class); + + private static ApplicationContext applicationContext; + + private static String LOINC_SYSTEM = "http://loinc.org"; + + @Autowired + private LabOnFhirConfig config; + + @Autowired + @Qualifier("labOrderFhirClient") + private IGenericClient client; + + @Autowired + private FhirTaskService taskService; + + @Autowired + FhirObservationDao observationDao; + + @Autowired + FhirObservationService observationService; + + @Autowired + OrderService orderService; + + @Autowired + ObservationReferenceTranslator observationReferenceTranslator; + + @Autowired + @Qualifier("sessionFactory") + SessionFactory sessionFactory; + + @Autowired + private LabOnFhirService labOnFhirService; + + @Override + public void execute() { + + try { + applicationContext.getAutowireCapableBeanFactory().autowireBean(this); + } + catch (Exception e) { + // return; + } + + if (!config.isLisEnabled()) { + return; + } + + try { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + Date newDate = new Date(); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(newDate); + calendar.add(Calendar.YEAR, -5); + Date fiveYearsAgo = calendar.getTime(); + + TaskRequest lastRequest = labOnFhirService.getLastTaskRequest(); + String lastRequstDate = dateFormat.format(fiveYearsAgo); + if (lastRequest != null) { + lastRequstDate = dateFormat.format(lastRequest.getRequestDate()); + } + + String currentTime = dateFormat.format(newDate); + DateRangeParam lastUpdated = new DateRangeParam().setLowerBound(lastRequstDate).setUpperBound(currentTime); + + // Get List of Tasks that belong to this instance and update them + Bundle taskBundle = client.search().forResource(Task.class) + .where(Task.IDENTIFIER.hasSystemWithAnyCode(FhirConstants.OPENMRS_FHIR_EXT_TASK_IDENTIFIER)) + .where(Task.STATUS.exactly().code(TaskStatus.REJECTED.toCode())).lastUpdated(lastUpdated) + .returnBundle(Bundle.class).execute(); + + List taskBundles = new ArrayList<>(); + taskBundles.add(taskBundle); + //Support FHIR Server Pagination + while (taskBundle.getLink(IBaseBundle.LINK_NEXT) != null) { + taskBundle = client.loadPage().next(taskBundle).execute(); + taskBundles.add(taskBundle); + } + Boolean tasksUpdated = updateTasksInBundle(taskBundles); + if (tasksUpdated) { + TaskRequest request = new TaskRequest(); + request.setRequestDate(newDate); + labOnFhirService.saveOrUpdateTaskRequest(request); + } + } + catch (Exception e) { + log.error("ERROR executing FetchTaskUpdates : " + e.toString() + getStackTrace(e)); + } + super.startExecuting(); + } + + @Override + public void shutdown() { + log.debug("shutting down FetchTaskUpdates Task"); + this.stopExecuting(); + } + + private Boolean updateTasksInBundle(List taskBundles) { + Boolean tasksUpdated = false; + for (Bundle bundle : taskBundles) { + for (Iterator tasks = bundle.getEntry().iterator(); tasks.hasNext();) { + String openmrsTaskUuid = null; + try { + Task openelisTask = (Task) ((Bundle.BundleEntryComponent) tasks.next()).getResource(); + openmrsTaskUuid = openelisTask.getIdentifierFirstRep().getValue(); + Task openmrsTask = taskService.get(openmrsTaskUuid); + if (openmrsTask != null) { + System.out.println("TASK-STAUS :"+ openelisTask.getStatus().toString()); + System.out.println("TASK-STAUS-2 :"+ TaskStatus.REJECTED.toString()); + System.out.println("OPEN-LIS :"+ TaskStatus.REJECTED.toString()); + + System.out.println("OPEN-TASK-COMPARE :"+ openelisTask.getStatus().toString().equals(TaskStatus.REJECTED.toString())); + if(openelisTask.getStatus().toString().equals(TaskStatus.REJECTED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + System.out.println("BaseOn" + openmrsTask.getBasedOn()); + System.out.println("START :"); + System.out.println("BASE-SIZE :"+ openelisTask.getBasedOn().size()); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode()); + System.out.println("END :"); + tasksUpdated = true; + } + } + } + catch (Exception e) { + log.error("Could not save task " + openmrsTaskUuid + ":" + e.toString() + getStackTrace(e)); + } + } + } + return tasksUpdated; + } + + private void setOrderStatus(List basedOn, String string) { + basedOn.forEach(ref -> { + if (ref.hasReferenceElement()) { + System.out.println("concidtion: 1"); + IIdType referenceElement = ref.getReferenceElement(); + if ("ServiceRequest".equals(referenceElement.getResourceType())) { + System.out.println("concidtion: 2"); + String serviceRequestUuid = referenceElement.getIdPart(); + try { + + Order order = orderService.getOrderByUuid(serviceRequestUuid); + if (order != null) { + System.out.println("concidtion: 3"); + String commentText = "Update Order with Accesion Number From SIGDEP"; + System.out.println("update order"); + String accessionNumber = ""; + orderService.updateOrderFulfillerStatus(order, Order.FulfillerStatus.EXCEPTION, + commentText, accessionNumber); + } + + } + catch (ResourceNotFoundException e) { + log.error( + "Could not Fetch ServiceRequest/" + serviceRequestUuid + ":" + e.toString() + getStackTrace(e)); + } + } + } + }); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java old mode 100644 new mode 100755 index dcd14b4..f0dc232 --- a/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java +++ b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java @@ -29,6 +29,8 @@ import org.hl7.fhir.r4.model.Task; import org.hl7.fhir.r4.model.codesystems.TaskStatus; import org.openmrs.Order; +import org.openmrs.Order.FulfillerStatus; +import org.openmrs.api.AdministrationService; import org.openmrs.api.OrderService; import org.openmrs.module.fhir2.FhirConstants; import org.openmrs.module.fhir2.api.FhirDiagnosticReportService; @@ -78,6 +80,9 @@ public class FetchTaskUpdates extends AbstractTask implements ApplicationContext @Autowired FhirObservationService observationService; + @Autowired + AdministrationService adminService; + @Autowired OrderService orderService; @@ -119,13 +124,21 @@ public void execute() { lastRequstDate = dateFormat.format(lastRequest.getRequestDate()); } + String practitionerId = adminService.getGlobalProperty("labonfhir.lisUserUuid"); + String currentTime = dateFormat.format(newDate); DateRangeParam lastUpdated = new DateRangeParam().setLowerBound(lastRequstDate).setUpperBound(currentTime); // Get List of Tasks that belong to this instance and update them Bundle taskBundle = client.search().forResource(Task.class) .where(Task.IDENTIFIER.hasSystemWithAnyCode(FhirConstants.OPENMRS_FHIR_EXT_TASK_IDENTIFIER)) - .where(Task.STATUS.exactly().code(TaskStatus.COMPLETED.toCode())).lastUpdated(lastUpdated) + .where(Task.OWNER.hasId(practitionerId)) + .where(Task.STATUS.exactly().codes( + TaskStatus.COMPLETED.toCode(), + TaskStatus.REQUESTED.toCode(), + TaskStatus.ACCEPTED.toCode(), + TaskStatus.REJECTED.toCode(), + TaskStatus.CANCELLED.toCode())).lastUpdated(lastUpdated) .returnBundle(Bundle.class).execute(); List taskBundles = new ArrayList<>(); @@ -158,6 +171,7 @@ public void shutdown() { private Boolean updateTasksInBundle(List taskBundles) { Boolean tasksUpdated = false; + String commentText = "Update Order with remote fhir status :)"; for (Bundle bundle : taskBundles) { for (Iterator tasks = bundle.getEntry().iterator(); tasks.hasNext();) { String openmrsTaskUuid = null; @@ -172,19 +186,42 @@ private Boolean updateTasksInBundle(List taskBundles) { // Only update if matching OpenMRS Task found if (openmrsTask != null) { // Handle status - openmrsTask.setStatus(openelisTask.getStatus()); + if (openelisTask.getStatus().toString().equals(TaskStatus.COMPLETED.toString())) { + + openmrsTask.setStatus(openelisTask.getStatus()); - Boolean taskOutPutUpdated = false; - if(openmrsTask.hasBasedOn()){ - setOrderNumberFromLIS(openmrsTask.getBasedOn()); + Boolean taskOutPutUpdated = false; + if(openmrsTask.hasBasedOn()){ + setOrderNumberFromLIS(openmrsTask.getBasedOn()); + } + if (openelisTask.hasOutput()) { + // openmrsTask.setOutput(openelisTask.getOutput()); + taskOutPutUpdated = updateOutput(openelisTask.getOutput(), openmrsTask); + } + if (taskOutPutUpdated) { + taskService.update(openmrsTaskUuid, openmrsTask); + tasksUpdated = taskOutPutUpdated; + } + } + + if(openelisTask.getStatus().toString().equals(TaskStatus.REJECTED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + commentText = openelisTask.getStatusReason().getText().toString().isEmpty() ? commentText: openelisTask.getStatusReason().getText(); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.EXCEPTION, commentText); + tasksUpdated = true; } - if (openelisTask.hasOutput()) { - // openmrsTask.setOutput(openelisTask.getOutput()); - taskOutPutUpdated = updateOutput(openelisTask.getOutput(), openmrsTask); + + if(openelisTask.getStatus().toString().equals(TaskStatus.CANCELLED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + commentText = TaskStatus.CANCELLED.toString(); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.EXCEPTION, commentText); + tasksUpdated = true; } - if (taskOutPutUpdated) { - taskService.update(openmrsTaskUuid, openmrsTask); - tasksUpdated = taskOutPutUpdated; + + if(openelisTask.getStatus().toString().equals(TaskStatus.REQUESTED.toString()) || openelisTask.getStatus().toString().equals(TaskStatus.ACCEPTED.toString()) ){ + openmrsTask.setStatus(openelisTask.getStatus()); + setOrderStatus(openmrsTask.getBasedOn(), openelisTask.getStatus().toCode(), Order.FulfillerStatus.RECEIVED, commentText); + tasksUpdated = true; } } } @@ -210,7 +247,7 @@ private void setOrderNumberFromLIS(List basedOn) { if (order != null) { String commentText = "Update Order with Accesion Number From LIS"; String accessionNumber = serviceRequest.getRequisition().getValue(); - orderService.updateOrderFulfillerStatus(order, Order.FulfillerStatus.IN_PROGRESS, + orderService.updateOrderFulfillerStatus(order, Order.FulfillerStatus.COMPLETED, commentText, accessionNumber); } } @@ -285,6 +322,32 @@ private Boolean updateOutput(List output, Task openmrs } return taskOutPutUpdated; } + private void setOrderStatus(List basedOn, String string, FulfillerStatus fulfillerStatus, String commentText) { + basedOn.forEach(ref -> { + if (ref.hasReferenceElement()) { + System.out.println("concidtion: 1"); + IIdType referenceElement = ref.getReferenceElement(); + if ("ServiceRequest".equals(referenceElement.getResourceType())) { + System.out.println("concidtion: 2"); + String serviceRequestUuid = referenceElement.getIdPart(); + try { + + Order order = orderService.getOrderByUuid(serviceRequestUuid); + if (order != null) { + String accessionNumber = ""; + orderService.updateOrderFulfillerStatus(order, fulfillerStatus, + commentText, accessionNumber); + } + + } + catch (ResourceNotFoundException e) { + log.error( + "Could not Fetch ServiceRequest/" + serviceRequestUuid + ":" + e.toString() + getStackTrace(e)); + } + } + } + }); + } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/RetryFailedTasks.java b/api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/RetryFailedTasks.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/service/LabOnFhirService.java b/api/src/main/java/org/openmrs/module/labonfhir/api/service/LabOnFhirService.java old mode 100644 new mode 100755 diff --git a/api/src/main/java/org/openmrs/module/labonfhir/api/service/impl/LabOnFhirServiceImpl.java b/api/src/main/java/org/openmrs/module/labonfhir/api/service/impl/LabOnFhirServiceImpl.java old mode 100644 new mode 100755 diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml old mode 100644 new mode 100755 diff --git a/api/src/main/resources/messages.properties b/api/src/main/resources/messages.properties old mode 100644 new mode 100755 diff --git a/api/src/main/resources/messages_es.properties b/api/src/main/resources/messages_es.properties old mode 100644 new mode 100755 diff --git a/api/src/main/resources/messages_fr.properties b/api/src/main/resources/messages_fr.properties old mode 100644 new mode 100755 diff --git a/api/src/main/resources/moduleApplicationContext.xml b/api/src/main/resources/moduleApplicationContext.xml old mode 100644 new mode 100755 diff --git a/api/src/test/java/org/openmrs/module/labonfhir/api/LabOrderHandlerTest.java b/api/src/test/java/org/openmrs/module/labonfhir/api/LabOrderHandlerTest.java old mode 100644 new mode 100755 diff --git a/api/src/test/java/org/openmrs/module/labonfhir/api/event/EncounterCreationListenerTest.java b/api/src/test/java/org/openmrs/module/labonfhir/api/event/EncounterCreationListenerTest.java old mode 100644 new mode 100755 diff --git a/api/src/test/java/org/openmrs/module/labonfhir/api/fhir/ServiceRequestDaoImplTest.java b/api/src/test/java/org/openmrs/module/labonfhir/api/fhir/ServiceRequestDaoImplTest.java old mode 100644 new mode 100755 diff --git a/api/src/test/java/org/openmrs/module/labonfhir/api/fhir/ServiceRequestTranslatorImplTest.java b/api/src/test/java/org/openmrs/module/labonfhir/api/fhir/ServiceRequestTranslatorImplTest.java old mode 100644 new mode 100755 diff --git a/api/src/test/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdatesTest.java b/api/src/test/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdatesTest.java old mode 100644 new mode 100755 diff --git a/license-format.xml b/license-format.xml old mode 100644 new mode 100755 diff --git a/license-header.txt b/license-header.txt old mode 100644 new mode 100755 diff --git a/omod/pom.xml b/omod/pom.xml old mode 100644 new mode 100755 index 750b938..3f3f5ff --- a/omod/pom.xml +++ b/omod/pom.xml @@ -3,7 +3,7 @@ org.openmrs.module labonfhir - 1.4.0-SNAPSHOT + 1.5.3-SNAPSHOT labonfhir-omod diff --git a/omod/src/main/java/org/openmrs/module/labonfhir/web/resources/FailedTaskResource.java b/omod/src/main/java/org/openmrs/module/labonfhir/web/resources/FailedTaskResource.java old mode 100644 new mode 100755 diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml old mode 100644 new mode 100755 diff --git a/omod/src/main/resources/webModuleApplicationContext.xml b/omod/src/main/resources/webModuleApplicationContext.xml old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 index e8519eb..642bbe4 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ org.openmrs.module labonfhir - 1.4.0-SNAPSHOT + 1.5.3-SNAPSHOT pom Lab on FHIR A module to support lab order communication between OpenMRS and an LIS like OpenELIS using FHIR-based