flowable筆記 - 加入form引擎

依賴

在以前的基礎上:flowable-spring-boot-starter-process flowable筆記 - 環境配置html

<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-form-spring-configurator</artifactId>
    <version>6.4.0</version>
</dependency>

demo地址:https://github.com/xwbz2017/flowable-demojava

流程和表單定義

bpmn20文件

官方那種使用<code><extensionElements></code>包裹在<code>startEvent</code>裏的方式,沒法用代碼獲取表單,多是用法不正確。git

  1. 流程和表單放在一塊兒
...
<startEvent id="start">
  <extensionElements>
    <flowable:formProperty id="speaker"
      name="Speaker"
      variable="SpeakerName"
      type="string" />

    <flowable:formProperty id="start"
      type="date"
      datePattern="dd-MMM-yyyy" />

    <flowable:formProperty id="direction" type="enum">
      <flowable:value id="left" name="Go Left" />
      <flowable:value id="right" name="Go Right" />
      <flowable:value id="up" name="Go Up" />
      <flowable:value id="down" name="Go Down" />
    </flowable:formProperty>

  </extensionElements>
</startEvent>
...
  1. 表單單獨存放
<process id="formRequest" name="表單申請流程" isExecutable="true">
    <startEvent id="start" name="填寫表單" flowable:formKey="test" />

    <sequenceFlow sourceRef="start" targetRef="viewFormDelegate" />

    <serviceTask id="viewFormDelegate" name="展現信息"
                 flowable:class="com.baison.bap.flowable.delegate.FormViewDelegate"/>

    <sequenceFlow sourceRef="viewFormDelegate" targetRef="end" />

    <endEvent id="end" name="結束"/>
</process>

test.form

與bpmn20文件裏的<code>flowable:formKey</code>對應
須要放在<code>resources/forms</code>文件夾下
<b>表單字段名須要全局惟一,由於值是存在流程<code>variables</code>裏的,若是重複,會有被覆蓋的狀況。</b>github

{
  "key": "test",
  "name": "請假流程",
  "fields": [
    {
      "id": "startTime",
      "name": "開始時間",
      "type": "date",
      "required": true,
      "placeholder": "empty"
    },
    {
      "id": "endTime",
      "name": "結束時間",
      "type": "date",
      "required": true,
      "placeholder": "empty"
    },
    {
      "id": "reason",
      "name": "請假緣由",
      "type": "text",
      "required": true,
      "placeholder": "empty"
    }
  ],
  "outcomes": [
    {
      "id": "sendToParent",
      "name": "發給上級"
    },
    {
      "id": "sendToHr",
      "name": "發給人事"
    }
  ]
}

FormViewDelegate定義

使用<code>runtimeService.getStartFormModel</code>獲取開始表單
由於表單數據其實是存在流程variables裏的,因此<b>使用<code>execution.getVariable()</code>獲取表單值</b>(不過有個<code>FormField.getValue()</code>方法,按照常理來講,是用來獲取值的,可是實際上並無,這個不知道是否版本問題,仍是其餘用途的)spring

package com.baison.bap.flowable.delegate;

import com.baison.bap.util.SpringUtil;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.form.api.FormInfo;
import org.flowable.form.model.FormField;
import org.flowable.form.model.SimpleFormModel;

import java.util.List;

public class FormViewDelegate implements JavaDelegate {

    private RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);

    @Override
    public void execute(DelegateExecution execution) {
        FormInfo info = runtimeService.getStartFormModel(execution.getProcessDefinitionId(), execution.getProcessInstanceId());

        SimpleFormModel sfm = (SimpleFormModel) info.getFormModel();
        List<FormField> fields = sfm.getFields();
        for (FormField ff : fields) {
            System.out.println();
            System.out.println("id: " + ff.getId());
            System.out.println("name: " + ff.getName());
            System.out.println("type: " + ff.getType());
            System.out.println("placeholder: " + ff.getPlaceholder());
            System.out.println("value: " + ff.getValue());
            System.out.println("value from variable: " + execution.getVariable(ff.getId()));
            System.out.println();
        }
    }
}

獲取流程表單

流程中的表單有兩種:流程開始表單和流程中表單json

  1. 查看是否有表單
// 流程開始表單
ProcessDefinition.hasStartFormKey();
// 流程中表單
Task.getFormKey();
  1. 獲取開始流程表單
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionKey(code).latestVersion().singleResult();
StartFormData form = formService.getStartFormData(pd.getId());

FormInfo info = formRepositoryService.getFormModelByKey(form.getFormKey());
info.getFormModel();
  1. 獲取流程中用戶須要填寫的表單
TaskFormData form = formService.getTaskFormData(taskId);

填寫表單

<code>runtimeService.startProcessInstanceWithForm</code>和<code>formService.submitStartFormData</code>都能填寫完成表單並開始流程
也就是說其實表單填寫的數據都是放在variables裏的api

  1. 開始表單
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionKey("formRequest").latestVersion().singleResult();

        Map<String, String> properties = new HashMap<>();
        properties.put("startTime", "2018-12-14");
        properties.put("endTime", "2018-12-20");
        properties.put("reason", "回家");
//        ProcessInstance pi = runtimeService.startProcessInstanceWithForm(pd.getId(), "sendToParent", properties, null);
        ProcessInstance pi = formService.submitStartFormData(pd.getId(), UUID.randomUUID().toString(), properties);
  1. 流程中表單
TaskService.completeTaskWithForm(String taskId, String formDefinitionId, String outcome, Map<String, Object> variables)
相關文章
相關標籤/搜索