看到网上有很多实现此功能的代码,大概是找出历史记录,并按照时间排序,将历史记录里面的节点和连线全部展示。
如果是简单的流程,没有问题,如上图。
但是如果是下面的图,涉及到将流程回转的,那么历史表中会记录整个过程,这时候的再展示就不对了。
比如上图,如果一直循环在这里,那么整个线条是一致在循环的,这样的话,看到的将是一个闭环的红线,即便是简单的流程,如果有回退,那么看到的记录里面,仍然会有混乱。
问题解决思路:
找到当前待审批的任务,逐步前推,自然就不会有问题了。
废话少说,上代码:
/** 显示流程图 * @param request */ @ResponseBody @RequestMapping("/displayFlowCurrPic") public void displayFlowCurrPicnew(HttpServletRequest request, HttpServletResponse response){ BufferedImage img = new BufferedImage(300, 150, BufferedImage.TYPE_INT_RGB); String deploymentId=request.getParameter("deploymentId"); try { ProcessDefinition processDefinition=repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId)//使用部署对象ID查询 .singleResult(); List<Task> activeTasks=taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list(); // 已执行的节点ID集合 List<String> executedActivityIdList = new ArrayList<String>(); List<String> highLines = new ArrayList<String>(); List<String> highNodes = new ArrayList<String>(); //log.info("获取已经执行的节点ID"); BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId()); for(Task tk:activeTasks){ executedActivityIdList.add(tk.getTaskDefinitionKey()); highLines.addAll(getHighLines(bpmnModel,tk.getTaskDefinitionKey())); highNodes.addAll(getHighNodes(bpmnModel,tk.getTaskDefinitionKey())); } // 获取流程图图像字符流 InputStream imageStream = new DefaultProcessDiagramGenerator().generateDiagram(bpmnModel, "png", highNodes, highLines, "宋体", "宋体", "宋体", null, 1.0); // 输出资源内容到相应对象 byte[] b = new byte[1024]; int len; while ((len = imageStream.read(b, 0, 1024)) != -1) { response.getOutputStream().write(b, 0, len); } } catch (Exception e) { e.printStackTrace(); } }
/** * 获取高亮的线 * @param bpmnModel * @param key * @return */ private Set<String> getHighLines(BpmnModel bpmnModel,String key){ FlowNode fl=(FlowNode) bpmnModel.getFlowElement(key); List<SequenceFlow> pvmTransitions = fl.getIncomingFlows(); Set<String> highLines=new HashSet<>(); for(SequenceFlow sf:pvmTransitions){ highLines.add(sf.getId()); if(StringUtils.isNotBlank(sf.getSourceRef())){ highLines.addAll(getHighLines(bpmnModel,sf.getSourceRef())); } } return highLines; } /** * 获取高亮的线 * @param bpmnModel * @param key * @return */ private Set<String> getHighNodes(BpmnModel bpmnModel,String key){ FlowNode fl=(FlowNode) bpmnModel.getFlowElement(key); List<SequenceFlow> sequenceFlows = fl.getIncomingFlows(); Set<String> highNodes=new HashSet<>(); highNodes.add(key); for(SequenceFlow sf:sequenceFlows){ if(StringUtils.isNotBlank(sf.getSourceRef())){ highNodes.addAll(getHighNodes(bpmnModel,sf.getSourceRef())); } } return highNodes; }
前端定义一个img标签,查询的时候:
$("#disFlowPic").attr("src","../api/displayFlowCurrPic?deploymentId="+deploymentId+"");