Activiti7工作流引擎:高阶篇 CallActivityTask 子流程跳转到父流程

Posted vbirdbest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti7工作流引擎:高阶篇 CallActivityTask 子流程跳转到父流程相关的知识,希望对你有一定的参考价值。

一:SubProcess简单示例

@Test
public void delopyAllBpmn() 
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    processEngine.getRepositoryService()
            .createDeployment()
            .addClasspathResource("bpmn/LeaveProcess2.bpmn")
            .name("请假流程")
            .deploy();

    processEngine.getRepositoryService()
            .createDeployment()
            .addClasspathResource("bpmn/LeaveProcess1.bpmn")
            .name("请假流程2")
            .deploy();

    processEngine.getRuntimeService()
            .startProcessInstanceByKey("LeaveProcess1");

    TaskService taskService = processEngine.getTaskService();
    Task task = taskService
            .createTaskQuery()
            .singleResult();
    taskService.setAssignee(task.getId(), "zhangsan");
    taskService.complete(task.getId());

二:从子流程内部跳转到父流程

  1. 删除子流程执行流相关的任务办理人
  2. 删除子流程实例对应的任务办理人
  3. 删除子流程对应的变量
  4. 删除当前任务
  5. 删除子流程执行流
  6. 删除子执行实例
  7. 设置当前的执行活动
public class Sub2ParentCommand implements Command<Object> 
    
    private String currentTaskId;

    private String targetTaskDefKey;

    public Sub2ParentCommand(String currentTaskId, String targetTaskDefKey) 
        this.currentTaskId = currentTaskId;
        this.targetTaskDefKey = targetTaskDefKey;
    

    @Override
    public Object execute(CommandContext commandContext) 
        // 根据当前任务id查找当前正在执行的任务
        TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
        TaskEntity taskEntity = taskEntityManager.findById(currentTaskId);

        ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
        // 根据当前任务的exection_id查找act_ru_execution中的执行子流记录(最后一条)(ACT_RU_EXECUTION.ID_=17)
        ExecutionEntity executionEntity = executionEntityManager.findById(taskEntity.getExecutionId());
        // 根据子执行流中的parent_id查找子执行实例对应的记录(ACT_RU_EXECUTION.ID_=15)
        ExecutionEntity parentExecutionEntity = executionEntityManager.findById(executionEntity.getParentId());
        // 根据子执行实例找到父执行流的记录(ACT_RU_EXECUTION.ID_=8)
        ExecutionEntity rootExecutionEntity = executionEntityManager.findById(parentExecutionEntity.getSuperExecutionId());

        // 删除相关的办理人(ID_=17 and ID_=15)  ACT_RU_IDENTITYLINK
        IdentityLinkEntityManager identityLinkEntityManager = commandContext.getIdentityLinkEntityManager();
        identityLinkEntityManager.deleteIdentityLink(executionEntity, null, null, null);
        identityLinkEntityManager.deleteIdentityLink(parentExecutionEntity, null, null, null);

        // 删除子流程实例相关的变量(ID_=15)     ACT_RU_VARIABLE
        VariableInstanceEntityManager variableInstanceEntityManager = commandContext.getVariableInstanceEntityManager();
        List<VariableInstanceEntity> variableInstancesEntityList = variableInstanceEntityManager.findVariableInstancesByExecutionId(parentExecutionEntity.getId());
        for (VariableInstanceEntity variableInstanceEntity : variableInstancesEntityList) 
            variableInstanceEntityManager.delete(variableInstanceEntity, true);
        

        // 删除正在执行的task
        taskEntityManager.deleteTask(taskEntity, "删除子节点", true, true);
        // 删除子执行流
        executionEntityManager.deleteChildExecutions(parentExecutionEntity, "", true);
        // 删除子执行实例
        executionEntityManager.delete(parentExecutionEntity, true);

        // 将根执行流的当前活动id设置为目标任务
        FlowElement targetFlowElement = ProcessDefinitionUtil.getProcess(rootExecutionEntity.getProcessDefinitionId()).getFlowElement(targetTaskDefKey);
        rootExecutionEntity.setCurrentFlowElement(targetFlowElement);

        // 在根流程实例上继续流程
        commandContext.getAgenda().planContinueProcessInCompensation(rootExecutionEntity);
        return null;
    


    public String getCurrentTaskId() 
        return currentTaskId;
    

    public void setCurrentTaskId(String currentTaskId) 
        this.currentTaskId = currentTaskId;
    

    public String getTargetTaskDefKey() 
        return targetTaskDefKey;
    

    public void setTargetTaskDefKey(String targetTaskDefKey) 
        this.targetTaskDefKey = targetTaskDefKey;
    

public void testSub2Parent() 
	// 从子流程中经理审批直接跳转到父流程中的发起申请节点
    String currentTaskId = "20";
    String targetTaskDefKey = "apply";

    Sub2ParentCommand sub2ParentCommand = new Sub2ParentCommand(currentTaskId, targetTaskDefKey);
    ProcessEngines.getDefaultProcessEngine().getManagementService()
            .executeCommand(sub2ParentCommand);

