Search in sources :

Example 1 with CpClientException

use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException 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 CpClientException

use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskService method update.

public void update(String id, UpdateTaskRequestDto update, UserDto user) {
    Bundle taskBundle = getTask(id, Task.INCLUDE_FOCUS, Task.INCLUDE_OWNER, Organization.INCLUDE_ENDPOINT.setRecurse(true));
    TaskUpdateInfoHolder updateInfo = new TaskUpdateBundleExtractor(id).extract(taskBundle);
    Task task = updateInfo.getTask();
    ServiceRequest serviceRequest = updateInfo.getServiceRequest();
    TaskUpdateBundleFactory updateBundleFactory = new TaskUpdateBundleFactory();
    updateBundleFactory.setTask(task);
    updateBundleFactory.setServiceRequest(serviceRequest);
    updateBundleFactory.setStatus(update.getFhirStatus());
    updateBundleFactory.setStatusReason(update.getStatusReason());
    updateBundleFactory.setComment(update.getComment());
    updateBundleFactory.setUser(user);
    ehrClient.transaction().withBundle(updateBundleFactory.createUpdateBundle()).execute();
    try {
        cpService.sync(task, serviceRequest, updateInfo.getEndpoint());
    } catch (CpClientException exc) {
        ehrClient.transaction().withBundle(new TaskFailBundleFactory(task, serviceRequest, exc.getMessage()).createFailBundle()).execute();
    }
}
Also used : TaskFailBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskFailBundleFactory) Task(org.hl7.fhir.r4.model.Task) CpClientException(org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) Bundle(org.hl7.fhir.r4.model.Bundle) TaskUpdateBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskUpdateBundleExtractor) TaskUpdateInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskUpdateBundleExtractor.TaskUpdateInfoHolder) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) TaskUpdateBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskUpdateBundleFactory)

Example 3 with CpClientException

use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class CpService method create.

public void create(final Task ehrTask, final Endpoint endpoint) throws CpClientException {
    Task cpTask = externalizeResource(ehrTask.copy(), identifierSystem);
    // TODO: Remove this after Logica bug response
    cpTask.setMeta(null);
    cpTask.addIdentifier().setSystem(identifierSystem).setValue(ehrTask.getIdElement().getIdPart());
    try {
        cpClient(endpoint).create().resource(cpTask).execute();
    } catch (BaseServerResponseException exc) {
        throw new CpClientException(String.format("Could not create a Task with identifier '%s' in CP at '%s'. Reason: %s.", identifierSystem + "|" + ehrTask.getIdElement().getIdPart(), endpoint.getAddress(), exc.getMessage()), exc);
    }
}
Also used : Task(org.hl7.fhir.r4.model.Task) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)

Example 4 with CpClientException

use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class CpService method read.

public TaskInfoHolder read(final String id, final Endpoint endpoint) throws CpClientException {
    try {
        Bundle taskBundle = cpClient(endpoint).search().forResource(Task.class).where(Task.IDENTIFIER.exactly().systemAndValues(identifierSystem, id)).include(Task.INCLUDE_FOCUS).returnBundle(Bundle.class).execute();
        // Additional validation
        int tasksSize = FhirUtil.getFromBundle(taskBundle, Task.class).size();
        if (tasksSize == 0) {
            throw new CpClientException(String.format("No Task is present at '%s' for identifier '%s'.", endpoint.getAddress(), identifierSystem + "|" + id));
        } else if (tasksSize > 1) {
            throw new CpClientException(String.format("More than one Task is present at '%s' for identifier '%s'.", endpoint.getAddress(), identifierSystem + "|" + id));
        }
        return new TaskInfoBundleExtractor().extract(taskBundle).stream().findFirst().get();
    } catch (BaseServerResponseException exc) {
        throw new CpClientException(String.format("Task retrieval failed for identifier '%s' at CP location '%s'. Reason: %s.", identifierSystem + "|" + id, endpoint.getAddress(), exc.getMessage()), exc);
    }
}
Also used : Task(org.hl7.fhir.r4.model.Task) Bundle(org.hl7.fhir.r4.model.Bundle) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) BaseServerResponseException(ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException) Endpoint(org.hl7.fhir.r4.model.Endpoint)

Example 5 with CpClientException

use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskPollingService method getUpdateBundle.

protected Bundle getUpdateBundle(Task task, ServiceRequest serviceRequest, Endpoint endpoint) throws CpClientException {
    Bundle resultBundle = new Bundle();
    resultBundle.setType(Bundle.BundleType.TRANSACTION);
    // Do a copy not to modify an input Task. Possibly this method will fail during execution, and we don't want to
    // end up with a modified Task.
    Task resultTask = task.copy();
    TaskInfoHolder cpTaskInfo = cpService.read(task.getIdElement().getIdPart(), endpoint);
    Task cpTask = cpTaskInfo.getTask();
    // If status is the same and comments size are the same  OR CP task in REQUESTED state - do nothing.
    if ((cpTask.getStatus().equals(task.getStatus()) && cpTask.getNote().size() == task.getNote().size()) || Task.TaskStatus.REQUESTED.equals(cpTask.getStatus())) {
        return resultBundle;
    }
    log.info("Task status/field change detected for id '{}'. '{}' -> '{}'. Updating...", task.getIdElement().getIdPart(), task.getStatus(), cpTask.getStatus());
    // Copy required Task fields
    copyTaskFields(resultTask, cpTask, endpoint);
    resultBundle.addEntry(FhirUtil.createPutEntry(resultTask));
    if (FINISHED_TASK_STATUSES.contains(cpTask.getStatus())) {
        // It is critical to pass a resultTask, not a task, since it will be modified inside.
        handleFinishedTask(resultBundle, resultTask, serviceRequest, cpTask, endpoint);
    }
    return resultBundle;
}
Also used : TaskInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder) Task(org.hl7.fhir.r4.model.Task) Bundle(org.hl7.fhir.r4.model.Bundle)

Aggregations

Task (org.hl7.fhir.r4.model.Task)7 Bundle (org.hl7.fhir.r4.model.Bundle)6 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)4 TaskInfoHolder (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder)3 CpClientException (org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException)3 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)2 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)2 Endpoint (org.hl7.fhir.r4.model.Endpoint)2 TaskInfoBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor)2 TaskFailBundleFactory (org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskFailBundleFactory)2 TokenClientParam (ca.uhn.fhir.rest.gclient.TokenClientParam)1 IdType (org.hl7.fhir.r4.model.IdType)1 Organization (org.hl7.fhir.r4.model.Organization)1 TaskCreateException (org.hl7.gravity.refimpl.sdohexchange.exception.TaskCreateException)1 TaskPrepareBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskPrepareBundleExtractor)1 TaskPrepareInfoHolder (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskPrepareBundleExtractor.TaskPrepareInfoHolder)1 TaskUpdateBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskUpdateBundleExtractor)1 TaskUpdateInfoHolder (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskUpdateBundleExtractor.TaskUpdateInfoHolder)1 TasksPollingBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor)1 TaskPollingUpdateException (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TaskPollingUpdateException)1