依赖
在之前的基础上:flowable-spring-boot-starter-process flowable笔记 - 环境配置
org.flowable
flowable-form-spring-configurator
6.4.0
流程和表单定义
bpmn20文件
官方那种使用包裹在startEvent里的方式,无法用代码获取表单,可能是用法不正确。
流程和表单放在一起
...
name="Speaker"
variable="SpeakerName"
type="string" />
type="date"
datePattern="dd-MMM-yyyy" />
...
表单单独存放
flowable:class="com.baison.bap.flowable.delegate.FormViewDelegate"/>
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 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();
}
}
}
获取流程表单
流程中的表单有两种:流程开始表单和流程中表单
查看是否有表单
// 流程开始表单
ProcessDefinition.hasStartFormKey();
// 流程中表单
Task.getFormKey();
获取开始流程表单
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionKey(code).latestVersion().singleResult();
StartFormData form = formService.getStartFormData(pd.getId());
FormInfo info = formRepositoryService.getFormModelByKey(form.getFormKey());
info.getFormModel();
获取流程中用户需要填写的表单
TaskFormData form = formService.getTaskFormData(taskId);
填写表单
runtimeService.startProcessInstanceWithForm和formService.submitStartFormData都能填写完成表单并开始流程
也就是说其实表单填写的数据都是放在variables里的
开始表单
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionKey("formRequest").latestVersion().singleResult();
Map 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);
流程中表单
TaskService.completeTaskWithForm(String taskId, String formDefinitionId, String outcome, Map variables)
原文:https://www.cnblogs.com/xwbz/p/10130654.html