1. 程式人生 > >Activiti 6.x【4】activiti 基礎流程demo

Activiti 6.x【4】activiti 基礎流程demo

Activiti6.x 基礎流程demo

接下來是一個demo了,首先,先把資料庫清理一下,雖然不清理也沒啥事。為了待會更直觀吧。

1.流程圖介紹:

流程圖總覽 這裡寫圖片描述 閘道器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的結果】 這裡寫圖片描述

TaskService

查詢當前人的個人任務

/**查詢當前人的個人任務*/
@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表裡面去。】