FormService

  • 解析流程定义中表单项的配置
  • 提交表单的方式驱动用户节点流转
  • 获取自定义外部表单key

示例

首先编写流程定义文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1536107421286" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="second_approve" isClosed="false" isExecutable="true" name="二级审批" processType="None">
    <startEvent id="startEvent" name="开始" activiti:formKey="/rest/process/form/start"/>
      <extensionElements>
        <activiti:formProperty id="message" name="信息" type="string" required="true"/>
      </extensionElements>
    <sequenceFlow id="flow1" sourceRef="startEvent" targetRef="myTask"/>
    <userTask id="myTask" name="my-task" activiti:candidateUsers="yubuyun"
      activiti:formKey="/rest/process/form/userTask">
      <extensionElements>
        <activiti:formProperty id="yesOrNo" name="审批" type="string" required="true"/>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow2" sourceRef="myTask" targetRef="endEvent"/>
    <endEvent id="endEvent" name="取消"/>
  </process>
</definitions>

从流程文件中可以看出在启动事件和用户任务节点都有表单,表单的属性用extensionElements标签包裹。

后台获取流程定义中配置的属性代码如下:

FormService formService = processEngine.getFormService();

// 获取表单key和启动节点表单数据
String formKey = formService.getStartFormKey(processDefinition.getId());
StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
List<FormProperty> formPropertyList = startFormData.getFormProperties();

// 提交开始节点的表单
Map<String,String> properties = new HashMap<String,String>();
properties.put("message","hello world");
ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(),properties);

// 获取当前用户任务节点的表单数据
TaskService taskService = processEngine.getTaskService();
Task task1 = taskService.createTaskQuery()
                .processInstanceId(processInstance.getId())
                .singleResult();
TaskFormData taskFormData = formService.getTaskFormData(task1.getId());
logger.info("taskformData={}",taskFormData);

// 提交任务节点的表单
Map<String,String> taskParams = new HashMap<String, String>();
taskParams.put("yesOrNo","yes");
formService.submitTaskFormData(processDefinition.getId(),taskParams);

从上述示例中可知:

  1. 当执行submitStartFormData方法时根据流程定义id提交开始事件的表单后,自动发起流程并从开始节点提交到用户任务节点。
  2. 提交到用户任务节点后就可以查询任务节点的表单数据TaskFormData对象。
  3. 当执行submitTaskFormData方法时候,流程自动从当前节点提交到下个节点。
  4. 由于表单属性required参数为true,表示提交表单的时候这个参数一定要传,不然会抛异常。