Activiti 6.x【5】剩下的核心API

剩下的核心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組件我就不講了。中間組件和補償邊界會全部涉及】。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章