Commit d6a6a01252d868bb8902587af96403a8e84e3ed4

Authored by jason
1 parent a207412e

工作流 Flowable 发布流程的部分实现

Showing 17 changed files with 331 additions and 125 deletions
yudao-module-bpm/yudao-module-bpm-base/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmAbstractModelService.java 0 → 100644
  1 +package cn.iocoder.yudao.module.bpm.service.definition;
  2 +
  3 +import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
  4 +import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
  5 +import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
  6 +import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelFormTypeEnum;
  7 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
  8 +
  9 +import java.util.Objects;
  10 +
  11 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  12 +import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
  13 +/**
  14 + * 流程模型通用抽象类
  15 + * Activiti 和 flowable 通用的流程模型抽象类, Activiti 和 flowable 两边通用的方法
  16 + *
  17 + * @author yunlongn
  18 + * @author jason
  19 + */
  20 +public abstract class BpmAbstractModelService {
  21 +
  22 + protected final BpmFormService bpmFormService;
  23 +
  24 + public BpmAbstractModelService(BpmFormService bpmFormService) {
  25 + this.bpmFormService = bpmFormService;
  26 + }
  27 +
  28 + /**
  29 + * 校验流程表单已配置
  30 + *
  31 + * @param metaInfoStr 流程模型 metaInfo 字段
  32 + * @return 流程表单
  33 + */
  34 + protected BpmFormDO checkFormConfig(String metaInfoStr) {
  35 + BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(metaInfoStr, BpmModelMetaInfoRespDTO.class);
  36 + if (metaInfo == null || metaInfo.getFormType() == null) {
  37 + throw exception(MODEL_DEPLOY_FAIL_FORM_NOT_CONFIG);
  38 + }
  39 + // 校验表单存在
  40 + if (Objects.equals(metaInfo.getFormType(), BpmModelFormTypeEnum.NORMAL.getType())) {
  41 + BpmFormDO form = bpmFormService.getForm(metaInfo.getFormId());
  42 + if (form == null) {
  43 + throw exception(FORM_NOT_EXISTS);
  44 + }
  45 + return form;
  46 + }
  47 + return null;
  48 + }
  49 +
  50 +
  51 + protected void checkKeyNCName(String key) {
  52 + if (!ValidationUtils.isXmlNCName(key)) {
  53 + throw exception(MODEL_KEY_VALID);
  54 + }
  55 + }
  56 +}
... ...
yudao-module-bpm/yudao-module-bpm-base/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelCommonService.java
... ... @@ -44,4 +44,26 @@ public interface BpmModelCommonService {
44 44 * @param updateReqVO 更新信息
45 45 */
46 46 void updateModel(@Valid BpmModelUpdateReqVO updateReqVO);
  47 +
  48 + /**
  49 + * 将流程模型,部署成一个流程定义
  50 + *
  51 + * @param id 编号
  52 + */
  53 + void deployModel(String id);
  54 +
  55 + /**
  56 + * 删除模型
  57 + *
  58 + * @param id 编号
  59 + */
  60 + void deleteModel(String id);
  61 +
  62 + /**
  63 + * 修改模型的状态,实际更新的部署的流程定义的状态
  64 + *
  65 + * @param id 编号
  66 + * @param state 状态
  67 + */
  68 + void updateModelState(String id, Integer state);
47 69 }
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/dto/BpmModelMetaInfoRespDTO.java renamed to yudao-module-bpm/yudao-module-bpm-base/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/dto/BpmModelMetaInfoRespDTO.java
... ... @@ -5,7 +5,7 @@ import lombok.Data;
5 5  
6 6 /**
7 7 * BPM 流程 MetaInfo Response DTO
8   - * 主要用于 {@link org.activiti.engine.repository.Model#setMetaInfo(String)} 的存储
  8 + * 主要用于 { Model#setMetaInfo(String)} 的存储
9 9 *
10 10 * @author 芋道源码
11 11 */
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/dto/BpmProcessDefinitionCreateReqDTO.java renamed to yudao-module-bpm/yudao-module-bpm-base/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/dto/BpmProcessDefinitionCreateReqDTO.java
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/convert/definition/BpmModelConvert.java
... ... @@ -2,11 +2,11 @@ package cn.iocoder.yudao.module.bpm.convert.definition;
2 2  
3 3 import cn.hutool.core.util.StrUtil;
4 4 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.*;
5   -import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
6   -import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
7 5 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
8 6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
9 7 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
  8 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
  9 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
