From 480b17507fa6d4b63124c75f7daaf2a0ceed9755 Mon Sep 17 00:00:00 2001 From: yunlongn Date: Wed, 17 Nov 2021 23:01:56 +0800 Subject: [PATCH] =?UTF-8?q?#=E5=AE=8C=E5=96=84=E6=B5=81=E7=A8=8Bmodel?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bpm/enums/BpmErrorCodeConstants.java | 2 + .../workflow/impl/BpmModelServiceImpl.java | 58 +++++++++++-------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/enums/BpmErrorCodeConstants.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/enums/BpmErrorCodeConstants.java index e3a4ce548..48564d41e 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/enums/BpmErrorCodeConstants.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/enums/BpmErrorCodeConstants.java @@ -26,5 +26,7 @@ public interface BpmErrorCodeConstants { // ========== OA 工作流模块 1-004-001-000 ========== ErrorCode BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS = new ErrorCode(1004001001, "模型数据为空,请先成功设计流程并保存"); + ErrorCode BPMN_MODEL_ERROR = new ErrorCode(1004001002, "工作流模型异常"); + ErrorCode BPMN_MODEL_PROCESS_NOT_EXISTS = new ErrorCode(1004001003, "流程数据为空"); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/workflow/impl/BpmModelServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/workflow/impl/BpmModelServiceImpl.java index bbc2480d7..750997301 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/workflow/impl/BpmModelServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/workflow/impl/BpmModelServiceImpl.java @@ -1,38 +1,30 @@ package cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.impl; -import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelCreateVO; import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelUpdateVO; import cn.iocoder.yudao.adminserver.modules.bpm.enums.BpmErrorCodeConstants; import cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.BpmModelService; -import cn.iocoder.yudao.adminserver.modules.tool.framework.errorcode.config.ErrorCodeProperties; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.RequiredArgsConstructor; 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.repository.Deployment; import org.activiti.engine.repository.Model; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.util.ObjectUtils; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; -import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.NOTICE_NOT_FOUND; - /** * 工作流模型实现 * @author yunlongn @@ -55,42 +47,49 @@ public class BpmModelServiceImpl implements BpmModelService { model.setKey(Optional.ofNullable(modelCreateVO.getKey()).orElse("processKey")); model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO)); repositoryService.saveModel(model); - return CommonResult.success("success"); + return CommonResult.success("保存成功"); }catch (Exception e){ log.info("模型创建失败!modelCreateVO = {} e = {} ", modelCreateVO, ExceptionUtils.getStackTrace(e)); + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_ERROR); } - return CommonResult.success("success"); } @Override public CommonResult updateModel(ModelUpdateVO modelUpdateVO) { try { Model model = repositoryService.getModel(modelUpdateVO.getId()); - + if (ObjectUtils.isEmpty(model)) { + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS); + } // 只能修改名字跟描述 ModelCreateVO modelCreateVO = JsonUtils.parseObject(model.getMetaInfo(), ModelCreateVO.class); + if (ObjectUtils.isEmpty(modelCreateVO)) { + modelCreateVO = new ModelCreateVO(); + } modelCreateVO.setName(modelUpdateVO.getName()); modelCreateVO.setDescription(modelUpdateVO.getDescription()); model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO)); model.setName(modelUpdateVO.getName()); - + // 更新模型 repositoryService.saveModel(model); repositoryService.addModelEditorSource(model.getId(), modelUpdateVO.getBpmnXml().getBytes(StandardCharsets.UTF_8)); - return CommonResult.success("success"); + return CommonResult.success("保存成功"); }catch (Exception e){ - log.info("模型修改失败!modelCreateVO = {} e = {} ", modelUpdateVO, ExceptionUtils.getStackTrace(e)); + log.info("模型更新失败!modelUpdateVO = {} e = {} ", modelUpdateVO, ExceptionUtils.getStackTrace(e)); + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_ERROR); } - - return CommonResult.success("success"); } @Override public CommonResult deploy(String modelId) { // 获取模型 Model modelData = repositoryService.getModel(modelId); + if (ObjectUtils.isEmpty(modelData)) { + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS); + } byte[] bytes = repositoryService.getModelEditorSource(modelData.getId()); if (bytes == null) { throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS); @@ -101,12 +100,23 @@ public class BpmModelServiceImpl implements BpmModelService { XMLInputFactory xif = XMLInputFactory.newInstance(); InputStreamReader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8); XMLStreamReader xtr = xif.createXMLStreamReader(in); + // 流数据转化为 model BpmnModel model = new BpmnXMLConverter().convertToBpmnModel(xtr); - log.info("model ={} ", model); + if(ObjectUtils.isEmpty(model.getProcesses())){ + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_PROCESS_NOT_EXISTS); + } + byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model); + // 部署发布模型流程 + String processName = modelData.getName() + ".bpmn20.xml"; + Deployment deployment = repositoryService.createDeployment() + .name(modelData.getName()) + .addString(processName, new String(bpmnBytes, StandardCharsets.UTF_8)) + .deploy(); + // 部署成功 + return CommonResult.success(deployment.getId()); } catch (Exception e) { - log.error("e {}", e); + log.info("模型部署失败!modelId = {} e = {} ", modelId, ExceptionUtils.getStackTrace(e)); + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_ERROR); } - - return CommonResult.success("success"); } }