package cn.autoform.web.formula.info.impl.cpn; import cn.autoform.db.entity.form.FormCpnInfo; import cn.autoform.web.formula.info.Form; import cn.autoform.web.formula.info.FormCpn; import cn.autoform.web.formula.info.FormCpnListener; import cn.autoform.web.formula.info.FormulaInfo; import cn.autoform.web.formula.prop.Attribute; import cn.autoform.web.formula.prop.CpnMsg; import cn.autoform.web.formula.prop.MsgType; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; import java.util.*; /** * 普通控件抽象类 * @author WangYX * */ @EqualsAndHashCode(callSuper=false, of={"fieldId", "index"}) public abstract class AbstractBaseFormCpn extends HashMap implements FormCpn{ /** * */ private static final long serialVersionUID = -1159764957350079218L; /** * 映射key */ protected static Map MAPPING_KEY = new HashMap<>(); static{ MAPPING_KEY.put("值", Attribute.value); } /* 表单控件监听 */ protected FormCpnListener formCpnListener; /* 触发事件 */ @Getter @Setter protected List formulas; /* 控件别名 */ @Getter @Setter protected String fieldId; /* 父控件 */ @Setter protected FormCpn parentFormCpn; /* 在子表单中的下标 */ @Getter @Setter protected Integer index; public AbstractBaseFormCpn(/*String fieldId*/) { /*this.fieldId = fieldId;*/ this.formCpnListener = new BaseFormCpnListener(); } public void init(FormCpnInfo formCpnInfo){ this.fieldId = formCpnInfo.getTagAttribute().getAlias(); } @Override public Object get(Object key) { Object mappIngKey = MAPPING_KEY.get(key); Object val = super.get(mappIngKey == null ? key : mappIngKey); return val == null ? "" : val; } @Override public Object put(Object key, Object value) { if(StringUtils.isBlank(Objects.toString(key))) return null; if(Objects.equals(FORMULA_KEY, key)) return setFormula((FormulaInfo) value); boolean isFormat = false; // 控件属性 可见,可编辑,值 if(!isFormat){ for(Attribute attr : Attribute.values()){ if(Objects.equals(attr.toString(), key.toString())){ key = Attribute.valueOf(Objects.toString(key)); isFormat = true; break; } } } // 表单提示消息 if(!isFormat){ for(MsgType msg : MsgType.values()){ if(Objects.equals(msg.toString(), key.toString())){ ((Form)get(FORM_KEY)).setMsg(MsgType.valueOf(Objects.toString(key)), Objects.toString(value, ""), this); return value; } } } // 控件错误消息 if(!isFormat){ for(CpnMsg msg : CpnMsg.values()){ if(Objects.equals(msg.toString(), key.toString())){ key = CpnMsg.valueOf(Objects.toString(key)); Object values = get(key); if(values == null) value = new ArrayList<>(Arrays.asList(value.toString())); else value = ((List)values).add(value); isFormat = true; break; } } } try{ return super.put(key, value); }finally { if((key instanceof Attribute) && ((Attribute)key) == Attribute.value) formCpnListener.change(this); } } /* * 添加事件 */ public Object setFormula(FormulaInfo value) { if(this.formulas == null) this.formulas = new ArrayList<>(); this.formulas.add(value); return value; } @Override public void setFormCpnListener(FormCpnListener formCpnListener) { this.formCpnListener = formCpnListener; } @Override public FormCpnListener getFormCpnListener() { return this.formCpnListener; } @Override public FormCpn getParentCpn() { return this.parentFormCpn; } @Override public FormCpn clone(){ // return FormCpnFactory.clone(this); return (FormCpn) super.clone(); } @Override public Object getValue() { return get(Attribute.value); } }