版權聲明:轉載注明出處就OK的說,有些東西會轉載,都會注明的說= =如果有冒犯麻煩見諒 https://blog.csdn.net/Pan1458689676/article/details/82597026
Activiti6.x 基礎流程demo
接下來是一個demo了,首先,先把資料庫清理一下,雖然不清理也沒啥事。為了待會更直覺吧。
流程圖總覽
網關condition設定
組設定
使用者設定
【一般以流程變量形式設定辦理人(把請假了解成一種任務,辦理人即請假人)】
流程圖代碼
<?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="EmployeeAskForLeave">
<process id="myProcess" name="員工請假流程" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask1" name="填寫請假申請" activiti:assignee="${userKey}"></userTask>
<exclusiveGateway id="exclusivegateway1" name="請假時間判斷(排他網關)"></exclusiveGateway>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
<userTask id="usertask2" name="經理審批" activiti:candidateGroups="HR"></userTask>
<userTask id="usertask3" name="總監審批" activiti:candidateGroups="ZJ"></userTask>
<sequenceFlow id="flow3" name="小于等于3天" sourceRef="exclusivegateway1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${days<=3}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" name="大于3天" sourceRef="exclusivegateway1" targetRef="usertask3">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${days>3}]]></conditionExpression>
</sequenceFlow>
<exclusiveGateway id="exclusivegateway2" name="請假時間判斷(排他網關)"></exclusiveGateway>
<sequenceFlow id="flow5" sourceRef="usertask2" targetRef="exclusivegateway2"></sequenceFlow>
<sequenceFlow id="flow6" sourceRef="usertask3" targetRef="exclusivegateway2"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="exclusivegateway2" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
<bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="200.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="1040.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="320.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
<omgdc:Bounds height="40.0" width="40.0" x="470.0" y="217.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="650.0" y="130.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="650.0" y="270.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">
<omgdc:Bounds height="40.0" width="40.0" x="920.0" y="217.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="235.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="320.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="425.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="470.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="490.0" y="217.0"></omgdi:waypoint>
<omgdi:waypoint x="490.0" y="157.0"></omgdi:waypoint>
<omgdi:waypoint x="650.0" y="157.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="66.0" x="490.0" y="198.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="490.0" y="257.0"></omgdi:waypoint>
<omgdi:waypoint x="490.0" y="297.0"></omgdi:waypoint>
<omgdi:waypoint x="650.0" y="297.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="42.0" x="490.0" y="257.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="755.0" y="157.0"></omgdi:waypoint>
<omgdi:waypoint x="940.0" y="157.0"></omgdi:waypoint>
<omgdi:waypoint x="940.0" y="217.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="755.0" y="297.0"></omgdi:waypoint>
<omgdi:waypoint x="940.0" y="297.0"></omgdi:waypoint>
<omgdi:waypoint x="940.0" y="257.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="960.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="1040.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
//部署流程定義,建立資料
@Test
public void prepare() {
Deployment deployment = repositoryService.createDeployment()//建立一個部署對象
.name("請假流程")
.addClasspathResource("bpmn/MyProcess.bpmn")
.addClasspathResource("bpmn/MyProcess.png")
.deploy();
System.out.println("部署ID:"+deployment.getId());
System.out.println("部署名稱:"+deployment.getName());
Group group1 = identityService.newGroup("HR");
group1.setName("HR");
group1.setType("HRassignment");
identityService.saveGroup(group1);//建立HR組
Group group2 = identityService.newGroup("ZJ");
group2.setName("ZJ");
group2.setType("ZJassignment");
identityService.saveGroup(group2);//建立ZJ組
Group group3 = identityService.newGroup("EP");
group3.setName("EP");
group3.setType("EPassignment");
identityService.saveGroup(group3);//建立員工組
//newUser傳的是key【不是名字】
identityService.saveUser(identityService.newUser("HR1"));//高管
identityService.saveUser(identityService.newUser("HR2"));//高管
identityService.saveUser(identityService.newUser("ZJ"));//總監
identityService.saveUser(identityService.newUser("ZJ2"));//總監
identityService.saveUser(identityService.newUser("PTM"));//員工
identityService.createMembership("HR1", "HR");
identityService.createMembership("HR2", "HR");
identityService.createMembership("ZJ", "ZJ");
identityService.createMembership("ZJ2", "ZJ");
identityService.createMembership("PTM", "EP");
}
/**啟動流程執行個體配置設定任務給個人*/
@Test
public void start() {
String userKey="PTM";//腦補一下這個是從前台傳過來的資料
String processDefinitionKey ="myProcess";//每一個流程有對應的一個key這個是某一個流程内固定的寫在bpmn内的
HashMap<String, Object> variables=new HashMap<>();
variables.put("userKey", userKey);//userKey在上文的流程變量中指定了
ProcessInstance instance=runtimeService
.startProcessInstanceByKey(processDefinitionKey,variables);
System.out.println("流程執行個體ID:"+instance.getId());
System.out.println("流程定義ID:"+instance.getProcessDefinitionId());
}
運作結果:
【通過流程變量指定User的結果】
查詢目前人的個人任務
/**查詢目前人的個人任務*/
@Test
public void findTask(){
String assignee = "PTM";
List<Task> list = taskService.createTaskQuery()//建立任務查詢對象
.taskAssignee(assignee)//指定個人任務查詢
.list();
if(list!=null && list.size()>0){
for(Task task:list){
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());
System.out.println("執行對象ID:"+task.getExecutionId());
System.out.println("流程定義ID:"+task.getProcessDefinitionId());
}
}
}
Query裡面可以寫多個查詢條件類是EF的那種
完成任務
/**完成任務*/
@Test
public void completeTask(){
//任務ID
String taskId = "47506";
HashMap<String, Object> variables=new HashMap<>();
variables.put("days", 4);//userKey在上文的流程變量中指定了
taskService.complete(taskId,variables);
System.out.println("完成任務:任務ID:"+taskId);
}
組任務查詢【因為days=4是以由總監組負責】
/**查詢目前人的組任務*/
@Test
public void findTaskGroup(){
//String assignee = "PTM";
List<Task> list = taskService.createTaskQuery()//建立任務查詢對象
.taskCandidateUser("ZJ")//指定組任務查詢
.list();
String taskid ="";
String instanceId ="";
if(list!=null && list.size()>0){
for(Task task:list){
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());
System.out.println("執行對象ID:"+task.getExecutionId());
System.out.println("流程定義ID:"+task.getProcessDefinitionId());
taskid=task.getId();
instanceId = task.getProcessInstanceId();
}
}
//查詢組任務成員[兩種方式]
//List<IdentityLink> listIdentity = taskService.getIdentityLinksForTask(taskid);
List<IdentityLink> listIdentity = runtimeService.getIdentityLinksForProcessInstance(instanceId);
for(IdentityLink identityLink:listIdentity ){
System.out.println("userId="+identityLink.getUserId());
System.out.println("taskId="+identityLink.getTaskId());
System.out.println("piId="+identityLink.getProcessInstanceId());
}
}
//taskService.claim(taskid,"ZJ2");//指定辦理人
//taskService.setAssignee(taskid, null);//回退為組任務狀态
指定辦理人後會變為使用者任務。
/**查詢目前人的組任務*/
@Test
public void findTaskGroup(){
String assignee = "ZJ2";
List<Task> list = taskService.createTaskQuery()//建立任務查詢對象
//.taskCandidateUser("ZJ")//指定組任務查詢
.taskAssignee(assignee)//指定個人任務查詢
.list();
String taskid ="";
String instanceId ="";
if(list!=null && list.size()>0){
for(Task task:list){
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());
System.out.println("執行對象ID:"+task.getExecutionId());
System.out.println("流程定義ID:"+task.getProcessDefinitionId());
taskid=task.getId();
instanceId = task.getProcessInstanceId();
}
}
//taskService.claim(taskid,"ZJ2");//指定辦理人
taskService.setAssignee(taskid, null);//回退為組任務狀态
List<IdentityLink> listIdentity = taskService.getIdentityLinksForTask(taskid);
//List<IdentityLink> listIdentity = runtimeService.getIdentityLinksForProcessInstance(instanceId);
//runtime查詢沒有taskId,task查詢沒有InstanceId
for(IdentityLink identityLink:listIdentity ){
System.out.println("userId="+identityLink.getUserId());
System.out.println("taskId="+identityLink.getTaskId());
System.out.println("piId="+identityLink.getProcessInstanceId());
}
}
代碼裡退回為組任務【即無辦理人】
【流程就結束了。記錄會被移到history表裡面去。】