


<definitionsxmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:flowable="http://flowable.org/bpmn"xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"typeLanguage="http://www.w3.org/2001/XMLSchema"expressionLanguage="http://www.w3.org/1999/XPath"targetNamespace="http://www.flowable.org/processdef"><!-- id:流程定义key name:流程定义名称 isExecutable:是否可执行--><!-- flowable:自定义属性名="自定义属性值" --><process id="test_bpmn" name="test BPMN" isExecutable="true"flowable:organizationCode="流程所属组织编码" ><!-- documentation:流程描述 --><documentation>test BPMN</documentation></process>... ...</definitions>


/*** bpmn xml和BpmnModel 转换器*/private BpmnXMLConverter bpmnXMLConverter = new BpmnXMLConverter();/*** bpmn json和BpmnModel 转换器*/private BpmnJsonConverter bpmnJsonConverter = new BpmnJsonConverter();XMLStreamReader reader = null;InputStream inputStream = null;try {XMLInputFactory factory = XMLInputFactory.newInstance();inputStream = new FileInputStream(new File(filePath));reader = factory.createXMLStreamReader(inputStream);//校验bpmn xml文件是否异常BpmnModel bpmnModel = bpmnXMLConverter.convertToBpmnModel(reader);List<Process> processes = bpmnModel.getProcesses();if (CollectionUtils.isEmpty(processes)) {log.error("BPMN模型没有配置流程");return null;}/*** 获取流程定义信息*/Process mainProcess = bpmnModel.getMainProcess();//获取流程定义信息//获取流程keyString id = mainProcess.getId();//获取流程名称String name = mainProcess.getName();//获取流程描述String documentation = mainProcess.getDocumentation();//获取process节点中的扩展属性信息Map<String, List<ExtensionAttribute>> attributes = mainProcess.getAttributes();for (Map.Entry<String, List<ExtensionAttribute>> entry : attributes.entrySet()) {System.out.println("扩展属性key:" + entry.getKey() + ",对应的值:" + entry.getValue().get(0).getValue());}//保存或修改的流程模板信息Model newModel = new Model();// 查询是否已经存在流程模板 :即判断是修改还是新增List<Model> existModelList = modelRepository.findByKeyAndType(mainProcess.getId(), AbstractModel.MODEL_TYPE_BPMN);if (!existModelList.isEmpty()) {newModel.setId(existModelList.get(0).getId());}User userDo = new FlowUser();userDo.setId("aaaaaaaa");//设置流程名称newModel.setName(mainProcess.getName());//设置流程编码newModel.setKey(mainProcess.getId());//设置流程引擎类型newModel.setModelType(AbstractModel.MODEL_TYPE_BPMN);// newModel.setCreated(DateUtils.getCurrentTime());//设置流程描述newModel.setDescription(mainProcess.getDocumentation());//设置模板json格式ObjectNode bpmnJson = bpmnJsonConverter.convertToJson(bpmnModel);newModel.setModelEditorJson(bpmnJson.toString());if (StringUtils.isEmpty(newModel.getId())) {//修改newModel.setLastUpdated(Calendar.getInstance().getTime());newModel.setLastUpdatedBy(userDo.getId());} else {//新增newModel.setCreated(Calendar.getInstance().getTime());newModel.setCreatedBy(userDo.getId());}modelService.createModel(newModel, userDo);} catch (Exception e) {log.error("BPMN模型创建流程异常", e);return null;} finally {try {reader.close();} catch (XMLStreamException e) {log.error("关闭异常", e);}}}
自定义:CustomBpmnJsonConverter 类继承BpmnJsonConverterpublic class CustomBpmnJsonConverter extends BpmnJsonConverter {/*** 重写BpmnModel 和 bpmn Json转换方法:实现扩展属性转换*/@Overridepublic ObjectNode convertToJson(BpmnModel model, Map<String, ModelInfo> formKeyMap, Map<String, ModelInfo> decisionTableKeyMap) {... ...if (mainProcess.getExtensionElements().containsKey("historyLevel")) {... ...}//扩展process中自定义属性Map<String, List<ExtensionAttribute>> extAttributes = mainProcess.getAttributes();for (Map.Entry<String, List<ExtensionAttribute>> entry : extAttributes.entrySet()) {String key = entry.getKey();String value = entry.getValue().get(0).getValue();//获取流程所属组织编码if (key.equalsIgnoreCase("organizationCode")) {propertiesNode.put("organizationCode", value);continue;}}... ...
}}
然后再实现将Json中的扩展属性转换到BpmnModel对应属性中:如下为扩展代码:
自定义:CustomBpmnJsonConverter 类继承BpmnJsonConverterpublic class CustomBpmnJsonConverter extends BpmnJsonConverter {/*** 重写bpmn Json和 BpmnModel转换方法:实现扩展属性转换*/@Overridepublic BpmnModel convertToBpmnModel(JsonNode modelNode, Map<String, String> formKeyMap, Map<String, String> decisionTableKeyMap) {... ...if (!nonEmptyPoolFound) {String organizationCode = BpmnJsonConverterUtil.getPropertyValueAsString("organizationCode ", modelNode);if (StringUtils.isNotEmpty(organizationCode)) {ExtensionAttribute organizationCodeExtensionAttribute = new ExtensionAttribute();organizationCodeExtensionAttribute.setName("organizationCode ");organizationCodeExtensionAttribute.setValue(organizationCode);organizationCodeExtensionAttribute.setNamespace("http://flowable.org/bpmn");organizationCodeExtensionAttribute.setNamespacePrefix("flowable");process.addAttribute(organizationCodeExtensionAttribute);}}
}... ...}
如上重写源码对应实现方法即可,具体为什么这样写,我觉得没必要粘贴源码和debug图了,感兴趣小伙伴看下源码,debug跟一下就明白啦~
长按二维码关注我们

ITSK
博客|yajing8
我知道你在看哟

文章转载自ITSK,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。





