Activiti服务类- FormService服务类
阅读原文时间:2023年07月09日阅读:3

转自:https://www.cnblogs.com/liuqing576598117/p/9814953.html

1.获取
//通过流程定义ID获取表单字段集合
StartFormData startFormData = formService.getStartFormData(processDefinitionId);

TaskFormData taskFormData = formService.getTaskFormData(taskId);//内置表单(直接在流程中定义)
Object renderedTaskForm = formService.getRenderedTaskForm(taskId);//这个方法是返回一个纯文本的(外置表单(一个.form结尾的文件,),可以是一个div标签)
外置表单判断:
流程启动时:boolean hasStartFormKey = processDefinition.hasStartFormKey();
流程中:TaskFormData taskFormData = formService.getTaskFormData(taskId);
boolean key = taskFormData.getFormKey()

2.任务签收
taskService.claim(taskId, userId);

//数据展示

TaskFormData taskFormData = formService.getTaskFormData(taskId);//内置表单(直接在流程中定义)
if (taskFormData.getFormKey() != null) {
  Object renderedTaskForm = formService.getRenderedTaskForm(taskId);//这个方法是返回一个纯文本的(外置表单(一个.form结尾的文件,),可以是一个div标签)
  Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
  mav.addObject("task", task);
  mav.addObject("taskFormData", renderedTaskForm);
  mav.addObject("hasFormKey", true);
} else {
  mav.addObject("taskFormData", taskFormData);
}

3.自定义表单引擎

/**
* Javascript表单字段
*
*/
public class JavascriptFormType extends AbstractFormType {

@Override  
public String getName() {  
    return "javascript";  
}

@Override  
public Object convertFormValueToModelValue(String propertyValue) {  
    return propertyValue;  
}

@Override  
public String convertModelValueToFormValue(Object modelValue) {  
    return (String) modelValue;  
}  

}

/**
* 自定义表单引擎
*
*/
public class MyFormEngine implements FormEngine {

@Override  
public String getName() {  
    return "myformengine";  
}

@Override  
public Object renderStartForm(StartFormData startForm) {  
    javax.swing.JButton jButton = new javax.swing.JButton();  
    jButton.setName("My Start Form Button");  
    return jButton;  
}

@Override  
public Object renderTaskForm(TaskFormData taskForm) {  
    javax.swing.JButton jButton = new javax.swing.JButton();  
    jButton.setName("My Task Form Button");  
    return jButton;  
}

自定义表单引擎 : public class MyFormEngine implements FormEngine (供C/S架构使用,eg:Swing,AWT,SWT)
Javascript表单字段: public class JavascriptFormType extends AbstractFormType








  


  

5. 提交表单字段并启动一个新的流程实例

//从请求中获取表单数据,然后启动流程
Map formValues = new HashMap();
if (hasStartFormKey) { // formkey表单
  Map parameterMap = request.getParameterMap();
  Set> entrySet = parameterMap.entrySet();
  for (Entry entry : entrySet) {
    String key = entry.getKey();
    formValues.put(key, entry.getValue()[0]);
  }
} else { // 动态表单
  // 先读取表单字段在根据表单字段的ID读取请求参数值
  StartFormData formData = formService.getStartFormData(pdid);

  // 从请求中获取表单字段的值
  List formProperties = formData.getFormProperties();
  for (FormProperty formProperty : formProperties) {
    String value = request.getParameter(formProperty.getId());
    formValues.put(formProperty.getId(), value);
  }
}
ProcessInstance processInstance = formService.submitStartFormData(pdid, formValues