Activiti 跟蹤流程執行情況用紅色框在流程圖上標識路線跟節點
阿新 • • 發佈:2019-02-04
/** * @Author 葛明 * @Note 讀取流程資源 * @Date 2017-1-3 15:11 * @param processDefinitionId 流程定義ID * @param resourceName 資源名稱 */ @RequestMapping(value = "/read-resource") public void readResource(String processDefinitionId, String resourceName,String pProcessInstanceId, HttpServletResponse response) throwsException { // 設定頁面不快取 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery(); ProcessDefinition pd = pdq.processDefinitionId(processDefinitionId).singleResult(); if(resourceName.endsWith(".png") && StringUtils.isEmpty(pProcessInstanceId) == false) { getActivitiProccessImage(pProcessInstanceId,response);} else { // 通過介面讀取 InputStream resourceAsStream = repositoryService.getResourceAsStream(pd.getDeploymentId(), resourceName); // 輸出資源內容到相應物件byte[] b = new byte[1024]; int len = -1; while ((len = resourceAsStream.read(b, 0, 1024)) != -1) { response.getOutputStream().write(b, 0, len); } } } /** * 獲取流程影象,已執行節點和流程線高亮顯示 */ public void getActivitiProccessImage(String pProcessInstanceId, HttpServletResponse response) {try { // 獲取歷史流程例項 HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery() .processInstanceId(pProcessInstanceId).singleResult(); if (historicProcessInstance != null) { // 獲取流程定義 ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService) .getDeployedProcessDefinition(historicProcessInstance.getProcessDefinitionId()); // 獲取流程歷史中已執行節點,並按照節點在流程中執行先後順序排序 List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery() .processInstanceId(pProcessInstanceId).orderByHistoricActivityInstanceId().asc().list(); // 已執行的節點ID集合 List<String> executedActivityIdList = new ArrayList<String>(); int index = 1; //獲取已經執行的節點ID for (HistoricActivityInstance activityInstance : historicActivityInstanceList) { executedActivityIdList.add(activityInstance.getActivityId()); index++; } // 已執行的線集合 List<String> flowIds = new ArrayList<String>(); // 獲取流程走過的線 flowIds = getHighLightedFlows(processDefinition, historicActivityInstanceList); BpmnModel bpmnModel = repositoryService .getBpmnModel(historicProcessInstance.getProcessDefinitionId()); // 獲取流程圖影象字元流 ProcessDiagramGenerator pec = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator(); //配置字型 InputStream imageStream = pec.generateDiagram(bpmnModel, "png", executedActivityIdList, flowIds,"宋體","微軟雅黑","黑體",null,2.0); 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(); } } catch (Exception e) { } }
/** *
*獲取流程走過的線
*/public List<String> getHighLightedFlows( ProcessDefinitionEntity processDefinitionEntity, List<HistoricActivityInstance> historicActivityInstances) { List<String> highFlows = new ArrayList<String>();// 用以儲存高亮的線flowIdfor (int i = 0; i < historicActivityInstances.size() - 1; i++) {// 對歷史流程節點進行遍歷ActivityImpl activityImpl = processDefinitionEntity .findActivity(historicActivityInstances.get(i) .getActivityId());// 得到節點定義的詳細資訊List<ActivityImpl> sameStartTimeNodes = new ArrayList<ActivityImpl>();// 用以儲存後需開始時間相同的節點ActivityImpl sameActivityImpl1 = processDefinitionEntity .findActivity(historicActivityInstances.get(i + 1) .getActivityId());// 將後面第一個節點放在時間相同節點的集合裡sameStartTimeNodes.add(sameActivityImpl1); for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) { HistoricActivityInstance activityImpl1 = historicActivityInstances .get(j);// 後續第一個節點HistoricActivityInstance activityImpl2 = historicActivityInstances .get(j + 1);// 後續第二個節點if (activityImpl1.getStartTime().equals( activityImpl2.getStartTime())) {// 如果第一個節點和第二個節點開始時間相同儲存ActivityImpl sameActivityImpl2 = processDefinitionEntity .findActivity(activityImpl2.getActivityId()); sameStartTimeNodes.add(sameActivityImpl2); } else {// 有不相同跳出迴圈break; } } List<PvmTransition> pvmTransitions = activityImpl .getOutgoingTransitions();// 取出節點的所有出去的線for (PvmTransition pvmTransition : pvmTransitions) {// 對所有的線進行遍歷ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition .getDestination();// 如果取出的線的目標節點存在時間相同的節點裡,儲存該線的id,進行高亮顯示if (sameStartTimeNodes.contains(pvmActivityImpl)) { highFlows.add(pvmTransition.getId()); } } } return highFlows;}