Activiti (3) - 流程变量的设置于获取

接着 BPMN 2.0 - Activiti, 我们以请假的工作流为例, 开始测试流程变量的设置于获取.

  1. 定义一个请假的流程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <?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="leave_process_with_variables" name="leave_process_with_variables【流程请假】" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="提交申请"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <userTask id="usertask2" name="上级审批" activiti:assignee="王二"></userTask>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    </process>
    <bpmndi:BPMNDiagram id="BPMNDiagram_leave_process_with_variables">
    <bpmndi:BPMNPlane bpmnElement="leave_process_with_variables" id="BPMNPlane_leave_process_with_variables">
    <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="90.0"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
    <omgdc:Bounds height="35.0" width="35.0" x="350.0" y="420.0"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
    <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="190.0"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
    <omgdc:Bounds height="55.0" width="105.0" x="315.0" y="300.0"></omgdc:Bounds>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
    <omgdi:waypoint x="367.0" y="125.0"></omgdi:waypoint>
    <omgdi:waypoint x="367.0" y="190.0"></omgdi:waypoint>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
    <omgdi:waypoint x="367.0" y="245.0"></omgdi:waypoint>
    <omgdi:waypoint x="367.0" y="300.0"></omgdi:waypoint>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
    <omgdi:waypoint x="367.0" y="355.0"></omgdi:waypoint>
    <omgdi:waypoint x="367.0" y="420.0"></omgdi:waypoint>
    </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
    </definitions>
  2. 发布工作流程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /**
    * 发布一个流程
    *
    * @throws IOException exception
    */
    @Test public void test2DeployTask() throws IOException {
    myService.createDynamicWorkflow(WORKFLOW_PROCESS_DEPLOYMENT_NAME, BPMNResourceType.BPMN_20,
    bpmnXML);

    // search workflow process
    List<Deployment> deployments = myService.findDeploymentByDeploymentName(WORKFLOW_PROCESS_DEPLOYMENT_NAME);
    Assert.assertEquals(1, deployments.size());

    deployments = myService.findDeploymentByProcessDefinitionKey(WORKFLOW_PROCESS_DEFINITION_KEY);
    Assert.assertEquals(1, deployments.size());

    // get last deployment
    Deployment deployment = deployments.get(0);

    List<String> resourceNames = myService.findDeploymentResourceNamesByDeploymentId(deployment.getId());
    printResources(deployment.getId(), resourceNames);
    }
  3. 启动工作流程, 创建工作流实例:

    1
    2
    3
    4
    5
    String          assignee        = "Tom";
    ProcessInstance processInstance = myService.startProcess(WORKFLOW_PROCESS_DEFINITION_KEY, assignee);

    String processInstanceId = processInstance.getId();
    logger.info("processInstanceId:" + processInstance.getId());
  4. 获取分配给当前用户的任务:

    1
    2
    3
    4
    5
    6
    7
    8
    // 获取TaskService服务对象的实例
    List<Task> tasks = myService.getTasks(assignee);
    Assert.assertEquals(1, tasks.size());

    Task task = tasks.get(0);
    logger.info("taskName:" + task.getName());
    logger.info("taskAssignee:" + task.getAssignee());
    Assert.assertEquals("提交申请", task.getName());
  5. 设置流程变量

    1
    2
    3
    4
    5
    6
    7
    8
    int    days   = 7;
    Date date = new Date();
    String reason = "回家过年";

    // 1.设置流程变量,使用基本数据类型
    myService.setTaskVariable(task.getId(), "请假天数", days); // 与任务ID邦德
    myService.setTaskVariable(task.getId(), "请假日期", date);
    myService.setTaskVariable(task.getId(), "请假原因", reason);
  6. 获取流程变量

    1
    2
    3
    4
    Map<String, Object> taskVariables = myService.getTaskVariables(task.getId());
    Assert.assertEquals(taskVariables.get("请假天数"), days);
    Assert.assertEquals(taskVariables.get("请假日期"), date);
    Assert.assertEquals(taskVariables.get("请假原因"), reason);

    这两个步骤的可以给流程设置一些


参考:

  1. http://blog.csdn.net/caoyue_new/article/details/52171472