1.实战
我们存储一个表单的实际数据,然后到接口中解析保存到数据库中,需要注意的一点是,我们要判断是否是有值的表单,如果有就使用带参数的完成任务否则就不带参数的完成任务:
//动态表单保存
@PostMapping(path = "/formDataSave")
public AjaxResponse formDataSave(String taskId,String formData) {
try {
if(GlobalConfig.Test){
securityUtil.logInAs("bajie");
}
Task task = taskRuntime.task(taskId);
UserTask userTask = (UserTask)repositoryService.getBpmnModel(task.getProcessDefinitionId())
.getFlowElement(task.getFormKey());
if(userTask==null){
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(),"无表单数据");
}
//解析数据
String[] formDataArr = formData.split("!_!");
List<Map<String,Object>> result = new ArrayList<>();
Boolean hasVariables = false;
HashMap<String,Object> variables = new HashMap<>();
for(String data:formDataArr){
String[] splitForm = data.split("-_!");
Map<String,Object> map = new HashMap<>();
map.put("process_def_id",task.getProcessDefinitionId());
map.put("process_ins_id",task.getProcessInstanceId());
map.put("form_key",task.getFormKey());
map.put("control_id",splitForm[0]);
map.put("control_value",splitForm[1]);
// map.put("control_param",splitForm[2]);
variables = new HashMap<>();
switch (splitForm[2]){
case "f":
System.out.println("空间不作为参数");
break;
case "s":
map.put(splitForm[0],splitForm[1]);
hasVariables = true;
break;
case "t":
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");
map.put(splitForm[0],simpleDateFormat.parse(splitForm[1]));
hasVariables = true;
break;
case "b":
map.put(splitForm[0], BooleanUtils.toBoolean(splitForm[1]));
hasVariables = true;
break;
default:
System.out.println("空间ID:"+splitForm[0]+":"+splitForm[2]);
}
result.add(map);
}
if(hasVariables){
//带参数完成任务
taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(taskId).withVariables(variables).build());
}else{
taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(taskId).build());
}
//插入数据
jdbcTemplate.batchUpdate("INSERT INTO school.formdata (PROC_DEF_ID_, PROC_INST_ID_, FORM_KEY_, Control_ID_, Control_VALUE_) VALUES (?,?,?,?,?)",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, String.valueOf(result.get(i).get("process_def_id")));//随机id
ps.setString(2, String.valueOf(result.get(i).get("process_ins_id")));
ps.setString(3, String.valueOf(result.get(i).get("form_key")));
ps.setString(4, String.valueOf(result.get(i).get("control_id")));
ps.setString(5, String.valueOf(result.get(i).get("control_value")));
}
public int getBatchSize() {
return result.size();
}
});
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(),null);
}catch (Exception e){
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.ERROR.getCode(),"保存动态表单失败!",null);
}
}
下面是我实际测试中用的数据和图:
FormProperty-_!不是参数-_!f!_!FormProperty1-_!是参数-_!s
注意:
在之前的帖子中说过在activiti7中全局变量,后面的不会覆盖之前的,也就是说如果有如下的流程:
我们请假6天就会到b,然后b觉得请假多然后就打回,这个时候用户请假的UEL表达式中的day变量改为2,则是不生效的。
这个不生效是针对于taskService.complete方法,如果使用
taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(taskId).withVariables(variables).build());
这种方式则不会覆盖。所以在之后我们尽量使用taskRuntime来完成任务。
2.表单历史数据填充
我们还会遇到这种需求,例如上图中所述,如果八戒表单1中使用了表单并且赋值了,如果在八戒表单2中也想使用怎么办?我们可以在表单字段中使用前一个的表单字段的key,那么在代码中实现,就是我们把formdata表中的数据查出来当做历史数据,然后在当前任务的表单字段中查找,如果有历史key则进行数值的替换。
//渲染动态表单
@GetMapping(path = "/formDataShow")
public AjaxResponse formDataShow(String taskId) {
try {
if (GlobalConfig.Test) {
securityUtil.logInAs("bajie");
}
Task task = taskRuntime.task(taskId);
//构建表单控件历史数据
Map<String, String> controlMap = new HashMap<>();
List<CommonInfo> commonInfos = jdbcTemplate.query("select Control_ID_ as id,Control_VALUE_ as value" +
" from formdata where PROC_INST_ID_='" + task.getProcessInstanceId() + "'", new BeanPropertyRowMapper(CommonInfo.class));
controlMap = commonInfos.stream().collect(Collectors.toMap(CommonInfo::getId, CommonInfo::getValue));
UserTask userTask = (UserTask) repositoryService.getBpmnModel(task.getProcessDefinitionId())
.getFlowElement(task.getFormKey());
if (userTask == null) {
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(), "无表单");
}
List<Map<String, Object>> result = new ArrayList<>();
List<FormProperty> formPropertiesList = userTask.getFormProperties();
for (FormProperty formProperty : formPropertiesList) {
String[] splitForm = formProperty.getId().split("-_!");
Map<String, Object> map = new HashMap<>();
map.put("id", splitForm[0]);
map.put("type", splitForm[1]);
map.put("label", splitForm[2]);
if (splitForm[3].startsWith("FormProperty_")) {
if (controlMap.containsKey(splitForm[3])) {
map.put("defaultValue", controlMap.get(splitForm[3]));
} else {
map.put("defaultValue", "读取失败,检查"+splitForm[0]+"的配置");
}
} else {
map.put("defaultValue", splitForm[3]);
}
map.put("param", splitForm[4]);
result.add(map);
}
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(), result);
} catch (Exception e) {
return AjaxResponse.ajaxData(GlobalConfig.ResponseCode.ERROR.getCode(), "渲染动态表单失败!", null);
}
}