三:执行过程分析

insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_, ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_, CATEGORY_, TENANT_ID_ ) values 
( '2502', 'LeaveProcess1:1:6', '7', '8', '发起申请', null, null, null, null, '2022-01-15 22:42:57.878', null, null, null, null, 'apply', null, 50, null, null, '' );


insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_, ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TENANT_ID_ ) values 
( '2501', 'LeaveProcess1:1:6', '7', '8', 'apply', '2502', null, '发起申请', 'userTask', null, '2022-01-15 22:42:57.835', null, null, null, '' );

insert into ACT_RU_TASK (ID_, REV_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_, DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_, CLAIM_TIME_) values 
('2502', 1, '发起申请', null, null, 50, '2022-01-15 22:42:57.835', null, null, null, '8', '7', 'LeaveProcess1:1:6', 'apply', null, null, 1, '', null, null );

UPDATE ACT_RU_EXECUTION 
SET 
	REV_ = 2,
	BUSINESS_KEY_ = NULL,
	PROC_DEF_ID_ = 'LeaveProcess2:1:3',
	ACT_ID_ = 'pmApprove',
	IS_ACTIVE_ = FALSE,
	IS_CONCURRENT_ = FALSE,
	IS_SCOPE_ = FALSE,
	IS_EVENT_SCOPE_ = FALSE,
	IS_MI_ROOT_ = FALSE,
	PARENT_ID_ = '15',
	SUPER_EXEC_ = NULL,
	ROOT_PROC_INST_ID_ = '7',
	SUSPENSION_STATE_ = 1,
	NAME_ = NULL,
	IS_COUNT_ENABLED_ = FALSE,
	EVT_SUBSCR_COUNT_ = 0,
	TASK_COUNT_ = 0,
	JOB_COUNT_ = 0,
	TIMER_JOB_COUNT_ = 0,
	SUSP_JOB_COUNT_ = 0,
	DEADLETTER_JOB_COUNT_ = 0,
	VAR_COUNT_ = 0,
	ID_LINK_COUNT_ = 0 
WHERE ID_ = '17' AND REV_ = 1;


UPDATE ACT_RU_EXECUTION 
SET 
	REV_ = 3,
	BUSINESS_KEY_ = NULL,
	PROC_DEF_ID_ = 'LeaveProcess1:1:6',
	ACT_ID_ = 'apply',
	IS_ACTIVE_ = TRUE,
	IS_CONCURRENT_ = FALSE,
	IS_SCOPE_ = FALSE,
	IS_EVENT_SCOPE_ = FALSE,
	IS_MI_ROOT_ = FALSE,
	PARENT_ID_ = '7',
	SUPER_EXEC_ = NULL,
	ROOT_PROC_INST_ID_ = '7',
	SUSPENSION_STATE_ = 1,
	NAME_ = NULL,
	IS_COUNT_ENABLED_ = FALSE,
	EVT_SUBSCR_COUNT_ = 0,
	TASK_COUNT_ = 0,
	JOB_COUNT_ = 0,
	TIMER_JOB_COUNT_ = 0,
	SUSP_JOB_COUNT_ = 0,
	DEADLETTER_JOB_COUNT_ = 0,
	VAR_COUNT_ = 0,
	ID_LINK_COUNT_ = 0 
WHERE ID_ = '8' AND REV_ = 2;

UPDATE ACT_HI_ACTINST 
SET 
	EXECUTION_ID_ = '17',
	ASSIGNEE_ = NULL,
	END_TIME_ = '2022-01-15 22:42:19.358',
	DURATION_ = 18527666,
	DELETE_REASON_ = '' 
WHERE ID_ = '19';

delete FROM ACT_HI_COMMENT WHERE TASK_ID_ = '20';
delete FROM ACT_RU_VARIABLE WHERE ID_ = '16' and REV_ = 1;
delete FROM ACT_RU_TASK WHERE ID_ = '20' and REV_ = 1;
delete FROM ACT_RU_EXECUTION WHERE ID_ = '17' and REV_ = 2;
delete FROM ACT_RU_EXECUTION WHERE ID_ = '15' and REV_ = 1;
delete FROM ACT_HI_TASKINST WHERE ID_ = '20';

以上是关于Activiti7工作流引擎:高阶篇 CallActivityTask 子流程跳转到父流程的主要内容,如果未能解决你的问题,请参考以下文章

Activiti7工作流引擎:高阶篇 ProcessDefinitionUtil

Activiti7工作流引擎:高阶篇 ProcessDefinitionUtil

Activiti7工作流引擎:高阶篇 多实例

Activiti7工作流引擎:高阶篇 ManualTask

Activiti7工作流引擎:高阶篇 ScriptTask

Activiti7工作流引擎:高阶篇 MailTask