Commit 480b17507fa6d4b63124c75f7daaf2a0ceed9755
1 parent
cfdf0498
#完善流程model设计
Showing
2 changed files
with
36 additions
and
24 deletions
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/bpm/enums/BpmErrorCodeConstants.java
| @@ -26,5 +26,7 @@ public interface BpmErrorCodeConstants { | @@ -26,5 +26,7 @@ public interface BpmErrorCodeConstants { | ||
| 26 | 26 | ||
| 27 | // ========== OA 工作流模块 1-004-001-000 ========== | 27 | // ========== OA 工作流模块 1-004-001-000 ========== |
| 28 | ErrorCode BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS = new ErrorCode(1004001001, "模型数据为空,请先成功设计流程并保存"); | 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 | package cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.impl; | 1 | package cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.StrUtil; | ||
| 4 | import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelCreateVO; | 3 | import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelCreateVO; |
| 5 | import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelUpdateVO; | 4 | import cn.iocoder.yudao.adminserver.modules.bpm.controller.workflow.vo.model.ModelUpdateVO; |
| 6 | import cn.iocoder.yudao.adminserver.modules.bpm.enums.BpmErrorCodeConstants; | 5 | import cn.iocoder.yudao.adminserver.modules.bpm.enums.BpmErrorCodeConstants; |
| 7 | import cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.BpmModelService; | 6 | import cn.iocoder.yudao.adminserver.modules.bpm.service.workflow.BpmModelService; |
| 8 | -import cn.iocoder.yudao.adminserver.modules.tool.framework.errorcode.config.ErrorCodeProperties; | ||
| 9 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | 7 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 10 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 8 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 11 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 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 | import lombok.RequiredArgsConstructor; | 10 | import lombok.RequiredArgsConstructor; |
| 16 | import lombok.extern.slf4j.Slf4j; | 11 | import lombok.extern.slf4j.Slf4j; |
| 17 | import org.activiti.bpmn.converter.BpmnXMLConverter; | 12 | import org.activiti.bpmn.converter.BpmnXMLConverter; |
| 18 | import org.activiti.bpmn.model.BpmnModel; | 13 | import org.activiti.bpmn.model.BpmnModel; |
| 19 | import org.activiti.engine.RepositoryService; | 14 | import org.activiti.engine.RepositoryService; |
| 15 | +import org.activiti.engine.repository.Deployment; | ||
| 20 | import org.activiti.engine.repository.Model; | 16 | import org.activiti.engine.repository.Model; |
| 21 | import org.apache.commons.lang3.exception.ExceptionUtils; | 17 | import org.apache.commons.lang3.exception.ExceptionUtils; |
| 22 | import org.springframework.stereotype.Service; | 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 | import javax.xml.stream.XMLInputFactory; | 21 | import javax.xml.stream.XMLInputFactory; |
| 27 | import javax.xml.stream.XMLStreamReader; | 22 | import javax.xml.stream.XMLStreamReader; |
| 28 | -import java.io.*; | 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.InputStreamReader; | ||
| 29 | import java.nio.charset.StandardCharsets; | 25 | import java.nio.charset.StandardCharsets; |
| 30 | -import java.util.HashMap; | ||
| 31 | -import java.util.Map; | ||
| 32 | import java.util.Optional; | 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 | * @author yunlongn | 30 | * @author yunlongn |
| @@ -55,42 +47,49 @@ public class BpmModelServiceImpl implements BpmModelService { | @@ -55,42 +47,49 @@ public class BpmModelServiceImpl implements BpmModelService { | ||
| 55 | model.setKey(Optional.ofNullable(modelCreateVO.getKey()).orElse("processKey")); | 47 | model.setKey(Optional.ofNullable(modelCreateVO.getKey()).orElse("processKey")); |
| 56 | model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO)); | 48 | model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO)); |
| 57 | repositoryService.saveModel(model); | 49 | repositoryService.saveModel(model); |
| 58 | - return CommonResult.success("success"); | 50 | + return CommonResult.success("保存成功"); |
| 59 | }catch (Exception e){ | 51 | }catch (Exception e){ |
| 60 | log.info("模型创建失败!modelCreateVO = {} e = {} ", modelCreateVO, ExceptionUtils.getStackTrace(e)); | 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 | @Override | 58 | @Override |
| 67 | public CommonResult<String> updateModel(ModelUpdateVO modelUpdateVO) { | 59 | public CommonResult<String> updateModel(ModelUpdateVO modelUpdateVO) { |
| 68 | try { | 60 | try { |
| 69 | Model model = repositoryService.getModel(modelUpdateVO.getId()); | 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 | ModelCreateVO modelCreateVO = JsonUtils.parseObject(model.getMetaInfo(), ModelCreateVO.class); | 66 | ModelCreateVO modelCreateVO = JsonUtils.parseObject(model.getMetaInfo(), ModelCreateVO.class); |
| 67 | + if (ObjectUtils.isEmpty(modelCreateVO)) { | ||
| 68 | + modelCreateVO = new ModelCreateVO(); | ||
| 69 | + } | ||
| 73 | modelCreateVO.setName(modelUpdateVO.getName()); | 70 | modelCreateVO.setName(modelUpdateVO.getName()); |
| 74 | modelCreateVO.setDescription(modelUpdateVO.getDescription()); | 71 | modelCreateVO.setDescription(modelUpdateVO.getDescription()); |
| 75 | model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO)); | 72 | model.setMetaInfo(JsonUtils.toJsonString(modelCreateVO)); |
| 76 | model.setName(modelUpdateVO.getName()); | 73 | model.setName(modelUpdateVO.getName()); |
| 77 | - | 74 | + // 更新模型 |
| 78 | repositoryService.saveModel(model); | 75 | repositoryService.saveModel(model); |
| 79 | 76 | ||
| 80 | repositoryService.addModelEditorSource(model.getId(), modelUpdateVO.getBpmnXml().getBytes(StandardCharsets.UTF_8)); | 77 | repositoryService.addModelEditorSource(model.getId(), modelUpdateVO.getBpmnXml().getBytes(StandardCharsets.UTF_8)); |
| 81 | 78 | ||
| 82 | - return CommonResult.success("success"); | 79 | + return CommonResult.success("保存成功"); |
| 83 | }catch (Exception e){ | 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 | @Override | 86 | @Override |
| 91 | public CommonResult<String> deploy(String modelId) { | 87 | public CommonResult<String> deploy(String modelId) { |
| 92 | // 获取模型 | 88 | // 获取模型 |
| 93 | Model modelData = repositoryService.getModel(modelId); | 89 | Model modelData = repositoryService.getModel(modelId); |
| 90 | + if (ObjectUtils.isEmpty(modelData)) { | ||
| 91 | + throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS); | ||
| 92 | + } | ||
| 94 | byte[] bytes = repositoryService.getModelEditorSource(modelData.getId()); | 93 | byte[] bytes = repositoryService.getModelEditorSource(modelData.getId()); |
| 95 | if (bytes == null) { | 94 | if (bytes == null) { |
| 96 | throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS); | 95 | throw ServiceExceptionUtil.exception(BpmErrorCodeConstants.BPMN_MODEL_EDITOR_SOURCE_NOT_EXISTS); |
| @@ -101,12 +100,23 @@ public class BpmModelServiceImpl implements BpmModelService { | @@ -101,12 +100,23 @@ public class BpmModelServiceImpl implements BpmModelService { | ||
| 101 | XMLInputFactory xif = XMLInputFactory.newInstance(); | 100 | XMLInputFactory xif = XMLInputFactory.newInstance(); |
| 102 | InputStreamReader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8); | 101 | InputStreamReader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8); |
| 103 | XMLStreamReader xtr = xif.createXMLStreamReader(in); | 102 | XMLStreamReader xtr = xif.createXMLStreamReader(in); |
| 103 | + // 流数据转化为 model | ||
| 104 | BpmnModel model = new BpmnXMLConverter().convertToBpmnModel(xtr); | 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 | } catch (Exception e) { | 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 | } |