两种方法对activiti运行过的节点高亮显示

方法一:使用activiti自带的image画

此种方法好像使用activiti中5.16以后的版本:

1、controller层编辑

	@RequestMapping(value="/queryActivityMap", produces = MediaType.IMAGE_PNG_VALUE)
	@ResponseBody
	public byte[] queryActivityMap(String processInstanceId, Model model, HttpServletResponse response) {
		try {
			return orderService.traceProcessImage(processInstanceId);
		} catch (Exception e) {
			System.err.println("获取图片流错误!!");
			e.printStackTrace();
			return null;
		}
	}

2、service层编辑

	/**
	 * 得到带有高亮节点的流程图
	 * @param processInstanceId	流程实例id
	 * @return
	 */
	public byte[] traceProcessImage(String processInstanceId) {
		String taskId = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult().getId();
		if (StringUtils.isBlank(taskId))
            throw new IllegalArgumentException("任务ID不能为空!");
        // 当前任务节点
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null)
            throw new IllegalArgumentException("任务不存在!");

        BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());
        // List activeActivityIds = runtimeService.getActiveActivityIds(task.getExecutionId());

        // 必须添加此行才能取到配置文件中的字体,待根本解决问题后删除
        // Context.setProcessEngineConfiguration(processEngineConfiguration);
        // return ProcessDiagramGenerator.generateDiagram(bpmnModel, "PNG", activeActivityIds);

        // 经过的节点
        List activeActivityIds = new ArrayList<>();
        List finishedActiveActivityIds = new ArrayList<>();

        // 已执行完的任务节点
        List finishedInstances = historyService.createHistoricActivityInstanceQuery().processInstanceId(task.getProcessInstanceId()).finished().list();
        for (HistoricActivityInstance hai : finishedInstances) {
            finishedActiveActivityIds.add(hai.getActivityId());
        }

        // 已完成的节点+当前节点
        activeActivityIds.addAll(finishedActiveActivityIds);
        activeActivityIds.addAll(runtimeService.getActiveActivityIds(task.getProcessInstanceId()));

        // 经过的流
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(task.getProcessDefinitionId());
        List highLightedFlows = new ArrayList<>();
        getHighLightedFlows(processDefinitionEntity.getActivities(), highLightedFlows, activeActivityIds);

        ProcessDiagramGenerator pdg = processEngineConfiguration.getProcessDiagramGenerator();
        InputStream inputStream = pdg.generateDiagram(bpmnModel, "PNG", finishedActiveActivityIds, highLightedFlows,
                processEngineConfiguration.getProcessEngineConfiguration().getActivityFontName(),
                processEngineConfiguration.getProcessEngineConfiguration().getLabelFontName(),
                processEngineConfiguration.getProcessEngineConfiguration().getProcessEngineConfiguration().getClassLoader(), 1.0);
       try {
           return IOUtils.toByteArray(inputStream);
        } catch (Exception e) {
            throw new RuntimeException("生成流程图异常!", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
	}
	/*
     * 递归查询经过的流
     */
    private void getHighLightedFlows(List activityList, List highLightedFlows, List historicActivityInstanceList) {
        for (ActivityImpl activity : activityList) {
            if (activity.getProperty("type").equals("subProcess")) {
                // get flows for the subProcess
                getHighLightedFlows(activity.getActivities(), highLightedFlows, historicActivityInstanceList);
            }

            if (historicActivityInstanceList.contains(activity.getId())) {
                List pvmTransitionList = activity.getOutgoingTransitions();
                for (PvmTransition pvmTransition : pvmTransitionList) {
                    String destinationFlowId = pvmTransition.getDestination().getId();
                    if (historicActivityInstanceList.contains(destinationFlowId)) {
                        highLightedFlows.add(pvmTransition.getId());
                    }
                }
            }
        }
    }

3、显示结果:

4、activit中所需要的jar包


方法二:定义一个遮罩层在流程图上

1、查询每个流程实例的流程图,流程图属于流程定义的内容,根据流程实例 id 找到流程定义 id processDefinitionId)

ProcessInstance processInstance = runtimeService
				.createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
String processDefinitionId = processInstance
					.getProcessDefinitionId();

2、据流程实例 id 找到当前运行活动标识activityId
// 当前任务id
String activityId = processInstance.getActivityId();

3、根据流程定义 id 得到流程定义实体对象ProcessDefinitionEntity,此对象中包括了 bpmn 定义的信息。
ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) repositoryService
					.getProcessDefinition(processDefinitionId);

4、从流程定义对象中根据上边找到的当前活动标识activityId找到当前活动定义对象ActivityImpl
ActivityImpl activityImpl = processDefinitionEntity
					.findActivity(activityId);

5、从ActivityImpl中取出坐标信息 .
Map map = new HashMap();
			// 使用map存放结点坐标
			map.put("x", activityImpl.getX());
			map.put("y", activityImpl.getY());
			map.put("width", activityImpl.getWidth());
			map.put("height", activityImpl.getHeight());

6、将坐标信息传到 jsp 页面,使用 div 显示红色边框效果
	
	

	
	












你可能感兴趣的:(activiti)