|
|
@@ -7,10 +7,12 @@ import cc.smtweb.framework.core.cache.CacheManager; |
|
|
|
import cc.smtweb.framework.core.common.SwEnum; |
|
|
|
import cc.smtweb.framework.core.mvc.variable.SwVariableFactory; |
|
|
|
import cc.smtweb.framework.core.session.UserSession; |
|
|
|
import cc.smtweb.framework.core.util.CommUtil; |
|
|
|
import cc.smtweb.framework.core.util.JsonUtil; |
|
|
|
import cc.smtweb.framework.core.util.MapUtil; |
|
|
|
import cc.smtweb.system.bpm.web.design.form.define.PageDataSet; |
|
|
|
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetField; |
|
|
|
import cc.smtweb.system.bpm.web.design.form.define.PageDatasetFilter; |
|
|
|
import cc.smtweb.system.bpm.web.design.form.define.PageModel; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
@@ -29,6 +31,7 @@ import java.util.*; |
|
|
|
public class ModelFormHelper { |
|
|
|
/** |
|
|
|
* 从缓存获取Form对象 |
|
|
|
* |
|
|
|
* @param formId |
|
|
|
* @return |
|
|
|
*/ |
|
|
@@ -48,7 +51,7 @@ public class ModelFormHelper { |
|
|
|
if (StringUtils.isEmpty(jsonStr)) return null; |
|
|
|
PageDataSet[] list = JsonUtil.parse(jsonStr, PageDataSet[].class); |
|
|
|
Map<String, PageDataSet> map = new LinkedHashMap<>(list.length); |
|
|
|
for (PageDataSet ds: list) { |
|
|
|
for (PageDataSet ds : list) { |
|
|
|
map.put(ds.name, ds); |
|
|
|
ds.resetFields(); |
|
|
|
} |
|
|
@@ -57,6 +60,7 @@ public class ModelFormHelper { |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据json字符串解析对象 |
|
|
|
* |
|
|
|
* @param jsonStr |
|
|
|
* @return |
|
|
|
*/ |
|
|
@@ -75,34 +79,32 @@ public class ModelFormHelper { |
|
|
|
* 主要处理内容: |
|
|
|
* 1、model的一些maxlen、默认值等信息,如果和数据库一致,则去掉 |
|
|
|
* 2、db中的只保留表、字段和别名 |
|
|
|
* |
|
|
|
* @param form |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String buildSaveModel(ModelForm form) { |
|
|
|
Map<String, PageDataSet> mapDataset = parsePageDataset(form.getDataset()); |
|
|
|
PageModel pageInfo = parsePageInfo(form.getContent()); |
|
|
|
for (Map<String, Object> model: pageInfo.model) { |
|
|
|
Map<String, Object> props = (Map<String, Object>)model.get("props"); |
|
|
|
buildSaveModelFields(mapDataset, pageInfo, props, true); |
|
|
|
buildSaveModelFields(mapDataset, pageInfo, props, false); |
|
|
|
for (Map<String, Object> model : pageInfo.model) { |
|
|
|
|
|
|
|
buildSaveModelFields(mapDataset, pageInfo, model, true); |
|
|
|
buildSaveModelFields(mapDataset, pageInfo, model, false); |
|
|
|
} |
|
|
|
return JsonUtil.encodeString(pageInfo); |
|
|
|
} |
|
|
|
|
|
|
|
//处理model的fields和filters |
|
|
|
private static void buildSaveModelFields(Map<String, PageDataSet> mapDataset, PageModel pageInfo, Map<String, Object> props, boolean isField) { |
|
|
|
String db = (String)props.get("db"); |
|
|
|
private static void buildSaveModelFields(Map<String, PageDataSet> mapDataset, PageModel pageInfo, Map<String, Object> model, boolean isField) { |
|
|
|
String db = (String) model.get("dataset"); |
|
|
|
//没有配置db,配置有误,不处理 |
|
|
|
if (StringUtils.isEmpty(db)) throw new SwException("model未配置数据集db,无法解析!"); |
|
|
|
PageDataSet pds = mapDataset.get(db); |
|
|
|
String key = isField ? "fields":"filters"; |
|
|
|
List<Map<String, Object>> fields = (List<Map<String, Object>>)props.get(key); |
|
|
|
String key = isField ? "fields" : "filters"; |
|
|
|
List<Map<String, Object>> fields = (List<Map<String, Object>>) model.get(key); |
|
|
|
if (fields == null || fields.isEmpty()) return; |
|
|
|
for (Map<String, Object> fi: fields) { |
|
|
|
Map<String, Object> field = (Map<String, Object>)fi.get("props"); |
|
|
|
if (field == null) throw new SwException("model[" + db + "]." + key + "未配置props,无法解析!"); |
|
|
|
|
|
|
|
String fn = (String)field.get("field"); |
|
|
|
for (Map<String, Object> field : fields) { |
|
|
|
String fn = (String) field.get("field"); |
|
|
|
if (StringUtils.isEmpty(fn)) throw new SwException("model[" + db + "]." + key + "未配置字段名field,无法解析!"); |
|
|
|
PageDatasetField pdf = isField ? pds.getField(fn) : pds.getFilter(fn); |
|
|
|
if (pdf == null) throw new SwException("model[" + db + "]." + key + "未找到定义的数据集字段(" + fn + "),无法解析!"); |
|
|
@@ -112,7 +114,7 @@ public class ModelFormHelper { |
|
|
|
field.remove("required"); |
|
|
|
} |
|
|
|
|
|
|
|
if (MapUtil.readInt(field,"maxlength") == SwEnum.DataType.instance.getByValue(pdf.dataType).dataLength) { |
|
|
|
if (MapUtil.readInt(field, "maxlength") == SwEnum.DataType.instance.getByValue(pdf.dataType).dataLength) { |
|
|
|
field.remove("maxlength"); |
|
|
|
} |
|
|
|
} |
|
|
@@ -128,8 +130,10 @@ public class ModelFormHelper { |
|
|
|
throw new SwException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 构建待保存的数据集 |
|
|
|
* |
|
|
|
* @param datasets |
|
|
|
* @return |
|
|
|
* @throws JsonProcessingException |
|
|
@@ -137,17 +141,19 @@ public class ModelFormHelper { |
|
|
|
public static String buildSaveDataset(Collection<PageDataSet> datasets) throws JsonProcessingException { |
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
FilterProvider filters = new SimpleFilterProvider().addFilter("datasetField", new SimpleBeanPropertyFilter(){ |
|
|
|
FilterProvider filters = new SimpleFilterProvider().addFilter("datasetField", new SimpleBeanPropertyFilter() { |
|
|
|
private final String includes = "name,label,field,table"; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected boolean include(PropertyWriter writer) { |
|
|
|
return includes.contains(writer.getName()); |
|
|
|
} |
|
|
|
}).addFilter("datasetFilter", new SimpleBeanPropertyFilter(){ |
|
|
|
}).addFilter("datasetFilter", new SimpleBeanPropertyFilter() { |
|
|
|
private final String includes = ",name,label,field,table,dataType,type,linkDb,linkField,value"; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected boolean include(PropertyWriter writer) { |
|
|
|
return includes.contains(","+ writer.getName()); |
|
|
|
return includes.contains("," + writer.getName()); |
|
|
|
} |
|
|
|
}); |
|
|
|
return mapper.writer(filters).writeValueAsString(datasets); |
|
|
@@ -155,38 +161,34 @@ public class ModelFormHelper { |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据数据库存储的信息,加工处理成页面要的,与buildsave逆向 |
|
|
|
* |
|
|
|
* @param form |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String buildReqModel(ModelForm form) { |
|
|
|
Map<String, PageDataSet> mapDataset = parsePageDataset(form.getDataset()); |
|
|
|
PageModel pageInfo = parsePageInfo(form.getContent()); |
|
|
|
for (Map<String, Object> model: pageInfo.model) { |
|
|
|
Map<String, Object> props = (Map<String, Object>)model.get("props"); |
|
|
|
buildReqModelFields(mapDataset, pageInfo, props, true); |
|
|
|
buildReqModelFields(mapDataset, pageInfo, props, false); |
|
|
|
for (Map<String, Object> model : pageInfo.model) { |
|
|
|
String db = (String) model.get("dataset"); |
|
|
|
//没有配置db,配置有误,不处理 |
|
|
|
if (StringUtils.isEmpty(db)) continue; |
|
|
|
PageDataSet dataSet = mapDataset.get(db); |
|
|
|
if (dataSet == null) continue; |
|
|
|
buildReqModelFields(dataSet, model, true); |
|
|
|
buildReqModelFields(dataSet, model, false); |
|
|
|
} |
|
|
|
return JsonUtil.encodeString(pageInfo); |
|
|
|
} |
|
|
|
|
|
|
|
//处理model的fields和filters |
|
|
|
private static void buildReqModelFields(Map<String, PageDataSet> mapDataset, PageModel pageInfo, Map<String, Object> props, boolean isField) { |
|
|
|
String db = (String)props.get("db"); |
|
|
|
//没有配置db,配置有误,不处理 |
|
|
|
if (StringUtils.isEmpty(db)) return; |
|
|
|
PageDataSet pds = mapDataset.get(db); |
|
|
|
if (pds == null) return; |
|
|
|
|
|
|
|
String key = isField ? "fields":"filters"; |
|
|
|
List<Map<String, Object>> fields = (List<Map<String, Object>>)props.get(key); |
|
|
|
private static void buildReqModelFields(PageDataSet dataSet, Map<String, Object> model, boolean isField) { |
|
|
|
String key = isField ? "fields" : "filters"; |
|
|
|
List<Map<String, Object>> fields = (List<Map<String, Object>>) model.get(key); |
|
|
|
if (fields == null || fields.isEmpty()) return; |
|
|
|
for (Map<String, Object> fi: fields) { |
|
|
|
Map<String, Object> field = (Map<String, Object>)fi.get("props"); |
|
|
|
if (field == null) continue; |
|
|
|
|
|
|
|
String fn = (String)field.get("field"); |
|
|
|
for (Map<String, Object> field : fields) { |
|
|
|
String fn = (String) field.get("field"); |
|
|
|
if (StringUtils.isEmpty(fn)) continue; |
|
|
|
PageDatasetField pdf = isField ? pds.getField(fn) : pds.getFilter(fn); |
|
|
|
PageDatasetField pdf = isField ? dataSet.getField(fn) : dataSet.getFilter(fn); |
|
|
|
if (pdf == null) continue; |
|
|
|
|
|
|
|
//必填字段 |
|
|
@@ -202,6 +204,7 @@ public class ModelFormHelper { |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据数据库存储的信息,加工处理成页面要的,与buildsave逆向 |
|
|
|
* |
|
|
|
* @param form |
|
|
|
* @return |
|
|
|
*/ |
|
|
@@ -217,9 +220,8 @@ public class ModelFormHelper { |
|
|
|
List<SwMap> listModel = new ArrayList<>(); |
|
|
|
ret.put("model", listModel); |
|
|
|
//构建Model |
|
|
|
for (Map<String, Object> model: pageInfo.model) { |
|
|
|
Map<String, Object> props = (Map<String, Object>)model.get("props"); |
|
|
|
String db = (String)props.get("db"); |
|
|
|
for (Map<String, Object> model : pageInfo.model) { |
|
|
|
String db = (String) model.get("dataset"); |
|
|
|
PageDataSet dataSet = mapDataset.get(db); |
|
|
|
if (dataSet == null) throw new SwException("未找到指定的数据集定义!" + db); |
|
|
|
|
|
|
@@ -240,37 +242,13 @@ public class ModelFormHelper { |
|
|
|
ret.put("lazy", dataSet.lazy); |
|
|
|
ret.put("canEdit", dataSet.canEdit); |
|
|
|
|
|
|
|
ret.put("fields", buildEngineFields(dataSet, (List<Map<String, Object>>)model.get("fields"), true)); |
|
|
|
ret.put("filters", buildEngineFields(dataSet, (List<Map<String, Object>>)model.get("filters"), false)); |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
//构建fields和filters数组 |
|
|
|
private static List<SwMap> buildEngineFields(PageDataSet dataSet, List<Map<String, Object>> fields, boolean isField) { |
|
|
|
List<SwMap> listRet = new ArrayList<>(); |
|
|
|
for (Map<String, Object> field: fields) { |
|
|
|
SwMap fr = new SwMap(); |
|
|
|
fr.put("id", field.get("id")); |
|
|
|
Map<String, Object> props = (Map<String, Object>)field.get("props"); |
|
|
|
if (props == null) continue; |
|
|
|
fr.putAll(props); |
|
|
|
|
|
|
|
String fn = (String)fr.get("field"); |
|
|
|
if (StringUtils.isEmpty(fn)) continue; |
|
|
|
PageDatasetField pdf = isField ? dataSet.getField(fn) : dataSet.getFilter(fn); |
|
|
|
if (pdf == null) continue; |
|
|
|
ret.put("fields", model.get("fields")); |
|
|
|
ret.put("filters", model.get("filters")); |
|
|
|
|
|
|
|
//必填字段 |
|
|
|
if (!fr.containsKey("required")) { |
|
|
|
fr.put("required", pdf.isNotNull()); |
|
|
|
} |
|
|
|
buildReqModelFields(dataSet, model, true); |
|
|
|
buildReqModelFields(dataSet, model, false); |
|
|
|
|
|
|
|
if (!fr.containsKey("maxlength")) { |
|
|
|
fr.put("maxlength", SwEnum.DataType.instance.getByValue(pdf.dataType).dataLength); |
|
|
|
} |
|
|
|
} |
|
|
|
return listRet; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
//根据option构建Extra部分 |
|
|
@@ -281,8 +259,8 @@ public class ModelFormHelper { |
|
|
|
SwMap widget = new SwMap(); |
|
|
|
map.put("widget", widget); |
|
|
|
|
|
|
|
long[] widgetIds = (long[])option.get("widgetRef"); |
|
|
|
for (long id: widgetIds) { |
|
|
|
long[] widgetIds = (long[]) option.get("widgetRef"); |
|
|
|
for (long id : widgetIds) { |
|
|
|
ModelForm widgetForm = getFromCache(id); |
|
|
|
if (widgetForm == null) throw new SwException("没有找到指定的控件定义!id=" + id); |
|
|
|
SwMap widgetOpts = parseFormOption(widgetForm.getOption()); |
|
|
@@ -293,10 +271,29 @@ public class ModelFormHelper { |
|
|
|
//构建变量 |
|
|
|
SwMap mapVar = new SwMap(); |
|
|
|
map.put("var", mapVar); |
|
|
|
String[] vars = (String[])option.get("vars"); |
|
|
|
for (String var: vars) { |
|
|
|
String[] vars = (String[]) option.get("vars"); |
|
|
|
for (String var : vars) { |
|
|
|
mapVar.put(var, SwVariableFactory.getInstance().calcVar(var, params, us)); |
|
|
|
} |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
//获取控件的filter信息 |
|
|
|
public static List<SwMap> buildWidgetFilter(ModelForm bean) { |
|
|
|
List<SwMap> listRet = new ArrayList<>(); |
|
|
|
Map<String, PageDataSet> map = ModelFormHelper.parsePageDataset(bean.getDataset()); |
|
|
|
if (map == null) return listRet; |
|
|
|
for (PageDataSet ds: map.values()) { |
|
|
|
for (PageDatasetFilter filter: ds.filters) { |
|
|
|
if (SwEnum.FilterType.INPUT.value.equals(filter.type) || SwEnum.FilterType.PARAM.value.equals(filter.type)) { |
|
|
|
SwMap fret = new SwMap(); |
|
|
|
fret.put("name", filter.name); |
|
|
|
fret.put("label", filter.label); |
|
|
|
listRet.add(fret); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
listRet.sort((o1, o2) -> CommUtil.chineseCompare(o1.readString("label"), o2.readString("label"))); |
|
|
|
return listRet; |
|
|
|
} |
|
|
|
} |