Activiti6.0流程引擎学习——(24)activiti的表单管理服务(IdentityService)

FormService:

1、解析流程定义中表单项的配置;

2、提交表单的方式驱动用户节点流转,FormService可以通过submit表单方式将流程驱动;

3、获取自定义外部表单Key。

 

基于流程演示FormService功能:

一、创建FormServiceTest测试类与测试流程文件

首先在coreapi中创建FormServiceTest测试类:

Activiti6.0流程引擎学习——(24)activiti的表单管理服务(IdentityService)_第1张图片

然后在resources中创建my-process-form.bpmn20.xml流程文件:

Activiti6.0流程引擎学习——(24)activiti的表单管理服务(IdentityService)_第2张图片

 

二、在form表单配置文件中创建测试流程





	

		   
			     
				   
			
		
		
		
		
			
				
			
		
		

		

	

首先配置表单项,在startEvent中的扩展属性extensionElements中设计表单,这里就填写了一个消息。然后在userTask 中设计了一个审批结果的表单。

 

三、然后对表单管理服务进行测试

对FormServiceTest测试类添加测试方法:

package com.jjf.activiti.coreapi;

import com.google.common.collect.Maps;
import org.activiti.engine.FormService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.test.Deployment;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表单管理服务测试
 */
public class FormServiceTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(FormServiceTest.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule();

    @Test
    @Deployment(resources = {"my-process-form.bpmn20.xml"})
    public void testFormService(){
        FormService formService = activitiRule.getFormService();     //获取 FormService
        ProcessDefinition processDefinition = activitiRule.getRepositoryService()
                .createProcessDefinitionQuery().singleResult();   //流程定义对象

        String startFormKey = formService.getStartFormKey(processDefinition.getId());
        LOGGER.info("startFormKey = [{}]", startFormKey);   //[/rest/process/form/start]  配制文件中 formKey 参数值

        StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
        List formProperties = startFormData.getFormProperties();   //获得表单列表
        for (FormProperty formProperty:formProperties){
            LOGGER.info("formProperty = [{}]" , ToStringBuilder.reflectionToString(formProperty,ToStringStyle.JSON_STYLE));
        }

        Map properties = Maps.newHashMap();
        properties.put("message","test message");
        ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), properties);  //根据formService启动流程

        Task task = activitiRule.getTaskService().createTaskQuery().singleResult();    //获取唯一的Task

        TaskFormData taskFormData = formService.getTaskFormData(task.getId());     //获取task表单
        List taskFormDataFormProperties = taskFormData.getFormProperties();   //获取属性列表
        for (FormProperty property:taskFormDataFormProperties){
            LOGGER.info("property = [{}]" , ToStringBuilder.reflectionToString(property,ToStringStyle.JSON_STYLE));
        }

        HashMap yesORno = Maps.newHashMap();
        yesORno.put("yesORno","yes");
        formService.submitTaskFormData(task.getId(),yesORno);
        Task task1 = activitiRule.getTaskService().createTaskQuery().taskId(task.getId()).singleResult();
        LOGGER.info("task1 = [{}]" , task1);
    }
}

输出测试结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
startFormKey = [/rest/process/form/start]
formProperty = [{"id":"message","name":"消息","type":"org.activiti.engine.impl.form.StringFormType@10b892d5","isRequired":true,"isReadable":true,"isWritable":true,"value":null}]
property = [{"id":"yesORno","name":"审批结果","type":"org.activiti.engine.impl.form.StringFormType@10b892d5","isRequired":true,"isReadable":true,"isWritable":true,"value":null}]
task1 = [null]

Process finished with exit code 0

首先输出了startFormKey,这个值也就是流程定义中的对应的startEvent 中的 activiti:formKey对应的属性,然后输出了startEvent 中activiti:formProperty解析出来的内容,再输出了userTask 中的审批结果列表,最后通过submitTaskFormData方法将task与参数提交表单,整个流程结束。

你可能感兴趣的:(activiti)