Activiti流程图查看

1、测试用例查看图片

Activiti流程图查看
 public void viewImage() throws Exception {
  // 创建仓库服务对对象
  RepositoryService repositoryService = processEngine.getRepositoryService();
  // 从仓库中找需要展示的文件
  String deploymentId = "701";
  List<String> names = repositoryService.getDeploymentResourceNames(deploymentId);
  String imageName = null;
  for (String name : names) {
   if(name.indexOf(".png")>=0){
    imageName = name;
   }
  }
  if(imageName!=null){
//     System.out.println(imageName);
   File f = new File("e:/"+ imageName);
   // 通过部署ID和文件名称得到文件的输入流
   InputStream in =  repositoryService.getResourceAsStream(deploymentId, imageName);
   FileUtils.copyInputStreamToFile(in, f);
  }
Activiti流程图查看

 说明:

  1)     deploymentId为流程部署ID

  2)     resourceName为act_ge_bytearray表中NAME_列的值

  3)     使用repositoryService的getDeploymentResourceNames方法可以获取指定部署下得所有文件的名称

  4)     使用repositoryService的getResourceAsStream方法传入部署ID和文件名称可以获取部署下指定名称文件的输入流

  5)     最后的有关IO流的操作,使用FileUtils工具的copyInputStreamToFile方法完成流程流程到文件的拷贝

 

2、在web项目中流程定义页面查看图片

Activiti流程图查看
    public String viewImage(){
        InputStream  in = repositoryService.getResourceAsStream.getImageStream(deploymentId,imageName);//此处方法实际项目应该放在service里面
        HttpServletResponse resp = ServletActionContext.getResponse();
        try {
            OutputStream out = resp.getOutputStream();
            // 把图片的输入流程写入resp的输出流中
            byte[] b = new byte[1024];
            for (int len = -1; (len= in.read(b))!=-1; ) {
                out.write(b, 0, len);
            }
            // 关闭流
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
Activiti流程图查看

 说明:

  1)     deploymentId为流程部署ID,imageName为图片名称

  2)     因为是从流程定义列表页面查看图片,id和imageName可以从流程定义(ProcessDefinition)中获取(String getDeploymentId();和 String getDiagramResourceName();)

  3) web页面标签<a target="_blank" href="viewImage?deploymentId=1&imageName=imageName.png">查看流程图</a> 

 

3、在web项目任务列表页面查看当前流程图(显示活动节点)

Activiti流程图查看
    public String viewCurrentImage(){
        ProcessDefinition pd = service.getProcessDefinitionByTaskId(taskId);
        // 1. 获取流程部署ID
        putContext("deploymentId", pd.getDeploymentId());
        // 2. 获取流程图片的名称
        putContext("imageName", pd.getDiagramResourceName());
        
        // 3.获取当前活动的坐标
        Map<String,Object> currentActivityCoordinates =service.getCurrentActivityCoordinates(taskId);
        putContext("acs", currentActivityCoordinates);
        return "image";
    }
Activiti流程图查看

  其中service.getProcessDefinitionByTaskId(taskId);的代码实现:

Activiti流程图查看
public ProcessDefinition getProcessDefinitionByTaskId(String taskId) {
        // 1. 得到task
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        // 2. 通过task对象的pdid获取流程定义对象
        ProcessDefinition pd = repositoryService.getProcessDefinition(task.getProcessDefinitionId());
        return pd;
    }
Activiti流程图查看

  其中service.getCurrentActivityCoordinates(taskId);的代码实现:

Activiti流程图查看
    public Map<String, Object> getCurrentActivityCoordinates(String taskId) {
        Map<String, Object> coordinates = new HashMap<String, Object>();
        // 1. 获取到当前活动的ID
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
        String currentActivitiId = pi.getActivityId();
        // 2. 获取到流程定义
        ProcessDefinitionEntity pd = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(task.getProcessDefinitionId());
        // 3. 使用流程定义通过currentActivitiId得到活动对象
        ActivityImpl activity =  pd.findActivity(currentActivitiId);
        // 4. 获取活动的坐标
        coordinates.put("x", activity.getX());
        coordinates.put("y", activity.getY());
        coordinates.put("width", activity.getWidth());
        coordinates.put("height", activity.getHeight());
        
        //如果有多个流程活动节点(并发流程一般有多个活动节点)该方法应该返回一个list,代码应该使用下面的方法
        // 得到流程执行对象
/*        List<Execution> executions = runtimeService.createExecutionQuery()
                .processInstanceId(pi.getId()).list();
        // 得到正在执行的Activity的Id
        List<String> activityIds = new ArrayList<String>();
        for (Execution exe : executions) {
            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
            activityIds.addAll(ids);
        }
        List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
        for (String id : activityIds) {
            ActivityImpl activity1 = pd.findActivity(id);
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("x", activity1.getX());
            map.put("y", activity1.getY());
            map.put("width", activity1.getWidth());
            map.put("height", activity1.getHeight());
            list.add(map);
        }*/
        
        return coordinates;
    }
Activiti流程图查看

 

  image页面

  从个人任务列表页面点击<a target="_blank" href="/viewCurrentImage?taskId=1">查看当前流程图</a>跳转到下面页面

Activiti流程图查看
<body>
<!-- 1.获取到规则流程图 这里是用的strust2的标签得到上面上面放入值栈的值-->
<img style="position: absolute;top: 0px;left: 0px;" src="viewImage?deploymentId=<s:property value='#deploymentId'/>&imageName=<s:property value='#imageName'/>">

<!-- 2.根据当前活动的坐标,动态绘制DIV -->
<div style="position: absolute;border:1px solid red;top:<s:property value='#acs.y'/>px;left: <s:property value='#acs.x'/>px;width: <s:property value='#acs.width'/>px;height:<s:property value='#acs.height'/>px;   "></div>
</body>
Activiti流程图查看

 

4、另一种获取流程图的方法(并且显示活动节点)

  这种方法比上面的简单,但是没有上面的灵活

Activiti流程图查看
    public void test() throws Exception {
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("process1");
        taskService.complete(taskService.createTaskQuery().singleResult()
                .getId());

        // 得到流程定义实体类
//        ProcessDefinitionEntity pde = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
//                .getDeployedProcessDefinition(processInstance
//                        .getProcessDefinitionId());
ProcessDefinitionEntity pde = (ProcessDefinitionEntity)repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult();


        //得到流程执行对象
        List<Execution> executions = runtimeService.createExecutionQuery()
                .processInstanceId(processInstance.getId()).list();
        //得到正在执行的Activity的Id
        List<String> activityIds = new ArrayList<String>();
        for (Execution exe : executions) {
            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
            activityIds.addAll(ids);
        }
        InputStream in = ProcessDiagramGenerator.generateDiagram(pde, "png",
                activityIds);
        FileOutputStream out = new FileOutputStream("f:\\watch.png");
        FileCopyUtils.copy(in, out);
    }
Activiti流程图查看

 

上一篇:网络编程学习


下一篇:08《JAVA从零到壹》第八讲:系统常用类(文末有课后作业)