通过流程xml 直接先发布模型,然后再通过发布模型之后的流程定义获取bpmn model来创建Model.
1、通过xml先发布模型
bash
InputStream bpmnStream = multipartFile.getInputStream()
deployment = repositoryService.createDeployment()
.addInputStream(multipartFile.getOriginalFilename(), bpmnStream).deploy();
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
.deploymentId(deployment.getId()).list();
String processDefinitionId = list.get(0).getId();
convertToModel(processDefinitionId);
2. 发布之后 通过流程定义获取bpmn model新建model
注意这里的addModelEditorSource addModelEditorSourceExtra获取。
bash
Model modelData = repositoryService.newModel();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId).singleResult();
InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(),
processDefinition.getResourceName());
XMLInputFactory xif = XMLInputFactory.newInstance();
InputStreamReader in = new InputStreamReader(bpmnStream, "UTF-8");
XMLStreamReader xtr = xif.createXMLStreamReader(in);
BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
BpmnJsonConverter converter = new BpmnJsonConverter();
com.fasterxml.jackson.databind.node.ObjectNode modelNode = converter.convertToJson(bpmnModel);
modelData.setKey(processDefinition.getKey());
modelData.setName(processDefinition.getKey());
ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processDefinition.getResourceName());
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);// 版本
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION,processDefinition.getResourceName());
modelData.setMetaInfo(modelObjectNode.toString());
repositoryService.saveModel(modelData);
DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
generator.generateDiagram(bpmnModel, new ArrayList<>());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IoUtil.copy(bpmnStream, bos);
byte[] bytes = bos.toByteArray();
repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
repositoryService.addModelEditorSourceExtra(
modelData.getId(), bytes);
deploy(modelData.getId());