Activiti7 驳回任务

简介: Activiti7 驳回任务

Activiti7 驳回任务

下面操作在多分支情况下没有做详细判断,使用需注意.

操作说明

  1. 通过执行实例id获取bpmnModel
  2. 通过历史记录找到上一条任务并找到任务定义
  3. 通过任务定义找到上一个任务入口
  4. 设置当前执行实例的当前节点,并执行流程引擎计算下一节点.
package com.meijm.activiti.common.flow;
import cn.hutool.core.collection.CollectionUtil;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.delegate.event.ActivitiEventDispatcher;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.HistoricTaskInstanceQueryImpl;
import org.activiti.engine.impl.HistoricTaskInstanceQueryProperty;
import org.activiti.engine.impl.cmd.DeleteProcessInstanceCmd;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.identity.Authentication;
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.HistoricTaskInstanceEntityManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.activiti.engine.task.IdentityLinkType;
import java.util.List;
import java.util.Map;
public class RejectTask implements Command<Void> {
    private String taskId;
    private String rejectReason;
    private Map<String, Object> variables;
    public RejectTask(String taskId, Map<String, Object> variables, String rejectReason) {
        this.taskId = taskId;
        this.variables = variables;
        this.rejectReason = rejectReason;
    }
    @Override
    public Void execute(CommandContext commandContext) {
        TaskEntity task = commandContext.getTaskEntityManager().findById(taskId);
        HistoricTaskInstanceEntityManager historicTaskInstanceEntityManager = commandContext.getHistoricTaskInstanceEntityManager();
        HistoricTaskInstanceQueryImpl historicTaskInstanceQuery = new HistoricTaskInstanceQueryImpl();
        historicTaskInstanceQuery.processInstanceId(task.getProcessInstanceId());
        historicTaskInstanceQuery.orderBy(HistoricTaskInstanceQueryProperty.START).desc();
        List<HistoricTaskInstance> htiList = historicTaskInstanceEntityManager.findHistoricTaskInstancesByQueryCriteria(historicTaskInstanceQuery);
        if (CollectionUtil.isEmpty(htiList) || htiList.size() < 2) {
            DeleteProcessInstanceCmd cmd = new DeleteProcessInstanceCmd(task.getProcessInstanceId(), rejectReason);
            cmd.execute(commandContext);
        } else {
            // list里的第二条代表上一个任务
            HistoricTaskInstance lastTask = htiList.get(1);
            String processDefinitionId = lastTask.getProcessDefinitionId();
            BpmnModel bpmnModel = ProcessDefinitionUtil.getBpmnModel(processDefinitionId);
            // 得到上个节点的开始位置
            FlowNode lastFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(lastTask.getTaskDefinitionKey());
            FlowElement begin = lastFlowNode.getIncomingFlows().get(0).getSourceFlowElement();
            commandContext.getProcessEngineConfiguration().getListenerNotificationHelper().executeTaskListeners(task, TaskListener.EVENTNAME_COMPLETE);
            if (Authentication.getAuthenticatedUserId() != null && task.getProcessInstanceId() != null) {
                ExecutionEntity processInstanceEntity = commandContext.getExecutionEntityManager().findById(task.getProcessInstanceId());
                commandContext.getIdentityLinkEntityManager().involveUser(processInstanceEntity, Authentication.getAuthenticatedUserId(), IdentityLinkType.PARTICIPANT);
            }
            ActivitiEventDispatcher eventDispatcher = Context.getProcessEngineConfiguration().getEventDispatcher();
            if (eventDispatcher.isEnabled()) {
                eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityWithVariablesEvent(ActivitiEventType.TASK_COMPLETED, task, variables, false));
            }
            task.setVariables(variables);
            commandContext.getTaskEntityManager().deleteTask(task, rejectReason, false, false);
            ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(task.getExecutionId());
            executionEntity.setCurrentFlowElement(begin);
            commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
        }
        return null;
    }
}

源文件位置

com.meijm.activiti.common.flow.RejectTask

地址:https://gitee.com/MeiJM/spring-cram/tree/master/activiti

参考资料

https://segmentfault.com/a/1190000013952695

目录
相关文章
工作流(Activiti 6.0)之自由驳回任务实现
工作流版本使用6.0,参数为任务id(task中主键),目标节点ID(比如userTask1),以及业务主键信息(businessKey)。
|
7月前
|
移动开发 前端开发
基于jeecg-boot的flowable流程自定义业务退回撤回或驳回到发起人后的再次流程提交
基于jeecg-boot的flowable流程自定义业务退回撤回或驳回到发起人后的再次流程提交
240 0
|
7月前
|
移动开发 前端开发
基于jeecg-boot的flowable流程审批时增加下一个审批人设置
基于jeecg-boot的flowable流程审批时增加下一个审批人设置
612 0
|
7月前
|
前端开发
基于jeecg-boot的flowable流程增加部门经理审批功能
基于jeecg-boot的flowable流程增加部门经理审批功能
93 0
|
7月前
|
前端开发
基于jeecg-boot的flowable流程自定义业务驳回到发起人的一种处理方式
基于jeecg-boot的flowable流程自定义业务驳回到发起人的一种处理方式
308 0
|
7月前
|
移动开发 前端开发 Java
基于jeecg-boot的flowable流程提供一种动态设置发起人部门负责人的方式
基于jeecg-boot的flowable流程提供一种动态设置发起人部门负责人的方式
140 0
|
7月前
|
SQL 前端开发
基于jeecgboot的flowable驳回修改以及发起人设置
基于jeecgboot的flowable驳回修改以及发起人设置
249 0
|
7月前
|
运维
【问题篇】activiti通过修改表手动退回流程
【问题篇】activiti通过修改表手动退回流程
161 1
|
7月前
Activiti 流程发布
Activiti 流程发布
54 0
21activiti - 流程管理定义(办理任务)
21activiti - 流程管理定义(办理任务)
37 0