Activiti工作流-任务的办理、回退和拒绝

一、办理任务

	/**
	 * @description 办理任务
	 * @param taskId 需要办理的任务ID
	 * @author 	giserDev
	 * @email 	[email protected]
	 * @date	2020-10-17 11:18:13
	 */
	@Override
	public void completeTask(String taskId) {
		taskService.complete(taskId);
	}

二、回退任务

任务的回退即将当前任务退回到上一次审批级别。

	/**
	 * @description 驳回任务 
	 * @param taskId 任务ID
	 * @author 	giserDev
	 * @email 	[email protected]
	 * @date	2020-10-17 20:22:28
	 */
	void taskRollBack(String taskId);
	@Autowired
	private ProcessEngine processEngine;
	
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private HistoryService historyService;
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private RepositoryService repositoryService;
	
	@Autowired
	private ManagementService managementService;
	
	/**
	 * @description 任务回退
	 * @param taskId 当前任务ID
	 * @author 	giserDev
	 * @email 	[email protected]
	 * @date	2020-10-18 02:04:03
	 */
	public void taskRollBack(String taskId){
	    try {
           
            /**	取得当前任务	*/
            HistoricTaskInstance currTask = historyService
                    .createHistoricTaskInstanceQuery().taskId(taskId)
                    .singleResult();
            
            /**	取得流程实例	*/
            ProcessInstance currInstance = runtimeService
            		.createProcessInstanceQuery()
            		.processInstanceId(currTask.getProcessInstanceId())
            		.singleResult();
            if (currInstance == null) {
            	/**	流程结束	*/
            	return;
            }
            
            /**	取得流程定义	*/
            ProcessDefinitionEntity definition = (ProcessDefinitionEntity) (processEngine.getRepositoryService().getProcessDefinition(currTask
            		.getProcessDefinitionId()));
            if (definition == null) {
            	return ;
            }

            /**	获取前一步操作任务定义key	*/
            String previousTaskDefKey = TaskDefinitionKey.getPreviousTaskDefKey(currTask.getTaskDefinitionKey());
            if (previousTaskDefKey == null) {
				return;
			}
            
            /**	取得上一步活动	*/
            ActivityImpl currActivity = ((ProcessDefinitionImpl) definition).findActivity(currTask.getTaskDefinitionKey());
            List<PvmTransition> nextTransitionList = currActivity.getIncomingTransitions();
            
            /**	清除当前活动的出口	*/
            List<PvmTransition> oriPvmTransitionList = new ArrayList<PvmTransition>();
            List<PvmTransition> pvmTransitionList = currActivity
                    .getOutgoingTransitions();
            for (PvmTransition pvmTransition : pvmTransitionList) {
                oriPvmTransitionList.add(pvmTransition);
            }
            pvmTransitionList.clear();
            
            /**	建立新出口	*/
            List<TransitionImpl> newTransitions = new ArrayList<TransitionImpl>();
            for (PvmTransition nextTransition : nextTransitionList) {
                PvmActivity nextActivity = nextTransition.getSource();
                
                /**	只将目标任务作为新出口	*/
                if (nextActivity.getId().equals(previousTaskDefKey)) {
	                ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition).findActivity(nextActivity.getId());
	            	TransitionImpl newTransition = currActivity.createOutgoingTransition();
	            	newTransition.setDestination(nextActivityImpl);
	            	newTransitions.add(newTransition);
                }
            }
            
            /**	获取当前流程实例的流程变量	*/
            Map<String, Object> currInstanceVariables = currInstance.getProcessVariables();
            /**	完成任务	*/
            List<Task> tasks = taskService.createTaskQuery()
                    .processInstanceId(currInstance.getId())
                    .taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
            for (Task task : tasks) {
                taskService.complete(task.getId(), currInstanceVariables);
                historyService.deleteHistoricTaskInstance(task.getId());
            }

            /**	恢复方向	*/
            for (TransitionImpl transitionImpl : newTransitions) {
                currActivity.getOutgoingTransitions().remove(transitionImpl);
            }
            for (PvmTransition pvmTransition : oriPvmTransitionList) {
                pvmTransitionList.add(pvmTransition);
            }
          
            return ;
        } catch (Exception e) {
            return ;
        }
	}
  • 补充:
    根据流程图中各审批节点的定义,设置节点ID,对应实体类为:
