Search in sources :

Example 6 with TaskInfoHolder

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

the class TaskService method sync.

public void sync(Task task, final ServiceRequest serviceRequest) {
    Bundle ourUpdateBundle = new Bundle();
    ourUpdateBundle.setType(Bundle.BundleType.TRANSACTION);
    Bundle ourTaskBundle = taskRepository.findOurTask(task);
    TaskInfoHolder ourTaskInfo = new TaskInfoBundleExtractor().extract(ourTaskBundle).stream().findFirst().orElseThrow(() -> new ResourceNotFoundException("No our task is found for task " + task.getIdElement().getIdPart()));
    Task ourTask = ourTaskInfo.getTask();
    ourTask.setStatus(task.getStatus());
    ourTask.setStatusReason(task.getStatusReason());
    ourTask.setLastModifiedElement(task.getLastModifiedElement());
    ourTask.setNote(task.getNote());
    ourUpdateBundle.addEntry(FhirUtil.createPutEntry(ourTask));
    ServiceRequest ourServiceRequest = ourTaskInfo.getServiceRequest();
    if (!serviceRequest.getStatus().equals(ourServiceRequest.getStatus())) {
        ourServiceRequest.setStatus(serviceRequest.getStatus());
        ourUpdateBundle.addEntry(FhirUtil.createPutEntry(ourServiceRequest));
    }
    taskRepository.transaction(ourUpdateBundle);
}
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) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest)

Example 7 with TaskInfoHolder

use of org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder 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)

Example 8 with TaskInfoHolder

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

the class TaskService method newTask.

public String newTask(NewTaskRequestDto taskRequest, UserDto user) {
    Assert.notNull(SmartOnFhirContext.get().getPatient(), "Patient id cannot be null.");
    TaskPrepareBundleFactory taskPrepareBundleFactory = new TaskPrepareBundleFactory(SmartOnFhirContext.get().getPatient(), user.getId(), taskRequest.getPerformerId(), taskRequest.getConsent(), taskRequest.getConditionIds(), taskRequest.getGoalIds());
    Bundle taskRelatedResources = ehrClient.transaction().withBundle(taskPrepareBundleFactory.createPrepareBundle()).execute();
    TaskPrepareInfoHolder taskPrepareInfoHolder = new TaskPrepareBundleExtractor().extract(taskRelatedResources);
    TaskBundleFactory taskBundleFactory = new TaskBundleFactory();
    taskBundleFactory.setName(taskRequest.getName());
    taskBundleFactory.setPatient(taskPrepareInfoHolder.getPatient());
    taskBundleFactory.setCategory(sdohMappings.findCategoryCoding(taskRequest.getCategory()));
    taskBundleFactory.setRequestCode(sdohMappings.findResourceCoding(ServiceRequest.class, taskRequest.getCode()));
    taskBundleFactory.setPriority(taskRequest.getPriority());
    taskBundleFactory.setOccurrence(taskRequest.getOccurrence());
    taskBundleFactory.setPerformer(taskPrepareInfoHolder.getPerformerOrganization());
    taskBundleFactory.setRequester(taskPrepareInfoHolder.getPerformer());
    taskBundleFactory.setComment(taskRequest.getComment());
    taskBundleFactory.setUser(user);
    taskBundleFactory.setConsent(taskPrepareInfoHolder.getConsent());
    taskBundleFactory.setConditions(taskPrepareInfoHolder.getConditions(taskRequest.getConditionIds()));
    taskBundleFactory.setGoals(taskPrepareInfoHolder.getGoals(taskRequest.getGoalIds()));
    Bundle taskCreateBundle = ehrClient.transaction().withBundle(taskBundleFactory.createBundle()).execute();
    IdType taskId = FhirUtil.getFromResponseBundle(taskCreateBundle, Task.class);
    TaskInfoHolder createInfo = new TaskInfoBundleExtractor().extract(getTask(taskId.getIdPart(), Task.INCLUDE_FOCUS)).stream().findFirst().orElseThrow(() -> new TaskCreateException("Task/ServiceRequest are not found in the response bundle."));
    Task task = createInfo.getTask();
    try {
        cpService.create(task, taskPrepareInfoHolder.getEndpoint());
    } catch (CpClientException exc) {
        log.warn("Task '{}' creation failed at CP. Failing a local Task and related ServiceRequest.", task.getIdElement().getIdPart(), exc);
        ehrClient.transaction().withBundle(new TaskFailBundleFactory(task, createInfo.getServiceRequest(), exc.getMessage()).createFailBundle()).execute();
    }
    return task.getIdElement().getIdPart();
}
Also used : TaskInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder) Task(org.hl7.fhir.r4.model.Task) TaskPrepareBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskPrepareBundleExtractor) TaskPrepareBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskPrepareBundleFactory) IBaseBundle(org.hl7.fhir.instance.model.api.IBaseBundle) Bundle(org.hl7.fhir.r4.model.Bundle) TaskPrepareInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskPrepareBundleExtractor.TaskPrepareInfoHolder) TaskCreateException(org.hl7.gravity.refimpl.sdohexchange.exception.TaskCreateException) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) TaskBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskBundleFactory) IdType(org.hl7.fhir.r4.model.IdType) TaskFailBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskFailBundleFactory) CpClientException(org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor)

