依賴
在之前的基礎上: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里的方式,無法用代碼獲取表單,可能是用法不正確。
- 流程和表單放在一起
...
<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>
...
- 表單單獨存放
<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();
}
}
}
獲取流程表單
流程中的表單有兩種:流程開始表單和流程中表單
- 查看是否有表單
// 流程開始表單
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<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);
- 流程中表單
TaskService.completeTaskWithForm(String taskId, String formDefinitionId, String outcome, Map<String, Object> variables)
