Flowable代码片段

Flowable代码片段实例,可学习参考,直接复制

Posted by Shrmars Blog on June 30, 2022

判断流程定义中Task节点是否是会签

1
if (userTask.getBehavior() instanceof ParallelMultiInstanceBehavior)

强制结束普通用户任务而不生成新待办

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class DeleteCustTaskCommond implements Command<Execution>, Serializable {
    private String executioinId;
    public DeleteCustTaskCommond(String executioinId){
        this.executioinId=executioinId;
    }
    @Override
    public Execution execute(CommandContext commandContext) {
        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
        ExecutionEntity executionEntity = executionEntityManager.findById(executioinId);
        CommandContextUtil.getExecutionEntityManager(commandContext).deleteExecutionAndRelatedData(executionEntity, "abc", false);
        return null;
    }
}

强制结束会签任务而不生成新待办

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 以下参考源码  org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior.java
// cleanupMiRoot 方法
public class DeleteMultiTaskCommond implements Command<Boolean>, Serializable {
    private String executioinId;
    public DeleteMultiTaskCommond(String executioinId){
        this.executioinId=executioinId;
    }
    @Override
    public Boolean execute(CommandContext commandContext) {
        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
        ExecutionEntity executionEntity = executionEntityManager.findById(executioinId);

        // 删除 Execution
        ExecutionEntity multiInstanceRootExecution = (ExecutionEntity) getMultiInstanceRootExecution(executionEntity);
        FlowElement flowElement = multiInstanceRootExecution.getCurrentFlowElement();
        Collection<String> executionIdsNotToSendCancelledEventsFor = executionEntity.isMultiInstanceRoot() ? null : Collections.singletonList(executionEntity.getId());
        executionEntityManager.deleteChildExecutions(multiInstanceRootExecution, null, executionIdsNotToSendCancelledEventsFor, "user comeback", true, flowElement);
        executionEntityManager.deleteRelatedDataForExecution(multiInstanceRootExecution, "user comeback");
        executionEntityManager.delete(multiInstanceRootExecution);

        return true;
    }

    protected DelegateExecution getMultiInstanceRootExecution(DelegateExecution executionEntity) {
        DelegateExecution multiInstanceRootExecution = null;
        DelegateExecution currentExecution = executionEntity;
        while (currentExecution != null && multiInstanceRootExecution == null && currentExecution.getParent() != null) {
            if (currentExecution.isMultiInstanceRoot()) {
                multiInstanceRootExecution = currentExecution;
            } else {
                currentExecution = currentExecution.getParent();
            }
        }
        return multiInstanceRootExecution;
    }
}
// 如下方式调用,传入 会签节点 其中一个 ExecutionId
processEngine.getManagementService().executeCommand(new DeleteMultiTaskCommond("230013"));

修改历史Task信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package ruoli.work.commend;

import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.service.impl.persistence.entity.HistoricTaskInstanceEntity;

import java.io.Serializable;

public class UpdateHisTaskCommand implements Command<Boolean>, Serializable {
    private String taskId;
    public UpdateHisTaskCommand(String taskId) {
        this.taskId = taskId;
    }
    @Override
    public Boolean execute(CommandContext commandContext) {
        HistoricTaskInstanceEntity task=CommandContextUtil.getHistoricTaskService().getHistoricTask(taskId);
        task.setParentTaskId(null);
        return null;
    }
}