工作流(Activiti 6.0)之自由驳回任务实现

工作流版本使用6.0,参数为任务id(task中主键),目标节点ID(比如userTask1),以及业务主键信息(businessKey)。



	/**
	 * 任务节点跳转
	 * @param taskId 当前任务id
	 * @param flowElementId 跳转的目标节点的id
	 */
	public void taskBack(String taskId,String flowElementId,Map<String, Object> variables){
		ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
		//当前任务
		Task currentTask = processEngine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
		//更新业务信息
		processEngine.getTaskService().setVariables(taskId , variables);
		//获取流程定义
		org.activiti.bpmn.model.Process process = processEngine.getRepositoryService().getBpmnModel(currentTask.getProcessDefinitionId()).getMainProcess();
		//获取目标节点定义
		FlowNode targetNode = (FlowNode)process.getFlowElement(flowElementId);
		//删除当前运行任务
		String executionEntityId =processEngine.getManagementService().executeCommand(new DeleteTaskCommand(currentTask.getId()));
		//流程执行到来源节点
		processEngine.getManagementService().executeCommand(new JumpCommand(targetNode, executionEntityId));
	}

	/**
	 * 删除当前运行时任务命令
	 * 这里继承了NeedsActiveTaskCmd,主要是很多跳转业务场景下,要求不能时挂起任务。可以直接继承Command即可
	 */
	public class DeleteTaskCommand extends NeedsActiveTaskCmd<String> {
		public DeleteTaskCommand(String taskId){
			super(taskId);
		}
		@Override
		public String execute(CommandContext commandContext, TaskEntity currentTask){
			//获取所需服务
			TaskEntityManagerImpl taskEntityManager = (TaskEntityManagerImpl)commandContext.getTaskEntityManager();
			//获取当前任务的来源任务及来源节点信息
			ExecutionEntity executionEntity = currentTask.getExecution();
			//删除当前任务,来源任务
			taskEntityManager.deleteTask(currentTask, "jumpReason", false, false);
			return executionEntity.getId();
		}
		@Override
		public String getSuspendedTaskException() {
			return "挂起的任务不能跳转";
		}
	}


	/**
	 * 根据提供节点和执行对象id,进行跳转命令
	 */
	public class JumpCommand implements Command<Void> {
		private FlowNode flowElement;
		private String executionId;
		public JumpCommand(FlowNode flowElement, String executionId){
			this.flowElement = flowElement;
			this.executionId = executionId;
		}
		@Override
		public Void execute(CommandContext commandContext){
			//获取目标节点的来源连线
			List<SequenceFlow> flows = flowElement.getIncomingFlows();
			if(flows==null || flows.size()<1){
				throw new ActivitiException("操作错误,目标节点没有来源连线");
			}
			//随便选一条连线来执行,时当前执行计划为,从连线流转到目标节点,实现跳转
			ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
			executionEntity.setCurrentFlowElement(flows.get(0));
			commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
			return null;
		}
	}

0
分享到:

评论0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
网站所有系统都包含源代码+数据库+导入视频+演示视频+典型代码讲解视频+远程部署成功+售后答疑,需要联系客服微信:mtyeducation
没有账号? 忘记密码?
豫ICP备19004322号-2