Skip to main content

[Activiti 6.x] 剩下的核心API


Activiti 6.x 剩下的核心API讲解

HistoryService

流程定义与流程实例

/**HistoryProcessInstance*/
@Test
public void HistoryProcessInstance() {
    List<HistoricProcessInstance> datas = historyService.createHistoricProcessInstanceQuery()
            .finished().list();
    for (HistoricProcessInstance historicProcessInstance : datas) {
        System.out.println("流程实例id: "+historicProcessInstance.getId());
        System.out.println("部署id: "+historicProcessInstance.getDeploymentId());
        System.out.println("结束event: "+historicProcessInstance.getEndActivityId());
        System.out.println("流程名称: "+historicProcessInstance.getName());
        System.out.println("PROC_DEF_ID: "+historicProcessInstance.getProcessDefinitionId());
        System.out.println("流程定义Key: "+historicProcessInstance.getProcessDefinitionKey());
        System.out.println("流程定义名称: "+historicProcessInstance.getProcessDefinitionName());
        System.out.println("开始event: "+historicProcessInstance.getStartActivityId());
    }
}

【其他HistoryService查询类似】

FormService

【个人感觉在前端框架比较完善的今天几乎不会去用这个,但是还是做一些小demo】

activiti:formProperty【动态表单】

【流程图】

<?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="whatFk" name="whatFk" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="User Task">
      <extensionElements>
        <activiti:formProperty id="userName" name="userName" type="string" variable="userName"></activiti:formProperty>
        <activiti:formProperty id="age" name="age" type="string" variable="age"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <userTask id="usertask2" name="User Task"></userTask>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_whatFk">
    <bpmndi:BPMNPlane bpmnElement="whatFk" id="BPMNPlane_whatFk">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="210.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="310.0" y="120.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="840.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="560.0" y="120.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="245.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="310.0" y="147.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="415.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="560.0" y="147.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="665.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="840.0" y="147.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

@Test
public void FormType(){
    Deployment dep = repositoryService.createDeployment().addClasspathResource("bpmn/form.bpmn").deploy();
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("whatFk");
    System.out.println(pi);
    HashMap<String, String> variables = new HashMap<>();
    variables.put("userName", "潘天淼");
    variables.put("age", "18");

    Task task = taskService.createTaskQuery().deploymentId(dep.getId()).singleResult();
    formService.submitTaskFormData(task.getId(), variables);
}

数据库内以流程变量的形式存储了内容【注意:此方式设置的流程变量均为taskID】

@Test
public void FormTypeRead(){
    //75005为偷懒直接从数据库内复制的流程
    Task task = taskService.createTaskQuery().processInstanceId("75005").singleResult();
    TaskFormData taskFormData = formService.getTaskFormData(task.getId());
    List<FormProperty> formProperties = taskFormData.getFormProperties();
    for (FormProperty formProperty : formProperties) {
        System.out.println(formProperty.getId() + ",value=" + formProperty.getValue());
    }
}

上方代码在该task未执行时能够获取表单所有元素

activiti:formKey【外置表单】

【修改代码流程文件与form要一同部署】

@Test
public void FormType(){
    Deployment dep = repositoryService.createDeployment()
            .addClasspathResource("bpmn/form.bpmn")
            .addClasspathResource("bpmn/completeForm.form")
            .deploy();
    ProcessInstance pi = runtimeService.startProcessInstanceByKey("whatFk");
    HashMap<String, String> variables = new HashMap<>();
    variables.put("userName", "潘天淼");
    variables.put("age", "18");

    Task task = taskService.createTaskQuery().deploymentId(dep.getId()).singleResult();
    formService.submitTaskFormData(task.getId(), variables);
}

【测试结果】

@Test
public void OutFormTypeRead(){
    System.out.println(formService.getRenderedTaskForm("105016")==null);
    HashMap<String, String> variables = new HashMap<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar ca = Calendar.getInstance();
    String startDate = sdf.format(ca.getTime());
    ca.add(Calendar.DAY_OF_MONTH, 2); // 当前日期加2天
    String endDate = sdf.format(ca.getTime());
    variables.put("startDate", startDate);
    variables.put("endDate", endDate);
    variables.put("reason", "公休");
    formService.submitTaskFormData("105016", variables);
}

正常情况下呢这里是可以获取表单的。对于外置表单,只是把表单内容都保存在单独的form文件中,所以只能通过读取所有的请求参数作为流程启动参数。

图示部分为整块HTML的内容【想些啥都OK我是复制了一段HTML带表单的说】。

小结

activiti 基础部分到此就结束了。关于managementService据说是不怎么用的到的,呐网上资源也比较少,就不去细说了。接下来的话。本周内会把BPMN2.0涉及到的组件全部过一遍【不常用的Task组件我就不讲了。中间组件和补偿边界会全部涉及】。