package cn.autoform.web.formula.info.impl;
|
|
import java.lang.reflect.InvocationTargetException;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
|
import org.apache.commons.beanutils.PropertyUtils;
|
import org.springframework.ui.ModelMap;
|
|
import cn.autoform.bean.form.CpnType;
|
import cn.autoform.util.tool.JSONTool;
|
import cn.autoform.web.formula.info.Form;
|
import cn.autoform.web.formula.info.FormCpn;
|
import cn.autoform.web.formula.info.FormListener;
|
import cn.autoform.web.formula.info.FormulaInfo;
|
import cn.autoform.web.formula.info.impl.cpn.ChildCpn;
|
import cn.autoform.web.formula.info.impl.cpn.FormCpnFactory;
|
import cn.autoform.web.formula.prop.Attribute;
|
import cn.autoform.web.formula.prop.CpnMsg;
|
import cn.autoform.web.formula.prop.MsgType;
|
import lombok.Getter;
|
import lombok.extern.slf4j.Slf4j;
|
|
@Slf4j
|
public class SimpleForm extends ModelMap implements Form{
|
|
/**
|
*
|
*/
|
private static final long serialVersionUID = -2680390286555343976L;
|
|
/* 表单监听 */
|
private FormListener formListener;
|
|
private List<FormulaInfo> allFormulaInfos;
|
|
/* 公式触发事件 */
|
private List<FormulaInfo> formulaInfos;
|
|
/* 表单控件集合 */
|
private Map<String, FormCpn> formCpns;
|
|
/* 表单提示信息 */
|
@Getter
|
private List<Map<MsgType, String>> msgs;
|
|
/* 记录公式执行次数 */
|
private Map<FormCpn, Integer> recordListener;
|
|
/* 表单数据 */
|
private Map<String, Object> data;
|
|
/**
|
* 表单加载-初始化
|
*/
|
@Override
|
public void loading() {
|
recordListener = new HashMap<>();
|
// 监听
|
if(this.formListener == null)
|
this.formListener = new SimpleFormListener();
|
// 触发事件
|
if(this.allFormulaInfos == null)
|
this.allFormulaInfos = Collections.emptyList();
|
// 初始化数据
|
initData();
|
// 初始化表单公式
|
initFormulas();
|
// 表单初始化完后触发监听
|
this.formListener.loading(this);
|
}
|
|
/**
|
* 初始化数据
|
*/
|
protected void initData() {
|
if(this.data == null)
|
return;
|
FormCpn cpn = null;
|
for(Map.Entry<String, Object> entry : this.data.entrySet()){
|
cpn = (FormCpn) this.get(entry.getKey());
|
if(cpn == null)
|
break;
|
if(cpn.getCpnType() == CpnType.subform)
|
setChildCpnVal((ChildCpn) cpn, entry);
|
else
|
setCpnVal(cpn, entry);
|
}
|
}
|
|
protected void setChildCpnVal(ChildCpn childCpn, Map.Entry<String, Object> entry) {
|
if(!(entry.getValue() instanceof List))
|
return;
|
List<Map<String, Object>> values = (List<Map<String, Object>>) entry.getValue();
|
int index = 0;
|
FormCpn cloneCpn = null;
|
for(Map<String, Object> val : values){
|
for(Map.Entry<String, Object> valEntry : val.entrySet()){
|
FormCpn cpn = getFormCpn(childCpn.getFieldId()+"."+valEntry.getKey());
|
if(cpn == null)
|
break;
|
setCpnVal(cloneCpn = FormCpnFactory.clone(cpn), valEntry);
|
childCpn.put(FormCpn.CHILD_CPN_VAL, cloneCpn, index);
|
}
|
index ++;
|
}
|
}
|
|
protected void setCpnVal(FormCpn cpn, Map.Entry<String, Object> entry) {
|
cpn.put(Attribute.value, entry.getValue());
|
}
|
|
@Override
|
public void commit() {
|
this.formListener.commit(this);
|
}
|
|
/**
|
* 初始化表单公式
|
*/
|
protected void initFormulas(){
|
System.err.println(JSONTool.toJson(this.allFormulaInfos));
|
this.allFormulaInfos.stream().forEach(this::eachFormula);
|
}
|
|
|
@Override
|
public Object get(Object key) {
|
Object val = formCpns.get(key);
|
if(val != null)
|
return val;
|
return super.get(key);
|
}
|
|
/**
|
* 循环每个公式
|
* @param formula
|
*/
|
protected void eachFormula(FormulaInfo formula) {
|
System.err.println(JSONTool.toJson(formula));
|
|
String fieldName = formula.getEvent().getEventField();
|
if(Objects.equals(FORM_KEY, fieldName)){
|
if(this.formulaInfos == null)
|
this.formulaInfos = new ArrayList<>();
|
this.formulaInfos.add(formula);
|
return;
|
}
|
FormCpn cpn = getFormCpn(fieldName);
|
if(cpn != null)
|
cpn.put(FormCpn.FORMULA_KEY, formula);
|
}
|
|
protected FormCpn getFormCpn(String fieldName) {
|
try {
|
if(fieldName.indexOf('.') <= 0)
|
return (FormCpn) this.get(fieldName);
|
else
|
return (FormCpn) PropertyUtils.getProperty(this, fieldName);
|
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
log.error(e.getMessage(), e);
|
return null;
|
}
|
}
|
|
@Override
|
public void setAllFormulas(List<FormulaInfo> allFormulaInfos) {
|
this.allFormulaInfos = allFormulaInfos;
|
}
|
|
@Override
|
public void setFormListener(FormListener formListener) {
|
this.formListener = formListener;
|
}
|
|
@Override
|
public void addFormCpn(FormCpn formCpn) {
|
if(this.formCpns == null)
|
this.formCpns = new HashMap<>();
|
// 为控件添加表单引用
|
formCpn.put(FormCpn.FORM_KEY, this);
|
// 加入表单集合
|
this.formCpns.put(formCpn.getFieldId(), formCpn);
|
}
|
|
@Override
|
public void setMsg(MsgType key, String value, FormCpn formCpn) {
|
if(this.msgs == null)
|
this.msgs = new ArrayList<>();
|
Map<MsgType, String> map = new HashMap<>();
|
map.put(key, value);
|
this.msgs.add(map);
|
}
|
|
@Override
|
public List<FormulaInfo> getFormulas() {
|
return formulaInfos;
|
}
|
|
@Override
|
public boolean triggerEvent(FormCpn formCpn) {
|
Integer sum = recordListener.get(formCpn);
|
if(sum == null)
|
sum = 0;
|
else
|
if(sum > 1)
|
return false;
|
recordListener.put(formCpn, ++sum);
|
return true;
|
}
|
|
@Override
|
public void setData(Map<String, Object> data) {
|
this.data = data;
|
}
|
|
@Override
|
public Map<String, Object> getData() {
|
Map<String, Object> newData = new HashMap<>();
|
for(Map.Entry<String, FormCpn> entry : this.formCpns.entrySet()){
|
newData.put(entry.getKey(), entry.getValue().getValue());
|
}
|
return newData;
|
}
|
|
@Override
|
public Map<String, Object> getCpnMsgs() {
|
Map<String, Object> cpnMsg = new HashMap<>();
|
for(Map.Entry<String, FormCpn> entry : this.formCpns.entrySet()){
|
Object obj = entry.getValue().get(CpnMsg.error);
|
if(obj != null)
|
cpnMsg.put(entry.getKey(), obj);
|
}
|
return cpnMsg;
|
}
|
|
}
|