Example 9 with TaskInfoHolder

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

the class CpService method sync.

public void sync(final Task ehrTask, final ServiceRequest ehrServiceRequest, final Endpoint endpoint) throws CpClientException {
    Bundle cpUpdateBundle = new Bundle();
    cpUpdateBundle.setType(BundleType.TRANSACTION);
    TaskInfoHolder cpTaskInfo = read(ehrTask.getIdElement().getIdPart(), endpoint);
    Task cpTask = cpTaskInfo.getTask();
    cpTask.setStatus(ehrTask.getStatus());
    cpTask.setStatusReason(ehrTask.getStatusReason());
    cpTask.setLastModifiedElement(ehrTask.getLastModifiedElement());
    cpTask.setNote(ehrTask.getNote());
    cpUpdateBundle.addEntry(FhirUtil.createPutEntry(cpTask));
    ServiceRequest cpServiceRequest = cpTaskInfo.getServiceRequest();
    if (!ehrServiceRequest.getStatus().equals(cpServiceRequest.getStatus())) {
        cpServiceRequest.setStatus(ehrServiceRequest.getStatus());
        cpUpdateBundle.addEntry(FhirUtil.createPutEntry(cpServiceRequest));
    }
    transaction(cpUpdateBundle, endpoint);
}
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) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest)

Example 10 with TaskInfoHolder

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

the class ProblemInfoBundleExtractor method extract.

@Override
public List<ProblemInfoHolder> extract(Bundle bundle) {
    List<? extends ConditionInfoHolder> conditionInfoHolders = conditionInfoBundleExtractor.extract(bundle);
    Map<String, ProblemInfoHolder> idToHolderMap = new HashMap<>();
    for (ConditionInfoHolder h : conditionInfoHolders) {
        idToHolderMap.put(h.getCondition().getIdElement().getIdPart(), new ProblemInfoHolder(h));
    }
    for (Task task : FhirUtil.getFromBundle(bundle, Task.class)) {
        if (!task.hasFocus() || !(task.getFocus().getResource() instanceof ServiceRequest)) {
            continue;
        }
        ServiceRequest sr = (ServiceRequest) task.getFocus().getResource();
        sr.getReasonReference().stream().filter(ref -> Condition.class.getSimpleName().equals(ref.getReferenceElement().getResourceType()) && idToHolderMap.containsKey(ref.getReferenceElement().getIdPart())).forEach(ref -> idToHolderMap.get(ref.getReferenceElement().getIdPart()).getTasks().add(new TaskInfoHolder(task.getIdElement().getIdPart(), task.getDescription(), task.getStatus())));
    }
    for (Goal goal : FhirUtil.getFromBundle(bundle, Goal.class)) {
        goal.getAddresses().stream().filter(ref -> Condition.class.getSimpleName().equals(ref.getReferenceElement().getResourceType()) && idToHolderMap.containsKey(ref.getReferenceElement().getIdPart())).forEach(ref -> {
            idToHolderMap.get(ref.getReferenceElement().getIdPart()).getGoals().add(new GoalInfoHolder(goal.getIdElement().getIdPart(), goal.getDescription().getText(), goal.getLifecycleStatus()));
        });
    }
    return new ArrayList<>(idToHolderMap.values());
}
Also used : Task(org.hl7.fhir.r4.model.Task) List(java.util.List) Getter(lombok.Getter) Goal(org.hl7.fhir.r4.model.Goal) Map(java.util.Map) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Bundle(org.hl7.fhir.r4.model.Bundle) Condition(org.hl7.fhir.r4.model.Condition) HashMap(java.util.HashMap) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) ArrayList(java.util.ArrayList) Task(org.hl7.fhir.r4.model.Task) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) Goal(org.hl7.fhir.r4.model.Goal)

Aggregations

Task (org.hl7.fhir.r4.model.Task)10 Bundle (org.hl7.fhir.r4.model.Bundle)8 TaskInfoHolder (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder)6 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)4 TaskInfoBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor)4 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)2 List (java.util.List)2 Map (java.util.Map)2 Getter (lombok.Getter)2 Coding (org.hl7.fhir.r4.model.Coding)2 IdType (org.hl7.fhir.r4.model.IdType)2 Type (org.hl7.fhir.r4.model.Type)2 FhirUtil (org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil)2 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Objects (java.util.Objects)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1