update - 新增流程实例创建,取消,完成的事件

This commit is contained in:
wangli 2023-07-13 11:37:53 +08:00
parent af30f1810a
commit d5ae9d9364
8 changed files with 219 additions and 45 deletions

View File

@ -1,34 +0,0 @@
package cn.axzo.workflow.core.conf;
import com.alibaba.fastjson.JSON;
import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Collections;
/**
* TODO
*
* @author wangli
* @sine 2023/7/11 23:50
*/
@Component
public class CustomEngineConfigurationConfigurer implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
private final static Logger log = LoggerFactory.getLogger(CustomEngineConfigurationConfigurer.class);
@Override
public void configure(SpringProcessEngineConfiguration engineConfiguration) {
engineConfiguration.setEventListeners(Collections.singletonList(new AbstractFlowableEventListener() {
@Override
public void onEvent(FlowableEvent event) {
log.info("可以处理所有类型事件 onEvent: {}", JSON.toJSONString(event));
}
}));
}
}

View File

@ -0,0 +1,27 @@
package cn.axzo.workflow.core.conf;
import com.google.common.collect.Lists;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* TODO
*
* @author wangli
* @since 2023/7/13 11:18
*/
@Configuration
public class FlowableConfiguration {
@Bean
public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> processEngineConfigurer(
ObjectProvider<FlowableEventListener> listeners) {
return configuration -> {
configuration.setEventListeners(Lists.newArrayList(listeners));
};
}
}

View File

@ -1,5 +1,17 @@
package cn.axzo.workflow.core.listener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
public interface BpmProcessEventListener {
void onCreated(FlowableEngineEntityEvent event);
void onStarted(FlowableProcessStartedEvent event);
void onCancelled(FlowableCancelledEvent event);
void onCompleted(FlowableEngineEntityEvent event);
}

View File

@ -1,13 +1,12 @@
package cn.axzo.workflow.core.service;
import cn.axzo.workflow.core.service.dto.request.process.BpmProcessInstanceCreateDTO;
import cn.axzo.workflow.core.service.dto.request.process.BpmProcessInstanceMyPageReqVO;
import cn.axzo.workflow.core.service.dto.request.task.BpmTaskTodoBpmPageDTO;
import cn.axzo.workflow.core.service.dto.request.process.BpmProcessInstanceWithdrawDTO;
import cn.axzo.workflow.core.service.dto.response.BpmPageResult;
import cn.axzo.workflow.core.service.dto.response.process.BpmProcessInstancePageItemVO;
import cn.axzo.workflow.core.service.dto.response.process.BpmProcessInstanceVO;
import cn.axzo.workflow.core.service.dto.request.process.BpmProcessInstanceCreateDTO;
import cn.axzo.workflow.core.service.dto.request.process.BpmProcessInstanceWithdrawDTO;
import cn.axzo.workflow.core.service.dto.response.task.BpmTaskTodoPageItemRespVO;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
@ -76,6 +75,27 @@ public interface BpmProcessInstanceService {
/**
* 撤销流程实例
* */
*/
Boolean withdrawProcessInstance(BpmProcessInstanceWithdrawDTO withdrawDTO);
/**
* 创还能 ProcessInstance 扩展记录
*
* @param instance
*/
void createProcessInstanceExt(ProcessInstance instance);
/**
* 更新 ProcessInstance 扩展记录为取消
*
* @param instance
*/
void updateProcessInstanceExtComplete(ProcessInstance instance);
/**
* 更新 ProcessInstance 扩展记录为完成
*
* @param event
*/
void updateProcessInstanceExtCancel(FlowableCancelledEvent event);
}

View File

