use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException in project Gravity-SDOH-Exchange-RI by FHIR.
the class TaskPollingService method updateTasks.
public void updateTasks() {
log.info("Updating tasks from CP Organizations...");
Bundle tasksBundle = openEhrClient.search().forResource(Task.class).include(Task.INCLUDE_FOCUS).include(Task.INCLUDE_OWNER).include(Organization.INCLUDE_ENDPOINT.setRecurse(true)).where(new TokenClientParam("owner:Organization.type").exactly().systemAndCode(OrganizationTypeCode.CP.getSystem(), OrganizationTypeCode.CP.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.FAILED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.REJECTED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.COMPLETED.toCode())).where(new TokenClientParam(Task.SP_STATUS + ":" + SearchModifierCode.NOT.toCode()).exactly().code(Task.TaskStatus.CANCELLED.toCode())).where(Task.AUTHORED_ON.before().millis(Date.from(LocalDateTime.now().minusSeconds(10).atZone(ZoneId.systemDefault()).toInstant()))).returnBundle(Bundle.class).execute();
TasksPollingInfo tasksPollingInfo = new TasksPollingBundleExtractor().extract(tasksBundle);
// Collect all entries from every Task bundle for performance considerations.
Bundle updateBundle = new Bundle();
updateBundle.setType(Bundle.BundleType.TRANSACTION);
for (Task task : tasksPollingInfo.getTasks()) {
ServiceRequest serviceRequest = tasksPollingInfo.getServiceRequest(task);
Organization organization = tasksPollingInfo.getOrganization(task);
try {
Endpoint endpoint = tasksPollingInfo.getEndpoint(organization);
combineResult(updateBundle, getUpdateBundle(task, serviceRequest, endpoint));
} catch (TaskPollingUpdateException | CpClientException exc) {
combineResult(updateBundle, failTask(task, serviceRequest, exc.getMessage()));
}
}
// If there is at least one bundle entry - execute a transaction request.
if (updateBundle.getEntry().size() != 0) {
log.info("One or more tasks were changed. Storing updates to EHR...");
openEhrClient.transaction().withBundle(updateBundle).execute();
}
log.info("Task update process finished.");
}
use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException 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.gravity.refimpl.sdohexchange.service.CpService.CpClientException in project Gravity-SDOH-Exchange-RI by FHIR.
the class CpService method create.
public void create(final Task ehrTask, final Endpoint endpoint) throws CpClientException {
Task cpTask = externalizeResource(ehrTask.copy(), identifierSystem);
// TODO: Remove this after Logica bug response
cpTask.setMeta(null);
cpTask.addIdentifier().setSystem(identifierSystem).setValue(ehrTask.getIdElement().getIdPart());
try {
cpClient(endpoint).create().resource(cpTask).execute();
} catch (BaseServerResponseException exc) {
throw new CpClientException(String.format("Could not create a Task with identifier '%s' in CP at '%s'. Reason: %s.", identifierSystem + "|" + ehrTask.getIdElement().getIdPart(), endpoint.getAddress(), exc.getMessage()), exc);
}
}
use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException 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);
}
}
use of org.hl7.gravity.refimpl.sdohexchange.service.CpService.CpClientException 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;
}
Aggregations