activiti驳回到上级以及驳回到首个任务

参考:https://blog.csdn.net/u013292160/article/details/66973243
以及 https://blog.csdn.net/zhangpan_soft/article/details/78761937

上面第一个博客的驳回代码量比较少,开始是参考这个。但是不知道是不是版本的问题(我的activiti是5.18),实际运行会报空指针错误。debug后发现是outgoing transition的id为空导致。只需在初始化transitionImpl的时候传入id即可,id的值应为出口指向activity的id。
transitionImpl = currActivity.createOutgoingTransition(lastActivity.getId());

下面直接贴代码,思路上两个博客都有借鉴。代码量比第一个多比第二个少。
由于是驳回到上一个节点,将当前activity的outgoing出口清空,从历史中找到最后完成的activity,然后把当前出口指向最后完成的activity。complete task,任务就会回到上一个节点中。最后再把出口改回去即可。

@Test
	public void reject() {
		
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		TaskService taskService = processEngine.getTaskService();
		HistoryService historyService = processEngine.getHistoryService();
		RuntimeService runtimeService = processEngine.getRuntimeService();
		RepositoryService repositoryService = processEngine.getRepositoryService();
		String taskId = "120002";//这里根据你自己的taskid来写
		Map variables = new HashMap<>();
		
		//获取当前任务
		HistoricTaskInstance currTask = historyService.createHistoricTaskInstanceQuery()
						.taskId(taskId)
						.singleResult();
		//获取流程实例
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
						.processInstanceId(currTask.getProcessInstanceId())
						.singleResult();
		//获取流程定义
		ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(currTask.getProcessDefinitionId());
		if (processDefinitionEntity == null) {
			System.out.println("不存在的流程定义。");
			
		}
		
		//获取当前activity
		ActivityImpl currActivity = ((ProcessDefinitionImpl) processDefinitionEntity)
                .findActivity(currTask.getTaskDefinitionKey());
		
		//获取当前任务流入
		List histTransitionList = currActivity
                .getIncomingTransitions();
	
        
        //清除当前活动出口
        List originPvmTransitionList = new ArrayList();
        List pvmTransitionList = currActivity.getOutgoingTransitions();
        for (PvmTransition pvmTransition : pvmTransitionList) {
        	originPvmTransitionList.add(pvmTransition);
        }
        pvmTransitionList.clear();
        
		//查找上一个user task节点
		List historicActivityInstances = historyService
                .createHistoricActivityInstanceQuery().activityType("userTask")
                .processInstanceId(processInstance.getId())
                .finished()
                .orderByHistoricActivityInstanceEndTime().desc().list();
		TransitionImpl transitionImpl = null;
        if (historicActivityInstances.size() > 0) {
        	ActivityImpl lastActivity = ((ProcessDefinitionImpl) processDefinitionEntity)
                    .findActivity(historicActivityInstances.get(0).getActivityId());
        	//创建当前任务的新出口
        	transitionImpl = currActivity.createOutgoingTransition(lastActivity.getId());
        	transitionImpl.setDestination(lastActivity);
        }else
        {
        	System.out.println("上级节点不存在。");	
        }
        variables = processInstance.getProcessVariables();
        // 完成任务
        List tasks = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
        for (Task task : tasks) {
            taskService.complete(task.getId(), variables);
            historyService.deleteHistoricTaskInstance(task.getId());
        }
        
        // 恢复方向
        currActivity.getOutgoingTransitions().remove(transitionImpl);
        
        for (PvmTransition pvmTransition : originPvmTransitionList) {
            pvmTransitionList.add(pvmTransition);
        }
	}

驳回到最初任务道理相同,只是选择历史中最先完成的user task。

@Test
	public void rejectToTop() {
		
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		TaskService taskService = processEngine.getTaskService();
		HistoryService historyService = processEngine.getHistoryService();
		RuntimeService runtimeService = processEngine.getRuntimeService();
		RepositoryService repositoryService = processEngine.getRepositoryService();
		String taskId = "115003";
		Map variables = new HashMap<>();
		
		//获取当前任务
		HistoricTaskInstance currTask = historyService.createHistoricTaskInstanceQuery()
						.taskId(taskId)
						.singleResult();
		//获取流程实例
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
						.processInstanceId(currTask.getProcessInstanceId())
						.singleResult();
		//获取流程定义
		ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(currTask.getProcessDefinitionId());
		if (processDefinitionEntity == null) {
			System.out.println("不存在的流程定义。");
			
		}
		
		//获取当前activity
		ActivityImpl currActivity = ((ProcessDefinitionImpl) processDefinitionEntity)
                .findActivity(currTask.getTaskDefinitionKey());
		
		//获取当前任务流入
		List histTransitionList = currActivity
                .getIncomingTransitions();
	
        
        //清除当前活动出口
        List originPvmTransitionList = new ArrayList();
        List pvmTransitionList = currActivity.getOutgoingTransitions();
        for (PvmTransition pvmTransition : pvmTransitionList) {
        	originPvmTransitionList.add(pvmTransition);
        }
        pvmTransitionList.clear();
        
		//查找上一个user task节点
		List historicActivityInstances = historyService
                .createHistoricActivityInstanceQuery().activityType("userTask")
                .processInstanceId(processInstance.getId())
                .finished()
                .orderByHistoricActivityInstanceEndTime().asc().list();
		TransitionImpl transitionImpl = null;
        if (historicActivityInstances.size() > 0) {
        	ActivityImpl lastActivity = ((ProcessDefinitionImpl) processDefinitionEntity)
                    .findActivity(historicActivityInstances.get(0).getActivityId());
        	//创建当前任务的新出口
        	transitionImpl = currActivity.createOutgoingTransition(lastActivity.getId());
        	transitionImpl.setDestination(lastActivity);
        }else
        {
        	System.out.println("上级节点不存在。");	
        }
        variables = processInstance.getProcessVariables();
        // 完成任务
        List tasks = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
        for (Task task : tasks) {
            taskService.complete(task.getId(), variables);
            historyService.deleteHistoricTaskInstance(task.getId());
        }
        
        // 恢复方向
        currActivity.getOutgoingTransitions().remove(transitionImpl);
        
        for (PvmTransition pvmTransition : originPvmTransitionList) {
            pvmTransitionList.add(pvmTransition);
        }
		
	}

你可能感兴趣的:(code)