Search in sources :

Example 81 with Task

use of org.hl7.fhir.r5.model.Task in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskPollingService method handleFinishedTask.

protected void handleFinishedTask(Bundle resultBundle, Task ehrTask, ServiceRequest ehrServiceRequest, Task cpTask, Endpoint endpoint) {
    if (Task.TaskStatus.COMPLETED.equals(cpTask.getStatus())) {
        ehrServiceRequest.setStatus(ServiceRequest.ServiceRequestStatus.COMPLETED);
        resultBundle.addEntry(FhirUtil.createPutEntry(ehrServiceRequest));
    }
    // property.
    if (Task.TaskStatus.COMPLETED.equals(cpTask.getStatus()) || Task.TaskStatus.CANCELLED.equals(cpTask.getStatus())) {
        // Modify Task.output. If task output is of type resulting-activity and contains a Reference to a proper
        // Procedure - copy output changing a Procedure reference to a local one.
        List<TaskOutputComponent> cpOutputs = cpTask.getOutput().stream().filter(t -> t.getValue() instanceof CodeableConcept || (t.getValue() instanceof Reference && ((Reference) t.getValue()).getReferenceElement().getResourceType().equals(Procedure.class.getSimpleName()))).collect(Collectors.toList());
        if (cpOutputs.size() == 0) {
            log.warn("Not output of type 'http://hl7.org/fhir/us/sdoh-clinicalcare/CodeSystem/sdohcc-temporary-codes|resulting" + "-activity' with a reference to a proper Procedure is present in task with id '{}' at '{}'. " + "Expecting a reference to a Procedure resource.", cpTask.getIdElement().getIdPart(), endpoint.getAddress());
        }
        Map<String, Procedure> cpProcedureMap = getCpProcedures(cpOutputs, endpoint);
        for (Task.TaskOutputComponent cpOutput : cpOutputs) {
            Task.TaskOutputComponent ehrTaskOutput = cpOutput.copy();
            if (cpOutput.getValue() instanceof Reference) {
                Reference cpProcedureReference = (Reference) cpOutput.getValue();
                String cpProcedureId = cpProcedureReference.getReferenceElement().getIdPart();
                Procedure cpProcedure = cpProcedureMap.get(cpProcedureId);
                // All Procedures have the same reason reference as ServiceRequest
                Procedure resultProcedure = copyProcedure(cpProcedure, ehrTask.getFor(), ehrTask.getFocus(), ehrServiceRequest.getReasonReference());
                resultProcedure.setId(IdType.newRandomUuid());
                resultProcedure.addIdentifier().setSystem(endpoint.getAddress()).setValue(cpProcedureId);
                // Add Procedure to result bundle
                resultBundle.addEntry(FhirUtil.createPostEntry(resultProcedure));
                ehrTaskOutput.setValue(FhirUtil.toReference(Procedure.class, resultProcedure.getIdElement().getIdPart(), cpProcedureReference.getDisplay()));
            }
            ehrTask.addOutput(ehrTaskOutput);
        }
    }
}
Also used : Date(java.util.Date) TaskOutputComponent(org.hl7.fhir.r4.model.Task.TaskOutputComponent) Constants(ca.uhn.fhir.rest.api.Constants) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LocalDateTime(java.time.LocalDateTime) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Autowired(org.springframework.beans.factory.annotation.Autowired) Endpoint(org.hl7.fhir.r4.model.Endpoint) Scheduled(org.springframework.scheduling.annotation.Scheduled) Reference(org.hl7.fhir.r4.model.Reference) TaskStatus(org.hl7.fhir.r4.model.Task.TaskStatus) Function(java.util.function.Function) ArrayList(java.util.ArrayList) JSONParser(net.minidev.json.parser.JSONParser) TasksPollingInfo(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TasksPollingInfo) Procedure(org.hl7.fhir.r4.model.Procedure) Task(org.hl7.fhir.r4.model.Task) FhirContext(ca.uhn.fhir.context.FhirContext) TaskInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Map(java.util.Map) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) StringClientParam(ca.uhn.fhir.rest.gclient.StringClientParam) SearchModifierCode(org.hl7.fhir.r4.model.codesystems.SearchModifierCode) Observation(org.hl7.fhir.r4.model.Observation) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) SDOHProfiles(org.hl7.gravity.refimpl.sdohexchange.fhir.SDOHProfiles) TokenClientParam(ca.uhn.fhir.rest.gclient.TokenClientParam) Set(java.util.Set) TaskFailBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskFailBundleFactory) CpClientException(org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException) Collectors(java.util.stream.Collectors) IdType(org.hl7.fhir.r4.model.IdType) ZoneId(java.time.ZoneId) Organization(org.hl7.fhir.r4.model.Organization) Slf4j(lombok.extern.slf4j.Slf4j) QuestionnaireResponse(org.hl7.fhir.r4.model.QuestionnaireResponse) TaskPollingUpdateException(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor.TaskPollingUpdateException) List(java.util.List) TasksPollingBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TasksPollingBundleExtractor) JSONObject(net.minidev.json.JSONObject) Bundle(org.hl7.fhir.r4.model.Bundle) OrganizationTypeCode(org.hl7.gravity.refimpl.sdohexchange.codes.OrganizationTypeCode) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) Task(org.hl7.fhir.r4.model.Task) Reference(org.hl7.fhir.r4.model.Reference) Procedure(org.hl7.fhir.r4.model.Procedure) TaskOutputComponent(org.hl7.fhir.r4.model.Task.TaskOutputComponent) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) TaskOutputComponent(org.hl7.fhir.r4.model.Task.TaskOutputComponent)

