Commit fdc6f6ed6573245ce732aedaa05eed841c9a09f1
Committed by
Gitee
!1186 Simple设计器-监听器
Merge pull request !1186 from Lesan/feature/bpm-监听器
Showing
4 changed files
with
161 additions
and
0 deletions
yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/definition/BpmListenerMapType.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.enums.definition; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 4 | +import lombok.AllArgsConstructor; | ||
| 5 | +import lombok.Getter; | ||
| 6 | + | ||
| 7 | +import java.util.Arrays; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * BPM 任务监听器键值对类型 | ||
| 11 | + * | ||
| 12 | + * @author Lesan | ||
| 13 | + */ | ||
| 14 | +@Getter | ||
| 15 | +@AllArgsConstructor | ||
| 16 | +public enum BpmListenerMapType implements IntArrayValuable { | ||
| 17 | + | ||
| 18 | + FIXED_VALUE(1, "固定值"), | ||
| 19 | + FROM_FORM(2, "表单"); | ||
| 20 | + | ||
| 21 | + private final Integer type; | ||
| 22 | + private final String name; | ||
| 23 | + | ||
| 24 | + public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BpmListenerMapType::getType).toArray(); | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public int[] array() { | ||
| 28 | + return ARRAYS; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | +} | ||
| 0 | \ No newline at end of file | 32 | \ No newline at end of file |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/definition/vo/model/simple/BpmSimpleModelNodeVO.java
| @@ -96,6 +96,42 @@ public class BpmSimpleModelNodeVO { | @@ -96,6 +96,42 @@ public class BpmSimpleModelNodeVO { | ||
| 96 | */ | 96 | */ |
| 97 | private AssignEmptyHandler assignEmptyHandler; | 97 | private AssignEmptyHandler assignEmptyHandler; |
| 98 | 98 | ||
| 99 | + /** | ||
| 100 | + * 创建任务监听器 | ||
| 101 | + */ | ||
| 102 | + private ListenerHandler createTaskListener; | ||
| 103 | + | ||
| 104 | + @Schema(description = "任务监听器") | ||
| 105 | + @Data | ||
| 106 | + public static class ListenerHandler { | ||
| 107 | + | ||
| 108 | + @Schema(description = "是否开启任务监听器", example = "false") | ||
| 109 | + private Boolean enable; | ||
| 110 | + | ||
| 111 | + @Schema(description = "请求路径", example = "http://xxxxx") | ||
| 112 | + private String path; | ||
| 113 | + | ||
| 114 | + @Schema(description = "请求头", example = "[]") | ||
| 115 | + private List<ListenerMap> header; | ||
| 116 | + | ||
| 117 | + @Schema(description = "请求体", example = "[]") | ||
| 118 | + private List<ListenerMap> body; | ||
| 119 | + | ||
| 120 | + @Schema(description = "任务监听器键值对") | ||
| 121 | + @Data | ||
| 122 | + public static class ListenerMap { | ||
| 123 | + | ||
| 124 | + @Schema(description = "键", example = "xxx") | ||
| 125 | + private String key; | ||
| 126 | + | ||
| 127 | + @Schema(description = "值类型", example = "1") | ||
| 128 | + private Integer type; | ||
| 129 | + | ||
| 130 | + @Schema(description = "值", example = "xxx") | ||
| 131 | + private String value; | ||
| 132 | + } | ||
| 133 | + } | ||
| 134 | + | ||
| 99 | @Schema(description = "审批节点拒绝处理策略") | 135 | @Schema(description = "审批节点拒绝处理策略") |
| 100 | @Data | 136 | @Data |
| 101 | public static class RejectHandler { | 137 | public static class RejectHandler { |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/SimpleModelUtils.java
| @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; | @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; | ||
| 4 | import cn.hutool.core.lang.Assert; | 4 | import cn.hutool.core.lang.Assert; |
| 5 | import cn.hutool.core.map.MapUtil; | 5 | import cn.hutool.core.map.MapUtil; |
| 6 | import cn.hutool.core.util.*; | 6 | import cn.hutool.core.util.*; |
| 7 | +import cn.hutool.json.JSONUtil; | ||
| 7 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 8 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 8 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO; | 9 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO; |
| 9 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.ConditionGroups; | 10 | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO.ConditionGroups; |
| @@ -15,11 +16,14 @@ import org.flowable.bpmn.BpmnAutoLayout; | @@ -15,11 +16,14 @@ import org.flowable.bpmn.BpmnAutoLayout; | ||
| 15 | import org.flowable.bpmn.constants.BpmnXMLConstants; | 16 | import org.flowable.bpmn.constants.BpmnXMLConstants; |
| 16 | import org.flowable.bpmn.model.Process; | 17 | import org.flowable.bpmn.model.Process; |
| 17 | import org.flowable.bpmn.model.*; | 18 | import org.flowable.bpmn.model.*; |
| 19 | +import org.flowable.engine.delegate.TaskListener; | ||
| 18 | 20 | ||
| 19 | import java.util.*; | 21 | import java.util.*; |
| 20 | 22 | ||
| 21 | import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.*; | 23 | import static cn.iocoder.yudao.module.bpm.framework.flowable.core.enums.BpmnModelConstants.*; |
| 22 | import static cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils.*; | 24 | import static cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils.*; |
| 25 | +import static cn.iocoder.yudao.module.bpm.service.task.listener.BpmUserTaskListener.DELEGATE_EXPRESSION; | ||
| 26 | +import static cn.iocoder.yudao.module.bpm.service.task.listener.BpmUserTaskListener.EXTENSION_SUFFIX; | ||
| 23 | import static java.util.Arrays.asList; | 27 | import static java.util.Arrays.asList; |
| 24 | 28 | ||
| 25 | /** | 29 | /** |
| @@ -420,6 +424,20 @@ public class SimpleModelUtils { | @@ -420,6 +424,20 @@ public class SimpleModelUtils { | ||
| 420 | if (node.getTimeoutHandler() != null && node.getTimeoutHandler().getEnable()) { | 424 | if (node.getTimeoutHandler() != null && node.getTimeoutHandler().getEnable()) { |
| 421 | userTask.setDueDate(node.getTimeoutHandler().getTimeDuration()); | 425 | userTask.setDueDate(node.getTimeoutHandler().getTimeDuration()); |
| 422 | } | 426 | } |
| 427 | + // 设置监听器 | ||
| 428 | + List<FlowableListener> flowableListeners = new ArrayList<>(3); | ||
| 429 | + if (node.getCreateTaskListener().getEnable()) { | ||
| 430 | + FlowableListener flowableListener = new FlowableListener(); | ||
| 431 | + flowableListener.setEvent(TaskListener.EVENTNAME_CREATE); | ||
| 432 | + flowableListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION); | ||
| 433 | + flowableListener.setImplementation(DELEGATE_EXPRESSION); | ||
| 434 | + addExtensionElement(userTask, "create" + EXTENSION_SUFFIX, | ||
| 435 | + JSONUtil.toJsonStr(node.getCreateTaskListener())); | ||
| 436 | + flowableListeners.add(flowableListener); | ||
| 437 | + } | ||
| 438 | + if (!flowableListeners.isEmpty()) { | ||
| 439 | + userTask.setTaskListeners(flowableListeners); | ||
| 440 | + } | ||
| 423 | return userTask; | 441 | return userTask; |
| 424 | } | 442 | } |
| 425 | 443 |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/listener/BpmUserTaskListener.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.service.task.listener; | ||
| 2 | + | ||
| 3 | +import cn.hutool.http.HttpRequest; | ||
| 4 | +import cn.hutool.json.JSONUtil; | ||
| 5 | +import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.simple.BpmSimpleModelNodeVO; | ||
| 6 | +import cn.iocoder.yudao.module.bpm.enums.definition.BpmListenerMapType; | ||
| 7 | +import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils; | ||
| 8 | +import cn.iocoder.yudao.module.bpm.service.definition.BpmModelService; | ||
| 9 | +import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService; | ||
| 10 | +import jakarta.annotation.Resource; | ||
| 11 | +import lombok.extern.slf4j.Slf4j; | ||
| 12 | +import org.flowable.bpmn.model.BpmnModel; | ||
| 13 | +import org.flowable.bpmn.model.FlowElement; | ||
| 14 | +import org.flowable.engine.delegate.TaskListener; | ||
| 15 | +import org.flowable.engine.history.HistoricProcessInstance; | ||
| 16 | +import org.flowable.task.service.delegate.DelegateTask; | ||
| 17 | +import org.springframework.stereotype.Component; | ||
| 18 | + | ||
| 19 | +import java.util.HashMap; | ||
| 20 | +import java.util.Map; | ||
| 21 | + | ||
| 22 | +import static cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmnModelUtils.parseExtensionElement; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * BPM 用户任务通用监听器 | ||
| 26 | + * | ||
| 27 | + * @author Lesan | ||
| 28 | + */ | ||
| 29 | +@Component | ||
| 30 | +@Slf4j | ||
| 31 | +public class BpmUserTaskListener implements TaskListener { | ||
| 32 | + | ||
| 33 | + public static final String DELEGATE_EXPRESSION = "${bpmUserTaskListener}"; | ||
| 34 | + | ||
| 35 | + public static final String EXTENSION_SUFFIX = "TaskListenerMetaInfo"; | ||
| 36 | + | ||
| 37 | + @Resource | ||
| 38 | + private BpmModelService modelService; | ||
| 39 | + | ||
| 40 | + @Resource | ||
| 41 | + private BpmProcessInstanceService processInstanceService; | ||
| 42 | + | ||
| 43 | + @Override | ||
| 44 | + public void notify(DelegateTask delegateTask) { | ||
| 45 | + // 1. 获取所需基础信息 | ||
| 46 | + HistoricProcessInstance processInstance = processInstanceService.getHistoricProcessInstance(delegateTask.getProcessInstanceId()); | ||
| 47 | + BpmnModel bpmnModel = modelService.getBpmnModelByDefinitionId(delegateTask.getProcessDefinitionId()); | ||
| 48 | + FlowElement userTaskElement = BpmnModelUtils.getFlowElementById(bpmnModel, delegateTask.getTaskDefinitionKey()); | ||
| 49 | + BpmSimpleModelNodeVO.ListenerHandler listenerHandler = JSONUtil.toBean( | ||
| 50 | + parseExtensionElement(userTaskElement, delegateTask.getEventName() + EXTENSION_SUFFIX), | ||
| 51 | + BpmSimpleModelNodeVO.ListenerHandler.class); | ||
| 52 | + Map<String, Object> processVariables = processInstance.getProcessVariables(); | ||
| 53 | + // 2. 获取请求头和请求体 | ||
| 54 | + HashMap<String, String> headers = new HashMap<>(); | ||
| 55 | + HashMap<String, Object> body = new HashMap<>(); | ||
| 56 | + listenerHandler.getHeader().forEach(item -> { | ||
| 57 | + if (item.getType().equals(BpmListenerMapType.FIXED_VALUE.getType())) { | ||
| 58 | + headers.put(item.getKey(), item.getValue()); | ||
| 59 | + } else if (item.getType().equals(BpmListenerMapType.FROM_FORM.getType())) { | ||
| 60 | + headers.put(item.getKey(), processVariables.getOrDefault(item.getValue(), "").toString()); | ||
| 61 | + } | ||
| 62 | + }); | ||
| 63 | + listenerHandler.getBody().forEach(item -> { | ||
| 64 | + if (item.getType().equals(BpmListenerMapType.FIXED_VALUE.getType())) { | ||
| 65 | + body.put(item.getKey(), item.getValue()); | ||
| 66 | + } else if (item.getType().equals(BpmListenerMapType.FROM_FORM.getType())) { | ||
| 67 | + body.put(item.getKey(), processVariables.getOrDefault(item.getValue(), "")); | ||
| 68 | + } | ||
| 69 | + }); | ||
| 70 | + // 3. 异步发起请求 | ||
| 71 | + HttpRequest.post(listenerHandler.getPath()) | ||
| 72 | + .addHeaders(headers).form(body).executeAsync(); | ||
| 73 | + // 4. 是否需要后续操作? | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | +} | ||
| 0 | \ No newline at end of file | 77 | \ No newline at end of file |