發大水 ...
發大水
package com.java1234.activiti.variable; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; import com.java1234.activiti.model.Student; public class VariableTest { /** * 獲取預設流程引擎實例,預設讀取配置文件activiti.cfg.xml */ private ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine(); /** * 1.部署流程定義. */ @Test public void deployWidthClassPath(){ Deployment deployment= processEngine.getRepositoryService()// 部署相關service .createDeployment()//創建部署對象 .addClasspathResource("diagrams/StudentLeaveProcess.bpmn")//載入資源文件 .addClasspathResource("diagrams/StudentLeaveProcess.png") //載入資源圖片 .name("學生請假流程")//設置名稱 .deploy();//部署 System.out.println("流程部署Id:"+deployment.getId()); System.out.println("流程部署Name:"+deployment.getName()); /* 流程部署Id:15001 流程部署Name:學生請假流程 ------------------- ## 流程部署表 select * from act_re_deployment; ## 流程定義表 select * from act_re_procdef; ## 資源文件表 select * from act_ge_bytearray; */ } /** * 1.1查詢所以流程部署,名稱查詢,like查詢 */ @Test public void findDeploy(){ // processEngine.getRepositoryService().get } /** * 2.啟動流程實例 */ @Test public void start(){ ProcessInstance pi= processEngine.getRuntimeService() //運行服務 .startProcessInstanceByKey("studentLeaveProcess"); //資料庫中act_re_procdef的key_欄位 System.out.println("流程實例ID:"+pi.getId()); System.out.println("流程定義ID:"+pi.getProcessDefinitionId());//流程定義ID /* 流程實例ID:17501 ,30001 流程定義ID:studentLeaveProcess:1:15004 */ } /** * 3.查詢任務 * */ @Test public void findTask(){ List<Task> taskList= processEngine.getTaskService() //任務服務實例 .createTaskQuery()//創建任務查詢 .taskAssignee("王五")// 查詢 張三 ,李四,後歐洲王五 .list(); for(Task task:taskList){ // act_ru_task; 欄位 裡面的信息 System.out.println("任務Id:"+task.getId()); System.out.println("任務Name:"+task.getName()); System.out.println("任務創建時間:"+task.getCreateTime()); System.out.println("任務委派人:"+task.getAssignee()); System.out.println("任務實例ID:"+task.getProcessInstanceId()); } /*列印結果: 任務Id:17504, 30004 任務Name:學生請假申請 任務創建時間:Thu Jul 28 15:24:50 CST 2016 任務委派人:張三 任務實例ID:17501 ,30001 */ } /** * 4.完成任務,讓張三完成任務 */ @Test public void completeTask(){ processEngine.getTaskService()//任務伺服器 .complete("50002"); //使用上面: 任務Id:(17504) System.out.println("任務完成!"); } /** * 設置流程變數數據 * 42504,47502,如果再用42504會出現異常! */ private String taskId="47502"; @Test public void setVariableValues(){ /* # 運行時參數設定 , 先這個表的數據會在任務結束的時候被清空 select * from act_ru_variable; */ TaskService taskService= processEngine.getTaskService(); //任務服務實例 //String taskId=""; taskService.setVariable(taskId, "days", 2); taskService.setVariable(taskId, "date", new Date()); taskService.setVariable(taskId, "reason","發燒了"); Student student=new Student(); student.setId(1); student.setName("張三"); taskService.setVariable(taskId, "student", student); System.out.println("設置完成!"); } @Test public void getVariableValues(){ TaskService taskService= processEngine.getTaskService(); //任務服務實例 //String taskId=""; /** * 當任務提交的時候,下一個人可以根據自己的任務id獲取 到這些變數值 * 使用 42504 設置參數,使用47502也可獲取到 */ Integer days = (Integer) taskService.getVariable(taskId, "days"); Date date = (Date) taskService.getVariable(taskId, "date"); String reason= (String) taskService.getVariable(taskId, "reason"); Student student=(Student) taskService.getVariable(taskId, "student"); System.out.println("days:"+days+""); System.out.println("date:"+date); System.out.println("reason:"+reason); System.out.println("student:"+student); System.out.println("------查詢完成!"); /* days:2 date:Thu Jul 28 17:52:45 CST 2016 reason:發燒了 student:Student [id=1, name=張三] */ } /** * 這種設置會覆蓋數據! */ @Test public void setVariableMap(){ TaskService taskService= processEngine.getTaskService(); //任務服務實例 Map<String, Object> variables =new HashMap<String, Object>(); variables.put("days", 3); variables.put("date", new Date()); variables.put("reason","發燒了22"); Student student=new Student(); student.setId(2); student.setName("張三1"); variables.put("student", student); /** *這種設置方法會覆蓋task數據! */ taskService.setVariables(taskId, variables); System.out.println("設置完成!"); } @Test public void getVariableMap(){ TaskService taskService= processEngine.getTaskService(); //任務服務實例 //String taskId=""; /** * 當任務提交的時候,下一個人可以根據自己的任務id獲取 到這些變數值 * 使用 42504 設置參數,使用47502也可獲取到 */ Map<String ,Object> variables = (Map<String, Object>) taskService.getVariables(taskId); System.out.println(variables); System.out.println("------查詢完成!"); } }