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

首先給你們看一下個人流程圖:java

流程文件leaveBill.bpmnexpress

<?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>緩存

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

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

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

  -------- activiti:assignee="${inputUser}" 設置任務人,表達式使用相似於el表達式的寫法,使用activiti:assignee節點,指定辦理人。這裏須要動態傳入參數來指定 spa

    -----------我程序中,在提交申請啓動運行請假實例時的代碼片斷3d

        

        /**更新請假狀態,啓動流程實例,讓啓動的流程實例關聯業務*/
        @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添加業務orm

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

      }xml

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

 

 

  ---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流程變量名

相關文章
相關標籤/搜索