dengzedong
2024-12-24 aa0382e44311f9f7e62a688c8fcaa9c69a512e0f
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.framework.flowable.core.listener;
H 2
3 import cn.hutool.core.collection.CollUtil;
bb2880 4 import cn.hutool.core.util.ObjectUtil;
e7c126 5 import cn.hutool.core.util.StrUtil;
bb2880 6 import com.iailab.framework.common.util.number.NumberUtils;
49b4b6 7 import com.iailab.framework.datapermission.core.annotation.DataPermission;
bb2880 8 import com.iailab.module.bpm.enums.definition.BpmBoundaryEventType;
H 9 import com.iailab.module.bpm.framework.flowable.core.enums.BpmnModelConstants;
10 import com.iailab.module.bpm.framework.flowable.core.util.BpmnModelUtils;
11 import com.iailab.module.bpm.service.definition.BpmModelService;
e7c126 12 import com.iailab.module.bpm.service.task.BpmTaskService;
H 13 import com.google.common.collect.ImmutableSet;
14 import lombok.extern.slf4j.Slf4j;
bb2880 15 import org.flowable.bpmn.model.BoundaryEvent;
H 16 import org.flowable.bpmn.model.BpmnModel;
17 import org.flowable.bpmn.model.FlowElement;
e7c126 18 import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
H 19 import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
20 import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
21 import org.flowable.engine.delegate.event.FlowableActivityCancelledEvent;
22 import org.flowable.engine.history.HistoricActivityInstance;
bb2880 23 import org.flowable.job.api.Job;
e7c126 24 import org.flowable.task.api.Task;
H 25 import org.springframework.context.annotation.Lazy;
26 import org.springframework.stereotype.Component;
27
28 import javax.annotation.Resource;
29 import java.util.List;
30 import java.util.Set;
31
32 /**
33  * 监听 {@link Task} 的开始与完成
34  *
35  * @author jason
36  */
37 @Component
38 @Slf4j
39 public class BpmTaskEventListener extends AbstractFlowableEngineEventListener {
40
41     @Resource
bb2880 42     @Lazy // 延迟加载,避免循环依赖
H 43     private BpmModelService modelService;
e7c126 44     @Resource
H 45     @Lazy // 解决循环依赖
bb2880 46     private BpmTaskService taskService;
e7c126 47
H 48     public static final Set<FlowableEngineEventType> TASK_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
49             .add(FlowableEngineEventType.TASK_CREATED)
50             .add(FlowableEngineEventType.TASK_ASSIGNED)
51 //            .add(FlowableEngineEventType.TASK_COMPLETED) // 由于审批通过时,已经记录了 task 的 status 为通过,所以不需要监听了。
52             .add(FlowableEngineEventType.ACTIVITY_CANCELLED)
bb2880 53             .add(FlowableEngineEventType.TIMER_FIRED) // 监听审批超时
e7c126 54             .build();
H 55
bb2880 56     public BpmTaskEventListener() {
e7c126 57         super(TASK_EVENTS);
H 58     }
59
60     @Override
61     protected void taskCreated(FlowableEngineEntityEvent event) {
bb2880 62         taskService.processTaskCreated((Task) event.getEntity());
e7c126 63     }
H 64
65     @Override
66     protected void taskAssigned(FlowableEngineEntityEvent event) {
bb2880 67         taskService.processTaskAssigned((Task) event.getEntity());
e7c126 68     }
H 69
70     @Override
71     protected void activityCancelled(FlowableActivityCancelledEvent event) {
bb2880 72         List<HistoricActivityInstance> activityList = taskService.getHistoricActivityListByExecutionId(event.getExecutionId());
e7c126 73         if (CollUtil.isEmpty(activityList)) {
H 74             log.error("[activityCancelled][使用 executionId({}) 查找不到对应的活动实例]", event.getExecutionId());
75             return;
76         }
77         // 遍历处理
78         activityList.forEach(activity -> {
79             if (StrUtil.isEmpty(activity.getTaskId())) {
80                 return;
81             }
bb2880 82             taskService.processTaskCanceled(activity.getTaskId());
e7c126 83         });
H 84     }
85
bb2880 86     @Override
H 87     @SuppressWarnings("PatternVariableCanBeUsed")
88     protected void timerFired(FlowableEngineEntityEvent event) {
89         // 1.1 只处理 BoundaryEvent 边界计时时间
90         String processDefinitionId = event.getProcessDefinitionId();
91         BpmnModel bpmnModel = modelService.getBpmnModelByDefinitionId(processDefinitionId);
92         Job entity = (Job) event.getEntity();
93         FlowElement element = BpmnModelUtils.getFlowElementById(bpmnModel, entity.getElementId());
94         if (!(element instanceof BoundaryEvent)) {
95             return;
96         }
97         // 1.2 判断是否为超时处理
98         BoundaryEvent boundaryEvent = (BoundaryEvent) element;
99         String boundaryEventType = BpmnModelUtils.parseBoundaryEventExtensionElement(boundaryEvent,
100                 BpmnModelConstants.BOUNDARY_EVENT_TYPE);
101         BpmBoundaryEventType bpmTimerBoundaryEventType = BpmBoundaryEventType.typeOf(NumberUtils.parseInt(boundaryEventType));
102         if (ObjectUtil.notEqual(bpmTimerBoundaryEventType, BpmBoundaryEventType.USER_TASK_TIMEOUT)) {
103             return;
104         }
105
106         // 2. 处理超时
107         String timeoutHandlerType = BpmnModelUtils.parseBoundaryEventExtensionElement(boundaryEvent,
108                 BpmnModelConstants.USER_TASK_TIMEOUT_HANDLER_TYPE);
109         String taskKey = boundaryEvent.getAttachedToRefId();
110         taskService.processTaskTimeout(event.getProcessInstanceId(), taskKey, NumberUtils.parseInt(timeoutHandlerType));
111     }
e7c126 112 }