package com.giser.activiti.constant;

/**
 * @description 任务状态
 * @author 	giserDev
 * @email 	[email protected]
 * @date	2020-10-18 02:09:07
 */
public enum TaskDefinitionKey {
	
	START("startEvent", "开始"),
	LAUNCH("launchProc", "发起流程"),
	PROJECTAPPR("projManagerAppr", "项目经理审批"),
	DEPTAPPR("deptManagerAppr", "部门经理审批"),
	End("endEvent", "结束");
	
	private String key;
	private String message;
	
	private TaskDefinitionKey(String key, String message) {
		this.key = key;
		this.message = message;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	/**
	 * @description 获取前一步任务
	 * @return
	 * @author 	giserDev
	 * @email 	[email protected]
	 * @date	2020-10-18 02:25:53
	 */
	public static String getPreviousTaskDefKey(String currentTaskDefKey) {
		if (LAUNCH.key.equals(currentTaskDefKey)) {
			return null;
		}
		
		TaskDefinitionKey[] taskDefinitionKeyArray = TaskDefinitionKey.values();
		for (int i=taskDefinitionKeyArray.length - 1; i >= 0; i--){
			if (taskDefinitionKeyArray[i].key.equals(currentTaskDefKey)) {
				--i;
				return taskDefinitionKeyArray[i].key;
			}
		}
		return null;
	}
		
}

三、拒绝任务

任务的拒绝,即无论当前审批到哪一个节点,都回到待提交审批的状态。

	/**
	 * @description 拒绝任务
	 * @param taskId 任务ID
	 * @author 	giserDev
	 * @param processInstanceId 
	 * @email 	[email protected]
	 * @date	2020-10-18 11:20:36
	 */
	void rejectTask(String taskId, String processInstanceId);
	/**
	 * @description 拒绝任务
	 * @param taskId 任务ID
	 * @author 	giserDev
	 * @email 	[email protected]
	 * @date	2020-10-18 11:22:00
	 */
	public void rejectTask(String currTaskId, String processInstanceId){
		/**
		 *	获取要跳转的任务ID
		 */
		HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).taskDefinitionKey(TaskDefinitionKey.LAUNCH.getKey());
		historicTaskInstanceQuery.orderByTaskCreateTime().asc();
		List<HistoricTaskInstance> historicTaskInstanceList = historicTaskInstanceQuery.list();
		HistoricTaskInstance historicTaskInstance = historicTaskInstanceList.get(0);
		
	    //	根据要跳转的任务ID获取其任务
	    HistoricTaskInstance hisTask = historyService
	                .createHistoricTaskInstanceQuery().taskId(historicTaskInstance.getId())
	                .singleResult();
	    //	获取流程实例
	    ProcessInstance instance = runtimeService
	                .createProcessInstanceQuery()
	                .processInstanceId(hisTask.getProcessInstanceId())
	                .singleResult();
	    //	取得流程定义
	    ProcessDefinitionEntity definition = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(hisTask.getProcessDefinitionId());
	    //	获取历史任务的Activity
	    ActivityImpl hisActivity = definition.findActivity(hisTask.getTaskDefinitionKey());
	    //	实现跳转
	    managementService.executeCommand(new JumpCmd(instance.getId(), hisActivity.getId()));
	}

	public class JumpCmd implements Command<ExecutionEntity>{

	    private String processInstanceId;
	    private String activityId;
	    public static final String REASION_DELETE = "deleted";

	    public JumpCmd(String processInstanceId, String activityId) {
	        this.processInstanceId = processInstanceId;
	        this.activityId = activityId;
	    }

	    @Override
	    public ExecutionEntity execute(CommandContext commandContext) {
	        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);

	        executionEntity.destroyScope(REASION_DELETE);
	        ProcessDefinitionImpl processDefinition = executionEntity.getProcessDefinition();
	        ActivityImpl activity = processDefinition.findActivity(activityId);
	        executionEntity.executeActivity(activity);

	        return executionEntity;
	    }

	}

你可能感兴趣的:(#,Activiti)