Search in sources :

Example 1 with TasksPollingInfo

use of org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TasksPollingInfo in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskPollingService method updateTasks.

public void updateTasks() {
    log.info("Updating tasks from CP Organizations...");
    Bundle tasksBundle = openEhrClient.search().forResource(Task.class).include(Task.INCLUDE_FOCUS).include(Task.INCLUDE_OWNER).include(Organization.INCLUDE_ENDPOINT.setRecurse(true)).where(new TokenClientParam("owner:Organization.type").exactly().systemAndCode(OrganizationTypeCode.CP.getSystem(), OrganizationTypeCode.CP.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.FAILED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.REJECTED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.COMPLETED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.CANCELLED.toCode())).where(Task.AUTHORED_ON.before().millis(Date.from(LocalDateTime.now().minusSeconds(10).atZone(ZoneId.systemDefault()).toInstant()))).returnBundle(Bundle.class).execute();
    TasksPollingInfo tasksPollingInfo = new TasksPollingBundleExtractor().extract(tasksBundle);
    // Collect all entries from every Task bundle for performance considerations.
    Bundle updateBundle = new Bundle();
    updateBundle.setType(Bundle.BundleType.TRANSACTION);
    for (Task task : tasksPollingInfo.getTasks()) {
        ServiceRequest serviceRequest = tasksPollingInfo.getServiceRequest(task);
        Organization organization = tasksPollingInfo.getOrganization(task);
        try {
            Endpoint endpoint = tasksPollingInfo.getEndpoint(organization);
            combineResult(updateBundle, getUpdateBundle(task, serviceRequest, endpoint));
        } catch (TaskPollingUpdateException | CpClientException exc) {
            combineResult(updateBundle, failTask(task, serviceRequest, exc.getMessage()));
        }
    }
    // If there is at least one bundle entry - execute a transaction request.
    if (updateBundle.getEntry().size() != 0) {
        log.info("One or more tasks were changed. Storing updates to EHR...");
        openEhrClient.transaction().withBundle(updateBundle).execute();
    }
    log.info("Task update process finished.");
}
Also used : Task(org.hl7.fhir.r4.model.Task) TasksPollingInfo(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TasksPollingInfo) TasksPollingBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor) CpClientException(org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException) Organization(org.hl7.fhir.r4.model.Organization) TokenClientParam(ca.uhn.fhir.rest.gclient.TokenClientParam) Endpoint(org.hl7.fhir.r4.model.Endpoint) Bundle(org.hl7.fhir.r4.model.Bundle) TaskPollingUpdateException(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TaskPollingUpdateException) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest)

Example 2 with TasksPollingInfo

use of org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TasksPollingInfo in project Gravity-SDOH-Exchange-RI by FHIR.

the class OurTaskPollingService method updateTasks.

@Scheduled(fixedDelayString = "${scheduling.task-polling-delay-millis}")
public void updateTasks() {
    log.info("Updating tasks from our tasks...");
    Bundle tasksBundle = openCpClient.search().forResource(Task.class).revInclude(Task.INCLUDE_BASED_ON).include(Task.INCLUDE_FOCUS.setRecurse(true)).and(Task.INTENT.exactly().code(Task.TaskIntent.ORDER.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(TaskStatus.FAILED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(TaskStatus.REJECTED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(TaskStatus.COMPLETED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(TaskStatus.CANCELLED.toCode())).where(Task.AUTHORED_ON.before().millis(Date.from(LocalDateTime.now().minusSeconds(10).atZone(ZoneId.systemDefault()).toInstant()))).returnBundle(Bundle.class).execute();
    OurTasksPollingInfo tasksPollingInfo = new OurTasksPollingBundleExtractor().extract(tasksBundle);
    // Collect all entries from every Task bundle for performance considerations.
    Bundle updateBundle = new Bundle();
    updateBundle.setType(Bundle.BundleType.TRANSACTION);
    for (Task task : tasksPollingInfo.getTasks()) {
        // Skip for now. We create our tasks automatically when we set a status to Accepted.
        if (task.getStatus().equals(TaskStatus.REQUESTED) || task.getStatus().equals(TaskStatus.RECEIVED)) {
            continue;
        }
        ServiceRequest serviceRequest = getServiceRequest(task);
        try {
            Task ourTask = tasksPollingInfo.getOurTask(task);
            combineResult(updateBundle, getUpdateBundle(task, serviceRequest, ourTask));
        } catch (OurTaskPollingUpdateException exc) {
            combineResult(updateBundle, failTask(task, serviceRequest, exc.getMessage()));
        }
    }
    // If there is at least one bundle entry - execute a transaction request.
    if (updateBundle.getEntry().size() != 0) {
        log.info("One or more tasks were changed. Storing updates to CP...");
        openCpClient.transaction().withBundle(updateBundle).execute();
    }
    log.info("Task update process finished.");
}
Also used : OurTasksPollingBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor) Task(org.hl7.fhir.r4.model.Task) TokenClientParam(ca.uhn.fhir.rest.gclient.TokenClientParam) Bundle(org.hl7.fhir.r4.model.Bundle) OurTasksPollingInfo(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor.OurTasksPollingInfo) OurTaskPollingUpdateException(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor.OurTaskPollingUpdateException) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

TokenClientParam (ca.uhn.fhir.rest.gclient.TokenClientParam)2 Bundle (org.hl7.fhir.r4.model.Bundle)2 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)2 Task (org.hl7.fhir.r4.model.Task)2 Endpoint (org.hl7.fhir.r4.model.Endpoint)1 Organization (org.hl7.fhir.r4.model.Organization)1 OurTasksPollingBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor)1 OurTaskPollingUpdateException (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor.OurTaskPollingUpdateException)1 OurTasksPollingInfo (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor.OurTasksPollingInfo)1 TasksPollingBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor)1 TaskPollingUpdateException (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TaskPollingUpdateException)1 TasksPollingInfo (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TasksPollingInfo)1 CpClientException (org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1