1. 程式人生 > >JBPM4"公司請假流程"例項步驟

JBPM4"公司請假流程"例項步驟

在開始例項之前需安裝JBPM4開發環境和所需資料庫(百度搜索安裝)
1:畫流程圖和生成png圖片;
<?xml version="1.0" encoding="UTF-8"?>
<process name="leave" xmlns="http://jbpm.org/4.4/jpdl">
   <start g="214,37,48,48" name="start1">
      <transition g="-47,-17" name="to 申請" to="申請"/>
   </start>
   <task assignee="#{owner}" form="request.html" g="192,126,92,52" name="申請">
      <transition g="-71,-17" name="to 經理審批" to="經理審批"/>
   </task>
   <task assignee="manager" form="manager.html" g="194,241,92,52" name="經理審批">
      <transition g="-29,-14" name="批准" to="exclusive1"/>
      <transition g="105,267;103,152:-47,-17" name="駁回" to="申請"/>
   </task>
   <decision expr="#{day > 3 ? '老闆審批' : '結束'}" g="218,342,48,48" name="exclusive1">
      <transition g="415,367:-47,-17" name="老闆審批" to="老闆審批"/>
      <transition g="-31,-16" name="結束" to="end1"/>
   </decision>
   <end g="219,499,48,48" name="end1"/>
   <task assignee="boss" form="boss.html" g="370,408,92,52" name="老闆審批">
      <transition g="415,524:-91,-18" name="結束" to="end1"/>
   </task>
</process>
2:釋出流程圖;
           ProcessEngine processEngine=Configuration.getProcessEngine();
       RepositoryService repositoryService=processEngine.getRepositoryService();
       try{
       String file="J:/leave.zip";
       //將zip檔案封裝到IO流裡
        System.out.println(new File(file));
       InputStream is= new FileInputStream(new File(file));
       ZipInputStream zis=new ZipInputStream(is);
       //將zip流程檔案釋出到pvm(流程虛擬機器中,它會把zip包中的xml檔案和png圖片儲存到資料庫)
       repositoryService.createDeployment().addResourcesFromZipInputStream(zis).deploy();
       zis.close();
       is.close();
3啟動流程
首先要獲取已釋出流程列表:
public List<ProcessDefinition> getProcessDefinitions() {
return repositoryService.createProcessDefinitionQuery().list();
}
<s:iterator value="processDefinitions" var="dfinition">
<tr>
<td>
${id }
</td>
<td>
${deploymentId }
</td>
<td>
${key }
</td>
<td>
${name }
</td>
<td>
${version }
</td>
<td>
<a href="undeploy.html?id=${deploymentId }">刪除流程</a>
</td>
<td>
<a href="start.html?id=${id }">啟動流程</a>
</td>
</tr>
</s:iterator>
獲取流程id,給“owner”賦值
Map map = new HashMap<String, Object>();
map.put("owner", ActionContext.getContext().getSession().get("role"));
ProcessEngine processEngine=Configuration.getProcessEngine();
//流程例項服務
ExecutionService executionService=processEngine.getExecutionService();
executionService.startProcessInstanceById(id, map);//啟動流程
4:使用者待辦任務
ProcessEngine processEngine=Configuration.getProcessEngine();
//任務引擎
TaskService taskService=processEngine.getTaskService();
//當前使用者代辦任務集合
taskService.findPersonalTasks(roleName);
<s:iterator value="tasks" var="task">
<tr>
<td>
<s:property value="#task.id" />
</td>
<td>
<s:property value="#task.key" />
</td>
<td>
<s:property value="#task.name" />
</td>
<td>
<s:property value="#task.activityName" />
</td>
<td>
<s:property value="#task.assignee" />
</td>
<td>
<a href="${formResourceName }?id=${id}">辦理</a>//跳轉到request.html頁面
</td>
</tr>
</s:iterator>
-------------request.jsp頁面
<form action="submit.html" method="post">
<input type="hidden" name="processDto.taskId" value="${param.id}">
<!-- 此處的owner與day兩個name值要與流程圖中定義的一致,owner是在xml中定義的變數,不屬於具體登陸者 -->
申請人:
<input type="text" name="processDto.owner" value="${user}" />
<br />
請假時間:
<input type="text" name="processDto.day" value="" />
<br />
請假原因:
<textarea name="processDto.reason"></textarea>
<br />
<input type="submit" />
</form>
map = new HashMap<String, Object>();
map.put("owner", processDto.getOwner());
map.put("day", Integer.parseInt(processDto.getDay()));
map.put("reason", processDto.getReason());
map.put("name", processDto.getOwner());
taskService.setVariables(processDto.getTaskId(), map);
taskService.completeTask(processDto.getTaskId());//申請環節完成
----經理審批環節
根據“manager”角色獲取待辦任務
taskService.findPersonalTasks(roleName);
------進入manager.jsp
<fieldset>
<legend>
經理稽核 username: ${user } param.id:${param.id }
</legend>
<form action="submitManager.html" method="post">
<input type="hidden" name="id" value="${param.id}">
申請人:
<s:property value="map['owner']" />
<br />
請假時間:
<s:property value="map['day']" />
<br />
請假原因:
<s:property value="map['reason']" />
<br />
姓名:
<s:property value="map['name']" />
<br />
<input name="result" type="submit" value="批准" />
<input name="result" type="submit" value="駁回" />
</form>
</fieldset>
taskService.completeTask(id, result);--經理審批結束“result”代表“批准”和“駁回”
--如果請假天數大於三天,轉由老闆審批,否則結束
taskService.findPersonalTasks(roleName);//獲取老闆待辦任務
Task task = taskService.getTask(id);
String taskId = task.getId();
Set<String> strSet = new HashSet<String>();
strSet.add("owner");
strSet.add("day");
strSet.add("reason");
strSet.add("name");
strSet.add("age");
strSet.add("address");
return taskService.getVariables(taskId, strSet);--進入boss辦理頁面
<form action="submitBoss.html" method="post">
<input type="hidden" name="id" value="${param.id}">
申請人:
<s:property value="map['owner']" />
<br />
請假時間:
<s:property value="map['day']" />
<br />
請假原因:
<s:property value="map['reason']" />
<br />
姓名:
<s:property value="map['name']" />
<br />
性別:
<s:property value="map['sex']" />
<br />
年齡:
<s:property value="map['address']" />
<br />
<input type="submit" />
</form>
taskService.completeTask(id);//boss完成審批