From 62b720a5d01339dc4cf6d2baa4739bbdfa5150da Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 11 Jan 2022 21:06:54 +0800 Subject: [PATCH] =?UTF-8?q?BPM=20=E6=A8=A1=E5=9E=8B=E9=87=8D=E6=9E=84=202?= =?UTF-8?q?=EF=BC=9A=E5=AF=BC=E5=85=A5=E6=B5=81=E7=A8=8B=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=8F=AA=E5=A1=AB=E5=86=99=20name=E3=80=81description=E3=80=81?= =?UTF-8?q?key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/model/BpmModelController.java | 6 ++--- .../model/vo/BpmModeImportReqVO.java | 4 +-- .../service/definition/BpmModelService.java | 3 ++- .../definition/impl/BpmModelServiceImpl.java | 27 +++++++++++++------ yudao-admin-ui/src/views/bpm/model/index.vue | 4 +-- .../activiti/core/util/ActivitiUtils.java | 24 +++++++++++++++++ 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/BpmModelController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/BpmModelController.java index 1f58beff7..f4fc726fd 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/BpmModelController.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/BpmModelController.java @@ -47,7 +47,7 @@ public class BpmModelController { @ApiOperation(value = "新建模型") @PreAuthorize("@ss.hasPermission('bpm:model:create')") public CommonResult createModel(@Valid @RequestBody BpmModelCreateReqVO createRetVO) { - return success(bpmModelService.createModel(createRetVO)); + return success(bpmModelService.createModel(createRetVO, null)); } @PostMapping("/import") @@ -56,8 +56,8 @@ public class BpmModelController { public CommonResult importModel(@Valid BpmModeImportReqVO importReqVO) throws IOException { BpmModelCreateReqVO createReqVO = BpmModelConvert.INSTANCE.convert(importReqVO); // 读取文件 -// createReqVO.setBpmnXml(IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false)); - return success(bpmModelService.createModel(createReqVO)); + String bpmnXml = IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false); + return success(bpmModelService.createModel(createReqVO, bpmnXml)); } @PutMapping("/update") diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/vo/BpmModeImportReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/vo/BpmModeImportReqVO.java index 80995cce7..f70576222 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/vo/BpmModeImportReqVO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/controller/model/vo/BpmModeImportReqVO.java @@ -9,11 +9,11 @@ import org.springframework.web.multipart.MultipartFile; import javax.validation.constraints.NotNull; -@ApiModel("流程模型的导入 Request VO") +@ApiModel(value = "流程模型的导入 Request VO", description = "相比流程模型的新建来说,只是多了一个 bpmnFile 文件") @Data @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) -public class BpmModeImportReqVO extends BpmModelBaseVO { +public class BpmModeImportReqVO extends BpmModelCreateReqVO { @ApiModelProperty(value = "BPMN 文件", required = true) @NotNull(message = "BPMN 文件不能为空") diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/BpmModelService.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/BpmModelService.java index 48e715458..969b138f0 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/BpmModelService.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/BpmModelService.java @@ -32,9 +32,10 @@ public interface BpmModelService { * 创建流程模型 * * @param modelVO 创建信息 + * @param bpmnXml BPMN XML * @return 创建的流程模型的编号 */ - String createModel(@Valid BpmModelCreateReqVO modelVO); + String createModel(@Valid BpmModelCreateReqVO modelVO, String bpmnXml); /** * 修改流程模型 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/impl/BpmModelServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/impl/BpmModelServiceImpl.java index b0b184138..10c79d33e 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/impl/BpmModelServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/definition/impl/BpmModelServiceImpl.java @@ -9,6 +9,7 @@ import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.dto.BpmDefini import cn.iocoder.yudao.adminserver.modules.bpm.service.form.BpmFormService; import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.BpmModelService; import cn.iocoder.yudao.adminserver.modules.bpm.service.definition.dto.BpmModelMetaInfoRespDTO; +import cn.iocoder.yudao.framework.activiti.core.util.ActivitiUtils; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; @@ -108,7 +109,7 @@ public class BpmModelServiceImpl implements BpmModelService { @Override @Transactional(rollbackFor = Exception.class) // 因为进行多个 activiti 操作,所以开启事务 - public String createModel(BpmModelCreateReqVO createReqVO) { + public String createModel(BpmModelCreateReqVO createReqVO, String bpmnXml) { checkKeyNCName(createReqVO.getKey()); // 校验流程标识已经存在 Model keyModel = this.getModelByKey(createReqVO.getKey()); @@ -121,6 +122,8 @@ public class BpmModelServiceImpl implements BpmModelService { BpmModelConvert.INSTANCE.copy(model, createReqVO); // 保存流程定义 repositoryService.saveModel(model); + // 保存 BPMN XML + saveModelBpmnXml(model, bpmnXml); return model.getId(); } @@ -138,7 +141,16 @@ public class BpmModelServiceImpl implements BpmModelService { // 更新模型 repositoryService.saveModel(model); // 更新 BPMN XML - repositoryService.addModelEditorSource(model.getId(), StrUtil.utf8Bytes(updateReqVO.getBpmnXml())); + saveModelBpmnXml(model, updateReqVO.getBpmnXml()); + } + + private void saveModelBpmnXml(Model model, String bpmnXml) { + if (StrUtil.isEmpty(bpmnXml)) { + return; + } + byte[] bpmnBytes = ActivitiUtils.replaceBpmnMainProcessIdAndName(bpmnXml, + model.getKey(), model.getName()); + repositoryService.addModelEditorSource(model.getId(), bpmnBytes); } @Override @@ -211,11 +223,10 @@ 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().get(0).getId() -// } + public static void main(String[] args) { + // 创建转换对象 + BpmnXMLConverter converter = new BpmnXMLConverter(); + BpmnModel bpmnModel = converter.convertToBpmnModel(new StringStreamSource(""), true, true); + } } diff --git a/yudao-admin-ui/src/views/bpm/model/index.vue b/yudao-admin-ui/src/views/bpm/model/index.vue index ca9e78b9c..9841f121e 100644 --- a/yudao-admin-ui/src/views/bpm/model/index.vue +++ b/yudao-admin-ui/src/views/bpm/model/index.vue @@ -307,7 +307,7 @@ export default { return; } createModel(this.form).then(response => { - this.msgSuccess("新增成功"); + this.msgSuccess("新建流程模型成功"); this.open = false; this.getList(); }); @@ -404,7 +404,7 @@ export default { // 重置表单 this.uploadClose(); // 提示,并刷新 - this.msgSuccess("上传成功!请点击【设计流程】按钮,进行编辑保存后,才可以进行【发布流程】"); + this.msgSuccess("导入流程模型成功"); this.getList(); }, uploadClose() { diff --git a/yudao-framework/yudao-spring-boot-starter-activiti/src/main/java/cn/iocoder/yudao/framework/activiti/core/util/ActivitiUtils.java b/yudao-framework/yudao-spring-boot-starter-activiti/src/main/java/cn/iocoder/yudao/framework/activiti/core/util/ActivitiUtils.java index 82d1ade4c..aff555fad 100644 --- a/yudao-framework/yudao-spring-boot-starter-activiti/src/main/java/cn/iocoder/yudao/framework/activiti/core/util/ActivitiUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-activiti/src/main/java/cn/iocoder/yudao/framework/activiti/core/util/ActivitiUtils.java @@ -2,10 +2,13 @@ package cn.iocoder.yudao.framework.activiti.core.util; import cn.hutool.core.util.ReflectUtil; import com.alibaba.ttl.TransmittableThreadLocal; +import org.activiti.bpmn.converter.BpmnXMLConverter; +import org.activiti.bpmn.model.BpmnModel; import org.activiti.engine.history.HistoricProcessInstance; import org.activiti.engine.impl.identity.Authentication; import org.activiti.engine.impl.persistence.entity.HistoricProcessInstanceEntityImpl; import org.activiti.engine.impl.persistence.entity.HistoricScopeInstanceEntityImpl; +import org.activiti.engine.impl.util.io.StringStreamSource; /** * Activiti 工具类 @@ -37,4 +40,25 @@ public class ActivitiUtils { Authentication.setAuthenticatedUserId(null); } + // ========== BPMN XML 相关 ========== + + /** + * 替换 BPMN XML 主流程的 id 和 name 属性 + * + * @param bpmnXml 原始的 BPMN XML 字符串 + * @param id 编号,对应到 XML 实际是 key 属性 + * @param name 名字 + * @return 新的 BPMN XML 的字节数组 + */ + public static byte[] replaceBpmnMainProcessIdAndName(String bpmnXml, String id, String name) { + // 转换成 BpmnModel 对象 + BpmnXMLConverter converter = new BpmnXMLConverter(); + BpmnModel bpmnModel = converter.convertToBpmnModel(new StringStreamSource(bpmnXml), true, true); + // 设置 id 和 name 属性 + bpmnModel.getMainProcess().setId(id); + bpmnModel.getMainProcess().setName(name); + // 转换回字节数组 + return converter.convertToXML(bpmnModel); + } + }