commit | author | age
|
a18bfa
|
1 |
package cn.autoform.web.formula.info.impl; |
Z |
2 |
|
|
3 |
import java.lang.reflect.InvocationTargetException; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.Collections; |
|
6 |
import java.util.HashMap; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
import java.util.Objects; |
|
10 |
|
|
11 |
import org.apache.commons.beanutils.PropertyUtils; |
|
12 |
import org.springframework.ui.ModelMap; |
|
13 |
|
|
14 |
import cn.autoform.bean.form.CpnType; |
|
15 |
import cn.autoform.util.tool.JSONTool; |
|
16 |
import cn.autoform.web.formula.info.Form; |
|
17 |
import cn.autoform.web.formula.info.FormCpn; |
|
18 |
import cn.autoform.web.formula.info.FormListener; |
|
19 |
import cn.autoform.web.formula.info.FormulaInfo; |
|
20 |
import cn.autoform.web.formula.info.impl.cpn.ChildCpn; |
|
21 |
import cn.autoform.web.formula.info.impl.cpn.FormCpnFactory; |
|
22 |
import cn.autoform.web.formula.prop.Attribute; |
|
23 |
import cn.autoform.web.formula.prop.CpnMsg; |
|
24 |
import cn.autoform.web.formula.prop.MsgType; |
|
25 |
import lombok.Getter; |
|
26 |
import lombok.extern.slf4j.Slf4j; |
|
27 |
|
|
28 |
@Slf4j |
|
29 |
public class SimpleForm extends ModelMap implements Form{ |
|
30 |
|
|
31 |
/** |
|
32 |
* |
|
33 |
*/ |
|
34 |
private static final long serialVersionUID = -2680390286555343976L; |
|
35 |
|
|
36 |
/* 表单监听 */ |
|
37 |
private FormListener formListener; |
|
38 |
|
|
39 |
private List<FormulaInfo> allFormulaInfos; |
|
40 |
|
|
41 |
/* 公式触发事件 */ |
|
42 |
private List<FormulaInfo> formulaInfos; |
|
43 |
|
|
44 |
/* 表单控件集合 */ |
|
45 |
private Map<String, FormCpn> formCpns; |
|
46 |
|
|
47 |
/* 表单提示信息 */ |
|
48 |
@Getter |
|
49 |
private List<Map<MsgType, String>> msgs; |
|
50 |
|
|
51 |
/* 记录公式执行次数 */ |
|
52 |
private Map<FormCpn, Integer> recordListener; |
|
53 |
|
|
54 |
/* 表单数据 */ |
|
55 |
private Map<String, Object> data; |
|
56 |
|
|
57 |
/** |
|
58 |
* 表单加载-初始化 |
|
59 |
*/ |
|
60 |
@Override |
|
61 |
public void loading() { |
|
62 |
recordListener = new HashMap<>(); |
|
63 |
// 监听 |
|
64 |
if(this.formListener == null) |
|
65 |
this.formListener = new SimpleFormListener(); |
|
66 |
// 触发事件 |
|
67 |
if(this.allFormulaInfos == null) |
|
68 |
this.allFormulaInfos = Collections.emptyList(); |
|
69 |
// 初始化数据 |
|
70 |
initData(); |
|
71 |
// 初始化表单公式 |
|
72 |
initFormulas(); |
|
73 |
// 表单初始化完后触发监听 |
|
74 |
this.formListener.loading(this); |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* 初始化数据 |
|
79 |
*/ |
|
80 |
protected void initData() { |
|
81 |
if(this.data == null) |
|
82 |
return; |
|
83 |
FormCpn cpn = null; |
|
84 |
for(Map.Entry<String, Object> entry : this.data.entrySet()){ |
|
85 |
cpn = (FormCpn) this.get(entry.getKey()); |
|
86 |
if(cpn == null) |
|
87 |
break; |
|
88 |
if(cpn.getCpnType() == CpnType.subform) |
|
89 |
setChildCpnVal((ChildCpn) cpn, entry); |
|
90 |
else |
|
91 |
setCpnVal(cpn, entry); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
protected void setChildCpnVal(ChildCpn childCpn, Map.Entry<String, Object> entry) { |
|
96 |
if(!(entry.getValue() instanceof List)) |
|
97 |
return; |
|
98 |
List<Map<String, Object>> values = (List<Map<String, Object>>) entry.getValue(); |
|
99 |
int index = 0; |
|
100 |
FormCpn cloneCpn = null; |
|
101 |
for(Map<String, Object> val : values){ |
|
102 |
for(Map.Entry<String, Object> valEntry : val.entrySet()){ |
|
103 |
FormCpn cpn = getFormCpn(childCpn.getFieldId()+"."+valEntry.getKey()); |
|
104 |
if(cpn == null) |
|
105 |
break; |
|
106 |
setCpnVal(cloneCpn = FormCpnFactory.clone(cpn), valEntry); |
|
107 |
childCpn.put(FormCpn.CHILD_CPN_VAL, cloneCpn, index); |
|
108 |
} |
|
109 |
index ++; |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
protected void setCpnVal(FormCpn cpn, Map.Entry<String, Object> entry) { |
|
114 |
cpn.put(Attribute.value, entry.getValue()); |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public void commit() { |
|
119 |
this.formListener.commit(this); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* 初始化表单公式 |
|
124 |
*/ |
|
125 |
protected void initFormulas(){ |
|
126 |
System.err.println(JSONTool.toJson(this.allFormulaInfos)); |
|
127 |
this.allFormulaInfos.stream().forEach(this::eachFormula); |
|
128 |
} |
|
129 |
|
|
130 |
|
|
131 |
@Override |
|
132 |
public Object get(Object key) { |
|
133 |
Object val = formCpns.get(key); |
|
134 |
if(val != null) |
|
135 |
return val; |
|
136 |
return super.get(key); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 循环每个公式 |
|
141 |
* @param formula |
|
142 |
*/ |
|
143 |
protected void eachFormula(FormulaInfo formula) { |
|
144 |
System.err.println(JSONTool.toJson(formula)); |
|
145 |
|
|
146 |
String fieldName = formula.getEvent().getEventField(); |
|
147 |
if(Objects.equals(FORM_KEY, fieldName)){ |
|
148 |
if(this.formulaInfos == null) |
|
149 |
this.formulaInfos = new ArrayList<>(); |
|
150 |
this.formulaInfos.add(formula); |
|
151 |
return; |
|
152 |
} |
|
153 |
FormCpn cpn = getFormCpn(fieldName); |
|
154 |
if(cpn != null) |
|
155 |
cpn.put(FormCpn.FORMULA_KEY, formula); |
|
156 |
} |
|
157 |
|
|
158 |
protected FormCpn getFormCpn(String fieldName) { |
|
159 |
try { |
|
160 |
if(fieldName.indexOf('.') <= 0) |
|
161 |
return (FormCpn) this.get(fieldName); |
|
162 |
else |
|
163 |
return (FormCpn) PropertyUtils.getProperty(this, fieldName); |
|
164 |
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { |
|
165 |
log.error(e.getMessage(), e); |
|
166 |
return null; |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
@Override |
|
171 |
public void setAllFormulas(List<FormulaInfo> allFormulaInfos) { |
|
172 |
this.allFormulaInfos = allFormulaInfos; |
|
173 |
} |
|
174 |
|
|
175 |
@Override |
|
176 |
public void setFormListener(FormListener formListener) { |
|
177 |
this.formListener = formListener; |
|
178 |
} |
|
179 |
|
|
180 |
@Override |
|
181 |
public void addFormCpn(FormCpn formCpn) { |
|
182 |
if(this.formCpns == null) |
|
183 |
this.formCpns = new HashMap<>(); |
|
184 |
// 为控件添加表单引用 |
|
185 |
formCpn.put(FormCpn.FORM_KEY, this); |
|
186 |
// 加入表单集合 |
|
187 |
this.formCpns.put(formCpn.getFieldId(), formCpn); |
|
188 |
} |
|
189 |
|
|
190 |
@Override |
|
191 |
public void setMsg(MsgType key, String value, FormCpn formCpn) { |
|
192 |
if(this.msgs == null) |
|
193 |
this.msgs = new ArrayList<>(); |
|
194 |
Map<MsgType, String> map = new HashMap<>(); |
|
195 |
map.put(key, value); |
|
196 |
this.msgs.add(map); |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public List<FormulaInfo> getFormulas() { |
|
201 |
return formulaInfos; |
|
202 |
} |
|
203 |
|
|
204 |
@Override |
|
205 |
public boolean triggerEvent(FormCpn formCpn) { |
|
206 |
Integer sum = recordListener.get(formCpn); |
|
207 |
if(sum == null) |
|
208 |
sum = 0; |
|
209 |
else |
|
210 |
if(sum > 1) |
|
211 |
return false; |
|
212 |
recordListener.put(formCpn, ++sum); |
|
213 |
return true; |
|
214 |
} |
|
215 |
|
|
216 |
@Override |
|
217 |
public void setData(Map<String, Object> data) { |
|
218 |
this.data = data; |
|
219 |
} |
|
220 |
|
|
221 |
@Override |
|
222 |
public Map<String, Object> getData() { |
|
223 |
Map<String, Object> newData = new HashMap<>(); |
|
224 |
for(Map.Entry<String, FormCpn> entry : this.formCpns.entrySet()){ |
|
225 |
newData.put(entry.getKey(), entry.getValue().getValue()); |
|
226 |
} |
|
227 |
return newData; |
|
228 |
} |
|
229 |
|
|
230 |
@Override |
|
231 |
public Map<String, Object> getCpnMsgs() { |
|
232 |
Map<String, Object> cpnMsg = new HashMap<>(); |
|
233 |
for(Map.Entry<String, FormCpn> entry : this.formCpns.entrySet()){ |
|
234 |
Object obj = entry.getValue().get(CpnMsg.error); |
|
235 |
if(obj != null) |
|
236 |
cpnMsg.put(entry.getKey(), obj); |
|
237 |
} |
|
238 |
return cpnMsg; |
|
239 |
} |
|
240 |
|
|
241 |
} |