Example 82 with Task

use of org.hl7.fhir.r5.model.Task 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 83 with Task

use of org.hl7.fhir.r5.model.Task in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskReferenceResourcesLoader method loadTaskReferences.

private TaskReferencesHolder loadTaskReferences(IGenericClient ehrClient, Task task, String identifierSystem, Bundle bundle) {
    TaskReferenceResolver referenceResolver = new TaskReferenceResolver(task, identifierSystem);
    // Load local references in one transaction
    referenceResolver.setLocalResources(resourceLoader.getResourcesBySystem(openCpClient, identifierSystem, referenceResolver.getLocalReferences()));
    // Load EHR references in one transaction
    referenceResolver.setExternalResources(resourceLoader.getResources(ehrClient, referenceResolver.getExternalReferences()));
    Patient patient = referenceResolver.getPatient();
    if (referenceResolver.createPatient()) {
        updateResourceRefs(patient, ehrClient.getServerBase(), new HashMap());
        // Create CP Patient resource
        bundle.addEntry(FhirUtil.createPostEntry(patient));
    }
    Organization requester = referenceResolver.getRequester();
    if (referenceResolver.createRequester()) {
        // Create CP Organization resource
        bundle.addEntry(FhirUtil.createPostEntry(requester));
    }
    ServiceRequest serviceRequest = referenceResolver.getServiceRequest();
    // Link CP ServiceRequest with Patient
    serviceRequest.setSubject(FhirUtil.toReference(Patient.class, patient.getIdElement().getIdPart()));
    // Create CP ServiceRequest resource
    bundle.addEntry(FhirUtil.createPostEntry(serviceRequest));
    // Link CP Task with Patient, Organization and ServiceRequest
    task.setFor(FhirUtil.toReference(Patient.class, patient.getIdElement().getIdPart(), patient.getNameFirstRep().getNameAsSingleString()));
    task.setRequester(FhirUtil.toReference(Organization.class, requester.getIdElement().getIdPart(), requester.getName()));
    task.setFocus(FhirUtil.toReference(ServiceRequest.class, serviceRequest.getIdElement().getIdPart()));
    return new TaskReferencesHolder(patient, requester, serviceRequest);
}
Also used : Organization(org.hl7.fhir.r4.model.Organization) HashMap(java.util.HashMap) Patient(org.hl7.fhir.r4.model.Patient) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest)

Example 84 with Task

use of org.hl7.fhir.r5.model.Task in project Gravity-SDOH-Exchange-RI by FHIR.

the class OurTaskPollingService method handleFinishedTask.

protected void handleFinishedTask(Bundle resultBundle, Task task, ServiceRequest serviceRequest, Task ourTask) {
    boolean isFinished = false;
    if (TaskStatus.COMPLETED.equals(ourTask.getStatus())) {
        serviceRequest.setStatus(ServiceRequest.ServiceRequestStatus.COMPLETED);
        resultBundle.addEntry(FhirUtil.createPutEntry(serviceRequest));
        isFinished = true;
    } else if (TaskStatus.CANCELLED.equals(ourTask.getStatus()) || TaskStatus.REJECTED.equals(ourTask.getStatus()) || TaskStatus.FAILED.equals(ourTask.getStatus())) {
        serviceRequest.setStatus(ServiceRequest.ServiceRequestStatus.REVOKED);
        resultBundle.addEntry(FhirUtil.createPutEntry(serviceRequest));
        isFinished = true;
    }
    // property.
    if (isFinished) {
        // Modify Task.output. If task output is of type resulting-activity and contains a Reference to a proper
        // Procedure - copy output changing a Procedure reference to a local one.
        List<TaskOutputComponent> ourTaskOutputs = ourTask.getOutput().stream().filter(t -> t.getValue() instanceof CodeableConcept || (t.getValue() instanceof Reference && ((Reference) t.getValue()).getReferenceElement().getResourceType().equals(Procedure.class.getSimpleName()))).collect(Collectors.toList());
        if (ourTaskOutputs.size() == 0) {
            log.warn("No output of type 'http://hl7.org/fhir/us/sdoh-clinicalcare/CodeSystem/sdohcc-temporary-codes|resulting" + "-activity' with a reference to a proper Procedure is present in task with id '{}'. " + "Expecting a reference to a Procedure resource.", ourTask.getIdElement().getIdPart());
        }
        Map<String, Procedure> ourProcedureMap = getOurTaskProcedures(ourTaskOutputs);
        for (TaskOutputComponent ourTaskOutput : ourTaskOutputs) {
            TaskOutputComponent taskOutput = ourTaskOutput.copy();
            if (ourTaskOutput.getValue() instanceof Reference) {
                Reference ourProcedureReference = (Reference) ourTaskOutput.getValue();
                String ourProcedureId = ourProcedureReference.getReferenceElement().getIdPart();
                Procedure ourProcedure = ourProcedureMap.get(ourProcedureId);
                // All Procedures have the same reason reference as ServiceRequest
                Procedure resultProcedure = copyProcedure(ourProcedure, task.getFor(), task.getFocus(), serviceRequest.getReasonReference());
                resultProcedure.setId(IdType.newRandomUuid());
                // TODO add identifier
                // resultProcedure.addIdentifier()
                // .setSystem(SERVER_BASE)
                // .setValue(ourProcedureId);
                // Add Procedure to result bundle
                resultBundle.addEntry(FhirUtil.createPostEntry(resultProcedure));
                taskOutput.setValue(FhirUtil.toReference(Procedure.class, resultProcedure.getIdElement().getIdPart(), ourProcedureReference.getDisplay()));
            }
            task.addOutput(taskOutput);
        }
    }
}
Also used : OurTaskPollingUpdateException(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor.OurTaskPollingUpdateException) OurTasksPollingInfo(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor.OurTasksPollingInfo) Date(java.util.Date) TaskOutputComponent(org.hl7.fhir.r4.model.Task.TaskOutputComponent) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LocalDateTime(java.time.LocalDateTime) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) Autowired(org.springframework.beans.factory.annotation.Autowired) Scheduled(org.springframework.scheduling.annotation.Scheduled) Reference(org.hl7.fhir.r4.model.Reference) TaskStatus(org.hl7.fhir.r4.model.Task.TaskStatus) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Procedure(org.hl7.fhir.r4.model.Procedure) Task(org.hl7.fhir.r4.model.Task) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Map(java.util.Map) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) SearchModifierCode(org.hl7.fhir.r4.model.codesystems.SearchModifierCode) ServiceRequest(org.hl7.fhir.r4.model.ServiceRequest) SDOHProfiles(org.hl7.gravity.refimpl.sdohexchange.fhir.SDOHProfiles) TokenClientParam(ca.uhn.fhir.rest.gclient.TokenClientParam) TaskFailBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskFailBundleFactory) Collectors(java.util.stream.Collectors) IdType(org.hl7.fhir.r4.model.IdType) ZoneId(java.time.ZoneId) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) OurTasksPollingBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.OurTasksPollingBundleExtractor) Bundle(org.hl7.fhir.r4.model.Bundle) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) Reference(org.hl7.fhir.r4.model.Reference) Procedure(org.hl7.fhir.r4.model.Procedure) TaskOutputComponent(org.hl7.fhir.r4.model.Task.TaskOutputComponent) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept)

