activiti工作流显示流程图且当前节点高亮

1、将文件ProcessDiagramGrnerator.java和ProcessDiagramCanvas.java拷贝到自己的项目中(所需文件下载地址在文末附录中)。
2、将以下代码添加到service层的ProcessTaskService.java中。

    /** 
     * 获取流程图像,已执行节点和流程线高亮显示
     */
    public void getActivitiProccessImage(String pProcessInstanceId, HttpServletResponse response) throws Exception {
        log.info("[开始]-获取流程图图像");
        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        try {
            //  获取历史流程实例
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                    .processInstanceId(pProcessInstanceId).singleResult();

            if (historicProcessInstance == null) {
                throw new Exception();
            } else {
                // 获取流程定义
                ProcessDefinitionEntity processDefinition = processRepositoryService.getDeployedProcessDefinition(historicProcessInstance.getProcessDefinitionId());

                // 获取流程历史中已执行节点,并按照节点在流程中执行先后顺序排序
                List historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery()
                        .processInstanceId(pProcessInstanceId).orderByHistoricActivityInstanceId().asc().list();

                // 已执行的节点ID集合
                List executedActivityIdList = new ArrayList();
                @SuppressWarnings("unused")
                int index = 1;
                log.info("获取已经执行的节点ID");
                for (HistoricActivityInstance activityInstance : historicActivityInstanceList) {
                    executedActivityIdList.add(activityInstance.getActivityId());
                    log.info("第[" + index + "]个已执行节点=" + activityInstance.getActivityId() + " : " +activityInstance.getActivityName());
                    index++;
                }

                // 获取流程图图像字符流
                InputStream imageStream = ProcessDiagramGenerator.generateDiagram(processDefinition, "png", executedActivityIdList);

                response.setContentType("image/png");
                OutputStream os = response.getOutputStream();
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = imageStream.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.close();
                imageStream.close();
            }
            log.info("[完成]-获取流程图图像");
        } catch (Exception e) {
            log.error("【异常】-获取流程图失败!" + e.getMessage());
            throw new Exception(e);
        }
    }

3、在controller层注入ProcessTaskService并调用此方法。

    @Autowired  ProcessTaskService  processTaskService;


    /**
     * 获取流程图像,已执行节点和流程线高亮显示
     */
    @RequestMapping(value="/showProcessImg/{processInstanceId}")
    public void getProcessImg(@PathVariable String processInstanceId, HttpServletResponse response){
        try {
            processTaskService.getActivitiProccessImage(processInstanceId, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4、在页面上添加访问

<a href="/controller路径/showProcessImg/"+processInstanceId target="_blank">流程图a>

5、运行代码,点击步骤4中链接即出现所需图片。

附录:
①参考文档:http://download.csdn.net/detail/heishuang/9232955
②参考文档中文件的百度云盘地址:http://pan.baidu.com/s/1eScdC46

你可能感兴趣的:(activiti,activiti工作流)