use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException 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();
}
use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException 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);
}
Aggregations