Activiti工作流學習筆記入門(五)——流程變數
阿新 • • 發佈:2018-12-21
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.model.Student; /** * 流程變數測試 * @author user * */ public class VariableTest { /** * 獲取預設流程引擎例項,會自動讀取activiti.cfg.xml檔案 */ private ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine(); /** * 部署流程定義 */ @Test public void deploy(){ 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()); } /** * 啟動流程例項 */ @Test public void start(){ ProcessInstance pi=processEngine.getRuntimeService() // 執行時Service .startProcessInstanceByKey("studentLevaeProcess"); // 流程定義表的KEY欄位值 System.out.println("流程例項ID:"+pi.getId()); System.out.println("流程定義ID:"+pi.getProcessDefinitionId()); } /** * 檢視任務 */ @Test public void findTask(){ List<Task> taskList=processEngine.getTaskService() // 任務相關Service .createTaskQuery() // 建立任務查詢 .taskAssignee("李四") // 指定某個人 .list(); for(Task task:taskList){ System.out.println("任務ID:"+task.getId()); System.out.println("任務名稱:"+task.getName()); System.out.println("任務建立時間:"+task.getCreateTime()); System.out.println("任務委派人:"+task.getAssignee()); System.out.println("流程例項ID:"+task.getProcessInstanceId()); } } /** * 完成任務 */ @Test public void completeTask(){ processEngine.getTaskService() // 任務相關Service .complete("80002"); } /** * 設定流程變數資料 */ @Test public void setVariableValues(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="72504"; taskService.setVariable(taskId, "days", 2); // taskService.setVariable(taskId, "date", new Date()); taskService.setVariableLocal(taskId,"date", new Date()); taskService.setVariable(taskId, "reason", "發燒"); Student student=new Student(); student.setId(1); student.setName("張三"); taskService.setVariable(taskId, "student", student); // 存序列化物件 } /** * 獲取流程變數資料 */ @Test public void getVariableValues(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="80002"; Integer days=(Integer) taskService.getVariable(taskId, "days"); // Date date=(Date) taskService.getVariable(taskId, "date"); Date date=(Date) taskService.getVariableLocal(taskId, "date"); String reason=(String) taskService.getVariable(taskId, "reason"); Student student=(Student) taskService.getVariable(taskId, "student"); System.out.println("請假天數:"+days); System.out.println("請假日期:"+date); System.out.println("請假原因:"+reason); System.out.println("請假物件:"+student.getId()+","+student.getName()); } /** * 設定流程變數資料 */ @Test public void setVariableValues2(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="60004"; Student student=new Student(); student.setId(1); student.setName("張三"); Map<String, Object> variables=new HashMap<String,Object>(); variables.put("days", 2); variables.put("date", new Date()); variables.put("reason", "發燒"); variables.put("student", student); taskService.setVariables(taskId, variables); } /** * 獲取流程變數資料 */ @Test public void getVariableValues2(){ TaskService taskService=processEngine.getTaskService(); // 任務Service String taskId="65002"; Map<String,Object> variables=taskService.getVariables(taskId); Integer days=(Integer) variables.get("days"); Date date=(Date) variables.get("date"); String reason=(String) variables.get("reason"); Student student=(Student)variables.get("student"); System.out.println("請假天數:"+days); System.out.println("請假日期:"+date); System.out.println("請假原因:"+reason); System.out.println("請假物件:"+student.getId()+","+student.getName()); } }