Activiti工作流--流程變數應用之一:選擇連線--之七
阿新 • • 發佈:2018-12-15
流程的業務描述
財務報賬審批流程: 如果財務主管接到審批任務,發現金額只有500時,自己審批完流程結束 如果財務主管接到審批任務,發現金額是1000時,審批完以後轉給財務經理繼續審批,財務經理審批完流程結束
先獲取連線
private ProcessEngine processEngine;
@Before
public void initProcessEngine(){
processEngine = ProcessEngines.getDefaultProcessEngine();
}
流程定義與部署
@Test
public void testProcessDeployment (){
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("diagram/approve.bpmn")
.addClasspathResource("diagram/approve.png")
.name("財務報賬")
.deploy();
System.out.println("流程部署ID:"+deployment.getId());//60001
System. out.println("流程的名稱:"+deployment.getName());
}
流程圖 approve.bpmn程式碼
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns: bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="approve" name="approveProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="報賬申請" activiti:assignee="王五"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="財務審批" activiti:assignee="趙六"></userTask>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_approve">
<bpmndi:BPMNPlane bpmnElement="approve" id="BPMNPlane_approve">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="250.0" y="90.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="330.0" y="80.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="480.0" y="80.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="630.0" y="90.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="285.0" y="107.0"></omgdi:waypoint>
<omgdi:waypoint x="330.0" y="107.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="435.0" y="107.0"></omgdi:waypoint>
<omgdi:waypoint x="480.0" y="107.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="585.0" y="107.0"></omgdi:waypoint>
<omgdi:waypoint x="630.0" y="107.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
啟動流程
@Test
public void testStartProcess(){
String processDefinitionKey = "approve";
ProcessInstance processInstance = processEngine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println("流程部署ID:"+processInstance.getId());
System.out.println("流程定義ID:"+processInstance.getProcessDefinitionId());
System.out.println("流程例項ID:"+processInstance.getProcessInstanceId());//62501
}
查詢個人,完成個人任務
注意:在完成個人任務的同時,設定流程變數,通過流程變數的設值來決定流程的下一步走向
/**
* 流程處理過程:查詢個人任務
*/
@Test
public void testSetVariable(){
String assignee = "王五";
String processInstanceId = "62501";
List<Task> list = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee(assignee)
.processInstanceId(processInstanceId)
.list();
if (list != null && list .size() > 0) {
for (Task task : list) {
System.out.println("流程定義ID:"+task.getProcessDefinitionId());
System.out.println("流程例項ID:"+task.getProcessInstanceId());
System.out.println("執行物件ID:"+task.getExecutionId());
System.out.println("任務ID:"+task.getId());
System.out.println("任務名稱:"+task.getName());
System.out.println("任務建立時間:"+task.getCreateTime());
}
}
String taskId = "62504";
//設定流程變數,當任務id相同是,後面的值會覆蓋前面的值
/**
* 可以通過processEngine.getTaskService().setVariable(taskId taskid
* , variableName 變數名稱
* , value 變數名稱的值
* )
*/
//第一種方式
/*processEngine.getTaskService().setVariable(taskId, "報賬原因", "出差");
processEngine.getTaskService().setVariable(taskId, "報賬金額", 10000);
processEngine.getTaskService().setVariable(taskId, "報賬時間", new Date());*/
//第二種方式
/*Map<String, Object> map = new HashMap<String,Object>();
map.put("報賬原因", "餐費");
map.put("報賬金額", 1000);
map.put("報賬時間", new Date());
processEngine.getTaskService().setVariables(taskId, map);*/
//第三種方式,是通過bean的序列化的方式,往bean裡面賦值,此時的報賬資訊是存在流程圖的表中act_ge_bytearray表中
ApproveForm approveForm = new ApproveForm();
approveForm.setApproveReason("餐費");
approveForm.setApproveMoney("1000");
approveForm.setDate(new Date());
processEngine.getTaskService().setVariable(taskId, "報賬資訊", approveForm);
}
/**
* 流程處理過程,完成個人任務
*/
@Test
public void testCompleteTask(){
String taskId = "62504";
Map<String, Object> map = new HashMap<String,Object>();
map.put("審批意見", "批准");
processEngine.getTaskService().complete(taskId,map);
System.out.println("審批任務完成");
}
/**
* 根據查詢的任務id去獲取變數
*/
@Test
public void testGetVariable(){
String assignee = "趙六";
String processInstanceId = "62501";
List<Task> tasks = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee(assignee)
.processInstanceId(processInstanceId)
.list();
if (tasks != null && tasks.size() > 0) {
for (Task task : tasks) {
System.out.println("流程定義ID:"+task.getProcessDefinitionId());
System.out.println("流程例項ID:"+task.getProcessInstanceId());
System.out.println("執行物件ID:"+task.getExecutionId());
System.out.println("任務ID:"+task.getId());
System.out.println("任務名稱:"+task.getName());
System.out.println("任務建立時間:"+task.getCreateTime());
}
}
String taskId = "62504";
ApproveForm approveForm = (ApproveForm)processEngine.getTaskService().getVariable(taskId, "報賬資訊");
System.out.println("報賬資訊金額:"+approveForm.getApproveMoney());
System.out.println("報賬原因:"+approveForm.getApproveReason());
System.out.println("報賬時間:"+approveForm.getDate());
}
/**
* 當某個任務結束以後,在任務表act_ru_task中的記錄會被刪除 ,
* 但是act_hi_taskinst記錄的endtime會加上 ,所以我們可以從這個表查詢我們的任務執行歷史記錄
*
*
*/
@Test
public void testQueryHistoryTask(){
String assignee = "王五";
List<HistoricTaskInstance> historyTaskList = processEngine.getHistoryService()
.createHistoricTaskInstanceQuery()
.taskAssignee(assignee)
.list();
if(historyTaskList!=null&&historyTaskList.size()>0){
for(HistoricTaskInstance hisTask:historyTaskList){
System.out.println("流程定義ID:"+hisTask.getProcessDefinitionId());
System.out.println("流程例項ID:"+hisTask.getProcessInstanceId());
System.out.println("執行物件ID:"+hisTask.getExecutionId());
System.out.println("歷史任務ID:"+hisTask.getId());//任務ID:12502
System.out.println("歷史任務名稱:"+hisTask.getName());
System.out.println("歷史任務的結束時間:"+hisTask.getEndTime());
System.out.println("---------------------");
}
}
}
/**
* 當某個流程例項執行完以後以後,在流程例項表表act_ru_variable中的記錄會被刪除 ,
* 但是 act_hi_varinst表中儲存了歷史記錄
*
*/
@Test
public void testQueryHistoryVariables(){
String processInstanceId = "50001";
List<HistoricVariableInstance> hisVariableList = processEngine.getHistoryService()
.createHistoricVariableInstanceQuery()
.processInstanceId(processInstanceId)
.list();
if(hisVariableList != null && hisVariableList.size() > 0){
for(HistoricVariableInstance historicVariableInstance :hisVariableList){
System.out.println("流程定義ID:"+historicVariableInstance.getProcessInstanceId());
System.out.println("歷史流程變數名稱:"+historicVariableInstance.getVariableName());
System.out.println("歷史流程變數value:"+historicVariableInstance.getValue());
System.out.println("---------------------");
}
}
}
總結:
一個活動中可以指定一個或者多個sequenceFlow 結束活動沒有 sequenceFlow 如果一個活動中指定了多個sequenceFlow,則需要使用流程變數設定condition的表示式(返回結果是boolean)來判斷進入就哪條連線.