private void fillActions(WorkflowInstance instance, boolean includeStateVariables, Long maxActions) { Map<Integer, Map<String, String>> actionStates = includeStateVariables ? fetchActionStateVariables(instance) : EMPTY_ACTION_STATE_MAP; String limit = Long.toString(getMaxActions(maxActions)); String sql = sqlVariants.limit("select * from nflow_workflow_action where workflow_id = ? order by id desc", limit); instance.actions.addAll(jdbc.query(sql, new WorkflowInstanceActionRowMapper(actionStates), instance.id)); }
public WorkflowInstance getWorkflowInstance(int id) { String sql = "select w.*, " + "(select min(execution_start) from nflow_workflow_action a where a.workflow_id = w.id) as started " + "from nflow_workflow w where w.id = ?"; WorkflowInstance instance = jdbc.queryForObject(sql, new WorkflowInstanceRowMapper(), id); fillState(instance); fillChildWorkflows(instance); return instance; }
/** * Return the workflow instance matching the given id. * @param id Workflow instance id. * @return The workflow instance, or null if not found. */ public WorkflowInstance getWorkflowInstance(int id) { return workflowInstanceDao.getWorkflowInstance(id); }
void insertVariables(final int id, final int actionId, Map<String, String> changedStateVariables) { if (changedStateVariables.isEmpty()) { return; } if (sqlVariants.useBatchUpdate()) { insertVariablesWithBatchUpdate(id, actionId, changedStateVariables); } else { insertVariablesWithMultipleUpdates(id, actionId, changedStateVariables); } }
@Transactional(propagation = MANDATORY) public int insertWorkflowInstanceAction(final WorkflowInstance instance, final WorkflowInstanceAction action) { int actionId = insertWorkflowInstanceAction(action); insertVariables(action.workflowInstanceId, actionId, instance.getChangedStateVariables()); return actionId; }
@Override protected void doInTransactionWithoutResult(TransactionStatus status) { updateWorkflowInstance(instance); int parentActionId = insertWorkflowInstanceAction(action); insertVariables(action.workflowInstanceId, parentActionId, changedStateVariables); for (WorkflowInstance childTemplate : childWorkflows) { Integer rootWorkflowId = instance.rootWorkflowId == null ? instance.id : instance.rootWorkflowId; WorkflowInstance childWorkflow = new WorkflowInstance.Builder(childTemplate).setRootWorkflowId(rootWorkflowId) .setParentWorkflowId(instance.id).setParentActionId(parentActionId).build(); insertWorkflowInstance(childWorkflow); } for (WorkflowInstance workflow : workflows) { insertWorkflowInstance(workflow); } } });
/** * 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; }
sql += " where " + collectionToDelimitedString(conditions, " and ") + " order by w.created desc"; sql = sqlVariants.limit(sql, ":limit"); params.addValue("limit", getMaxResults(query.maxResults)); List<WorkflowInstance> ret = namedJdbc.query(sql, params, new WorkflowInstanceRowMapper()); for (WorkflowInstance instance : ret) { fillState(instance); fillActions(instance, query.includeActionStateVariables, query.maxActions); fillChildWorkflows(instance);
insertVariables(id, 0, instance.stateVariables); return id;