10 10 import org.activiti.engine.impl.persistence.entity.SuspensionState;
11 11 import org.activiti.engine.repository.Deployment;
12 12 import org.activiti.engine.repository.Model;
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/convert/definition/BpmProcessDefinitionConvert.java
1 1 package cn.iocoder.yudao.module.bpm.convert.definition;
2 2  
  3 +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
3 4 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionPageItemRespVO;
4 5 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
5 6 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
6 7 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
7 8 import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
8   -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
9 9 import org.activiti.engine.impl.persistence.entity.SuspensionState;
10 10 import org.activiti.engine.repository.Deployment;
11 11 import org.activiti.engine.repository.ProcessDefinition;
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelService.java
... ... @@ -13,29 +13,6 @@ import javax.validation.Valid;
13 13 */
14 14 public interface BpmModelService extends BpmModelCommonService {
15 15  
16   -
17   - /**
18   - * 将流程模型,部署成一个流程定义
19   - *
20   - * @param id 编号
21   - */
22   - void deployModel(String id);
23   -
24   - /**
25   - * 删除模型
26   - *
27   - * @param id 编号
28   - */
29   - void deleteModel(String id);
30   -
31   - /**
32   - * 修改模型的状态,实际更新的部署的流程定义的状态
33   - *
34   - * @param id 编号
35   - * @param state 状态 {@link org.activiti.engine.impl.persistence.entity.SuspensionState}
36   - */
37   - void updateModelState(String id, Integer state);
38   -
39 16 /**
40 17 * 获得流程模型编号对应的 BPMN Model
41 18 *
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmModelServiceImpl.java
... ... @@ -6,16 +6,14 @@ import cn.hutool.core.util.StrUtil;
6 6 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.*;
7 7 import cn.iocoder.yudao.module.bpm.convert.definition.BpmModelConvert;
8 8 import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
9   -import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
10 9 import cn.iocoder.yudao.framework.activiti.core.util.ActivitiUtils;
11 10 import cn.iocoder.yudao.framework.common.pojo.PageResult;
12 11 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
13 12 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
14 13 import cn.iocoder.yudao.framework.common.util.object.PageUtils;
15   -import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
16 14 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.rule.BpmTaskAssignRuleRespVO;
17 15 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
18   -import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelFormTypeEnum;
  16 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
19 17 import lombok.extern.slf4j.Slf4j;
20 18 import org.activiti.bpmn.model.BpmnModel;
21 19 import org.activiti.engine.RepositoryService;
... ... @@ -47,18 +45,22 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
47 45 @Service
48 46 @Validated
49 47 @Slf4j
50   -public class BpmModelServiceImpl implements BpmModelService {
  48 +public class BpmModelServiceImpl extends BpmAbstractModelService implements BpmModelService {
51 49  
52 50 @Resource
53 51 private RepositoryService repositoryService;
54   - @Resource
55   - private BpmFormService bpmFormService;
  52 +
56 53 @Resource
57 54 private BpmProcessDefinitionService processDefinitionService;
58 55 @Resource
59 56 @Lazy // 解决循环依赖
60 57 private BpmTaskAssignRuleService taskAssignRuleService;
61 58  
  59 +
  60 + public BpmModelServiceImpl(BpmFormService bpmFormService) {
  61 + super(bpmFormService);
  62 + }
  63 +
62 64 @Override
63 65 public PageResult<BpmModelPageItemRespVO> getModelPage(BpmModelPageReqVO pageVO) {
64 66 ModelQuery modelQuery = repositoryService.createModelQuery();
... ... @@ -167,7 +169,7 @@ public class BpmModelServiceImpl implements BpmModelService {
167 169 }
168 170 // TODO 芋艿:校验流程图的有效性;例如说,是否有开始的元素,是否有结束的元素;
169 171 // 校验表单已配
170   - BpmFormDO form = checkFormConfig(model);
  172 + BpmFormDO form = checkFormConfig(model.getMetaInfo());
171 173 // 校验任务分配规则已配置
172 174 checkTaskAssignRuleAllConfig(id);
173 175  
... ... @@ -214,28 +216,6 @@ public class BpmModelServiceImpl implements BpmModelService {
214 216 });
215 217 }
216 218  
217   - /**
218   - * 校验流程表单已配置
219   - *
220   - * @param model 流程模型
221   - * @return 流程表单
222   - */
223   - private BpmFormDO checkFormConfig(Model model) {
224   - BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
225   - if (metaInfo == null || metaInfo.getFormType() == null) {
226   - throw exception(MODEL_DEPLOY_FAIL_FORM_NOT_CONFIG);
227   - }
228   - // 校验表单存在
229   - if (Objects.equals(metaInfo.getFormType(), BpmModelFormTypeEnum.NORMAL.getType())) {
230   - BpmFormDO form = bpmFormService.getForm(metaInfo.getFormId());
231   - if (form == null) {
232   - throw exception(FORM_NOT_EXISTS);
233   - }
234   - return form;
235   - }
236   - return null;
237   - }
238   -
239 219 @Override
240 220 @Transactional(rollbackFor = Exception.class)
241 221 public void deleteModel(String id) {
... ... @@ -291,10 +271,4 @@ public class BpmModelServiceImpl implements BpmModelService {
291 271 return repositoryService.createModelQuery().modelKey(key).singleResult();
292 272 }
293 273  
294   - private void checkKeyNCName(String key) {
295   - if (!ValidationUtils.isXmlNCName(key)) {
296   - throw exception(MODEL_KEY_VALID);
297   - }
298   - }
299   -
300 274 }
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmProcessDefinitionService.java
... ... @@ -5,9 +5,9 @@ import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmPro
5 5 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionPageReqVO;
6 6 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
7 7 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
8   -import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
9 8 import cn.iocoder.yudao.framework.common.pojo.PageResult;
10 9 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
  10 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
11 11 import org.activiti.bpmn.model.BpmnModel;
12 12 import org.activiti.engine.repository.Deployment;
13 13 import org.activiti.engine.repository.ProcessDefinition;
... ...
yudao-module-bpm/yudao-module-bpm-impl-activiti/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmProcessDefinitionServiceImpl.java
... ... @@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.ObjectUtil;
5 5 import cn.hutool.core.util.StrUtil;
6 6 import cn.iocoder.yudao.module.bpm.convert.definition.BpmProcessDefinitionConvert;
7   -import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
8 7 import cn.iocoder.yudao.framework.activiti.core.util.ActivitiUtils;
9 8 import cn.iocoder.yudao.framework.common.pojo.PageResult;
10 9 import cn.iocoder.yudao.framework.common.util.object.PageUtils;
... ... @@ -15,6 +14,7 @@ import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmPro
15 14 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
16 15 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
17 16 import cn.iocoder.yudao.module.bpm.dal.mysql.definition.BpmProcessDefinitionExtMapper;
  17 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.activiti.bpmn.model.BpmnModel;
20 20 import org.activiti.engine.RepositoryService;
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/definition/FlowableModelController.java
... ... @@ -67,4 +67,13 @@ public class FlowableModelController {
67 67 String bpmnXml = IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false);
68 68 return success(modelService.createModel(createReqVO, bpmnXml));
69 69 }
  70 +
  71 + @PostMapping("/deploy")
  72 + @ApiOperation(value = "部署模型")
  73 + @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
  74 + @PreAuthorize("@ss.hasPermission('bpm:model:deploy')")
  75 + public CommonResult<Boolean> deployModel(@RequestParam("id") String id) {
  76 + modelService.deployModel(id);
  77 + return success(true);
  78 + }
