Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Reject and cancel status form fhir #43

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .github/workflows/main.yml
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion api/pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>labonfhir</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
</parent>

<artifactId>labonfhir-api</artifactId>
Expand Down
Empty file modified api/src/main/java/org/openmrs/module/labonfhir/FhirConfig.java
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions api/src/main/java/org/openmrs/module/labonfhir/api/event/OrderCreationListener.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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<Bundle> 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<Bundle> 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<Reference> 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;
}
}
87 changes: 75 additions & 12 deletions api/src/main/java/org/openmrs/module/labonfhir/api/scheduler/FetchTaskUpdates.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +80,9 @@ public class FetchTaskUpdates extends AbstractTask implements ApplicationContext
@Autowired
FhirObservationService observationService;

@Autowired
AdministrationService adminService;

@Autowired
OrderService orderService;

Expand Down Expand Up @@ -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<Bundle> taskBundles = new ArrayList<>();
Expand Down Expand Up @@ -158,6 +171,7 @@ public void shutdown() {

private Boolean updateTasksInBundle(List<Bundle> 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;
Expand All @@ -172,19 +186,42 @@ private Boolean updateTasksInBundle(List<Bundle> 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;
}
}
}
Expand All @@ -210,7 +247,7 @@ private void setOrderNumberFromLIS(List<Reference> 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);
}
}
Expand Down Expand Up @@ -285,6 +322,32 @@ private Boolean updateOutput(List<Task.TaskOutputComponent> output, Task openmrs
}
return taskOutPutUpdated;
}
private void setOrderStatus(List<Reference> 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 {
Expand Down
Empty file.
Empty file.
Empty file.
Empty file modified api/src/main/resources/liquibase.xml
100644 → 100755
Empty file.
Empty file modified api/src/main/resources/messages.properties
100644 → 100755
Empty file.
Empty file modified api/src/main/resources/messages_es.properties
100644 → 100755
Empty file.
Empty file modified api/src/main/resources/messages_fr.properties
100644 → 100755
Empty file.
Empty file modified api/src/main/resources/moduleApplicationContext.xml
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified license-format.xml
100644 → 100755
Empty file.
Empty file modified license-header.txt
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion omod/pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>labonfhir</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
</parent>

<artifactId>labonfhir-omod</artifactId>
Expand Down
Empty file.
Empty file modified omod/src/main/resources/config.xml
100644 → 100755
Empty file.
Empty file modified omod/src/main/resources/webModuleApplicationContext.xml
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>org.openmrs.module</groupId>
<artifactId>labonfhir</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.5.3-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Lab on FHIR</name>
<description>A module to support lab order communication between OpenMRS and an LIS like OpenELIS using FHIR-based
Expand Down