增加 BpmUserTaskActivitiBehavior 自定义实现,为后续做自定义的任务分配机制做准备

This commit is contained in:
YunaiV 2022-01-10 21:02:13 +08:00
parent 9734eacf01
commit 645fd7624b
9 changed files with 144 additions and 34 deletions

View File

@ -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));
}
}

View File

@ -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();
}
}

View File

@ -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
}

View File

@ -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<String> candidateUsers, List<String> candidateGroups,
TaskEntity task, ExpressionManager expressionManager, DelegateExecution execution) {
System.out.println("");
taskEntityManager.changeTaskAssignee(task, "1");
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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<T extends TaskRuntimeEvent<? extends Task>>
implements TaskEventListener<T> {
public class BpmTaskEventListener<T extends RuntimeEvent<?, ?>>
implements TaskRuntimeEventListener<T> {
@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<Task> event = (TaskRuntimeEvent<Task>) rawEvent;
// 创建时插入拓展表
if (event.getEventType() == TaskRuntimeEvent.TaskEvents.TASK_CREATED) {
taskService.createTaskExt(event.getEntity());

View File

@ -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()
// }
}

View File

@ -87,8 +87,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
runtimeService.setProcessInstanceName(instance.getId(), definition.getName());
// TODO 芋艿临时使用, 保证分配
List<Task> tasks = taskService.getTasksByProcessInstanceId(instance.getId());
tasks.forEach(task -> taskService.updateTaskAssign(task.getId(), userId));
// List<Task> tasks = taskService.getTasksByProcessInstanceId(instance.getId());
// tasks.forEach(task -> taskService.updateTaskAssign(task.getId(), userId));
// 添加初始的评论 TODO 芋艿在思考下
// Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();