flowable動態(tài)創(chuàng)建多級流程模板實現(xiàn)demo
更新時間:2023年05月23日 11:15:21 作者:多喝灬丶燙水
這篇文章主要為大家介紹了flowable動態(tài)創(chuàng)建多級流程模板實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
flowable動態(tài)創(chuàng)建多級流程模板
引入Maven依賴
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.6.0</version>
</dependency>動態(tài)創(chuàng)建流程節(jié)點工具類
/**
* 動態(tài)創(chuàng)建流程節(jié)點
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class FlowableBpmnHandler {
/**
* 創(chuàng)建開始節(jié)點信息
*
* @return
*/
public static FlowElement createStartFlowElement(String id, String name) {
StartEvent startEvent = new StartEvent();
startEvent.setId(id);
startEvent.setName(name);
return startEvent;
}
/**
* 創(chuàng)建結(jié)束節(jié)點信息
*
* @param id
* @param name
* @return
*/
public static FlowElement createEndFlowElement(String id, String name) {
EndEvent endEvent = new EndEvent();
endEvent.setId(id);
endEvent.setName(name);
return endEvent;
}
/**
* 循環(huán)創(chuàng)建普通任務節(jié)點信息
*
* @param processEntity 流程實體
* @return
*/
public static List<UserTask> createCommonUserTask(ProcessEntity processEntity) {
List<ProcessNode> processNodes = processEntity.getProcessNodeList();
List<UserTask> userTaskList = Lists.newLinkedList();
for (int i = 0; i < processNodes.size(); i++) {
ProcessNode node = processNodes.get(i);
node.setNodeId(UUIDHelper.randomUUID());
node.setNodeLevel(i + 1);
UserTask userTask = new UserTask();
userTask.setId(TypeEnum.getProcessKey(processEntity.getType()) + "_task_" + node.getNodeId());
userTask.setCategory(String.valueOf(i + 1));
userTask.setDocumentation(i == processNodes.size() - 1 ? "true" : "false");
userTask.setName(node.getNodeName());
userTaskList.add(userTask);
}
return userTaskList;
}
/**
* 創(chuàng)建排它網(wǎng)關(guān)節(jié)點
*
* @return
*/
public static ExclusiveGateway createExclusiveGateway(Integer i) {
ExclusiveGateway gateway = new ExclusiveGateway();
gateway.setName("getaway_name_" + i);
gateway.setId("getaway_id_" + i);
return gateway;
}
}動態(tài)創(chuàng)建流程模板工具類
/**
* 動態(tài)創(chuàng)建流程模板
*
* @author ll
* @date 2023/04/19 14:28
**/
@Slf4j
@Component
public class DynamicProcessDefinition {
@Resource
private RepositoryService repositoryService;
public String createProcessDefinition(ProcessEntity processEntity) {
//數(shù)據(jù)校驗:傳入的數(shù)據(jù)節(jié)點字段:流程配置id、節(jié)點id、節(jié)點順序、節(jié)點描述
List<ProcessNode> nodeList = processEntity.getProcessNodeList();
if (CollUtil.isEmpty(nodeList)) {
throw new RuntimeException("流程節(jié)點不能少于一個,請配置發(fā)起人節(jié)點和至少一個審批節(jié)點");
}
BpmnModel bpmnModel = new BpmnModel();
Process process = new Process();
bpmnModel.addProcess(process);
// 流程標識
process.setId(TypeEnum.getProcessKey(processEntity.getType()));
process.setName(TypeEnum.getProcessKey(processEntity.getType()));
//開始事件
FlowElement startEvent = FlowableBpmnHandler.createStartFlowElement("start", "開始");
process.addFlowElement(startEvent);
//結(jié)束事件--任務正常完成
FlowElement endEvent = FlowableBpmnHandler.createEndFlowElement("end", "結(jié)束");
process.addFlowElement(endEvent);
//創(chuàng)建用戶節(jié)點任務
List<UserTask> userTaskList = FlowableBpmnHandler.createCommonUserTask(processEntity);
//構(gòu)建流程模板
buildProcessTemplate(process, startEvent, endEvent, userTaskList);
//該流程的流程xml字符串
//BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
//byte[] convertToXML = bpmnXMLConverter.convertToXML(bpmnModel);
//String bytes = new String(convertToXML);
//log.info("該流程的流程xml為:{}", bytes);
// 創(chuàng)建一個新的部署
Deployment deploy = repositoryService.createDeployment().key(TypeEnum.getProcessKey(processEntity.getType()))
.name(TypeEnum.getProcessKey(processEntity.getType()))
.addBpmnModel(TypeEnum.getProcessKey(processEntity.getType()) + ".bpmn", bpmnModel)
.deploy();
log.info("部署id:{}", deploy.getId());
return deploy.getId();
}
/**
* 構(gòu)建流程模板
*
* @param process
* @param startEvent
* @param endEvent
* @param userTaskList
* @return void
* @author ll
* @date 2023/04/24 08:53
*/
private void buildProcessTemplate(Process process, FlowElement startEvent, FlowElement endEvent, List<UserTask> userTaskList) {
for (int i = 0; i < userTaskList.size(); i++) {
//用戶任務
UserTask userTask = userTaskList.get(i);
process.addFlowElement(userTask);
//創(chuàng)建排它網(wǎng)關(guān)節(jié)點
ExclusiveGateway exclusiveGateway = FlowableBpmnHandler.createExclusiveGateway(i);
process.addFlowElement(exclusiveGateway);
//開始事件到第一個節(jié)點
if (i == 0) {
// 開始節(jié)點到第一級節(jié)點
SequenceFlow startSequenceFlow = new SequenceFlow(startEvent.getId(), userTask.getId());
process.addFlowElement(startSequenceFlow);
}
//用戶任務到網(wǎng)關(guān)節(jié)點
SequenceFlow sequenceFlow = new SequenceFlow(userTask.getId(), exclusiveGateway.getId());
sequenceFlow.setName(userTask.getName() + "_開始審批");
process.addFlowElement(sequenceFlow);
//同意:取下一步用戶任務的節(jié)點id,可能為結(jié)束節(jié)點或者用戶任務節(jié)點
String nextUserTaskId = endEvent.getId();
if (userTaskList.size() - i > 1) {
nextUserTaskId = userTaskList.get(i + 1).getId();
}
SequenceFlow sequenceFlowAgree = new SequenceFlow(exclusiveGateway.getId(), nextUserTaskId);
sequenceFlowAgree.setConditionExpression("${auditResult}");
sequenceFlowAgree.setName("同意");
process.addFlowElement(sequenceFlowAgree);
//不同意:回退到上一步,取上一步的節(jié)點id
String preUserTaskId = userTaskList.get(0).getId();
if (i != 0) {
preUserTaskId = userTaskList.get(i - 1).getId();
}
SequenceFlow sequenceFlowRefuse = new SequenceFlow(exclusiveGateway.getId(), preUserTaskId);
sequenceFlowRefuse.setConditionExpression("${!auditResult}");
sequenceFlowRefuse.setName("拒絕");
process.addFlowElement(sequenceFlowRefuse);
}
}
}保存流程配置模板的接口
@Resource
private DynamicProcessDefinition dynamicProcessDefinition;
@RequestMapping("/save")
public Result<String> save(@RequestBody ProcessEntity processEntity) {
return dynamicProcessDefinition.createProcessDefinition(processEntity);;
}開啟流程接口
@RequestMapping("/start")
public String start(@RequestBody AuditData auditData) {
//業(yè)務自己定義key
String businessKey = auditData.getBusinessKey();
//流程定義key
String processKey = auditData.getType();
// 判斷是否啟動流程
boolean isStartProcess = null != runtimeService.createProcessInstanceQuery()
.processDefinitionKey(processKey)
.processInstanceBusinessKey(businessKey)
.singleResult();
if (!isStartProcess) {
//記錄開啟流程的用戶
HashMap<String, Object> variable = Maps.newHashMap();
//啟動流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey, variable);
log.info("啟動流程實例成功:processInstanceId={}", processInstance.getId());
}
return !isStartProcess ? "流程啟動成功" : "流程啟動失敗";
}以上就是flowable動態(tài)創(chuàng)建多級流程模板demo的詳細內(nèi)容,更多關(guān)于flowable動態(tài)多級流程模板的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring MVC簡介_動力節(jié)點Java學院整理
Spring MVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在Spring Web Flow里面。今天先從寫一個Spring MVC的HelloWorld開始,讓我們看看如何搭建起一個Spring mvc的環(huán)境并運行程序,感興趣的朋友一起學習吧2017-08-08
SpringBoot項目中出現(xiàn)不同端口跨域問題的解決方法
這篇文章主要介紹了SpringBoot項目中出現(xiàn)不同端口跨域問題的解決方法,文中介紹了兩種解決方法,并給出了詳細的代碼供大家參考,具有一定的參考價值,需要的朋友可以參考下2024-03-03
Spring Boot 實現(xiàn)敏感詞及特殊字符過濾處理
這篇文章主要介紹了SpringBoot實現(xiàn)敏感詞及特殊字符過濾處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java面試之如何實現(xiàn)10億數(shù)據(jù)判重
當數(shù)據(jù)量比較大時,使用常規(guī)的方式來判重就不行了,所以這篇文章小編主要來和大家介紹一下Java實現(xiàn)10億數(shù)據(jù)判重的相關(guān)方法,希望對大家有所幫助2024-02-02

