com.iqiny.silly.activiti.RejectToAnyWhereTaskCmd Maven / Gradle / Ivy
/*
* Copyright iqiny.com
*
* https://gitee.com/iqiny/silly
*
* project name:silly-activiti-52
* project description:top silly project pom.xml file
*/
package com.iqiny.silly.activiti;
import com.iqiny.silly.common.util.StringUtils;
import com.iqiny.silly.core.engine.SillyTask;
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.process.ActivityImpl;
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 ActivityImpl desActivity;
/**
* 流程变量
*/
protected Map varMap;
/**
* 当前节点对象
*/
protected ActivityImpl currentActivity;
/**
* 是否删除其余执行器上的任务
*/
protected boolean deleteOtherTaskFlag;
public RejectToAnyWhereTaskCmd(SillyTask currentTask, ActivityImpl desActivity,
Map varMap, ActivityImpl 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 = Context.getCommandContext().getExecutionEntityManager();
ExecutionEntity executionEntity = executionEntityManager.findExecutionById(executionId);
executionEntity.setVariables(varMap);
executionEntity.setEventSource(this.currentActivity);
executionEntity.setActivity(this.currentActivity);
if (deleteOtherTaskFlag) {
String parentId = executionEntity.getParentId();
deleteOtherTask(parentId);
} else {
TaskEntity taskEntity = Context.getCommandContext()
.getTaskEntityManager().findTaskById(currentTask.getId());
deleteTask(taskEntity);
}
// 并行节点需要额外添加一层 execution
Object multiInstance = this.desActivity.getProperty("multiInstance");
if ("parallel".equals(multiInstance) || "sequential".equals(multiInstance)) {
executionEntity = executionEntity.createExecution();
}
executionEntity.executeActivity(this.desActivity);
return null;
}
protected void deleteOtherTask(String parentId) {
List executionEntities = new ArrayList<>();
if (StringUtils.isNotEmpty(parentId)) {
ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
executionEntities = executionEntityManager.findChildExecutionsByParentExecutionId(parentId);
}
if (executionEntities.isEmpty()) {
// 非子流程、非多实例
List tasksByExecutionId = Context.getCommandContext()
.getTaskEntityManager()
.findTasksByExecutionId(this.executionId);
for (TaskEntity taskEntity : tasksByExecutionId) {
deleteTask(taskEntity);
}
} else {
// 子流程/多实例
for (ExecutionEntity execution : executionEntities) {
List tasksByExecutionId = Context.getCommandContext()
.getTaskEntityManager()
.findTasksByExecutionId(execution.getId());
// 子任务全部进行驳回操作(触发驳回事件 并删除任务)
for (TaskEntity taskEntity : tasksByExecutionId) {
deleteTask(taskEntity);
}
}
}
}
protected void deleteTask(TaskEntity taskEntity) {
// 触发任务监听
taskEntity.fireEvent(EVENT_NAME);
// 删除任务的原因
Context.getCommandContext().getTaskEntityManager()
.deleteTask(taskEntity, TaskEntity.DELETE_REASON_DELETED, false);
}
}