Search in sources :

Example 1 with TaskInfoBundleExtractor

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

the class OurTaskService method update.

public void update(String id, UpdateOurTaskRequestDto update, UserDto user) {
    Bundle taskBundle = taskRepository.find(id, Lists.newArrayList(Task.INCLUDE_FOCUS));
    TaskInfoHolder taskInfo = new TaskInfoBundleExtractor().extract(taskBundle).stream().findFirst().orElseThrow(() -> new ResourceNotFoundException(new IdType(Task.class.getSimpleName(), id)));
    OurTaskUpdateBundleFactory bundleFactory = new OurTaskUpdateBundleFactory();
    bundleFactory.setTask(taskInfo.getTask());
    bundleFactory.setServiceRequest(taskInfo.getServiceRequest());
    bundleFactory.setStatus(update.getTaskStatus());
    bundleFactory.setStatusReason(update.getStatusReason());
    bundleFactory.setComment(update.getComment());
    bundleFactory.setUser(user);
    taskRepository.transaction(bundleFactory.createUpdateBundle());
}
Also used : TaskInfoHolder(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder) Task(org.hl7.fhir.r4.model.Task) OurTaskUpdateBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.OurTaskUpdateBundleFactory) Bundle(org.hl7.fhir.r4.model.Bundle) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) IdType(org.hl7.fhir.r4.model.IdType)

Example 2 with TaskInfoBundleExtractor

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

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

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

the class TaskService method update.

public void update(String id, UpdateTaskRequestDto update) throws AuthClientException {
    Server server = serverRepository.findById(update.getServerId()).orElseThrow(() -> new ServerNotFoundException(String.format("No server was found by id '%s'", update.getServerId())));
    IGenericClient fhirClient = fhirContext.newRestfulGenericClient(server.getFhirServerUrl());
    // Doesn't support now
    // fhirClient.registerInterceptor(new BearerTokenAuthInterceptor(
    // authorizationClient.getTokenResponse(URI.create(server.getAuthServerUrl()), server.getClientId(),
    // server.getClientSecret(), SCOPE)
    // .getAccessToken()));
    TaskRepository taskRepository = new TaskRepository(fhirClient, applicationUrl);
    // Validates and converts Procedure codes to Coding
    List<Coding> procedureCodes = Optional.ofNullable(update.getProcedureCodes()).orElse(Collections.emptyList()).stream().map(code -> sdohMappings.findResourceCoding(Procedure.class, code)).collect(Collectors.toList());
    Bundle taskBundle = taskRepository.find(id, Lists.newArrayList(Task.INCLUDE_FOCUS));
    TaskInfoBundleExtractor.TaskInfoHolder taskInfo = new TaskInfoBundleExtractor().extract(taskBundle).stream().findFirst().orElseThrow(() -> new ResourceNotFoundException(new IdType(Task.class.getSimpleName(), id)));
    TaskUpdateBundleFactory bundleFactory = new TaskUpdateBundleFactory();
    bundleFactory.setTask(taskInfo.getTask());
    bundleFactory.setStatus(update.getTaskStatus());
    bundleFactory.setServiceRequest(taskInfo.getServiceRequest());
    bundleFactory.setStatusReason(update.getStatusReason());
    bundleFactory.setComment(update.getComment());
    bundleFactory.setOutcome(update.getOutcome());
    bundleFactory.setProcedureCodes(procedureCodes);
    taskRepository.transaction(bundleFactory.createUpdateBundle());
}
Also used : TaskDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskDto) ServerNotFoundException(org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException) AuthorizationClient(org.hl7.gravity.refimpl.sdohexchange.auth.AuthorizationClient) TaskUpdateBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskUpdateBundleFactory) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Procedure(org.hl7.fhir.r4.model.Procedure) Task(org.hl7.fhir.r4.model.Task) FhirContext(ca.uhn.fhir.context.FhirContext) UpdateTaskRequestDto(org.hl7.gravity.refimpl.sdohexchange.dto.request.UpdateTaskRequestDto) Lists(com.google.common.collect.Lists) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) Service(org.springframework.stereotype.Service) TaskRepository(org.hl7.gravity.refimpl.sdohexchange.dao.TaskRepository) TaskReadException(org.hl7.gravity.refimpl.sdohexchange.exception.TaskReadException) Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) RestTemplate(org.springframework.web.client.RestTemplate) AuthClientException(org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException) TaskBundleToDtoConverter(org.hl7.gravity.refimpl.sdohexchange.dto.converter.TaskBundleToDtoConverter) ServerRepository(org.hl7.gravity.refimpl.sdohexchange.dao.ServerRepository) Collectors(java.util.stream.Collectors) IdType(org.hl7.fhir.r4.model.IdType) Objects(java.util.Objects) List(java.util.List) Coding(org.hl7.fhir.r4.model.Coding) Optional(java.util.Optional) Bundle(org.hl7.fhir.r4.model.Bundle) Collections(java.util.Collections) SDOHMappings(org.hl7.gravity.refimpl.sdohexchange.sdohmappings.SDOHMappings) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) Task(org.hl7.fhir.r4.model.Task) Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.r4.model.Bundle) TaskRepository(org.hl7.gravity.refimpl.sdohexchange.dao.TaskRepository) IdType(org.hl7.fhir.r4.model.IdType) Coding(org.hl7.fhir.r4.model.Coding) ServerNotFoundException(org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) TaskUpdateBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskUpdateBundleFactory)

Example 5 with TaskInfoBundleExtractor

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

Aggregations

Bundle (org.hl7.fhir.r4.model.Bundle)5 Task (org.hl7.fhir.r4.model.Task)5 TaskInfoBundleExtractor (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor)5 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)3 IdType (org.hl7.fhir.r4.model.IdType)3 TaskInfoHolder (org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor.TaskInfoHolder)3 ServiceRequest (org.hl7.fhir.r4.model.ServiceRequest)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)1 BaseServerResponseException (ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException)1 Lists (com.google.common.collect.Lists)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)1 Coding (org.hl7.fhir.r4.model.Coding)1 Endpoint (org.hl7.fhir.r4.model.Endpoint)1