/** * Update the workflow instance in the database if it is currently not running, and insert the workflow instance action. * If the state of the instance is not null, the status of the instance is updated based on the new state. * If the state of the instance is null, neither state nor status are updated. * @param instance The instance to be updated. * @param action The action to be inserted. Can be null. * @return True if the update was successful, false otherwise. */ @Transactional public boolean updateWorkflowInstance(WorkflowInstance instance, WorkflowInstanceAction action) { Assert.notNull(instance, "Workflow instance can not be null"); Assert.notNull(action, "Workflow instance action can not be null"); WorkflowInstance.Builder builder = new WorkflowInstance.Builder(instance); if (instance.state == null) { builder.setStatus(null); } else { String type = workflowInstanceDao.getWorkflowInstance(instance.id).type; AbstractWorkflowDefinition<?> definition = workflowDefinitionService.getWorkflowDefinition(type); builder.setStatus(definition.getState(instance.state).getType().getStatus(instance.nextActivation)); } WorkflowInstance updatedInstance = builder.build(); boolean updated = workflowInstanceDao.updateNotRunningWorkflowInstance(updatedInstance); if (updated) { String currentState = workflowInstanceDao.getWorkflowInstanceState(updatedInstance.id); WorkflowInstanceAction updatedAction = new WorkflowInstanceAction.Builder(action).setState(currentState).build(); workflowInstanceDao.insertWorkflowInstanceAction(updatedInstance, updatedAction); } return updated; }