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);
}
}
}
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();
}
}
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);
}
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);
}
}
}
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();
}
Aggregations