All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iqiny.silly.flowable.RejectToAnyWhereTaskCmd Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
/*
 *  Copyright  iqiny.com
 *
 *  https://gitee.com/iqiny/silly
 *
 *  project name:silly-flowable-68
 *  project description:top silly project pom.xml file
 */
package com.iqiny.silly.flowable;

import com.iqiny.silly.common.util.StringUtils;
import com.iqiny.silly.core.engine.SillyTask;
import org.flowable.bpmn.model.Activity;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * activiti 任务驳回任意节点 CMD
 */
public class RejectToAnyWhereTaskCmd implements Command {

    public static final String EVENT_NAME = "reject";

    /**
     * 当前任务ID
     */
    protected SillyTask currentTask;
    /**
     * 当前执行器ID
     */
    protected String executionId;
    /**
     * 目标节点ID (nodeKey)
     */
    protected FlowElement desActivity;
    /**
     * 流程变量
     */
    protected Map varMap;
    /**
     * 当前节点对象
     */
    protected FlowElement currentActivity;
    /**
     * 是否删除其余执行器上的任务
     */
    protected boolean deleteOtherTaskFlag;

    public RejectToAnyWhereTaskCmd(SillyTask currentTask, FlowElement desActivity,
                                   Map varMap, FlowElement currentActivity,
                                   boolean deleteOtherTaskFlag) {
        this.currentTask = currentTask;
        this.executionId = currentTask.getExecutionId();
        this.desActivity = desActivity;
        this.varMap = varMap;
        this.currentActivity = currentActivity;
        this.deleteOtherTaskFlag = deleteOtherTaskFlag;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
        ExecutionEntity executionEntity = executionEntityManager.findById(executionId);
        executionEntity.setVariables(varMap);
        executionEntity.setCurrentFlowElement(this.currentActivity);
        if (deleteOtherTaskFlag) {
            String parentId = executionEntity.getParentId();
            deleteOtherTask(parentId);
        } else {
            TaskEntity taskEntity = CommandContextUtil.getTaskService().getTask(currentTask.getId());
            deleteTask(taskEntity);
        }

        if (this.desActivity instanceof Activity) {
            // 多实例节点需要额外添加一层 execution
            final MultiInstanceLoopCharacteristics loopCharacteristics = ((Activity) this.desActivity)
                    .getLoopCharacteristics();
            if (loopCharacteristics != null) {
                executionEntity = executionEntityManager.create();
            }
        }

        executionEntity.setCurrentFlowElement(this.desActivity);

        return null;
    }

    protected void deleteOtherTask(String parentId) {
        List executionEntities = new ArrayList<>();
        if (StringUtils.isNotEmpty(parentId)) {
            ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
            executionEntities = executionEntityManager.findChildExecutionsByParentExecutionId(parentId);
        }
        if (executionEntities.isEmpty()) {
            // 非子流程、非多实例
            List tasksByExecutionId = CommandContextUtil.getTaskService()
                    .findTasksByExecutionId(this.executionId);
            for (TaskEntity taskEntity : tasksByExecutionId) {
                deleteTask(taskEntity);
            }

        } else {
            // 子流程/多实例
            for (ExecutionEntity execution : executionEntities) {
                List tasksByExecutionId = CommandContextUtil.getTaskService()
                        .findTasksByExecutionId(execution.getId());
                // 子任务全部进行驳回操作(触发驳回事件 并删除任务)
                for (TaskEntity taskEntity : tasksByExecutionId) {
                    deleteTask(taskEntity);
                }
            }
        }
    }


    protected void deleteTask(TaskEntity taskEntity) {
        // 删除任务的原因
        CommandContextUtil.getTaskService()
                .deleteTask(taskEntity, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy