| | |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.mybatis.core.page.PageQuery; |
| | | import org.dromara.common.mybatis.core.page.TableDataInfo; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.workflow.common.constant.FlowConstant; |
| | | import org.dromara.workflow.domain.bo.ModelBo; |
| | | import org.dromara.workflow.domain.bo.WfFormDefinitionBo; |
| | | import org.dromara.workflow.domain.vo.ModelVo; |
| | | import org.dromara.workflow.domain.vo.WfFormDefinitionVo; |
| | | import org.dromara.workflow.service.IActModelService; |
| | | import org.dromara.workflow.service.IWfFormDefinitionService; |
| | | import org.dromara.workflow.utils.ModelUtils; |
| | | import org.dromara.workflow.utils.QueryUtils; |
| | | import org.flowable.bpmn.model.BpmnModel; |
| | | import org.flowable.engine.RepositoryService; |
| | | import org.flowable.engine.repository.*; |
| | |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.*; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | |
| | | public class ActModelServiceImpl implements IActModelService { |
| | | |
| | | private final RepositoryService repositoryService; |
| | | private final IWfFormDefinitionService iWfFormDefinitionService; |
| | | |
| | | /** |
| | | * 分页查询模型 |
| | |
| | | * @return 返回分页列表 |
| | | */ |
| | | @Override |
| | | public TableDataInfo<Model> page(ModelBo modelBo) { |
| | | ModelQuery query = repositoryService.createModelQuery(); |
| | | if (TenantHelper.isEnable()) { |
| | | query.modelTenantId(TenantHelper.getTenantId()); |
| | | } |
| | | public TableDataInfo<Model> page(ModelBo modelBo, PageQuery pageQuery) { |
| | | ModelQuery query = QueryUtils.modelQuery(); |
| | | if (StringUtils.isNotEmpty(modelBo.getName())) { |
| | | query.modelNameLike("%" + modelBo.getName() + "%"); |
| | | } |
| | |
| | | // 创建时间降序排列 |
| | | query.orderByCreateTime().desc(); |
| | | // 分页查询 |
| | | List<Model> modelList = query.listPage(modelBo.getPageNum(), modelBo.getPageSize()); |
| | | List<Model> modelList = query.listPage(pageQuery.getFirstNum(), pageQuery.getPageSize()); |
| | | // 总记录数 |
| | | long total = query.count(); |
| | | return new TableDataInfo<>(modelList, total); |
| | | TableDataInfo<Model> build = TableDataInfo.build(); |
| | | build.setRows(modelList); |
| | | build.setTotal(total); |
| | | return build; |
| | | } |
| | | |
| | | /** |
| | |
| | | String description = modelBo.getDescription(); |
| | | String categoryCode = modelBo.getCategoryCode(); |
| | | String xml = modelBo.getXml(); |
| | | ModelQuery query = repositoryService.createModelQuery(); |
| | | query.modelKey(key); |
| | | if (TenantHelper.isEnable()) { |
| | | query.modelTenantId(TenantHelper.getTenantId()); |
| | | } |
| | | Model checkModel = query.singleResult(); |
| | | Model checkModel = QueryUtils.modelQuery().modelKey(key).singleResult(); |
| | | if (ObjectUtil.isNotNull(checkModel)) { |
| | | throw new ServiceException("模型key已存在!"); |
| | | } |
| | |
| | | model.setVersion(version); |
| | | model.setCategory(categoryCode); |
| | | model.setMetaInfo(description); |
| | | if (TenantHelper.isEnable()) { |
| | | model.setTenantId(TenantHelper.getTenantId()); |
| | | } |
| | | model.setTenantId(TenantHelper.getTenantId()); |
| | | //保存初始化的模型基本信息数据 |
| | | repositoryService.saveModel(model); |
| | | repositoryService.addModelEditorSource(model.getId(), StrUtil.utf8Bytes(xml)); |
| | |
| | | public boolean update(ModelBo modelBo) { |
| | | try { |
| | | Model model = repositoryService.getModel(modelBo.getId()); |
| | | ModelQuery query = repositoryService.createModelQuery(); |
| | | if (TenantHelper.isEnable()) { |
| | | query.modelTenantId(TenantHelper.getTenantId()); |
| | | } |
| | | List<Model> list = query.modelKey(modelBo.getKey()).list(); |
| | | List<Model> list = QueryUtils.modelQuery().modelKey(modelBo.getKey()).list(); |
| | | list.stream().filter(e -> !e.getId().equals(model.getId())).findFirst().ifPresent(e -> { |
| | | throw new ServiceException("模型KEY已存在!"); |
| | | }); |
| | |
| | | BpmnModel bpmnModel = ModelUtils.xmlToBpmnModel(xml); |
| | | ModelUtils.checkBpmnModel(bpmnModel); |
| | | Model model = repositoryService.getModel(modelId); |
| | | ModelQuery query = repositoryService.createModelQuery(); |
| | | if (TenantHelper.isEnable()) { |
| | | query.modelTenantId(TenantHelper.getTenantId()); |
| | | } |
| | | List<Model> list = query.modelKey(key).list(); |
| | | List<Model> list = QueryUtils.modelQuery().modelKey(key).list(); |
| | | list.stream().filter(e -> !e.getId().equals(model.getId())).findFirst().ifPresent(e -> { |
| | | throw new ServiceException("模型KEY已存在!"); |
| | | }); |
| | |
| | | } |
| | | // 查询模型的基本信息 |
| | | Model model = repositoryService.getModel(id); |
| | | ProcessDefinition processDefinition = QueryUtils.definitionQuery().processDefinitionKey(model.getKey()).latestVersion().singleResult(); |
| | | // xml资源的名称 ,对应act_ge_bytearray表中的name_字段 |
| | | String processName = model.getName() + ".bpmn20.xml"; |
| | | // 调用部署相关的api方法进行部署流程定义 |
| | | DeploymentBuilder builder = repositoryService.createDeployment(); |
| | | if (TenantHelper.isEnable()) { |
| | | builder.tenantId(TenantHelper.getTenantId()); |
| | | } |
| | | Deployment deployment = builder |
| | | Deployment deployment = repositoryService.createDeployment() |
| | | // 部署名称 |
| | | .name(model.getName()) |
| | | // 部署标识key |
| | |
| | | .category(model.getCategory()) |
| | | // bpmn20.xml资源 |
| | | .addBytes(processName, xmlBytes) |
| | | // 租户id |
| | | .tenantId(TenantHelper.getTenantId()) |
| | | .deploy(); |
| | | |
| | | // 更新 部署id 到流程定义模型数据表中 |
| | | model.setDeploymentId(deployment.getId()); |
| | | repositoryService.saveModel(model); |
| | | // 更新分类 |
| | | ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult(); |
| | | ProcessDefinition definition = QueryUtils.definitionQuery().deploymentId(deployment.getId()).singleResult(); |
| | | repositoryService.setProcessDefinitionCategory(definition.getId(), model.getCategory()); |
| | | if (processDefinition != null) { |
| | | WfFormDefinitionVo definitionVo = iWfFormDefinitionService.getByDefId(processDefinition.getId()); |
| | | if (definitionVo != null) { |
| | | WfFormDefinitionBo wfFormDefinition = new WfFormDefinitionBo(); |
| | | wfFormDefinition.setDefinitionId(definition.getId()); |
| | | wfFormDefinition.setProcessKey(definition.getKey()); |
| | | wfFormDefinition.setPath(definitionVo.getPath()); |
| | | wfFormDefinition.setRemark(definitionVo.getRemark()); |
| | | iWfFormDefinitionService.saveOrUpdate(wfFormDefinition); |
| | | } |
| | | } |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | /** |
| | | * 导出模型zip压缩包 |
| | | * |
| | | * @param modelId 模型id |
| | | * @param modelIds 模型id |
| | | * @param response 相应 |
| | | */ |
| | | @Override |
| | | public void exportZip(String modelId, HttpServletResponse response) { |
| | | public void exportZip(List<String> modelIds, HttpServletResponse response) { |
| | | ZipOutputStream zos = null; |
| | | try { |
| | | zos = ZipUtil.getZipOutputStream(response.getOutputStream(), StandardCharsets.UTF_8); |
| | | // 压缩包文件名 |
| | | String zipName = "模型不存在"; |
| | | // 查询模型基本信息 |
| | | Model model = repositoryService.getModel(modelId); |
| | | byte[] xmlBytes = repositoryService.getModelEditorSource(modelId); |
| | | if (ObjectUtil.isNotNull(model)) { |
| | | if (JSONUtil.isTypeJSON(IOUtils.toString(xmlBytes, StandardCharsets.UTF_8.toString())) && ArrayUtil.isEmpty(ModelUtils.bpmnJsonToXmlBytes(xmlBytes))) { |
| | | zipName = "模型不能为空,请至少设计一条主线流程!"; |
| | | zos.putNextEntry(new ZipEntry(zipName + ".txt")); |
| | | zos.write(zipName.getBytes(StandardCharsets.UTF_8)); |
| | | } else if (ArrayUtil.isEmpty(xmlBytes)) { |
| | | zipName = "模型数据为空,请先设计流程定义模型,再进行部署!"; |
| | | zos.putNextEntry(new ZipEntry(zipName + ".txt")); |
| | | zos.write(zipName.getBytes(StandardCharsets.UTF_8)); |
| | | } else { |
| | | String fileName = model.getName() + "-" + model.getKey(); |
| | | // 压缩包文件名 |
| | | zipName = fileName + ".zip"; |
| | | // 将xml添加到压缩包中(指定xml文件名:请假流程.bpmn20.xml |
| | | zos.putNextEntry(new ZipEntry(fileName + ".bpmn20.xml")); |
| | | zos.write(xmlBytes); |
| | | for (String modelId : modelIds) { |
| | | Model model = repositoryService.getModel(modelId); |
| | | byte[] xmlBytes = repositoryService.getModelEditorSource(modelId); |
| | | if (ObjectUtil.isNotNull(model)) { |
| | | if (JSONUtil.isTypeJSON(IOUtils.toString(xmlBytes, StandardCharsets.UTF_8.toString())) && ArrayUtil.isEmpty(ModelUtils.bpmnJsonToXmlBytes(xmlBytes))) { |
| | | zipName = "模型不能为空,请至少设计一条主线流程!"; |
| | | zos.putNextEntry(new ZipEntry(zipName + ".txt")); |
| | | zos.write(zipName.getBytes(StandardCharsets.UTF_8)); |
| | | } else if (ArrayUtil.isEmpty(xmlBytes)) { |
| | | zipName = "模型数据为空,请先设计流程定义模型,再进行部署!"; |
| | | zos.putNextEntry(new ZipEntry(zipName + ".txt")); |
| | | zos.write(zipName.getBytes(StandardCharsets.UTF_8)); |
| | | } else { |
| | | String fileName = model.getName() + "-" + model.getKey(); |
| | | // 压缩包文件名 |
| | | zipName = fileName + ".zip"; |
| | | // 将xml添加到压缩包中(指定xml文件名:请假流程.bpmn20.xml |
| | | zos.putNextEntry(new ZipEntry(fileName + ".bpmn20.xml")); |
| | | zos.write(xmlBytes); |
| | | } |
| | | } |
| | | } |
| | | response.setHeader("Content-Disposition", |