Activiti 历史流程图高亮显示

package com.test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableUpdate;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/ViewPorcess")
public class ViewPorcessAction {
   @Autowired
   RepositoryService repositoryService;
   @Autowired
   protected RuntimeService runtimeService;
   @Autowired
   ProcessEngineConfiguration processEngineConfiguration;
   @Autowired
   ProcessEngineFactoryBean processEngine;
   @Autowired
   HistoryService historyService;
   @Autowired
   TaskService taskService;
   
   /**
    * 读取带跟踪的图片
    */
   @RequestMapping("/view.do")
   public void test(HttpServletResponse response,@RequestParam("processInstanceId") String processInstanceId) throws Exception {
       //获取历史流程实例
       HistoricProcessInstance processInstance =  historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
       //获取流程图
       BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
       processEngineConfiguration = processEngine.getProcessEngineConfiguration();
       Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);

       ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
       ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity)repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());

       List highLightedActivitList =  historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();
       //高亮环节id集合
       List highLightedActivitis = new ArrayList();
       //高亮线路id集合
       List highLightedFlows = getHighLightedFlows(definitionEntity,highLightedActivitList);

      /* for(HistoricActivityInstance tempActivity : highLightedActivitList){
           String activityId = tempActivity.getActivityId();
           highLightedActivitis.add(activityId);
       }*/
       //当前流程实例执行到哪个节点
ExecutionEntity execution = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(processInstanceId).singleResult();// 执行实例
       highLightedActivitis.add(execution.getActivityId());

       //中文显示的是口口口,设置字体就好了
       InputStream imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivitis,highLightedFlows,"宋体","宋体",null,1.0);
       //单独返回流程图,不高亮显示
//        InputStream imageStream = diagramGenerator.generatePngDiagram(bpmnModel);
       // 输出资源内容到相应对象
       byte[] b = new byte[1024];
       int len;
       while ((len = imageStream.read(b, 0, 1024)) != -1) {
           response.getOutputStream().write(b, 0, len);
       }
       
   }

   /**
    * 获取需要高亮的线
    * @param processDefinitionEntity
    * @param historicActivityInstances
    * @return
    */
   private List getHighLightedFlows(
           ProcessDefinitionEntity processDefinitionEntity,
           List historicActivityInstances) {
       List highFlows = new ArrayList();// 用以保存高亮的线flowId
       for (int i = 0; i < historicActivityInstances.size() - 1; i++) {// 对历史流程节点进行遍历
           ActivityImpl activityImpl = processDefinitionEntity
                   .findActivity(historicActivityInstances.get(i)
                           .getActivityId());// 得到节点定义的详细信息
           List sameStartTimeNodes = new ArrayList();// 用以保存后需开始时间相同的节点
           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 pvmTransitions = activityImpl
                   .getOutgoingTransitions();// 取出节点的所有出去的线
           for (PvmTransition pvmTransition : pvmTransitions) {
               // 对所有的线进行遍历
               ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition
                       .getDestination();
               // 如果取出的线的目标节点存在时间相同的节点里,保存该线的id,进行高亮显示
               if (sameStartTimeNodes.contains(pvmActivityImpl)) {
                   highFlows.add(pvmTransition.getId());
               }
           }
       }
       return highFlows;
   }
       
}

你可能感兴趣的:(Activiti 历史流程图高亮显示)