1. 程式人生 > >activiti 工作流 動態 設定 指定 節點任務人、責任人、組 的實現方式

activiti 工作流 動態 設定 指定 節點任務人、責任人、組 的實現方式

首先給大家看一下我的流程圖:

流程檔案leaveBill.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="LeaveBill" name="LeaveBillProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="提交申請" activiti:assignee="${inputUser}" activiti:formKey="workflowAction_audit.action"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="經紀人審批" activiti:formKey="workflowAction_audit.action">
<extensionElements>
<activiti:taskListener event="create" class="cn.itcast.ssh.utils.MangerTaskHandlerCandidateUsers"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<userTask id="usertask3" name="老總審批" activiti:assignee="王中軍" activiti:formKey="workflowAction_audit.action"></userTask>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_LeaveBill">
<bpmndi:BPMNPlane bpmnElement="LeaveBill" id="BPMNPlane_LeaveBill">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="290.0" y="60.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="255.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="255.0" y="230.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="255.0" y="310.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="290.0" y="400.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="307.0" y="95.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="140.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="307.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="230.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="307.0" y="285.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="310.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="307.0" y="365.0"></omgdi:waypoint>
<omgdi:waypoint x="307.0" y="400.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

---------好了進入正題,我這個例子主要是兩個地方表現了兩種動態指定任務人的例子;

  一個就是提交申請,usertask1節點的動態設定辦理人

  ---<userTask id="usertask1" name="提交申請" activiti:assignee="${inputUser}" activiti:formKey="workflowAction_audit.action"></userTask>

  -------- activiti:assignee="${inputUser}" 設定任務人,表示式使用類似於el表示式的寫法,使用activiti:assignee節點,指定辦理人。這裡需要動態傳入引數來指定 

    -----------我程式中,在提交申請啟動執行請假例項時的程式碼片段

        /**更新請假狀態,啟動流程例項,讓啟動的流程例項關聯業務*/
        @Override
        public void saveStartProcess(WorkflowBean workflowBean) {
          //1:獲取請假單ID,使用請假單ID,查詢請假單的物件LeaveBill
          Long id = workflowBean.getId();
          LeaveBill leaveBill = leaveBillDao.findLeaveBillById(id);
          //2:更新請假單的請假狀態從0變成1(初始錄入-->稽核中)
            leaveBill.setState(1);
          //3:使用當前物件獲取到流程定義的key(物件的名稱就是流程定義的key)
          String key = leaveBill.getClass().getSimpleName();
          /**
          * 4:從Session中獲取當前任務的辦理人,使用流程變數設定下一個任務的辦理人
          * inputUser是流程變數的名稱,
          * 獲取的辦理人是流程變數的值
          */
          Map<String, Object> variables = new HashMap<String,Object>();
         variables.put("inputUser", SessionContext.get().getName());//表示惟一使用者,通過流程變數inputUser,指定當前登入讓人為節點辦理人
        /**
         * 5: (1)使用流程變數設定字串(格式:LeaveBill.id的形式),通過設定,讓啟動的流程(流程例項)關聯業務
          (2)使用正在執行物件表中的一個欄位BUSINESS_KEY(Activiti提供的一個欄位),讓啟動的流程(流程例項)關聯業務
        */
        //格式:LeaveBill.id的形式(使用流程變數)
        String objId = key+"."+id;
        variables.put("objId", objId);
          //6:使用流程定義的key,啟動流程例項,同時設定流程變數,同時向正在執行的執行物件表中的欄位BUSINESS_KEY新增業務

          //資料, 同時讓流程關聯業務
        runtimeService.startProcessInstanceByKey(key,objId,variables);

      }

二、動態指定多人處理節點任務 使用了自己實現監聽的方式。我想讓在經紀人稽核這個節點上,不止指定一個人來處理,這個節點的處理人有可能會變。

  ---bpmn檔案節點明細

   <userTask id="usertask2" name="經紀人審批" activiti:formKey="workflowAction_audit.action">
    <extensionElements>
      <activiti:taskListener event="create" class="cn.itcast.ssh.utils.MangerTaskHandlerCandidateUsers"></activiti:taskListener>
    </extensionElements>
   </userTask>

  -----下面是我的實現類

  1. package cn.itcast.ssh.utils;
  2. import java.util.Arrays;
  3. import org.activiti.engine.delegate.DelegateTask;
  4. import org.activiti.engine.delegate.TaskListener;
  5. import cn.itcast.ssh.domain.Employee;
  6. public class MangerTaskHandlerCandidateUsers implements TaskListener {
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 1L;
  11. @Override
  12. public void notify(DelegateTask delegateTask) {
  13. System.out.println("進入MangerTaskHandlerCandidateUsers=========");
  14. /**從新查詢當前使用者,再獲取當前使用者對應的領導*/
  15. Employee employee = SessionContext.get();
  16. //當前使用者
  17. String name = employee.getName();
  18. System.out.println("當前登入人========"+name);
  19. String[] empLoyees = {"馮小剛經紀人","范冰冰經紀人","馮小剛"};
  20. delegateTask.addCandidateUsers(Arrays.asList(empLoyees));//完成多處理人的指定
  21. System.out.println("節點任務人========馮小剛經紀人,范冰冰經紀人,馮小剛");
  22. }

}

------------以上就是動態設定多使用者任務人的例子,這裡我是手寫的demo,實際專案中我們可以在notify(DelegateTask delegateTask)中,通過查詢固定資料表或者快取來,實現靈活的任務人設定。這是我的個人拙見啊。

再囉嗦一下,如果是設定使用者組的話,這裡應該是delegateTask.addCandidateGroup("某組");

設定流程變數值:delegateTask.setVariable("inputName", "小明");//inputName流程變數名

自己現在也是一知半解,自己加強印象,也希望幫到需要的人