一、前言
本章继续学习activiti6.0的核心api之FormService
二、FormService
1、复制 testProcess.bpmn20.xml 流程定义文件,并将其改名为 testProcess-form.bpmn20.xml
<process id="myProcess" isClosed="false" isExecutable="true" name="测试流程" processType="None">
<startEvent id="startevent1" name="start" activiti:formKey="/rest/process/form/start">
<activiti:formProperty id="message" name="信息" type="string" required="true"/>
<endEvent id="endevent1" name="End"/>
<userTask activiti:assignee="张三" activiti:exclusive="true" id="test" name="用户"
activiti:formKey="/rest/process/form/userTask">
<activiti:formProperty id="yesORno" name="审批" required="true" type="string"/>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="test"/>
<sequenceFlow id="flow2" sourceRef="test" targetRef="endevent1"/>
</process>
2、测试代码如下
/**
*
* 测试FormService
* FormService的作用:
* 1、解析流程定义中表单项的配置
* 2、提交表单的方式驱动用户节点流转
* 3、获取自动以外部表单key
*
*
* @author chaoge
* @since 2018/10/9 10:33
*/
public class FormServiceTest {
private static final LoggerLOGGER = LoggerFactory.getLogger(IdentityServiceTest.class);
@Rule
public ActivitiRulerule =new ActivitiRule();
/**
* 测试表单属性获取
*/
@Test
@Deployment(resources ="processes/testProcess-form.bpmn20.xml")
public void testFormService(){
FormService formService =rule.getFormService();
ProcessDefinition processDefinition =rule.getRepositoryService().createProcessDefinitionQuery().singleResult();
// startFormKey 对应流程文件中的startEvent中的属性activiti:formKey
String startFormKey = formService.getStartFormKey(processDefinition.getId());
LOGGER.info("startFormKey = {}",startFormKey );
//获取开始表单中的数据 对应的是startEvent标签下的扩展属性标签extensionElements下的activiti:formProperty标签的属性
//每一个formProperty对应下面的一个FormProperty
StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
List startFormProperties = startFormData.getFormProperties();
startFormProperties.forEach(formProperty ->
LOGGER.info("startFormProperty = {}", ToStringBuilder.reflectionToString(formProperty,ToStringStyle.JSON_STYLE)));
//下面的方法需要获取taskId,taskId需要流程启动以后才能获取,而不是流程文件中的userTask的id,所以下面的获取方式是错误的
//TaskFormData taskFormData = formService.getTaskFormData("test");//错误,会报错
//启动流程并获取taskId
//启动方式一
// ProcessInstance processInstance = rule.getRuntimeService().startProcessInstanceByKey("myProcess");
//启动方式二
Map properties = Maps.newHashMap();
properties.put("message","my test message" );
ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), properties);
Task task =rule.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
TaskFormData taskFormData = formService.getTaskFormData(task.getId());
List taskFormProperties = taskFormData.getFormProperties();
taskFormProperties.forEach(taskFormProperty->
LOGGER.info("taskFormProperty = {}", ToStringBuilder.reflectionToString(taskFormProperty,ToStringStyle.JSON_STYLE)));
Map properties1 =Maps.newHashMap();
properties1.put("yesORno","yes" );
formService.submitTaskFormData(task.getId(),properties1 );
Task task1 =rule.getTaskService().createTaskQuery().taskId(task.getId()).singleResult();
LOGGER.info("task1 = {}",task1 );
}
}