Activiti流程图查看实例
投稿:shichen2014
本文实例展示了Activiti流程图查看的实现方法,具体步骤如下所示:
1、测试用例查看图片代码如下:
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); }
说明:
1) deploymentId为流程部署ID
2) resourceName为act_ge_bytearray表中NAME_列的值
3) 使用repositoryService的getDeploymentResourceNames方法可以获取指定部署下得所有文件的名称
4) 使用repositoryService的getResourceAsStream方法传入部署ID和文件名称可以获取部署下指定名称文件的输入流
5) 最后的有关IO流的操作,使用FileUtils工具的copyInputStreamToFile方法完成流程流程到文件的拷贝
2、web项目中在流程定义页面查看图片:
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; }
说明:
1) deploymentId为流程部署ID,imageName为图片名称
2) 因为是从流程定义列表页面查看图片,id和imageName可以从流程定义(ProcessDefinition)中获取(String getDeploymentId();和 String getDiagramResourceName();)
3) web页面标签<a target="_blank" href="viewImage?deploymentId=1&imageName=imageName.png" rel="external nofollow" >查看流程图</a>
3、web项目查看当前流程图
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"; }
其中service.getProcessDefinitionByTaskId(taskId);的代码实现:
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; }
其中service.getCurrentActivityCoordinates(taskId);的代码实现:
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()); return coordinates; }
image页面部分:
从个人任务列表页面点击<a target="_blank" href="/viewCurrentImage?taskId=1" rel="external nofollow" >查看当前流程图</a>跳转到下面页面:
<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>