Activiti 任意驳回

参考:http://blog.csdn.net/qq_30739519/article/details/51248516

本文基于分享牛(http://blog.csdn.net/qq_30739519)的博文进行了完善,首先在此表示感谢!

修改内容主要涉及类如下:

Activiti源码:

1、org.activiti.engine.impl.persistence.entity.TaskEntity增加删除原因常量的定义

  public static final String DELETE_REASON_REJECTED = "rejected";

2、org.activiti.engine.delegate.TaskListener增加事件名称常量的定义

  String EVENTNAME_REJECT = "reject";


3、自定义cmd(基于分享牛博文完善,解决了对并行网关的处理)

package com.xszhao.activiti.cmd;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;

/**
 * 
 * JD节点的跳转 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)
 */
public class RejectToAnyWhereTaskCmd implements Command {
	protected String executionId;
	protected ActivityImpl desActivity;
	protected Map paramvar;
	protected ActivityImpl currentActivity;
	protected ReadOnlyProcessDefinition processDefinitionEntity;
	/**
	 * 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)
	 */
	public Void execute(CommandContext commandContext) {
		ExecutionEntityManager executionEntityManager = Context
				.getCommandContext().getExecutionEntityManager();
		// 获取当前流程的executionId,因为在并发的情况下executionId是唯一的。
		ExecutionEntity executionEntity = executionEntityManager
				.findExecutionById(executionId);
		executionEntity.setVariables(paramvar);
		executionEntity.setEventSource(this.currentActivity);
		executionEntity.setActivity(this.currentActivity);
		
		List executionEntitys = executionEntityManager.findChildExecutionsByParentExecutionId(executionEntity.getParentId());
		if(!executionEntitys.isEmpty()){
			String executionParentId = executionEntity.getParentId();
			for(ExecutionEntity execution : executionEntitys){
				// 根据executionId 获取Task
				Iterator localIterator = Context.getCommandContext()
						.getTaskEntityManager()
						.findTasksByExecutionId(execution.getId()).iterator();
				
				while (localIterator.hasNext()) {
					TaskEntity taskEntity = (TaskEntity) localIterator.next();
					// 触发任务监听
					taskEntity.fireEvent(TaskListener.EVENTNAME_REJECT);
					// 删除任务的原因
					Context.getCommandContext().getTaskEntityManager()
					.deleteTask(taskEntity, TaskEntity.DELETE_REASON_REJECTED, false);
				}
				execution.remove();
			}
			
			ExecutionEntity parentExecutionEntity = executionEntityManager
					.findExecutionById(executionParentId);
			ActivityImpl pActivityImpl = (ActivityImpl) this.processDefinitionEntity.findActivity(parentExecutionEntity.getActivityId());
			executionEntity.setVariables(paramvar);
			executionEntity.setEventSource(pActivityImpl);
			executionEntity.setActivity(pActivityImpl);
			parentExecutionEntity.executeActivity(this.desActivity);
			
		}else{
			// 根据executionId 获取Task
			Iterator localIterator = Context.getCommandContext()
					.getTaskEntityManager()
					.findTasksByExecutionId(this.executionId).iterator();
			
			while (localIterator.hasNext()) {
				TaskEntity taskEntity = (TaskEntity) localIterator.next();
				// 触发任务监听
				taskEntity.fireEvent("complete");
				// 删除任务的原因
				Context.getCommandContext().getTaskEntityManager()
				.deleteTask(taskEntity, "completed", false);
			}
			executionEntity.executeActivity(this.desActivity);
		}
		return null;
	}

	/**
	 * 构造参数 可以根据自己的业务需要添加更多的字段 分享牛原创(尊重原创
	 * 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)
	 * 
	 * @param executionId
	 * @param desActivity
	 * @param paramvar
	 * @param currentActivity
	 */
	public RejectToAnyWhereTaskCmd(String executionId, ActivityImpl desActivity,
			Map paramvar, ActivityImpl currentActivity,
			ReadOnlyProcessDefinition processDefinitionEntity) {
		this.executionId = executionId;
		this.desActivity = desActivity;
		this.paramvar = paramvar;
		this.currentActivity = currentActivity;
		this.processDefinitionEntity = processDefinitionEntity;
	}
}

4、测试流程,初审任务提交后,生成下户调研和订单面审两个任务,任意一个驳回,都跳转到初审(其实流程的其他任何节点都可以,只是我这个目的是驳回,逻辑上跳到之前的节点才合理,具体怎么用,可以参考分享牛的博文(http://blog.csdn.net/qq_30739519/article/details/51248516),写的很清晰。)
Activiti 任意驳回_第1张图片


	@Test
	public void jumpTask() {

		ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("activitiWithoutPropertyPlaceholderConfigurer.cfg.xml");
		ProcessEngine processEngine = processEngineConfiguration
				.buildProcessEngine();

		Map submitParams = new HashMap();
		
		// 分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)
		RepositoryService repositoryService = processEngine.getRepositoryService();
		ReadOnlyProcessDefinition processDefinitionEntity = (ReadOnlyProcessDefinition) repositoryService
				.getProcessDefinition("houseMortgageLoan:1:4");
		// 目标节点
		ActivityImpl destinationActivity = (ActivityImpl) processDefinitionEntity
				.findActivity("preAudit");
		String executionId = "27503";

		// 当前节点
		ActivityImpl currentActivity = (ActivityImpl) processDefinitionEntity
				.findActivity("faceToFaceAudit");
		processEngine.getManagementService().executeCommand(
				new RejectToAnyWhereTaskCmd(executionId, destinationActivity, submitParams,
						currentActivity,processDefinitionEntity));
	}


你可能感兴趣的:(Activiti)