@ -1,6 +1,59 @@
package cn.axzo.workflow.core.service.engine;
import cn.axzo.workflow.core.listener.BpmProcessEventListener;
import cn.axzo.workflow.core.service.BpmProcessInstanceService;
import com.google.common.collect.ImmutableSet;
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.delegate.event.FlowableProcessStartedEvent;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
public class EngineProcessInstanceEventListener implements BpmProcessEventListener {
import javax.annotation.Resource;
import java.util.Set;
@Component
public class EngineProcessInstanceEventListener extends AbstractFlowableEngineEventListener {
@Resource
BpmProcessEventListener processEventListener;
@Resource
@Lazy
BpmProcessInstanceService processInstanceService;
public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS =
ImmutableSet.<FlowableEngineEventType>builder()
.add(FlowableEngineEventType.PROCESS_CREATED)
.add(FlowableEngineEventType.PROCESS_STARTED)
.add(FlowableEngineEventType.PROCESS_CANCELLED)
.add(FlowableEngineEventType.PROCESS_COMPLETED)
.build();
public EngineProcessInstanceEventListener() {
super(PROCESS_INSTANCE_EVENTS);
}
@Override
protected void processCreated(FlowableEngineEntityEvent event) {
processInstanceService.createProcessInstanceExt((ProcessInstance) event.getEntity());
processEventListener.onCreated(event);
}
@Override
protected void processStarted(FlowableProcessStartedEvent event) {
processEventListener.onStarted(event);
}
@Override
protected void processCompleted(FlowableEngineEntityEvent event) {
this.processInstanceService.updateProcessInstanceExtComplete((ProcessInstance) event.getEntity());
processEventListener.onCompleted(event);
}
@Override
protected void processCancelled(FlowableCancelledEvent event) {
processInstanceService.updateProcessInstanceExtCancel(event);
processEventListener.onCancelled(event);
}
}

View File

@ -20,10 +20,12 @@ import cn.azxo.framework.common.utils.StringUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.db.SuspensionState;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.*;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
@ -223,6 +225,77 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
return true;
}
@Override
public void createProcessInstanceExt(ProcessInstance instance) {
// 获得流程定义
ProcessDefinition definition = processDefinitionService.getProcessDefinition(
instance.getProcessDefinitionId());
String startUserId = instance.getStartUserId();
Long startIdentityId = null;
if (StringUtils.isNotBlank(startUserId)) {
startIdentityId = Long.valueOf(startUserId);
}
// 插入 BpmProcessInstanceExtDO 对象
BpmProcessInstanceExtDO instanceExtDO = new BpmProcessInstanceExtDO();
instanceExtDO.setCustomProInstId(IdWorker.get32UUID());
instanceExtDO.setProcessInstanceId(instance.getId());
instanceExtDO.setProcessDefinitionId(definition.getId());
instanceExtDO.setCategory(definition.getCategory());
instanceExtDO.setName(instance.getName());
instanceExtDO.setTenantId(instance.getTenantId());
instanceExtDO.setFormVariables(instance.getProcessVariables());
instanceExtDO.setStartIdentityId(startIdentityId);
instanceExtDO.setStatus(BpmProcessInstanceStatusEnum.RUNNING.getStatus());
instanceExtDO.setResult(BpmProcessInstanceResultEnum.PROCESS.getResult());
processInstanceExtMapper.insert(instanceExtDO);
}
@Override
public void updateProcessInstanceExtCancel(FlowableCancelledEvent event) {
// 判断是否为 Reject 不通过如果是则不进行更新
if (BpmProcessInstanceDeleteReasonEnum.isRejectReason((String) event.getCause())) {
return;
}
// 需要主动查询因为 instance 只有 id 属性
// 另外此时如果去查询 ProcessInstance 的话字段是不全的所以去查询了 HistoricProcessInstance
HistoricProcessInstance processInstance = getHistoricProcessInstance(
event.getProcessInstanceId(), null);
// 更新拓展表
BpmProcessInstanceExtDO instanceExtDO = new BpmProcessInstanceExtDO();
instanceExtDO.setProcessInstanceId(event.getProcessInstanceId());
instanceExtDO.setEndTime(new Date()); // 由于 ProcessInstance 里没有办法拿到 endTime所以这里设置
instanceExtDO.setStatus(BpmProcessInstanceStatusEnum.FINISH.getStatus());
instanceExtDO.setResult(BpmProcessInstanceResultEnum.CANCEL.getResult());
processInstanceExtMapper.updateByProcessInstanceId(instanceExtDO);
// 发送流程实例的状态事件
// processInstanceResultEventPublisher.sendProcessInstanceResultEvent(
// BpmProcessInstanceConvert.INSTANCE.convert(this, processInstance,
// instanceExtDO.getResult()));
}
@Override
public void updateProcessInstanceExtComplete(ProcessInstance instance) {
// 需要主动查询因为 instance 只有 id 属性
// 另外此时如果去查询 ProcessInstance 的话字段是不全的所以去查询了 HistoricProcessInstance
HistoricProcessInstance processInstance = getHistoricProcessInstance(instance.getId(),
instance.getTenantId());
// 更新拓展表
BpmProcessInstanceExtDO instanceExtDO = new BpmProcessInstanceExtDO();
instanceExtDO.setProcessInstanceId(instance.getProcessInstanceId());
instanceExtDO.setEndTime(new Date()); // 由于 ProcessInstance 里没有办法拿到 endTime所以这里设置
instanceExtDO.setStatus(BpmProcessInstanceStatusEnum.FINISH.getStatus());
instanceExtDO.setResult(BpmProcessInstanceResultEnum.APPROVE.getResult()); // 如果正常完全说明审批通过
processInstanceExtMapper.updateByProcessInstanceId(instanceExtDO);
// 发送流程实例的状态事件
// processInstanceResultEventPublisher.sendProcessInstanceResultEvent(
// BpmProcessInstanceConvert.INSTANCE.convert(this, processInstance,
// instanceExtDO.getResult()));
}
@Transactional(rollbackFor = Exception.class)
public void updateProcessInstanceExtReject(String id, String comment, String tenantId) {
// 需要主动查询因为 instance 只有 id 属性
@ -258,7 +331,6 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
}
/**
* 获得历史的流程实例 Map
*

View File

@ -116,10 +116,12 @@ public class BpmTaskServiceImpl implements BpmTaskService {
bpmTaskTodoPageItemRespVOS.forEach(vo -> {
BpmTaskTodoPageItemRespVO.ProcessInstance processInstance = vo.getProcessInstance();
BpmProcessInstanceExtDO instanceExtDO = instanceExtMap.get(processInstance.getId());
processInstance.setCustomProInstId(instanceExtDO.getCustomProInstId());
processInstance.setCategory(instanceExtDO.getCategory());
processInstance.setStartUserName(instanceExtDO.getStartUserName());
processInstance.setStartTime(instanceExtDO.getCreateAt());
if (Objects.nonNull(instanceExtDO)) {
processInstance.setCustomProInstId(instanceExtDO.getCustomProInstId());
processInstance.setCategory(instanceExtDO.getCategory());
processInstance.setStartUserName(instanceExtDO.getStartUserName());
processInstance.setStartTime(instanceExtDO.getCreateAt());
}
});
return new BpmPageResult<>(bpmTaskTodoPageItemRespVOS, taskQuery.count());
}

View File

@ -1,6 +1,9 @@
package cn.axzo.server.controller.listener;
import cn.axzo.workflow.core.listener.BpmProcessEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.delegate.event.FlowableCancelledEvent;
import org.flowable.engine.delegate.event.FlowableProcessStartedEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@ -13,4 +16,23 @@ import org.springframework.stereotype.Component;
@Profile("local")
@Component
public class CustomBpmProcessEventListener implements BpmProcessEventListener {
@Override
public void onCreated(FlowableEngineEntityEvent event) {
}
@Override
public void onStarted(FlowableProcessStartedEvent event) {
}
@Override
public void onCancelled(FlowableCancelledEvent event) {
}
@Override
public void onCompleted(FlowableEngineEntityEvent event) {
}
}