70 79 }
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/convert/definition/BpmProcessDefinitionConvert.java 0 → 100644
  1 +package cn.iocoder.yudao.module.bpm.convert.definition;
  2 +
  3 +import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
  4 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
  5 +import org.mapstruct.Mapper;
  6 +import org.mapstruct.factory.Mappers;
  7 +
  8 +/**
  9 + * Bpm 流程定义的 Convert
  10 + *
  11 + * @author yunlong.li
  12 + */
  13 +@Mapper
  14 +public interface BpmProcessDefinitionConvert {
  15 + BpmProcessDefinitionConvert INSTANCE = Mappers.getMapper(BpmProcessDefinitionConvert.class);
  16 +
  17 + BpmProcessDefinitionExtDO convert2(BpmProcessDefinitionCreateReqDTO bean);
  18 +}
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/convert/definition/ModelConvert.java
... ... @@ -5,7 +5,8 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
5 5 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
6 6 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.*;
7 7 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
8   -import cn.iocoder.yudao.module.bpm.service.definition.dto.ModelMetaInfoRespDTO;
  8 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
  9 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
9 10 import org.flowable.common.engine.impl.db.SuspensionState;
10 11 import org.flowable.engine.repository.Deployment;
11 12 import org.flowable.engine.repository.Model;
... ... @@ -32,7 +33,7 @@ public interface ModelConvert {
32 33 Map<String, Deployment> deploymentMap,
33 34 Map<String, ProcessDefinition> processDefinitionMap) {
34 35 return CollectionUtils.convertList(list, model -> {
35   - ModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), ModelMetaInfoRespDTO.class);
  36 + BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
36 37 BpmFormDO form = metaInfo != null ? formMap.get(metaInfo.getFormId()) : null;
37 38 Deployment deployment = model.getDeploymentId() != null ? deploymentMap.get(model.getDeploymentId()) : null;
38 39 ProcessDefinition processDefinition = model.getDeploymentId() != null ? processDefinitionMap.get(model.getDeploymentId()) : null;
... ... @@ -75,12 +76,32 @@ public interface ModelConvert {
75 76 to.setKey(model.getKey());
76 77 to.setCategory(model.getCategory());
77 78 // metaInfo
78   - ModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), ModelMetaInfoRespDTO.class);
  79 + BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
