Skip to content
一、FormService
  • 相关API

    image-20200729145918343

  • 解析流程定义中表单项的配置

  • 提交表单的方式驱动用户节点流转

  • 获取自定义外部表单key

二、相关代码演示
  • 相关代码演示

    java
    package com.laogoubi.coreapi;
    
    import com.google.common.collect.Maps;
    import org.activiti.engine.FormService;
    import org.activiti.engine.form.FormProperty;
    import org.activiti.engine.form.StartFormData;
    import org.activiti.engine.form.TaskFormData;
    import org.activiti.engine.repository.ProcessDefinition;
    import org.activiti.engine.runtime.ProcessInstance;
    import org.activiti.engine.task.Task;
    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.List;
    import java.util.Map;
    
    /**
     * @ClassName FormServiceTest
     * @Description FormService
     * @Author eastern
     * @Date 2020/7/3 下午11:35
     * @Version 1.0
     **/
    public class FormServiceTest {
    	private static final Logger logger = LoggerFactory.getLogger(FormServiceTest.class);
    
    	@Rule
    	public ActivitiRule activitiRule = new ActivitiRule();
    
    	@Test
    	@Deployment(resources = {"diagrams/my-process-form.bpmn20.xml"})
    	public void testFromService(){
    		FormService formService = activitiRule.getFormService();
    		ProcessDefinition processDefinition = activitiRule.getRepositoryService().createProcessDefinitionQuery().singleResult();
    		String startFormKey = formService.getStartFormKey(processDefinition.getId());
    		logger.info("startFormKey = {}", startFormKey);
    
    		StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
    		List<FormProperty> formProperties = startFormData.getFormProperties();
    		for (FormProperty formProperty : formProperties) {
    			logger.info("formProperty = {}", ToStringBuilder.reflectionToString(formProperty, ToStringStyle.JSON_STYLE));
    		}
    
    
    		Map<String, String> properties = Maps.newHashMap();
    		properties.put("message", "my test message");
    		ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), properties);
    
    		List<Task> tasks = activitiRule.getTaskService().createTaskQuery().listPage(0, 100);
    		for (Task task : tasks) {
    			logger.info("task.id = {}", task.getId());
    		}
    
    		Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
    
    		TaskFormData taskFormData = formService.getTaskFormData(task.getId());
    		List<FormProperty> taskProperties = taskFormData.getFormProperties();
    		for (FormProperty taskProperty : taskProperties) {
    			logger.info("taskProperty = {}", ToStringBuilder.reflectionToString(taskProperty, ToStringStyle.JSON_STYLE));
    		}
    
    
    		Map<String, String> params = Maps.newHashMap();
    		params.put("yesORno", "yes");
    
    		formService.submitTaskFormData(task.getId(), params);
    
    		logger.info("task.id = {}", task.getId());
    		Task singleResult = activitiRule.getTaskService().createTaskQuery().taskId(task.getId()).singleResult();
    		logger.info("singleResult = {}", singleResult);
    
    	}
    }
  • 资源

    • my-process-form.bpmn20.xml

      xml
      <?xml version="1.0" encoding="UTF-8"?>
      
      <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" typeLanguage="http://www.w3.org/2001/XMLSchema"
      	expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
      
      	<process id="my-process">
      		<startEvent id="start" activiti:formKey="/rest/process/form/start">
      			<extensionElements>
      				<activiti:formProperty id="message" name="信息" type="string" required="true"></activiti:formProperty>
      			</extensionElements>
      		</startEvent>
      		<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" />
      		<userTask id="someTask" name="Activiti is awesome!" activiti:formKey="/rest/process/form/userTask">
      			<extensionElements>
      				<activiti:formProperty id="yesORno" name="审批" type="string" required="true"></activiti:formProperty>
      			</extensionElements>
      		</userTask>
      		<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" />
      		<endEvent id="end" />
      	</process>
      </definitions>

Released under the MIT License.