本文主要是介绍工作流Activiti初体验—流程撤回【二】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
已经玩工作流了,打算还是研究一下撤回的功能。但是流程图里面并不带撤回的组件,所以需要自己动态改造一下,还是延续上一个流程继续试验撤回功能。《工作流Activiti初体验【一】》
完整流程图
我们研究一下分发任务撤回到发起任务,其他环节的撤回类似
撤回的原理大概如下:
将分发任务后面的方向清空,把发起任务拼接到原来的判断网关,然后结束分发任务,这样流程就到发起任务了
此时的流程如上图,并且当前节点是发起任务,而后再将分发任务后面的方向清空,并还原为最初的方向,如下图:
撤回代码
/*** 撤回流程*/private void recall() {List<String> roleCodeList = new ArrayList<>();roleCodeList.add("user1");// 取得已提交的任务HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskCandidateGroupIn(roleCodeList).singleResult();// HistoricTaskInstance historicTaskInstance =// historyService.createHistoricTaskInstanceQuery().taskAssignee(assignee).singleResult();// HistoricTaskInstance historicTaskInstance =// historyService.createHistoricTaskInstanceQuery().taskCandidateUser(assignee).singleResult();System.out.println(historicTaskInstance.getId());// 取得流程实例ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(historicTaskInstance.getProcessInstanceId()).singleResult();System.out.println(processInstance.getId());Map<String, Object> variables = runtimeService.getVariables(historicTaskInstance.getExecutionId());System.out.println(variables);// 取得流程定义ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(historicTaskInstance.getProcessDefinitionId());System.out.println(definitionEntity);// 取得上一步活动ActivityImpl hisActivity = definitionEntity.findActivity(historicTaskInstance.getTaskDefinitionKey());System.out.println(hisActivity);// 取得当前活动List<PvmTransition> currTransitionList = hisActivity.getOutgoingTransitions();System.out.println(currTransitionList);for (PvmTransition currTransition : currTransitionList) {PvmActivity currActivity = currTransition.getDestination();List<Task> currTasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDefinitionKey(currActivity.getId()).list();for (Task currTask : currTasks) {ArrayList<PvmTransition> oriPvmTransitionList = new ArrayList<>();List<PvmTransition> pvmTransitionList = currActivity.getOutgoingTransitions();System.out.println(pvmTransitionList);for (PvmTransition pvmTransition : pvmTransitionList) {oriPvmTransitionList.add(pvmTransition);}System.out.println(oriPvmTransitionList);pvmTransitionList.clear();// 建立新方向ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definitionEntity).findActivity(currActivity.getId());TransitionImpl newTransition = nextActivityImpl.createOutgoingTransition();newTransition.setDestination(hisActivity);taskService.claim(currTask.getId(), null);taskService.complete(currTask.getId(), variables);historyService.deleteHistoricTaskInstance(currTask.getId());// 恢复方向hisActivity.getIncomingTransitions().remove(newTransition);List<PvmTransition> pvmTList = currActivity.getOutgoingTransitions();pvmTList.clear();for (PvmTransition pvmTransition : oriPvmTransitionList) {pvmTransitionList.add(pvmTransition);}System.out.println(pvmTransitionList);}}historyService.deleteHistoricTaskInstance(historicTaskInstance.getId());}
项目地址 Activiti-Test
这篇关于工作流Activiti初体验—流程撤回【二】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!