From 645fd7624b8238ae251bb7f36acbeaf84b3dcc14 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 10 Jan 2022 21:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20BpmUserTaskActivitiBehavio?= =?UTF-8?q?r=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=AE=9E=E7=8E=B0=EF=BC=8C?= =?UTF-8?q?=E4=B8=BA=E5=90=8E=E7=BB=AD=E5=81=9A=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=9A=84=E4=BB=BB=E5=8A=A1=E5=88=86=E9=85=8D=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=E5=81=9A=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activiti/BpmActivitiConfiguration.java | 26 ---------- .../config/BpmActivitiConfiguration.java | 51 +++++++++++++++++++ .../behavior/BpmActivityBehaviorFactory.java | 31 +++++++++++ .../behavior/BpmUserTaskActivitiBehavior.java | 31 +++++++++++ .../BpmProcessInstanceEventListener.java | 2 +- .../BpmTackActivitiEventListener.java | 2 +- .../core}/listener/BpmTaskEventListener.java | 19 +++++-- .../model/impl/BpmModelServiceImpl.java | 12 +++++ .../impl/BpmProcessInstanceServiceImpl.java | 4 +- 9 files changed, 144 insertions(+), 34 deletions(-) delete mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/BpmActivitiConfiguration.java create mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/config/BpmActivitiConfiguration.java create mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmActivityBehaviorFactory.java create mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmUserTaskActivitiBehavior.java rename yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/{service/task => framework/activiti/core}/listener/BpmProcessInstanceEventListener.java (96%) rename yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/{service/task => framework/activiti/core}/listener/BpmTackActivitiEventListener.java (95%) rename yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/{service/task => framework/activiti/core}/listener/BpmTaskEventListener.java (65%) diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/BpmActivitiConfiguration.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/BpmActivitiConfiguration.java deleted file mode 100644 index 6101d2d46..000000000 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/BpmActivitiConfiguration.java +++ /dev/null @@ -1,26 +0,0 @@ -package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti; - -import cn.iocoder.yudao.adminserver.modules.bpm.service.task.listener.BpmTackActivitiEventListener; -import org.activiti.spring.SpringProcessEngineConfiguration; -import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer; -import org.springframework.context.annotation.Configuration; - -import javax.annotation.Resource; -import java.util.Collections; - -/** - * BPM 模块的 Activiti 配置类 - */ -@Configuration -public class BpmActivitiConfiguration implements ProcessEngineConfigurationConfigurer { - - @Resource - private BpmTackActivitiEventListener taskActivitiEventListener; - - @Override - public void configure(SpringProcessEngineConfiguration configuration) { - // 注册监听器,例如说 BpmActivitiEventListener - configuration.setEventListeners(Collections.singletonList(taskActivitiEventListener)); - } - -} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/config/BpmActivitiConfiguration.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/config/BpmActivitiConfiguration.java new file mode 100644 index 000000000..9a02efc6d --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/config/BpmActivitiConfiguration.java @@ -0,0 +1,51 @@ +package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.config; + +import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior.BpmActivityBehaviorFactory; +import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.listener.BpmTackActivitiEventListener; +import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Collections; + +import static org.activiti.spring.boot.ProcessEngineAutoConfiguration.BEHAVIOR_FACTORY_MAPPING_CONFIGURER; + +/** + * BPM 模块的 Activiti 配置类 + */ +@Configuration +public class BpmActivitiConfiguration { + + /** + * BPM 模块的 ProcessEngineConfigurationConfigurer 实现类,主要设置各种监听器 + */ + @Bean + public ProcessEngineConfigurationConfigurer bpmProcessEngineConfigurationConfigurer( + BpmTackActivitiEventListener taskActivitiEventListener) { + return configuration -> { + // 注册监听器,例如说 BpmActivitiEventListener + configuration.setEventListeners(Collections.singletonList(taskActivitiEventListener)); + }; + } + + /** + * 用于设置自定义的 ActivityBehaviorFactory 实现的 ProcessEngineConfigurationConfigurer 实现类 + * + * 目的:覆盖 {@link org.activiti.spring.boot.ProcessEngineAutoConfiguration} 的 + * defaultActivityBehaviorFactoryMappingConfigurer 方法创建的 Bean + */ + @Bean(name = BEHAVIOR_FACTORY_MAPPING_CONFIGURER) + public ProcessEngineConfigurationConfigurer defaultActivityBehaviorFactoryMappingConfigurer( + BpmActivityBehaviorFactory bpmActivityBehaviorFactory) { + return configuration -> { + // 设置 ActivityBehaviorFactory 实现类,用于流程任务的审核人的自定义 + configuration.setActivityBehaviorFactory(bpmActivityBehaviorFactory); + }; + } + + @Bean + public BpmActivityBehaviorFactory bpmActivityBehaviorFactory() { + return new BpmActivityBehaviorFactory(); + } + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmActivityBehaviorFactory.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmActivityBehaviorFactory.java new file mode 100644 index 000000000..bbf652833 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmActivityBehaviorFactory.java @@ -0,0 +1,31 @@ +package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior; + +import cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior.BpmUserTaskActivitiBehavior; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.activiti.bpmn.model.UserTask; +import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior; +import org.activiti.engine.impl.bpmn.parser.factory.DefaultActivityBehaviorFactory; + +/** + * 自定义的 ActivityBehaviorFactory 实现类,目的如下: + * 1. 自定义 {@link #createUserTaskActivityBehavior(UserTask)}:实现自定义的流程任务的 assignee 负责人的分配 + * + * @author 芋道源码 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class BpmActivityBehaviorFactory extends DefaultActivityBehaviorFactory { + + @Override + public UserTaskActivityBehavior createUserTaskActivityBehavior(UserTask userTask) { + return new BpmUserTaskActivitiBehavior(userTask); + } + + // TODO 芋艿:并行任务 ParallelMultiInstanceBehavior + + // TODO 芋艿:并行任务 SequentialMultiInstanceBehavior + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmUserTaskActivitiBehavior.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmUserTaskActivitiBehavior.java new file mode 100644 index 000000000..e66b4b336 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/behavior/BpmUserTaskActivitiBehavior.java @@ -0,0 +1,31 @@ +package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.behavior; + +import org.activiti.bpmn.model.UserTask; +import org.activiti.engine.delegate.DelegateExecution; +import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior; +import org.activiti.engine.impl.el.ExpressionManager; +import org.activiti.engine.impl.persistence.entity.TaskEntity; +import org.activiti.engine.impl.persistence.entity.TaskEntityManager; + +import java.util.List; + +/** + * 自定义的流程任务的 assignee 负责人的分配 + * + * @author 芋道源码 + */ +public class BpmUserTaskActivitiBehavior extends UserTaskActivityBehavior { + + public BpmUserTaskActivitiBehavior(UserTask userTask) { + super(userTask); + } + + @Override + protected void handleAssignments(TaskEntityManager taskEntityManager, + String assignee, String owner, List candidateUsers, List candidateGroups, + TaskEntity task, ExpressionManager expressionManager, DelegateExecution execution) { + System.out.println(""); + taskEntityManager.changeTaskAssignee(task, "1"); + } + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmProcessInstanceEventListener.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmProcessInstanceEventListener.java similarity index 96% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmProcessInstanceEventListener.java rename to yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmProcessInstanceEventListener.java index 3b1268a29..0fe462bf5 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmProcessInstanceEventListener.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmProcessInstanceEventListener.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.adminserver.modules.bpm.service.task.listener; +package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.listener; import cn.iocoder.yudao.adminserver.modules.bpm.dal.dataobject.task.BpmProcessInstanceExtDO; import cn.iocoder.yudao.adminserver.modules.bpm.service.task.BpmProcessInstanceService; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmTackActivitiEventListener.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmTackActivitiEventListener.java similarity index 95% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmTackActivitiEventListener.java rename to yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmTackActivitiEventListener.java index de46ebcd3..307263e0c 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmTackActivitiEventListener.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmTackActivitiEventListener.java @@ -1,4 +1,4 @@ -package cn.iocoder.yudao.adminserver.modules.bpm.service.task.listener; +package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.listener; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmProcessDefinitionService; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmTaskEventListener.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmTaskEventListener.java similarity index 65% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmTaskEventListener.java rename to yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmTaskEventListener.java index cc3b665f5..241782a87 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/listener/BpmTaskEventListener.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/framework/activiti/core/listener/BpmTaskEventListener.java @@ -1,10 +1,14 @@ -package cn.iocoder.yudao.adminserver.modules.bpm.service.task.listener; +package cn.iocoder.yudao.adminserver.modules.bpm.framework.activiti.core.listener; import cn.iocoder.yudao.adminserver.modules.bpm.dal.dataobject.task.BpmTaskExtDO; import cn.iocoder.yudao.adminserver.modules.bpm.service.task.BpmTaskService; +import org.activiti.api.model.shared.event.RuntimeEvent; +import org.activiti.api.process.model.ProcessInstance; +import org.activiti.api.process.model.events.ProcessRuntimeEvent; import org.activiti.api.task.model.Task; import org.activiti.api.task.model.events.TaskRuntimeEvent; import org.activiti.api.task.runtime.events.listener.TaskEventListener; +import org.activiti.api.task.runtime.events.listener.TaskRuntimeEventListener; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; @@ -16,15 +20,22 @@ import javax.annotation.Resource; * @author 芋道源码 */ @Component -public class BpmTaskEventListener> - implements TaskEventListener { +public class BpmTaskEventListener> + implements TaskRuntimeEventListener { @Resource @Lazy // 解决循环依赖 private BpmTaskService taskService; @Override - public void onEvent(T event) { + @SuppressWarnings("unchecked") + public void onEvent(T rawEvent) { + // 由于 TaskRuntimeEventListener 无法保证只监听 TaskRuntimeEvent 事件,所以通过这样的方式 + if (!(rawEvent instanceof TaskRuntimeEvent)) { + return; + } + TaskRuntimeEvent event = (TaskRuntimeEvent) rawEvent; + // 创建时,插入拓展表 if (event.getEventType() == TaskRuntimeEvent.TaskEvents.TASK_CREATED) { taskService.createTaskExt(event.getEntity()); diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/model/impl/BpmModelServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/model/impl/BpmModelServiceImpl.java index 29b72efdc..1900457ba 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/model/impl/BpmModelServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/model/impl/BpmModelServiceImpl.java @@ -15,8 +15,11 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.object.PageUtils; import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; import lombok.extern.slf4j.Slf4j; +import org.activiti.bpmn.converter.BpmnXMLConverter; +import org.activiti.bpmn.model.BpmnModel; import org.activiti.engine.RepositoryService; import org.activiti.engine.impl.persistence.entity.SuspensionState; +import org.activiti.engine.impl.util.io.StringStreamSource; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.Model; import org.activiti.engine.repository.ModelQuery; @@ -27,6 +30,8 @@ import org.springframework.util.ObjectUtils; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -211,4 +216,11 @@ public class BpmModelServiceImpl implements BpmModelService { } } +// public static void main(String[] args) { +// // 创建转换对象 +// BpmnXMLConverter converter = new BpmnXMLConverter(); +// BpmnModel bpmnModel = converter.convertToBpmnModel(new StringStreamSource(""), true, true); +// bpmnModel.getProcesses() +// } + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/impl/BpmProcessInstanceServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/impl/BpmProcessInstanceServiceImpl.java index 6f00bf5c1..e738da0a6 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/impl/BpmProcessInstanceServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/task/impl/BpmProcessInstanceServiceImpl.java @@ -87,8 +87,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService runtimeService.setProcessInstanceName(instance.getId(), definition.getName()); // TODO 芋艿:临时使用, 保证分配 - List tasks = taskService.getTasksByProcessInstanceId(instance.getId()); - tasks.forEach(task -> taskService.updateTaskAssign(task.getId(), userId)); +// List tasks = taskService.getTasksByProcessInstanceId(instance.getId()); +// tasks.forEach(task -> taskService.updateTaskAssign(task.getId(), userId)); // 添加初始的评论 TODO 芋艿:在思考下 // Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();