activiti5.17.0流程图及节点显示

引用:activiti流程图上获取各节点的信息获取

这篇文章写得很好,揭示了图片点击出现信息的方法,于是我也做了,只不过有些改动,可能是activiti的版本不同的原因。

jsp页面,通过流程实例id进行操作

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/pages/common/common-js.jsp"%>

  
      
          
        Insert title here  
          
          
      
      
        
java代码,通过反射调用源码中的节点x、y以及信息

 /** 
    * 根据流程的key生成图片 
    *  
    * @param request 
    * @param response 
    * @param wfKey 流程定义的key 
    */  
    @RequestMapping(value="readResource.htm")
	public void readResource(HttpServletRequest request,HttpServletResponse response) {  
		try {
			IMap params = new IMap(request);
			String processDefinitionId = ProcessEngines.getDefaultProcessEngine().getHistoryService()
					.createHistoricProcessInstanceQuery()
					.processInstanceId(params.getString("pid")).singleResult()
					.getProcessDefinitionId();
			RepositoryService repositoryService =  ProcessEngines.getDefaultProcessEngine().getRepositoryService();  
			ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
					.getDeployedProcessDefinition(processDefinitionId);
			
			InputStream imageStream = iActivitiUtilSvc.getFlowImageByKey(processDefinition.getKey());

			// 输出资源内容到相应对象
			byte[] b = new byte[1024];
			int len;
			while ((len = imageStream.read(b, 0, 1024)) != -1) {
				response.getOutputStream().write(b, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    /** 
     * 获取各个节点的具体的信息 
     * @param wfKey 
     *      流程定义的key 
     * @return 
     */  
    @RequestMapping("getProcessTrace.htm")  
    @ResponseBody  
    public List> getProcessTrace(String pid) throws Exception {  
        List> activityInfos = new ArrayList>();  
        String processDefinitionId = ProcessEngines.getDefaultProcessEngine().getHistoryService()
				.createHistoricProcessInstanceQuery()
				.processInstanceId(pid).singleResult()
				.getProcessDefinitionId();
        RepositoryService repositoryService =  ProcessEngines.getDefaultProcessEngine().getRepositoryService();  
		ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(processDefinitionId);
        List activitiList = processDefinition.getActivities();  
        InputStream xmlIs = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());  
        BpmnModel bm = new BpmnXMLConverter().convertToBpmnModel(new InputStreamSource(xmlIs), false, true);  
        // 下方使用反射获取最小的x和y,仔细看就会发现调用的是上方2.1节的方法 
        Class clazz = Class.forName("org.activiti.image.impl.DefaultProcessDiagramGenerator");  
        Method method = clazz.getDeclaredMethod("initProcessDiagramCanvas", BpmnModel.class, String.class,
        	      String.class, String.class, ClassLoader.class);  
        method.setAccessible(true);   
        DefaultProcessDiagramCanvas pdc = (DefaultProcessDiagramCanvas) method.invoke(clazz.newInstance(), bm,"png", "宋体", "宋体",  null); // 调用方法  
  
        clazz = Class.forName("org.activiti.image.impl.DefaultProcessDiagramCanvas");  
        Field minXField = clazz.getDeclaredField("minX"); // 得到minX字段  
        Field minYField = clazz.getDeclaredField("minY");  
        minXField.setAccessible(true);  
        minYField.setAccessible(true);  
        int minX = minXField.getInt(pdc);// 最小的x值    
        int minY = minYField.getInt(pdc); // 最小的y的值  
  

        minX = minX > 0 ? minX - 5 : 0;   // 此处为什么需要减5,上方2.2中activiti源码中有  
        minY = minY > 0 ? minY - 5 : 0;  
        for (ActivityImpl activity : activitiList) {  
            Map activityInfo = new HashMap();  
            activityInfo.put("width", activity.getWidth());  
            activityInfo.put("height", activity.getHeight());  
            activityInfo.put("x", activity.getX() - minX);  
            activityInfo.put("y", activity.getY() - minY);  
            activityInfo.put("actId", activity.getId());  
            activityInfo.put("name", activity.getProperty("name")); // ActivityImpl 中没有getName方法,所以此处需要这样获取。  
            activityInfos.add(activityInfo);  
        }  
       
        
        return activityInfos;  
    }  

public InputStream getFlowImageByKey(String flowKey){
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(flowKey).latestVersion()
				.singleResult();
		BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
		// 不使用spring请使用下面的两行代码
		ProcessEngineImpl defaultProcessEngine = (ProcessEngineImpl) ProcessEngines.getDefaultProcessEngine();
		Context.setProcessEngineConfiguration(defaultProcessEngine.getProcessEngineConfiguration());

		// 使用spring注入引擎请使用下面的这行代码
		// Context.setProcessEngineConfiguration(processEngine.getProcessEngineConfiguration());
		ProcessDiagramGenerator a = defaultProcessEngine.getProcessEngineConfiguration().getProcessDiagramGenerator();
		return a.generateDiagram(bpmnModel, "png", "宋体", "宋体", null, 1.0);
	}


activiti5.17.0流程图及节点显示_第1张图片

你可能感兴趣的:(activiti)