79 80 copyTo(metaInfo, to);
80 81 }
  82 +
81 83 BpmModelCreateReqVO convert(BpmModeImportReqVO bean);
82 84  
83   - void copyTo(ModelMetaInfoRespDTO from, @MappingTarget BpmModelBaseVO to);
  85 + default BpmProcessDefinitionCreateReqDTO convert2(Model model, BpmFormDO form) {
  86 + BpmProcessDefinitionCreateReqDTO createReqDTO = new BpmProcessDefinitionCreateReqDTO();
  87 + createReqDTO.setModelId(model.getId());
  88 + createReqDTO.setName(model.getName());
  89 + createReqDTO.setKey(model.getKey());
  90 + createReqDTO.setCategory(model.getCategory());
  91 + BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
  92 + // metaInfo
  93 + copyTo(metaInfo, createReqDTO);
  94 + // form
  95 + if (form != null) {
  96 + createReqDTO.setFormConf(form.getConf());
  97 + createReqDTO.setFormFields(form.getFields());
  98 + }
  99 + return createReqDTO;
  100 + }
  101 +
  102 + void copyTo(BpmModelMetaInfoRespDTO from, @MappingTarget BpmProcessDefinitionCreateReqDTO to);
  103 +
  104 + void copyTo(BpmModelMetaInfoRespDTO from, @MappingTarget BpmModelBaseVO to);
