#完善流程model设计

This commit is contained in:
yunlongn 2021-11-17 23:01:56 +08:00
parent cfdf04981a
commit 480b17507f
2 changed files with 36 additions and 24 deletions

View File

@ -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, "流程数据为空");
}

View File

@ -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<String> 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<String> 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");
}
}