flowable笔记 - 加入form引擎

依赖

在之前的基础上:flowable-spring-boot-starter-process flowable笔记 - 环境配置

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

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

流程和表单定义

bpmn20文件

官方那种使用包裹在startEvent里的方式,无法用代码获取表单,可能是用法不正确。

  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文件里的flowable:formKey对应
需要放在resources/forms文件夹下
表单字段名需要全局唯一,因为值是存在流程variables里的,如果重复,会有被覆盖的情况。

{
  "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定义

使用runtimeService.getStartFormModel获取开始表单
因为表单数据实际上是存在流程variables里的,所以使用execution.getVariable()获取表单值(不过有个FormField.getValue()方法,按照常理来说,是用来获取值的,但是实际上并没有,这个不知道是否版本问题,还是其他用途的)

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();
        }
    }
}

获取流程表单

流程中的表单有两种:流程开始表单和流程中表单

  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);

填写表单

runtimeService.startProcessInstanceWithFormformService.submitStartFormData都能填写完成表单并开始流程
也就是说其实表单填写的数据都是放在variables里的

  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)
posted @ 2018-12-17 11:51  林生草木深  阅读(13751)  评论(1编辑  收藏  举报