思路: 将文件解析为xml格式
使用hutool工具类修改id
将xml文件合并为bpmn流程文件,上传流程文件
String path = sysFile.getRelativePath();
log.debug("获取文件下载路径====: {}", path);
InputStream downloadInputStream = fileUpload.getDownloadInputStream(path, null);
ByteArrayOutputStream output = cloneInputStream(downloadInputStream);
BpmnModel bpmnModel = ProcessUtil.convertToBpmnModel(new ByteArrayInputStream(output.toByteArray()));
//获取文件转化为xml
BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();
byte[] convertToXML = bpmnXMLConverter.convertToXML(bpmnModel);
String xmlData = new String(convertToXML);
log.info("该流程的流程xml为:{}", xmlData);
//读取xml文件
Document document = XmlUtil.parseXml(xmlData);
Element rootElement = XmlUtil.getRootElement(document);
Element process = XmlUtil.getElement(rootElement, "process");
String timeD = "a"+new Date().getTime();
process.setAttribute("id",timeD);
Element bpmndiB = XmlUtil.getElement(rootElement, "bpmndi:BPMNDiagram");
bpmndiB.setAttribute("id","BPMNDiagram_"+timeD);
Element bpmndiP = XmlUtil.getElement(bpmndiB,"bpmndi:BPMNPlane");
bpmndiP.setAttribute("bpmnElement",timeD);
bpmndiP.setAttribute("id","BPMNPlane_"+timeD);
String formatNew = XmlUtil.format(document);
log.info("formatNew"+formatNew);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(formatNew.getBytes(StandardCharsets.UTF_8));
@Override
@Transactional(rollbackFor = Exception.class)
public HbpmsDeploymentOut uploadBpmn(InputStream ins, Boolean loadPreConfig) {
ByteArrayOutputStream output = cloneInputStream(ins);
if (output == null) {
throw new BadArgumentException("导入流程定义文件失败,请检查文件内容是否正确!");
}
BpmnModel bpmnModel = ProcessUtil.convertToBpmnModel(new ByteArrayInputStream(output.toByteArray()));
String defKey = bpmnModel.getMainProcess().getId();
//为了防止导入的时候乱上传,需要对导入的文件进行校验
HbpmsProcdefBase defBase = hbpmsDefBaseService.get(defKey);
if (defBase == null) {
throw new BadArgumentException("导入流程定义[ " + defKey + " ]不存在,请先完善流程的基础配置信息");
}
/*ProcessDefinition preDefinition = null;
if (BooleanUtils.isTrue(loadPreConfig)) {
preDefinition = hbpmsConfiguration.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionKey(defKey)
.latestVersion()
.singleResult();
}*/
HbpmsDeploymentInputStream deploymentIns = new HbpmsDeploymentInputStream();
deploymentIns.setInputStream(new ByteArrayInputStream(output.toByteArray()));
deploymentIns.setValidateBomIs(new ByteArrayInputStream(output.toByteArray()));
// 添加默认标签,否则查询不到
List<Label> labels = new ArrayList<>(ONE);
Label label = new Label();
label.setLabelKey(BaseBizHandler.BIZ_NAME);
label.setLabelValue(defBase.getBizType());
labels.add(label);
deploymentIns.setLabels(labels);
// 部署
HbpmsDeploymentOut deploy = hbpmsDeploymentService.deploy(deploymentIns);
deploy.setKey(defKey);
return deploy;