feat(REQ-3340) - 优化解析表单实例数据的逻辑

This commit is contained in:
wangli 2025-02-11 17:13:49 +08:00
parent 475759a9cc
commit e5a92af9ec

View File

@ -298,61 +298,26 @@ public class CustomGetFormInstanceModelCmd extends GetFormInstanceModelCmd {
} }
} else if (FORM_FIELD_TYPE_CUSTOM_COMPONENT.equals(field.getType())) { } else if (FORM_FIELD_TYPE_CUSTOM_COMPONENT.equals(field.getType())) {
String listJson = (String) variables.get(field.getId()); String listJson = (String) variables.get(field.getId());
ObjectMapper objectMapper = formEngineConfiguration.getObjectMapper(); if (listJson != null) {
try { ObjectMapper objectMapper = formEngineConfiguration.getObjectMapper();
List<Map> rows = objectMapper.readValue(listJson, new TypeReference<List<Map>>() { try {
}); List<Map> rows = objectMapper.readValue(listJson, new TypeReference<List<Map>>() {
if (!CollectionUtils.isEmpty(rows)) { });
List<List<FormField>> fields = ((FormContainer) field).getFields(); if (!CollectionUtils.isEmpty(rows)) {
if (rows.size() == 1) { List<List<FormField>> fields = ((FormContainer) field).getFields();
fields.get(0).forEach(sf -> { if (rows.size() == 1) {
if (FORM_FIELD_TYPE_INPUT.equals(sf.getType()) fields.get(0).forEach(sf -> {
|| FORM_FIELD_TYPE_TEXTAREA.equals(sf.getType())) {
if (rows.get(0).containsKey(sf.getId())) {
sf.setValue(rows.get(0).get(sf.getId()));
}
} else if (FormFieldTypes.UPLOAD.equals(sf.getType())
|| FORM_FIELD_TYPE_IMAGE.equals(sf.getType())) {
if (rows.get(0).containsKey(sf.getId())) {
try {
String updateValueJson = objectMapper.writeValueAsString(rows.get(0).get(sf.getId()));
if (updateValueJson != null) {
List<UploadFieldDTO> uploadFiles = formEngineConfiguration.getObjectMapper()
.readValue(updateValueJson, new TypeReference<List<UploadFieldDTO>>() {
});
sf.setValue(uploadFiles);
}
} catch (JsonProcessingException e) {
throw new FlowableException("Error parsing upload files json ", e);
}
}
}
});
} else {
// 根据传入的表单自定义组件数据构建表单模型Field 是二维数组一维用于行二维用于列
for (int i = 1; i < rows.size(); i++) {
List<FormField> newRow = new ArrayList<>();
fields.get(0).forEach(e -> {
newRow.add(FormFieldClone.clone(e));
});
fields.add(newRow);
}
// 为每行赋值
for (int i = 0; i < rows.size(); i++) {
Map<String, Object> rowMap = rows.get(i);
fields.get(i).forEach(sf -> {
if (FORM_FIELD_TYPE_INPUT.equals(sf.getType()) if (FORM_FIELD_TYPE_INPUT.equals(sf.getType())
|| FORM_FIELD_TYPE_TEXTAREA.equals(sf.getType())) { || FORM_FIELD_TYPE_TEXTAREA.equals(sf.getType())) {
if (rowMap.containsKey(sf.getId())) { if (rows.get(0).containsKey(sf.getId())) {
sf.setValue(rowMap.get(sf.getId())); sf.setValue(rows.get(0).get(sf.getId()));
} }
} else if (FormFieldTypes.UPLOAD.equals(sf.getType()) } else if (FormFieldTypes.UPLOAD.equals(sf.getType())
|| FORM_FIELD_TYPE_IMAGE.equals(sf.getType())) { || FORM_FIELD_TYPE_IMAGE.equals(sf.getType())) {
if (rowMap.containsKey(sf.getId())) { if (rows.get(0).containsKey(sf.getId())) {
try { try {
String updateValueJson = objectMapper.writeValueAsString(rowMap.get(sf.getId())); String updateValueJson = objectMapper.writeValueAsString(rows.get(0).get(sf.getId()));
if (updateValueJson != null) { if (updateValueJson != null) {
List<UploadFieldDTO> uploadFiles = formEngineConfiguration.getObjectMapper() List<UploadFieldDTO> uploadFiles = formEngineConfiguration.getObjectMapper()
.readValue(updateValueJson, new TypeReference<List<UploadFieldDTO>>() { .readValue(updateValueJson, new TypeReference<List<UploadFieldDTO>>() {
@ -362,14 +327,51 @@ public class CustomGetFormInstanceModelCmd extends GetFormInstanceModelCmd {
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new FlowableException("Error parsing upload files json ", e); throw new FlowableException("Error parsing upload files json ", e);
} }
} }
} }
}); });
} else {
// 根据传入的表单自定义组件数据构建表单模型Field 是二维数组一维用于行二维用于列
for (int i = 1; i < rows.size(); i++) {
List<FormField> newRow = new ArrayList<>();
fields.get(0).forEach(e -> {
newRow.add(FormFieldClone.clone(e));
});
fields.add(newRow);
}
// 为每行赋值
for (int i = 0; i < rows.size(); i++) {
Map<String, Object> rowMap = rows.get(i);
fields.get(i).forEach(sf -> {
if (FORM_FIELD_TYPE_INPUT.equals(sf.getType())
|| FORM_FIELD_TYPE_TEXTAREA.equals(sf.getType())) {
if (rowMap.containsKey(sf.getId())) {
sf.setValue(rowMap.get(sf.getId()));
}
} else if (FormFieldTypes.UPLOAD.equals(sf.getType())
|| FORM_FIELD_TYPE_IMAGE.equals(sf.getType())) {
if (rowMap.containsKey(sf.getId())) {
try {
String updateValueJson = objectMapper.writeValueAsString(rowMap.get(sf.getId()));
if (updateValueJson != null) {
List<UploadFieldDTO> uploadFiles = formEngineConfiguration.getObjectMapper()
.readValue(updateValueJson, new TypeReference<List<UploadFieldDTO>>() {
});
sf.setValue(uploadFiles);
}
} catch (JsonProcessingException e) {
throw new FlowableException("Error parsing upload files json ", e);
}
}
}
});
}
} }
} }
} catch (JsonProcessingException e) {
throw new WorkflowEngineException(FORM_DATA_PARSE_ERROR_BY_CUSTOM_COMPONENT);
} }
} catch (JsonProcessingException e) {
throw new WorkflowEngineException(FORM_DATA_PARSE_ERROR_BY_CUSTOM_COMPONENT);
} }
} else { } else {
Object variableValue = variables.get(field.getId()); Object variableValue = variables.get(field.getId());