Commit 480b17507fa6d4b63124c75f7daaf2a0ceed9755

Authored by yunlongn
1 parent cfdf0498

#完善流程model设计

yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/enums/BpmErrorCodeConstants.java
... ... @@ -26,5 +26,7 @@ public interface BpmErrorCodeConstants {
26 26  
27 27 // ========== OA 工作流模块 1-004-001-000 ==========
28 28 ErrorCode BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS = new ErrorCode(1004001001, "模型数据为空,请先成功设计流程并保存");
  29 + ErrorCode BPMN_MODEL_ERROR = new ErrorCode(1004001002, "工作流模型异常");
  30 + ErrorCode BPMN_MODEL_PROCESS_NOT_EXISTS = new ErrorCode(1004001003, "流程数据为空");
29 31  
30 32 }
... ...
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/service/workflow/impl/BpmModelServiceImpl.java
1 1 package cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.impl;
2 2  
3   -import cn.hutool.core.util.StrUtil;
4 3 import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelCreateVO;
5 4 import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelUpdateVO;
6 5 import cn.iocoder.yudao.adminserver.modules.bpm.enums.BpmErrorCodeConstants;
7 6 import cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.BpmModelService;
8   -import cn.iocoder.yudao.adminserver.modules.tool.framework.errorcode.config.ErrorCodeProperties;
9 7 import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
10 8 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
11 9 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
12   -import com.fasterxml.jackson.databind.JsonNode;
13   -import com.fasterxml.jackson.databind.ObjectMapper;
14   -import com.fasterxml.jackson.databind.node.ObjectNode;
15 10 import lombok.RequiredArgsConstructor;
16 11 import lombok.extern.slf4j.Slf4j;
17 12 import org.activiti.bpmn.converter.BpmnXMLConverter;
18 13 import org.activiti.bpmn.model.BpmnModel;
19 14 import org.activiti.engine.RepositoryService;
  15 +import org.activiti.engine.repository.Deployment;
20 16 import org.activiti.engine.repository.Model;
21 17 import org.apache.commons.lang3.exception.ExceptionUtils;
22 18 import org.springframework.stereotype.Service;
23   -import org.springframework.web.bind.annotation.PathVariable;
24   -import org.springframework.web.bind.annotation.ResponseBody;
  19 +import org.springframework.util.ObjectUtils;
25 20  
26 21 import javax.xml.stream.XMLInputFactory;
27 22 import javax.xml.stream.XMLStreamReader;
28   -import java.io.*;
  23 +import java.io.ByteArrayInputStream;
  24 +import java.io.InputStreamReader;
29 25 import java.nio.charset.StandardCharsets;
30   -import java.util.HashMap;
31   -import java.util.Map;
32 26 import java.util.Optional;
33 27  
34   -import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.NOTICE_NOT_FOUND;
35   -
36 28 /**
37 29 * 工作流模型实现
38 30 * @author yunlongn
... ... @@ -55,42 +47,49 @@ public class BpmModelServiceImpl implements BpmModelService {
55 47 model.setKey(Optional.ofNullable(modelCreateVO.getKey()).orElse("processKey"));
56 48 model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO));
57 49 repositoryService.saveModel(model);
58   - return CommonResult.success("success");
  50 + return CommonResult.success("保存成功");
59 51 }catch (Exception e){
60 52 log.info("模型创建失败!modelCreateVO = {} e = {} ", modelCreateVO, ExceptionUtils.getStackTrace(e));
  53 + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_ERROR);
61 54 }
62 55  
63   - return CommonResult.success("success");
64 56 }
65 57  
66 58 @Override
67 59 public CommonResult<String> updateModel(ModelUpdateVO modelUpdateVO) {
68 60 try {
69 61 Model model = repositoryService.getModel(modelUpdateVO.getId());
70   -
  62 + if (ObjectUtils.isEmpty(model)) {
  63 + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS);
  64 + }
71 65 // 只能修改名字跟描述
72 66 ModelCreateVO modelCreateVO = JsonUtils.parseObject(model.getMetaInfo(), ModelCreateVO.class);
  67 + if (ObjectUtils.isEmpty(modelCreateVO)) {
  68 + modelCreateVO = new ModelCreateVO();
  69 + }
73 70 modelCreateVO.setName(modelUpdateVO.getName());
74 71 modelCreateVO.setDescription(modelUpdateVO.getDescription());
75 72 model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO));
76 73 model.setName(modelUpdateVO.getName());
77   -
  74 + // 更新模型
78 75 repositoryService.saveModel(model);
79 76  
80 77 repositoryService.addModelEditorSource(model.getId(), modelUpdateVO.getBpmnXml().getBytes(StandardCharsets.UTF_8));
81 78  
82   - return CommonResult.success("success");
  79 + return CommonResult.success("保存成功");
83 80 }catch (Exception e){
84   - log.info("模型修改失败!modelCreateVO = {} e = {} ", modelUpdateVO, ExceptionUtils.getStackTrace(e));
  81 + log.info("模型更新失败!modelUpdateVO = {} e = {} ", modelUpdateVO, ExceptionUtils.getStackTrace(e));
  82 + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_ERROR);
85 83 }
86   -
87   - return CommonResult.success("success");
88 84 }
89 85  
90 86 @Override
91 87 public CommonResult<String> deploy(String modelId) {
92 88 // 获取模型
93 89 Model modelData = repositoryService.getModel(modelId);
  90 + if (ObjectUtils.isEmpty(modelData)) {
  91 + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS);
  92 + }
94 93 byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
95 94 if (bytes == null) {
96 95 throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS);
... ... @@ -101,12 +100,23 @@ public class BpmModelServiceImpl implements BpmModelService {
101 100 XMLInputFactory xif = XMLInputFactory.newInstance();
102 101 InputStreamReader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
103 102 XMLStreamReader xtr = xif.createXMLStreamReader(in);
  103 + // 流数据转化为 model
104 104 BpmnModel model = new BpmnXMLConverter().convertToBpmnModel(xtr);
105   - log.info("model ={} ", model);
  105 + if(ObjectUtils.isEmpty(model.getProcesses())){
  106 + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_PROCESS_NOT_EXISTS);
  107 + }
  108 + byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
  109 + // 部署发布模型流程
  110 + String processName = modelData.getName() + ".bpmn20.xml";
  111 + Deployment deployment = repositoryService.createDeployment()
  112 + .name(modelData.getName())
  113 + .addString(processName, new String(bpmnBytes, StandardCharsets.UTF_8))
  114 + .deploy();
  115 + // 部署成功
  116 + return CommonResult.success(deployment.getId());
106 117 } catch (Exception e) {
107   - log.error("e {}", e);
  118 + log.info("模型部署失败!modelId = {} e = {} ", modelId, ExceptionUtils.getStackTrace(e));
  119 + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_ERROR);
108 120 }
109   -
110   - return CommonResult.success("success");
111 121 }
112 122 }
... ...