houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.service.definition;
H 2
3 import cn.hutool.core.util.StrUtil;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.common.util.json.JsonUtils;
6 import com.iailab.framework.common.util.object.PageUtils;
7 import com.iailab.framework.common.util.validation.ValidationUtils;
8 import com.iailab.module.bpm.controller.admin.definition.vo.model.BpmModelCreateReqVO;
9 import com.iailab.module.bpm.controller.admin.definition.vo.model.BpmModelPageReqVO;
10 import com.iailab.module.bpm.controller.admin.definition.vo.model.BpmModelUpdateReqVO;
11 import com.iailab.module.bpm.convert.definition.BpmModelConvert;
12 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
13 import com.iailab.module.bpm.enums.definition.BpmModelFormTypeEnum;
14 import com.iailab.module.bpm.framework.flowable.core.candidate.BpmTaskCandidateInvoker;
15 import com.iailab.module.bpm.framework.flowable.core.util.BpmnModelUtils;
16 import com.iailab.module.bpm.framework.flowable.core.util.FlowableUtils;
17 import com.iailab.module.bpm.service.definition.dto.BpmModelMetaInfoRespDTO;
18 import lombok.extern.slf4j.Slf4j;
19 import org.flowable.bpmn.model.BpmnModel;
20 import org.flowable.bpmn.model.StartEvent;
21 import org.flowable.bpmn.model.UserTask;
22 import org.flowable.common.engine.impl.db.SuspensionState;
23 import org.flowable.engine.RepositoryService;
24 import org.flowable.engine.repository.Model;
25 import org.flowable.engine.repository.ModelQuery;
26 import org.flowable.engine.repository.ProcessDefinition;
27 import org.springframework.stereotype.Service;
28 import org.springframework.transaction.annotation.Transactional;
29 import org.springframework.util.ObjectUtils;
30 import org.springframework.validation.annotation.Validated;
31
32 import javax.annotation.Resource;
33 import javax.validation.Valid;
34 import java.util.List;
35 import java.util.Objects;
36
37 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
38 import static com.iailab.module.bpm.enums.ErrorCodeConstants.*;
39
40 /**
41  * Flowable流程模型实现
42  * 主要进行 Flowable {@link Model} 的维护
43  *
44  * @author yunlongn
45  * @author iailab
46  * @author jason
47  */
48 @Service
49 @Validated
50 @Slf4j
51 public class BpmModelServiceImpl implements BpmModelService {
52
53     @Resource
54     private RepositoryService repositoryService;
55     @Resource
56     private BpmProcessDefinitionService processDefinitionService;
57     @Resource
58     private BpmFormService bpmFormService;
59
60     @Resource
61     private BpmTaskCandidateInvoker taskCandidateInvoker;
62
63     @Override
64     public PageResult<Model> getModelPage(BpmModelPageReqVO pageVO) {
65         ModelQuery modelQuery = repositoryService.createModelQuery();
66         if (StrUtil.isNotBlank(pageVO.getKey())) {
67             modelQuery.modelKey(pageVO.getKey());
68         }
69         if (StrUtil.isNotBlank(pageVO.getName())) {
70             modelQuery.modelNameLike("%" + pageVO.getName() + "%"); // 模糊匹配
71         }
72         if (StrUtil.isNotBlank(pageVO.getCategory())) {
73             modelQuery.modelCategory(pageVO.getCategory());
74         }
75         // 执行查询
76         long count = modelQuery.count();
77         if (count == 0) {
78             return PageResult.empty(count);
79         }
80         List<Model> models = modelQuery
81                 .modelTenantId(FlowableUtils.getTenantId())
82                 .orderByCreateTime().desc()
83                 .listPage(PageUtils.getStart(pageVO), pageVO.getPageSize());
84         return new PageResult<>(models, count);
85     }
86
87     @Override
88     @Transactional(rollbackFor = Exception.class)
89     public String createModel(@Valid BpmModelCreateReqVO createReqVO, String bpmnXml) {
90         if (!ValidationUtils.isXmlNCName(createReqVO.getKey())) {
91             throw exception(MODEL_KEY_VALID);
92         }
93         // 校验流程标识已经存在
94         Model keyModel = getModelByKey(createReqVO.getKey());
95         if (keyModel != null) {
96             throw exception(MODEL_KEY_EXISTS, createReqVO.getKey());
97         }
98
99         // 创建流程定义
100         Model model = repositoryService.newModel();
101         BpmModelConvert.INSTANCE.copyToCreateModel(model, createReqVO);
102         model.setTenantId(FlowableUtils.getTenantId());
103         // 保存流程定义
104         repositoryService.saveModel(model);
105         // 保存 BPMN XML
106         saveModelBpmnXml(model, bpmnXml);
107         return model.getId();
108     }
109
110     @Override
111     @Transactional(rollbackFor = Exception.class) // 因为进行多个操作,所以开启事务
112     public void updateModel(@Valid BpmModelUpdateReqVO updateReqVO) {
113         // 校验流程模型存在
114         Model model = getModel(updateReqVO.getId());
115         if (model == null) {
116             throw exception(MODEL_NOT_EXISTS);
117         }
118
119         // 修改流程定义
120         BpmModelConvert.INSTANCE.copyToUpdateModel(model, updateReqVO);
121         // 更新模型
122         repositoryService.saveModel(model);
123         // 更新 BPMN XML
124         saveModelBpmnXml(model, updateReqVO.getBpmnXml());
125     }
126
127     @Override
128     @Transactional(rollbackFor = Exception.class) // 因为进行多个操作,所以开启事务
129     public void deployModel(String id) {
130         // 1.1 校验流程模型存在
131         Model model = getModel(id);
132         if (ObjectUtils.isEmpty(model)) {
133             throw exception(MODEL_NOT_EXISTS);
134         }
135         // 1.2 校验流程图
136         byte[] bpmnBytes = getModelBpmnXML(model.getId());
137         validateBpmnXml(bpmnBytes);
138         // 1.3 校验表单已配
139         BpmModelMetaInfoRespDTO metaInfo = JsonUtils.parseObject(model.getMetaInfo(), BpmModelMetaInfoRespDTO.class);
140         BpmFormDO form = validateFormConfig(metaInfo);
141         // 1.4 校验任务分配规则已配置
142         taskCandidateInvoker.validateBpmnConfig(bpmnBytes);
143
144         // 2.1 创建流程定义
145         String definitionId = processDefinitionService.createProcessDefinition(model, metaInfo, bpmnBytes, form);
146
147         // 2.2 将老的流程定义进行挂起。也就是说,只有最新部署的流程定义,才可以发起任务。
148         updateProcessDefinitionSuspended(model.getDeploymentId());
149
150         // 2.3 更新 model 的 deploymentId,进行关联
151         ProcessDefinition definition = processDefinitionService.getProcessDefinition(definitionId);
152         model.setDeploymentId(definition.getDeploymentId());
153         repositoryService.saveModel(model);
154     }
155
156     private void validateBpmnXml(byte[] bpmnBytes) {
157         BpmnModel bpmnModel = BpmnModelUtils.getBpmnModel(bpmnBytes);
158         if (bpmnModel == null) {
159             throw exception(MODEL_NOT_EXISTS);
160         }
161         // 1. 没有 StartEvent
162         StartEvent startEvent = BpmnModelUtils.getStartEvent(bpmnModel);
163         if (startEvent == null) {
164             throw exception(MODEL_DEPLOY_FAIL_BPMN_START_EVENT_NOT_EXISTS);
165         }
166         // 2. 校验 UserTask 的 name 都配置了
167         List<UserTask> userTasks = BpmnModelUtils.getBpmnModelElements(bpmnModel, UserTask.class);
168         userTasks.forEach(userTask -> {
169             if (StrUtil.isEmpty(userTask.getName())) {
170                 throw exception(MODEL_DEPLOY_FAIL_BPMN_USER_TASK_NAME_NOT_EXISTS, userTask.getId());
171             }
172         });
173     }
174
175     @Override
176     @Transactional(rollbackFor = Exception.class)
177     public void deleteModel(String id) {
178         // 校验流程模型存在
179         Model model = getModel(id);
180         if (model == null) {
181             throw exception(MODEL_NOT_EXISTS);
182         }
183         // 执行删除
184         repositoryService.deleteModel(id);
185         // 禁用流程定义
186         updateProcessDefinitionSuspended(model.getDeploymentId());
187     }
188
189     @Override
190     public void updateModelState(String id, Integer state) {
191         // 1.1 校验流程模型存在
192         Model model = getModel(id);
193         if (model == null) {
194             throw exception(MODEL_NOT_EXISTS);
195         }
196         // 1.2 校验流程定义存在
197         ProcessDefinition definition = processDefinitionService.getProcessDefinitionByDeploymentId(model.getDeploymentId());
198         if (definition == null) {
199             throw exception(PROCESS_DEFINITION_NOT_EXISTS);
200         }
201
202         // 2. 更新状态
203         processDefinitionService.updateProcessDefinitionState(definition.getId(), state);
204     }
205
206     @Override
207     public BpmnModel getBpmnModelByDefinitionId(String processDefinitionId) {
208         return repositoryService.getBpmnModel(processDefinitionId);
209     }
210
211     /**
212      * 校验流程表单已配置
213      *
214      * @param metaInfo 流程模型元数据
215      * @return 表单配置
216      */
217     private BpmFormDO validateFormConfig(BpmModelMetaInfoRespDTO  metaInfo) {
218         if (metaInfo == null || metaInfo.getFormType() == null) {
219             throw exception(MODEL_DEPLOY_FAIL_FORM_NOT_CONFIG);
220         }
221         // 校验表单存在
222         if (Objects.equals(metaInfo.getFormType(), BpmModelFormTypeEnum.NORMAL.getType())) {
223             if (metaInfo.getFormId() == null) {
224                 throw exception(MODEL_DEPLOY_FAIL_FORM_NOT_CONFIG);
225             }
226             BpmFormDO form = bpmFormService.getForm(metaInfo.getFormId());
227             if (form == null) {
228                 throw exception(FORM_NOT_EXISTS);
229             }
230             return form;
231         } else {
232             if (StrUtil.isEmpty(metaInfo.getFormCustomCreatePath()) || StrUtil.isEmpty(metaInfo.getFormCustomViewPath())) {
233                 throw exception(MODEL_DEPLOY_FAIL_FORM_NOT_CONFIG);
234             }
235             return null;
236         }
237     }
238
239     private void saveModelBpmnXml(Model model, String bpmnXml) {
240         if (StrUtil.isEmpty(bpmnXml)) {
241             return;
242         }
243         repositoryService.addModelEditorSource(model.getId(), StrUtil.utf8Bytes(bpmnXml));
244     }
245
246     /**
247      * 挂起 deploymentId 对应的流程定义
248      *
249      * 注意:这里一个 deploymentId 只关联一个流程定义
250      *
251      * @param deploymentId 流程发布Id
252      */
253     private void updateProcessDefinitionSuspended(String deploymentId) {
254         if (StrUtil.isEmpty(deploymentId)) {
255             return;
256         }
257         ProcessDefinition oldDefinition = processDefinitionService.getProcessDefinitionByDeploymentId(deploymentId);
258         if (oldDefinition == null) {
259             return;
260         }
261         processDefinitionService.updateProcessDefinitionState(oldDefinition.getId(), SuspensionState.SUSPENDED.getStateCode());
262     }
263
264     private Model getModelByKey(String key) {
265         return repositoryService.createModelQuery().modelKey(key).singleResult();
266     }
267
268     @Override
269     public Model getModel(String id) {
270         return repositoryService.getModel(id);
271     }
272
273     @Override
274     public byte[] getModelBpmnXML(String id) {
275         return repositoryService.getModelEditorSource(id);
276     }
277
278 }