关于Activiti5.22版本整合官方modler监听丢失解决办法

转载请注明出处:https://www.cnblogs.com/rolayblog/p/10444866.html 

我当时遇到这个问题的时候很懵,各种度娘谷歌,始终找不到解决办法。无奈,只有断点跟源码一步一步的看,最终找到了问题所在。然后根据自己有限的思维和经验修改了,下面上代码。

1 Model modelData = repositoryService.getModel(modelId);
2 repositoryService.saveModel(modelData);
3 ObjectNode modelNode = (ObjectNode) new ObjectMapper()
4                     .readTree(repositoryService.getModelEditorSource(modelData.getId()));
5 BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);

在convertToBpmnModel的386行有如下代码片段

1     // boundary events only contain attached ref id
2     for (Process process : bpmnModel.getProcesses()) {
3       postProcessElements(process, process.getFlowElements());
4     }

断点调试process.getFlowElements()中可以看到此时监听已经没有了,问题就在这里,需要我们自己来解析一次监听,下面是我自己写的,只解析了taskListeners,如有需要可自行扩展添加。

 1 private Collection<FlowElement> addLostListenter(Collection<FlowElement> elements, ArrayNode shapesArrayNode) {
 2         Collection<FlowElement> newelements=new ArrayList<>();
 3         for (FlowElement flowElement : elements) {
 4 
 5             JsonNode thenode = null;
 6             // 查找bpmn节点对应的xml节点
 7             for (JsonNode node : shapesArrayNode) {
 8                 JsonNode properties = node.get(EDITOR_SHAPE_PROPERTIES);
 9                 if (properties != null) {
10                     JsonNode overrideidnode = properties.get(PROPERTY_OVERRIDE_ID);
11                     if (overrideidnode != null) {
12                         if (StringUtils.isNotEmpty(overrideidnode.asText())) {
13                             if (overrideidnode.asText().equals(flowElement.getId())) {
14                                 thenode = node;
15                                 // 找到当前对应的xml节点跳出
16                                 break;
17                             }
18 
19                         } else {
20                             String resourceId = node.get(EDITOR_SHAPE_ID).asText();
21                             if (StringUtils.isNotEmpty(resourceId)) {
22                                 if (resourceId.equals(flowElement.getId())) {
23                                     thenode = node;
24                                     // 找到当前对应的xml节点跳出
25                                     break;
26                                 }
27 
28                             }
29                         }
30 
31                     } else {
32                         String resourceId = node.get(EDITOR_SHAPE_ID).asText();
33                         if (StringUtils.isNotEmpty(resourceId)) {
34                             if (resourceId.equals(flowElement.getId())) {
35                                 thenode = node;
36                                 // 找到当前对应的xml节点跳出
37                                 break;
38                             }
39                         }
40 
41                     }
42                 }
43             }
44             if (thenode != null&flowElement.getClass().equals(UserTask.class)) {
45                 // 获取tasklisteners节点
46                 
47                 FlowElement newflowelement= addTaskListenerToflowElement(flowElement,thenode);
48                 newelements.add(newflowelement);
49             }else
50             {
51                 newelements.add(flowElement);
52             }
53         }
54         return newelements;
55     }

然后更改386行处的代码如下:

1 // boundary events only contain attached ref id
2         for (Process process : bpmnModel.getProcesses()) {
3 
4             Collection<FlowElement> elements = process.getFlowElements();
5             Collection<FlowElement> newelements=addLostListenter(elements, shapesArrayNode);
6             postProcessElements(process, newelements);
7         }

然后再调用就能解析到监听了,如果大家有更好的方法,欢迎讨论。

posted @ 2019-02-27 16:59  Rolay  阅读(1081)  评论(0编辑  收藏  举报