84 105  
85 106 BpmModelPageItemRespVO.ProcessDefinition convert(ProcessDefinition bean);
86 107  
... ... @@ -94,15 +115,15 @@ public interface ModelConvert {
94 115 default void copy(Model model, BpmModelUpdateReqVO bean) {
95 116 model.setName(bean.getName());
96 117 model.setCategory(bean.getCategory());
97   - model.setMetaInfo(buildMetaInfoStr(JsonUtils.parseObject(model.getMetaInfo(), ModelMetaInfoRespDTO.class),
  118 + model.setMetaInfo(buildMetaInfoStr(JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class),
98 119 bean.getDescription(), bean.getFormType(), bean.getFormId(),
99 120 bean.getFormCustomCreatePath(), bean.getFormCustomViewPath()));
100 121 }
101 122  
102   - default String buildMetaInfoStr(ModelMetaInfoRespDTO metaInfo, String description, Integer formType,
  123 + default String buildMetaInfoStr(BpmModelMetaInfoRespDTO metaInfo, String description, Integer formType,
103 124 Long formId, String formCustomCreatePath, String formCustomViewPath) {
104 125 if (metaInfo == null) {
105   - metaInfo = new ModelMetaInfoRespDTO();
  126 + metaInfo = new BpmModelMetaInfoRespDTO();
106 127 }
107 128 // 只有非空,才进行设置,避免更新时的覆盖
108 129 if (StrUtil.isNotEmpty(description)) {
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/FlowableModelServiceImpl.java
... ... @@ -5,12 +5,13 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 5 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
6 6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
7 7 import cn.iocoder.yudao.framework.common.util.object.PageUtils;
8   -import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
9 8 import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.model.*;
10 9 import cn.iocoder.yudao.module.bpm.convert.definition.ModelConvert;
11 10 import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmFormDO;
12   -import cn.iocoder.yudao.module.bpm.service.definition.dto.ModelMetaInfoRespDTO;
  11 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
  12 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
13 13 import lombok.extern.slf4j.Slf4j;
  14 +import org.flowable.common.engine.impl.db.SuspensionState;
14 15 import org.flowable.engine.RepositoryService;
15 16 import org.flowable.engine.repository.Deployment;
16 17 import org.flowable.engine.repository.Model;
... ... @@ -18,14 +19,12 @@ import org.flowable.engine.repository.ModelQuery;
18 19 import org.flowable.engine.repository.ProcessDefinition;
19 20 import org.springframework.stereotype.Service;
20 21 import org.springframework.transaction.annotation.Transactional;
  22 +import org.springframework.util.ObjectUtils;
21 23 import org.springframework.validation.annotation.Validated;
22 24  
23 25 import javax.annotation.Resource;
24 26 import javax.validation.Valid;
25   -import java.util.HashSet;
26   -import java.util.List;
27   -import java.util.Map;
28   -import java.util.Set;
  27 +import java.util.*;
29 28  
30 29 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
31 30 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
... ... @@ -42,15 +41,17 @@ import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;
42 41 @Service
43 42 @Validated
44 43 @Slf4j
45   -public class FlowableModelServiceImpl implements FlowableModelService {
  44 +public class FlowableModelServiceImpl extends BpmAbstractModelService implements FlowableModelService {
46 45  
47 46 @Resource
48 47 private RepositoryService repositoryService;
49 48 @Resource
50   - private BpmFormService bpmFormService;
51   - @Resource
52 49 private FlowableProcessDefinitionService processDefinitionService;
53 50  
  51 + public FlowableModelServiceImpl(BpmFormService bpmFormService){
  52 + super(bpmFormService);
  53 + }
  54 +
54 55 @Override
55 56 public PageResult<BpmModelPageItemRespVO> getModelPage(BpmModelPageReqVO pageVO) {
56 57 ModelQuery modelQuery = repositoryService.createModelQuery();
... ... @@ -69,7 +70,7 @@ public class FlowableModelServiceImpl implements FlowableModelService {
69 70  
70 71 // 获得 Form Map
71 72 Set<Long> formIds = CollectionUtils.convertSet(models, model -> {
72   - ModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), ModelMetaInfoRespDTO.class);
  73 + BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
73 74 return metaInfo != null ? metaInfo.getFormId() : null;
74 75 });
75 76 Map<Long, BpmFormDO> formMap = bpmFormService.getFormMap(formIds);
... ... @@ -136,15 +137,63 @@ public class FlowableModelServiceImpl implements FlowableModelService {
136 137 saveModelBpmnXml(model, updateReqVO.getBpmnXml());
137 138 }
138 139  
  140 + @Override
  141 + public void deployModel(String id) {
  142 + // 校验流程模型存在
  143 + Model model = repositoryService.getModel(id);
  144 + if (ObjectUtils.isEmpty(model)) {
  145 + throw exception(MODEL_NOT_EXISTS);
  146 + }
  147 + // 校验流程图
  148 + byte[] bpmnBytes = repositoryService.getModelEditorSource(model.getId());
  149 + if (bpmnBytes == null) {
  150 + throw exception(MODEL_NOT_EXISTS);
  151 + }
  152 + // TODO 芋艿:校验流程图的有效性;例如说,是否有开始的元素,是否有结束的元素;
  153 + // 校验表单已配
  154 + BpmFormDO form = checkFormConfig(model.getMetaInfo());
  155 + //TODO 校验任务分配规则已配置
  156 + //checkTaskAssignRuleAllConfig(id);
  157 +
  158 + BpmProcessDefinitionCreateReqDTO definitionCreateReqDTO = ModelConvert.INSTANCE.convert2(model, form).setBpmnBytes(bpmnBytes);
  159 + // TODO 校验模型是否发生修改。如果未修改,则不允许创建
  160 +// if (processDefinitionService.isProcessDefinitionEquals(definitionCreateReqDTO)) { // 流程定义的信息相等
  161 +// ProcessDefinition oldProcessInstance = processDefinitionService.getProcessDefinitionByDeploymentId(model.getDeploymentId());
  162 +// if (oldProcessInstance != null && taskAssignRuleService.isTaskAssignRulesEquals(model.getId(), oldProcessInstance.getId())) {
  163 +// throw exception(MODEL_DEPLOY_FAIL_TASK_INFO_EQUALS);
  164 +// }
  165 +// }
  166 + // 创建流程定义
  167 + String definitionId = processDefinitionService.createProcessDefinition(definitionCreateReqDTO);
  168 +
  169 + // 将老的流程定义进行挂起。也就是说,只有最新部署的流程定义,才可以发起任务。
  170 + updateProcessDefinitionSuspended(model.getDeploymentId());
  171 +
  172 + // 更新 model 的 deploymentId,进行关联
  173 + ProcessDefinition definition = processDefinitionService.getProcessDefinition(definitionId);
  174 + model.setDeploymentId(definition.getDeploymentId());
  175 + repositoryService.saveModel(model);
  176 +
  177 + //TODO 复制任务分配规则
  178 + //taskAssignRuleService.copyTaskAssignRules(id, definition.getId());
  179 + }
  180 +
  181 + @Override
  182 + public void deleteModel(String id) {
  183 +
  184 + }
  185 +
  186 + @Override
  187 + public void updateModelState(String id, Integer state) {
  188 +
  189 + }
  190 +
  191 +
  192 +
139 193 private Model getModelByKey(String key) {
140 194 return repositoryService.createModelQuery().modelKey(key).singleResult();
141 195 }
142 196  
143   - private void checkKeyNCName(String key) {
144   - if (!ValidationUtils.isXmlNCName(key)) {
145   - throw exception(MODEL_KEY_VALID);
146   - }
147   - }
148 197  
149 198 private void saveModelBpmnXml(Model model, String bpmnXml) {
150 199 if (StrUtil.isEmpty(bpmnXml)) {
... ... @@ -152,4 +201,15 @@ public class FlowableModelServiceImpl implements FlowableModelService {
152 201 }
153 202 repositoryService.addModelEditorSource(model.getId(), StrUtil.utf8Bytes(bpmnXml));
154 203 }
  204 +
  205 + private void updateProcessDefinitionSuspended(String deploymentId) {
  206 + if (StrUtil.isEmpty(deploymentId)) {
  207 + return;
  208 + }
  209 + ProcessDefinition oldDefinition = processDefinitionService.getProcessDefinitionByDeploymentId(deploymentId);
  210 + if (oldDefinition == null) {
  211 + return;
  212 + }
  213 + processDefinitionService.updateProcessDefinitionState(oldDefinition.getId(), SuspensionState.SUSPENDED.getStateCode());
  214 + }
155 215 }
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/FlowableProcessDefinitionService.java
1 1 package cn.iocoder.yudao.module.bpm.service.definition;
2 2  
3 3 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
  4 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
4 5 import org.flowable.engine.repository.Deployment;
5 6 import org.flowable.engine.repository.ProcessDefinition;
6 7  
  8 +import javax.validation.Valid;
7 9 import java.util.List;
8 10 import java.util.Map;
9 11 import java.util.Set;
... ... @@ -17,6 +19,22 @@ import java.util.Set;
17 19 public interface FlowableProcessDefinitionService {
18 20  
19 21 /**
  22 + * 获得编号对应的 ProcessDefinition
  23 + *
  24 + * @param id 编号
  25 + * @return 流程定义
  26 + */
  27 + ProcessDefinition getProcessDefinition(String id);
  28 +
  29 + /**
  30 + * 获得 deploymentId 对应的 ProcessDefinition
  31 + *
  32 + * @param deploymentId 部署编号
  33 + * @return 流程定义
  34 + */
  35 + ProcessDefinition getProcessDefinitionByDeploymentId(String deploymentId);
  36 +
  37 + /**
20 38 * 获得 deploymentIds 对应的 ProcessDefinition 数组
21 39 *
22 40 * @param deploymentIds 部署编号的数组
... ... @@ -49,4 +67,20 @@ public interface FlowableProcessDefinitionService {
49 67 * @return 流程部署
50 68 */
51 69 Deployment getDeployment(String id);
  70 +
  71 + /**
  72 + * 创建流程定义
  73 + *
  74 + * @param createReqDTO 创建信息
  75 + * @return 流程编号
  76 + */
  77 + String createProcessDefinition(@Valid BpmProcessDefinitionCreateReqDTO createReqDTO);
  78 +
  79 + /**
  80 + * 更新流程定义状态
  81 + *
  82 + * @param id 流程定义的编号
  83 + * @param state 状态
  84 + */
  85 + void updateProcessDefinitionState(String id, Integer state);
52 86 }
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/FlowableProcessDefinitionServiceImpl.java
... ... @@ -2,7 +2,12 @@ package cn.iocoder.yudao.module.bpm.service.definition;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.StrUtil;
  5 +import cn.iocoder.yudao.module.bpm.convert.definition.BpmProcessDefinitionConvert;
  6 +import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
  7 +import cn.iocoder.yudao.module.bpm.dal.mysql.definition.BpmProcessDefinitionExtMapper;
  8 +import cn.iocoder.yudao.module.bpm.service.definition.dto.BpmProcessDefinitionCreateReqDTO;
5 9 import lombok.extern.slf4j.Slf4j;
  10 +import org.flowable.common.engine.impl.db.SuspensionState;
6 11 import org.flowable.engine.RepositoryService;
7 12 import org.flowable.engine.repository.Deployment;
8 13 import org.flowable.engine.repository.ProcessDefinition;
... ... @@ -10,11 +15,16 @@ import org.springframework.stereotype.Service;
10 15 import org.springframework.validation.annotation.Validated;
11 16  
12 17 import javax.annotation.Resource;
  18 +import javax.validation.Valid;
13 19 import java.util.ArrayList;
14 20 import java.util.List;
  21 +import java.util.Objects;
15 22 import java.util.Set;
16 23  
  24 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
17 25 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.addIfNotNull;
  26 +import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.PROCESS_DEFINITION_KEY_NOT_MATCH;
  27 +import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.PROCESS_DEFINITION_NAME_NOT_MATCH;
18 28 import static java.util.Collections.emptyList;
19 29  
20 30 /**
... ... @@ -29,9 +39,28 @@ import static java.util.Collections.emptyList;
29 39 @Validated
30 40 @Slf4j
31 41 public class FlowableProcessDefinitionServiceImpl implements FlowableProcessDefinitionService {
  42 +
  43 + private static final String BPMN_FILE_SUFFIX = ".bpmn";
  44 +
32 45 @Resource
33 46 private RepositoryService repositoryService;
34 47  
  48 + @Resource
  49 + private BpmProcessDefinitionExtMapper processDefinitionMapper;
  50 +
  51 + @Override
  52 + public ProcessDefinition getProcessDefinition(String id) {
  53 + return repositoryService.getProcessDefinition(id);
  54 + }
  55 +
  56 + @Override
  57 + public ProcessDefinition getProcessDefinitionByDeploymentId(String deploymentId) {
  58 + if (StrUtil.isEmpty(deploymentId)) {
  59 + return null;
  60 + }
  61 + return repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
  62 + }
  63 +
35 64 @Override
36 65 public List<ProcessDefinition> getProcessDefinitionListByDeploymentIds(Set<String> deploymentIds) {
37 66 if (CollUtil.isEmpty(deploymentIds)) {
... ... @@ -59,4 +88,49 @@ public class FlowableProcessDefinitionServiceImpl implements FlowableProcessDefi
59 88 }
60 89 return repositoryService.createDeploymentQuery().deploymentId(id).singleResult();
61 90 }
  91 +
  92 + @Override
  93 + public String createProcessDefinition(@Valid BpmProcessDefinitionCreateReqDTO createReqDTO) {
  94 + // 创建 Deployment 部署
  95 + Deployment deploy = repositoryService.createDeployment()
  96 + .key(createReqDTO.getKey()).name(createReqDTO.getName()).category(createReqDTO.getCategory())
  97 + .addBytes(createReqDTO.getKey() + BPMN_FILE_SUFFIX, createReqDTO.getBpmnBytes())
  98 + .deploy();
  99 +
  100 + // 设置 ProcessDefinition 的 category 分类
  101 + ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
  102 + repositoryService.setProcessDefinitionCategory(definition.getId(), createReqDTO.getCategory());
  103 + // 注意 1,ProcessDefinition 的 key 和 name 是通过 BPMN 中的 <bpmn2:process /> 的 id 和 name 决定
  104 + // 注意 2,目前该项目的设计上,需要保证 Model、Deployment、ProcessDefinition 使用相同的 key,保证关联性。
  105 + // 否则,会导致 ProcessDefinition 的分页无法查询到。
  106 + if (!Objects.equals(definition.getKey(), createReqDTO.getKey())) {
  107 + throw exception(PROCESS_DEFINITION_KEY_NOT_MATCH, createReqDTO.getKey(), definition.getKey());
  108 + }
  109 + if (!Objects.equals(definition.getName(), createReqDTO.getName())) {
  110 + throw exception(PROCESS_DEFINITION_NAME_NOT_MATCH, createReqDTO.getName(), definition.getName());
  111 + }
  112 +
  113 + // 插入拓展表
  114 + BpmProcessDefinitionExtDO definitionDO = BpmProcessDefinitionConvert.INSTANCE.convert2(createReqDTO)
  115 + .setProcessDefinitionId(definition.getId());
  116 + processDefinitionMapper.insert(definitionDO);
  117 + return definition.getId();
  118 + }
  119 +
  120 + @Override
  121 + public void updateProcessDefinitionState(String id, Integer state) {
  122 + // 激活
  123 + if (Objects.equals(SuspensionState.ACTIVE.getStateCode(), state)) {
  124 + repositoryService.activateProcessDefinitionById(id, false, null);
  125 + return;
  126 + }
  127 + // 挂起
  128 + if (Objects.equals(SuspensionState.SUSPENDED.getStateCode(), state)) {
  129 + // suspendProcessInstances = false,进行中的任务,不进行挂起。
  130 + // 原因:只要新的流程不允许发起即可,老流程继续可以执行。
  131 + repositoryService.suspendProcessDefinitionById(id, false, null);
  132 + return;
  133 + }
  134 + log.error("[updateProcessDefinitionState][流程定义({}) 修改未知状态({})]", id, state);
  135 + }
62 136 }
... ...
yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/dto/ModelMetaInfoRespDTO.java deleted
1   -package cn.iocoder.yudao.module.bpm.service.definition.dto;
2   -
3   -import cn.iocoder.yudao.module.bpm.enums.definition.BpmModelFormTypeEnum;
4   -import lombok.Data;
5   -
6   -/**
7   - * BPM 流程 MetaInfo Response DTO
8   - * 主要用于 {@link org.flowable.engine.repository.Model#setMetaInfo(String)} 的存储
9   - *
10   - * @author 芋道源码
11   - */
12   -@Data
13   -public class ModelMetaInfoRespDTO {
14   -
15   - /**
16   - * 流程描述
17   - */
18   - private String description;
19   - /**
20   - * 表单类型
21   - */
22   - private Integer formType;
23   - /**
24   - * 表单编号
25   - * 在表单类型为 {@link BpmModelFormTypeEnum#NORMAL} 时
26   - */
27   - private Long formId;
28   - /**
29   - * 自定义表单的提交路径,使用 Vue 的路由地址
30   - * 在表单类型为 {@link BpmModelFormTypeEnum#CUSTOM} 时
31   - */
32   - private String formCustomCreatePath;
33   - /**
34   - * 自定义表单的查看路径,使用 Vue 的路由地址
35   - * 在表单类型为 {@link BpmModelFormTypeEnum#CUSTOM} 时
36   - */
37   - private String formCustomViewPath;
38   -
39   -}