jbpm4.4學習筆記(3)

[html] view plaincopy
  1. <span style="font-family: Arial, Helvetica, sans-serif;">經過上一節的學習,我們已經將真整個請假流程的部署,發起以及執行分配都集成到web應用中,這裏我們要做的新工作是爲我們之前發佈的請假流程實現流程圖的跟蹤。</span>  

1、流程定義圖片生成

流程定義的圖片生成很簡單,這個工作jbpm4已經幫我們完成了,所以我們只需要將編輯好的流程定義保存即可,如下圖6-1所示:


圖6-1 自動生成流程定義png圖片

2、更改流程定義發佈方式

爲了要在web應用中顯示流程圖片,我們要將流程圖片和流程定義一併的發佈。

首先將leave.jpdl.xml和leave.png打包成leave.zip壓縮文件。然後更改流程定義的發佈方式,主要是更改deploy.jsp的源碼

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ page import="java.util.*,org.jbpm.api.*,java.util.zip.*" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.     <%  
  12.     ProcessEngine processEngine=Configuration.getProcessEngine();  
  13.     RepositoryService repositoryService=processEngine.getRepositoryService();  
  14.     //repositoryService.createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy();  
  15.     ZipInputStream zis = new ZipInputStream(this.getClass()  
  16.             .getResourceAsStream("/leave.zip"));  
  17.     repositoryService.createDeployment()  
  18.             .addResourcesFromZipInputStream(zis).deploy();  
  19.     response.sendRedirect("index.jsp");  
  20.     %>  
  21. </body>  
  22. </html>  

可以看到,我們是通過生成ZipInputStream對象,然後調用RepositoryService對象的相應流程部署函數完成的流程部署。

3、獲取當前活動的task任務

我們主要通過view.jsp實現獲得當前正在活動的任務,view.jsp的代碼如下:

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@page import="org.jbpm.api.*,java.util.*,org.jbpm.api.model.*" %>  
  4. <%  
  5.     String id = request.getParameter("id");  
  6.     ProcessEngine processEngine = Configuration.getProcessEngine();  
  7.     RepositoryService repositoryService = processEngine.getRepositoryService();  
  8.     ExecutionService executionService = processEngine.getExecutionService();  
  9.     ProcessInstance processInstance = executionService.findProcessInstanceById(id);  
  10.     Set<String> activityNames = processInstance.findActiveActivityNames();  
  11.       
  12.     ActivityCoordinates ac = repositoryService.getActivityCoordinates(processInstance.getProcessDefinitionId(),activityNames.iterator().next());  
  13. %>  
  14. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  15. <html>  
  16. <head>  
  17. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  18. <title>Insert title here</title>  
  19. </head>  
  20. <body>  
  21. <img src="pic.jsp?id=<%=id %>" style="position:absolute;left:0px;top:0px;">  
  22. <div style="position:absolute;border:1px solid red;left:<%=ac.getX()%>px;top:<%=ac.getY()%>px;width:<%=ac.getWidth()%>px;height:<%=ac.getHeight()%>px;"></div>  
  23. </body>  
  24. </html>  
對於這段代碼,作如下解釋:

1)活動任務的相關屬性獲得

[html] view plaincopy
  1. //獲得當前id對應的流程實例  
  2. ProcessInstance processInstance = executionService.findProcessInstanceById(id);  
  3. //或等當前流程實例的活動任務名稱   
  4. Set<String> activityNames = processInstance.findActiveActivityNames();  
  5. //獲得當前流程實例活動任務的座標屬性  
  6. ActivityCoordinates ac = repositoryService.getActivityCoordinates(processInstance.getProcessDefinitionId(),activityNames.iterator().next());  

2)整體流程定義圖片的顯示

[html] view plaincopy
  1. <img src="pic.jsp?id=<%=id %>" style="position:absolute;left:0px;top:0px;">  
這裏我們調用了pic.jsp其代碼如下:

[html] view plaincopy
  1. <%@page import="org.jbpm.api.*,java.io.*"%>  
  2. <%  
  3.     ProcessEngine processEngine = Configuration.getProcessEngine();  
  4.     RepositoryService repositoryService = processEngine  
  5.             .getRepositoryService();  
  6.     ExecutionService executionService = processEngine  
  7.             .getExecutionService();  
  8.     String id = request.getParameter("id");  
  9.     //獲得當前id對應流程實例  
  10.     ProcessInstance processInstance = executionService  
  11.             .findProcessInstanceById(id);  
  12.     //獲得當前流程實例對應流程定義id  
  13.     String processDefinitionId = processInstance  
  14.             .getProcessDefinitionId();  
  15.     //根據流程定義id最終獲得當前流程實例id對應的流程定義  
  16.     ProcessDefinition processDefinition = repositoryService  
  17.             .createProcessDefinitionQuery().processDefinitionId(  
  18.                     processDefinitionId).uniqueResult();  
  19.     //由流程定義或等當前流程定義對應的圖片,並生成inputStream流顯示出來  
  20.     InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(),"leave.png");  
  21.     byte[] b = new byte[1024];  
  22.     int len = -1;  
  23.     while ((len = inputStream.read(b, 0, 1024)) != -1) {  
  24.         response.getOutputStream().write(b, 0, len);  
  25.     }  
  26. %>  

3)標記當前活動任務
[html] view plaincopy
  1. <div style="position:absolute;border:1px solid red;left:<%=ac.getX()%>px;top:<%=ac.getY()%>px;width:<%=ac.getWidth()%>px;height:<%=ac.getHeight()%>px;"></div>  

即在之前獲得的當前活動任務對應座標處繪製一個紅色標記框。

4、程序運行效果

發佈了5 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章