工作流Activiti初体验—流程撤回【二】

已经玩工作流了,打算还是研究一下撤回的功能。但是流程图里面并不带撤回的组件,所以需要自己动态改造一下,还是延续上一个流程继续试验撤回功能。《工作流Activiti初体验【一】》

完整流程图
工作流Activiti初体验—流程撤回【二】_第1张图片

我们研究一下分发任务撤回到发起任务,其他环节的撤回类似
撤回的原理大概如下:

工作流Activiti初体验—流程撤回【二】_第2张图片

将分发任务后面的方向清空,把发起任务拼接到原来的判断网关,然后结束分发任务,这样流程就到发起任务了
工作流Activiti初体验—流程撤回【二】_第3张图片
此时的流程如上图,并且当前节点是发起任务,而后再将分发任务后面的方向清空,并还原为最初的方向,如下图:
工作流Activiti初体验—流程撤回【二】_第4张图片

撤回代码

	/**
	 * 撤回流程
	 */
	private void recall() {
		List 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 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 currTransitionList = hisActivity.getOutgoingTransitions();
		System.out.println(currTransitionList);

		for (PvmTransition currTransition : currTransitionList) {
			PvmActivity currActivity = currTransition.getDestination();
			List currTasks = taskService.createTaskQuery().processInstanceId(processInstance.getId())
					.taskDefinitionKey(currActivity.getId()).list();
			for (Task currTask : currTasks) {
				ArrayList oriPvmTransitionList = new ArrayList<>();
				List 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 pvmTList = currActivity.getOutgoingTransitions();
				pvmTList.clear();
				for (PvmTransition pvmTransition : oriPvmTransitionList) {
					pvmTransitionList.add(pvmTransition);
				}
				System.out.println(pvmTransitionList);
			}
		}
		historyService.deleteHistoricTaskInstance(historicTaskInstance.getId());
	}

项目地址 Activiti-Test

你可能感兴趣的:(后端,工作流,Activiti,工作流撤回)