Example 85 with Task

use of org.hl7.fhir.r5.model.Task in project Gravity-SDOH-Exchange-RI by FHIR.

the class OurTaskService method read.

public TaskDto read(String id) {
    Bundle taskBundle = taskRepository.find(id, Lists.newArrayList(Task.INCLUDE_FOCUS, Task.INCLUDE_BASED_ON, Task.INCLUDE_OWNER.asNonRecursive()));
    Task ourTask = FhirUtil.getFirstFromBundle(taskBundle, Task.class);
    if (Objects.isNull(ourTask)) {
        throw new ResourceNotFoundException(new IdType(Task.class.getSimpleName(), id));
    }
    if (!ourTask.getIntent().equals(Task.TaskIntent.FILLERORDER)) {
        throw new TaskReadException("The intent of Task/" + id + " is not filler-order.");
    }
    return new OurTaskBundleToDtoConverter().convert(taskBundle).stream().findFirst().get();
}
Also used : Task(org.hl7.fhir.r4.model.Task) OurTaskBundleToDtoConverter(org.hl7.gravity.refimpl.sdohexchange.dto.converter.OurTaskBundleToDtoConverter) Bundle(org.hl7.fhir.r4.model.Bundle) TaskReadException(org.hl7.gravity.refimpl.sdohexchange.exception.TaskReadException) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) IdType(org.hl7.fhir.r4.model.IdType)

Aggregations

Test (org.junit.Test)127 Task (org.hl7.fhir.r4.model.Task)120 FhirTask (org.openmrs.module.fhir2.model.FhirTask)59 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Bundle (org.hl7.fhir.r4.model.Bundle)40 Reference (org.hl7.fhir.r4.model.Reference)28 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)24 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)23 IBundleProvider (ca.uhn.fhir.rest.api.server.IBundleProvider)22 Task (org.hl7.fhir.dstu3.model.Task)21 IdType (org.hl7.fhir.r4.model.IdType)21 CodeableConcept (org.hl7.fhir.r4.model.CodeableConcept)17 Date (java.util.Date)16 ArrayList (java.util.ArrayList)13 List (java.util.List)13 SearchParameterMap (org.openmrs.module.fhir2.api.search.param.SearchParameterMap)13 BaseModuleContextSensitiveTest (org.openmrs.test.BaseModuleContextSensitiveTest)12 TokenAndListParam (ca.uhn.fhir.rest.param.TokenAndListParam)11 TokenParam (ca.uhn.fhir.rest.param.TokenParam)11 Collectors (java.util.stream.Collectors)11