| | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.codec.Base64; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.SneakyThrows; |
| | | import org.apache.commons.io.IOUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.workflow.common.constant.FlowConstant; |
| | | import org.dromara.workflow.domain.WfCategory; |
| | | import org.dromara.workflow.domain.WfDefinitionConfig; |
| | | import org.dromara.workflow.domain.WfNodeConfig; |
| | | import org.dromara.workflow.domain.bo.ProcessDefinitionBo; |
| | | import org.dromara.workflow.domain.bo.WfDefinitionConfigBo; |
| | | import org.dromara.workflow.domain.vo.ProcessDefinitionVo; |
| | | import org.dromara.workflow.domain.vo.WfDefinitionConfigVo; |
| | | import org.dromara.workflow.mapper.WfDefinitionConfigMapper; |
| | | import org.dromara.workflow.service.IActProcessDefinitionService; |
| | | import org.dromara.workflow.service.IWfCategoryService; |
| | | import org.dromara.workflow.service.IWfDefinitionConfigService; |
| | |
| | | import org.flowable.bpmn.model.UserTask; |
| | | import org.flowable.engine.ProcessMigrationService; |
| | | import org.flowable.engine.RepositoryService; |
| | | import org.flowable.engine.history.HistoricProcessInstance; |
| | | import org.flowable.engine.impl.bpmn.deployer.ResourceNameUtil; |
| | | import org.flowable.engine.repository.*; |
| | | import org.flowable.task.api.history.HistoricTaskInstance; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipInputStream; |
| | | |
| | |
| | | * |
| | | * @author may |
| | | */ |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | @Service |
| | | public class ActProcessDefinitionServiceImpl implements IActProcessDefinitionService { |
| | | |
| | | private final RepositoryService repositoryService; |
| | | private final ProcessMigrationService processMigrationService; |
| | | @Autowired(required = false) |
| | | private RepositoryService repositoryService; |
| | | @Autowired(required = false) |
| | | private ProcessMigrationService processMigrationService; |
| | | private final IWfCategoryService wfCategoryService; |
| | | private final IWfDefinitionConfigService iWfDefinitionConfigService; |
| | | private final IWfNodeConfigService iWfNodeConfigService; |
| | | private final IWfDefinitionConfigService wfDefinitionConfigService; |
| | | private final WfDefinitionConfigMapper wfDefinitionConfigMapper; |
| | | private final IWfNodeConfigService wfNodeConfigService; |
| | | |
| | | /** |
| | | * 分页查询 |
| | |
| | | } |
| | | if (CollUtil.isNotEmpty(definitionList)) { |
| | | List<String> ids = StreamUtils.toList(definitionList, ProcessDefinition::getId); |
| | | List<WfDefinitionConfigVo> wfDefinitionConfigVos = iWfDefinitionConfigService.queryList(ids); |
| | | List<WfDefinitionConfigVo> wfDefinitionConfigVos = wfDefinitionConfigService.queryList(ids); |
| | | for (ProcessDefinition processDefinition : definitionList) { |
| | | ProcessDefinitionVo processDefinitionVo = BeanUtil.toBean(processDefinition, ProcessDefinitionVo.class); |
| | | if (CollUtil.isNotEmpty(deploymentList)) { |
| | |
| | | } |
| | | if (CollUtil.isNotEmpty(definitionList)) { |
| | | List<String> ids = StreamUtils.toList(definitionList, ProcessDefinition::getId); |
| | | List<WfDefinitionConfigVo> wfDefinitionConfigVos = iWfDefinitionConfigService.queryList(ids); |
| | | List<WfDefinitionConfigVo> wfDefinitionConfigVos = wfDefinitionConfigService.queryList(ids); |
| | | for (ProcessDefinition processDefinition : definitionList) { |
| | | ProcessDefinitionVo processDefinitionVo = BeanUtil.toBean(processDefinition, ProcessDefinitionVo.class); |
| | | if (CollUtil.isNotEmpty(deploymentList)) { |
| | |
| | | processDefinitionVoList.add(processDefinitionVo); |
| | | } |
| | | } |
| | | return CollectionUtil.reverse(processDefinitionVoList); |
| | | return CollUtil.reverse(processDefinitionVoList); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public String definitionImage(String processDefinitionId) { |
| | | InputStream inputStream = repositoryService.getProcessDiagram(processDefinitionId); |
| | | return Base64.encode(IOUtils.toByteArray(inputStream)); |
| | | return Base64.encode(IoUtil.readBytes(inputStream)); |
| | | } |
| | | |
| | | /** |
| | |
| | | public String definitionXml(String processDefinitionId) { |
| | | StringBuilder xml = new StringBuilder(); |
| | | ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processDefinitionId); |
| | | InputStream inputStream; |
| | | try { |
| | | inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName()); |
| | | xml.append(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName()); |
| | | xml.append(IoUtil.read(inputStream, StandardCharsets.UTF_8)); |
| | | return xml.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 删除流程定义 |
| | | * |
| | | * @param deploymentId 部署id |
| | | * @param processDefinitionId 流程定义id |
| | | * @param deploymentIds 部署id |
| | | * @param processDefinitionIds 流程定义id |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean deleteDeployment(String deploymentId, String processDefinitionId) { |
| | | public boolean deleteDeployment(List<String> deploymentIds, List<String> processDefinitionIds) { |
| | | try { |
| | | List<HistoricTaskInstance> taskInstanceList = QueryUtils.hisTaskInstanceQuery() |
| | | .processDefinitionId(processDefinitionId).list(); |
| | | if (CollectionUtil.isNotEmpty(taskInstanceList)) { |
| | | throw new ServiceException("当前流程定义已被使用不可删除!"); |
| | | List<HistoricProcessInstance> historicProcessInstances = QueryUtils.hisInstanceQuery().deploymentIdIn(deploymentIds).list(); |
| | | if (CollUtil.isNotEmpty(historicProcessInstances)) { |
| | | Set<String> defIds = StreamUtils.toSet(historicProcessInstances, HistoricProcessInstance::getProcessDefinitionId); |
| | | List<ProcessDefinition> processDefinitions = QueryUtils.definitionQuery().processDefinitionIds(defIds).list(); |
| | | if (CollUtil.isNotEmpty(processDefinitions)) { |
| | | Set<String> keys = StreamUtils.toSet(processDefinitions, ProcessDefinition::getKey); |
| | | throw new ServiceException("当前【" + String.join(",", keys) + "】流程定义已被使用不可删除!"); |
| | | } |
| | | } |
| | | //删除流程定义 |
| | | repositoryService.deleteDeployment(deploymentId); |
| | | for (String deploymentId : deploymentIds) { |
| | | repositoryService.deleteDeployment(deploymentId); |
| | | } |
| | | //删除流程定义配置 |
| | | iWfDefinitionConfigService.deleteByDefIds(Collections.singletonList(processDefinitionId)); |
| | | wfDefinitionConfigService.deleteByDefIds(processDefinitionIds); |
| | | //删除节点配置 |
| | | iWfNodeConfigService.deleteByDefIds(Collections.singletonList(processDefinitionId)); |
| | | wfNodeConfigService.deleteByDefIds(processDefinitionIds); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error(e.getMessage(), e); |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | } |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error(e.getMessage(), e); |
| | | throw new ServiceException("操作失败:" + e.getMessage()); |
| | | } |
| | | } |
| | |
| | | .migrateProcessInstances(fromProcessDefinitionId); |
| | | return true; |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | Model modelData = repositoryService.newModel(); |
| | | modelData.setKey(pd.getKey()); |
| | | modelData.setName(pd.getName()); |
| | | modelData.setCategory(pd.getCategory()); |
| | | modelData.setTenantId(pd.getTenantId()); |
| | | repositoryService.saveModel(modelData); |
| | | repositoryService.addModelEditorSource(modelData.getId(), IoUtil.readBytes(inputStream)); |
| | | } |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error(e.getMessage(), e); |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | .name(processName).key(processKey).category(categoryCode).deploy(); |
| | | ProcessDefinition definition = QueryUtils.definitionQuery().deploymentId(deployment.getId()).singleResult(); |
| | | repositoryService.setProcessDefinitionCategory(definition.getId(), categoryCode); |
| | | setForm(oldProcessDefinition, definition); |
| | | setWfConfig(oldProcessDefinition, definition); |
| | | zipInputStream.closeEntry(); |
| | | } |
| | | } catch (IOException e) { |
| | |
| | | zipInputStream.close(); |
| | | } |
| | | } |
| | | //初始化配置数据(demo使用,不用可删除) |
| | | initWfDefConfig(); |
| | | } else { |
| | | String originalFilename = file.getOriginalFilename(); |
| | | String bpmnResourceSuffix = ResourceNameUtil.BPMN_RESOURCE_SUFFIXES[0]; |
| | | if (originalFilename.contains(bpmnResourceSuffix)) { |
| | | if (StringUtils.containsAny(originalFilename, ResourceNameUtil.BPMN_RESOURCE_SUFFIXES)) { |
| | | // 文件名 = 流程名称-流程key |
| | | String[] splitFilename = originalFilename.substring(0, originalFilename.lastIndexOf(".")).split("-"); |
| | | if (splitFilename.length < 2) { |
| | |
| | | //流程key |
| | | String processKey = splitFilename[1]; |
| | | ProcessDefinition oldProcessDefinition = QueryUtils.definitionQuery().processDefinitionKey(processKey).latestVersion().singleResult(); |
| | | |
| | | DeploymentBuilder builder = repositoryService.createDeployment(); |
| | | Deployment deployment = builder.addInputStream(originalFilename, inputStream) |
| | | .tenantId(TenantHelper.getTenantId()) |
| | |
| | | // 更新分类 |
| | | ProcessDefinition definition = QueryUtils.definitionQuery().deploymentId(deployment.getId()).singleResult(); |
| | | repositoryService.setProcessDefinitionCategory(definition.getId(), categoryCode); |
| | | setForm(oldProcessDefinition, definition); |
| | | |
| | | setWfConfig(oldProcessDefinition, definition); |
| | | } else { |
| | | throw new ServiceException("文件类型上传错误!"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 初始化配置数据(demo使用,不用可删除) |
| | | */ |
| | | private void initWfDefConfig() { |
| | | List<WfDefinitionConfig> wfDefinitionConfigs = wfDefinitionConfigMapper.selectList(); |
| | | if (CollUtil.isEmpty(wfDefinitionConfigs)) { |
| | | ProcessDefinition processDefinition = QueryUtils.definitionQuery().processDefinitionKey("leave1").latestVersion().singleResult(); |
| | | if (processDefinition != null) { |
| | | WfDefinitionConfigBo wfDefinitionConfigBo = new WfDefinitionConfigBo(); |
| | | wfDefinitionConfigBo.setDefinitionId(processDefinition.getId()); |
| | | wfDefinitionConfigBo.setProcessKey(processDefinition.getKey()); |
| | | wfDefinitionConfigBo.setTableName("test_leave"); |
| | | wfDefinitionConfigBo.setVersion(processDefinition.getVersion()); |
| | | wfDefinitionConfigService.saveOrUpdate(wfDefinitionConfigBo); |
| | | } |
| | | } |
| | | |
| | |
| | | * @param oldProcessDefinition 部署前最新流程定义 |
| | | * @param definition 部署后最新流程定义 |
| | | */ |
| | | private void setForm(ProcessDefinition oldProcessDefinition, ProcessDefinition definition) { |
| | | private void setWfConfig(ProcessDefinition oldProcessDefinition, ProcessDefinition definition) { |
| | | //更新流程定义表单 |
| | | if (oldProcessDefinition != null) { |
| | | WfDefinitionConfigVo definitionVo = iWfDefinitionConfigService.getByDefId(oldProcessDefinition.getId()); |
| | | WfDefinitionConfigVo definitionVo = wfDefinitionConfigService.getByDefId(oldProcessDefinition.getId()); |
| | | if (definitionVo != null) { |
| | | WfDefinitionConfigBo wfFormDefinition = new WfDefinitionConfigBo(); |
| | | wfFormDefinition.setDefinitionId(definition.getId()); |
| | | wfFormDefinition.setProcessKey(definition.getKey()); |
| | | wfFormDefinition.setFormId(ObjectUtil.isNotNull(definitionVo.getFormId()) ? definitionVo.getFormId() : null); |
| | | wfFormDefinition.setRemark(definitionVo.getRemark()); |
| | | iWfDefinitionConfigService.saveOrUpdate(wfFormDefinition); |
| | | wfDefinitionConfigService.deleteByDefIds(Collections.singletonList(oldProcessDefinition.getId())); |
| | | WfDefinitionConfigBo wfDefinitionConfigBo = new WfDefinitionConfigBo(); |
| | | wfDefinitionConfigBo.setDefinitionId(definition.getId()); |
| | | wfDefinitionConfigBo.setProcessKey(definition.getKey()); |
| | | wfDefinitionConfigBo.setTableName(definitionVo.getTableName()); |
| | | wfDefinitionConfigBo.setVersion(definition.getVersion()); |
| | | wfDefinitionConfigBo.setRemark(definitionVo.getRemark()); |
| | | wfDefinitionConfigService.saveOrUpdate(wfDefinitionConfigBo); |
| | | } |
| | | } |
| | | //更新流程节点配置表单 |
| | | List<UserTask> userTasks = ModelUtils.getuserTaskFlowElements(definition.getId()); |
| | | List<UserTask> userTasks = ModelUtils.getUserTaskFlowElements(definition.getId()); |
| | | UserTask applyUserTask = ModelUtils.getApplyUserTask(definition.getId()); |
| | | List<WfNodeConfig> wfNodeConfigList = new ArrayList<>(); |
| | | for (UserTask userTask : userTasks) { |
| | | if (StringUtils.isNotBlank(userTask.getFormKey()) && userTask.getFormKey().contains(StrUtil.COLON)) { |
| | |
| | | String[] split = userTask.getFormKey().split(StrUtil.COLON); |
| | | wfNodeConfig.setFormType(split[0]); |
| | | wfNodeConfig.setFormId(Long.valueOf(split[1])); |
| | | wfNodeConfig.setApplyUserTask(applyUserTask.getId().equals(userTask.getId()) ? FlowConstant.TRUE : FlowConstant.FALSE); |
| | | wfNodeConfigList.add(wfNodeConfig); |
| | | } |
| | | } |
| | | if (CollUtil.isNotEmpty(wfNodeConfigList)) { |
| | | iWfNodeConfigService.saveOrUpdate(wfNodeConfigList); |
| | | wfNodeConfigService.saveOrUpdate(wfNodeConfigList); |
| | | } |
| | | } |
| | | } |