流转工作流程
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.njcn.bpm.config;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.njcn.bpm.service.task.IBpmProcessInstanceService;
|
||||
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
|
||||
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
|
||||
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
|
||||
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
|
||||
import org.flowable.engine.runtime.ProcessInstance;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 监听 {@link ProcessInstance} 的状态变更,更新其对应的 status 状态
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@Component
|
||||
public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEventListener {
|
||||
|
||||
@Resource
|
||||
@Lazy
|
||||
private IBpmProcessInstanceService processInstanceService;
|
||||
|
||||
public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
|
||||
.add(FlowableEngineEventType.PROCESS_CANCELLED)
|
||||
.add(FlowableEngineEventType.PROCESS_COMPLETED)
|
||||
.build();
|
||||
|
||||
public BpmProcessInstanceEventListener(){
|
||||
super(PROCESS_INSTANCE_EVENTS);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processCancelled(FlowableCancelledEvent event) {
|
||||
processInstanceService.updateProcessInstanceWhenCancel(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processCompleted(FlowableEngineEntityEvent event) {
|
||||
processInstanceService.updateProcessInstanceWhenApprove((ProcessInstance)event.getEntity());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.njcn.bpm.config;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.njcn.bpm.service.task.IBpmActivityService;
|
||||
import com.njcn.bpm.service.task.IBpmTaskService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
|
||||
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
|
||||
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
|
||||
import org.flowable.engine.delegate.event.FlowableActivityCancelledEvent;
|
||||
import org.flowable.engine.history.HistoricActivityInstance;
|
||||
import org.flowable.task.api.Task;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 监听 {@link Task} 的开始与完成
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class BpmTaskEventListener extends AbstractFlowableEngineEventListener {
|
||||
|
||||
@Resource
|
||||
@Lazy // 解决循环依赖
|
||||
private IBpmTaskService taskService;
|
||||
@Resource
|
||||
@Lazy // 解决循环依赖
|
||||
private IBpmActivityService activityService;
|
||||
|
||||
public static final Set<FlowableEngineEventType> TASK_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
|
||||
.add(FlowableEngineEventType.TASK_CREATED)
|
||||
.add(FlowableEngineEventType.TASK_ASSIGNED)
|
||||
// .add(FlowableEngineEventType.TASK_COMPLETED) // 由于审批通过时,已经记录了 task 的 status 为通过,所以不需要监听了。
|
||||
.add(FlowableEngineEventType.ACTIVITY_CANCELLED)
|
||||
.build();
|
||||
|
||||
public BpmTaskEventListener(){
|
||||
super(TASK_EVENTS);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void taskCreated(FlowableEngineEntityEvent event) {
|
||||
taskService.updateTaskStatusWhenCreated((Task) event.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void taskAssigned(FlowableEngineEntityEvent event) {
|
||||
taskService.updateTaskExtAssign((Task)event.getEntity());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void activityCancelled(FlowableActivityCancelledEvent event) {
|
||||
List<HistoricActivityInstance> activityList = activityService.getHistoricActivityListByExecutionId(event.getExecutionId());
|
||||
if (CollUtil.isEmpty(activityList)) {
|
||||
log.error("[activityCancelled][使用 executionId({}) 查找不到对应的活动实例]", event.getExecutionId());
|
||||
return;
|
||||
}
|
||||
// 遍历处理
|
||||
activityList.forEach(activity -> {
|
||||
if (StrUtil.isEmpty(activity.getTaskId())) {
|
||||
return;
|
||||
}
|
||||
taskService.updateTaskStatusWhenCanceled(activity.getTaskId());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.njcn.bpm.config;
|
||||
|
||||
import com.njcn.bpm.enums.WebFilterOrderEnum;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* bpm 模块的 web 组件的 Configuration
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class BpmWebConfiguration {
|
||||
|
||||
// /**
|
||||
// * bpm 模块的 API 分组
|
||||
// */
|
||||
// @Bean
|
||||
// public GroupedOpenApi bpmGroupedOpenApi() {
|
||||
// return YudaoSwaggerAutoConfiguration.buildGroupedOpenApi("bpm");
|
||||
// }
|
||||
|
||||
/**
|
||||
* 配置 Flowable Web 过滤器
|
||||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean<FlowableWebFilter> flowableWebFilter() {
|
||||
FilterRegistrationBean<FlowableWebFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
registrationBean.setFilter(new FlowableWebFilter());
|
||||
registrationBean.setOrder(WebFilterOrderEnum.FLOWABLE_FILTER);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.njcn.bpm.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.njcn.bpm.utils.FlowableUtils;
|
||||
import com.njcn.web.utils.RequestUtil;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Flowable Web 过滤器,将 userId 设置到 {@link org.flowable.common.engine.impl.identity.Authentication} 中
|
||||
*
|
||||
* @author jason
|
||||
*/
|
||||
public class FlowableWebFilter extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
String userIndex = RequestUtil.getUserIndex();
|
||||
// 设置工作流的用户
|
||||
if (StrUtil.isNotBlank(userIndex)) {
|
||||
FlowableUtils.setAuthenticatedUserId(userIndex);
|
||||
}
|
||||
// 过滤
|
||||
chain.doFilter(request, response);
|
||||
} finally {
|
||||
// 清理
|
||||
FlowableUtils.clearAuthenticatedUserId();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user