DymWebFormBuilder.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using DymWebForm.Common.Linq;
  11. using DymWebForm.Common.Web;
  12. using DymWebForm.Common.BasicDataType;
  13. namespace DymWebForm
  14. {
  15. public class DymWebFormBuilder
  16. {
  17. private readonly FormField[] _allField;
  18. private readonly FormField _pkField;
  19. private readonly FormFieldFactory _formFieldFactory;
  20. private readonly Dictionary<Type, FormFieldFactory> _dicFormFieldFactory;
  21. public Type TableType { private set; get; }
  22. public DymWebFormBuilder(Type type)
  23. {
  24. TableType = type;
  25. _formFieldFactory = new FormFieldFactory(type);
  26. _allField = _formFieldFactory.GetFormFields();
  27. _pkField = _allField.FirstOrDefault(p => p.IsPK);
  28. if (_pkField == null)
  29. throw new ArgumentException("Must define PK");
  30. _dicFormFieldFactory = new Dictionary<Type, FormFieldFactory>();
  31. _dicFormFieldFactory[type] = _formFieldFactory;
  32. }
  33. private void ProcTdFK(Func<Type, object[], object[]> fkQuery, List<FieldFK> lstFKs)
  34. {
  35. lstFKs
  36. .GroupBy(p => p.Field.FKParentType)
  37. .ToDictionary(p => p.Key, p => p.ToArray())
  38. .ForEach(p =>
  39. {
  40. var ff = _dicFormFieldFactory.ContainsKey(p.Key)
  41. ? _dicFormFieldFactory[p.Key]
  42. : (_dicFormFieldFactory[p.Key] = new FormFieldFactory(p.Key));
  43. var arrFk = p.Value
  44. .Select(q => q.FKVal)
  45. .Distinct()
  46. .ToArray();
  47. var dicFKobj = fkQuery(p.Key, arrFk)
  48. .ToDictionary(
  49. ff.GetPKValue
  50. , q => q
  51. );
  52. p.Value.ForEach(q =>
  53. {
  54. q.Td.Controls.Add(new Literal { Text = q.FKVal.ToDisplayString() });
  55. q.Td.Controls.Add(new Literal { Text = ": " });
  56. object parVal = null;
  57. var found = false;
  58. if (dicFKobj.ContainsKey(q.FKVal))
  59. {
  60. found = true;
  61. var par = dicFKobj[q.FKVal];
  62. parVal = ff.GetFKValue(par);
  63. }
  64. if (found)
  65. {
  66. q.Td.AddControl(new Label { Text = parVal.ToDisplayString() ?? "NULL", ForeColor = Color.Blue })
  67. .Font.Italic = (parVal == null);
  68. }
  69. else
  70. {
  71. q.Td.Controls.Add(new Label { Text = "无对应", ForeColor = Color.Red });
  72. }
  73. });
  74. });
  75. }
  76. private Table RenderDetailsInternal(WebControl container, object entity, Func<Type, object[], object[]> fkQuery, string[] skipFields = null)
  77. {
  78. skipFields = skipFields ?? new string[] { };
  79. var arrFieldsToDisplay = _allField
  80. .Where(p => skipFields.Contains(p.FieldName) == false)
  81. .ToArray();
  82. //init
  83. var tbl = new Table();
  84. container.Controls.Add(tbl);
  85. tbl.Attributes.Add("border", "1");
  86. tbl.Style[HtmlTextWriterStyle.BorderCollapse] = "Collapse";
  87. tbl.CellSpacing = 1;
  88. tbl.CellPadding = 2;
  89. var lstFKs = new List<FieldFK>();
  90. //generate fields of form
  91. foreach (var field in arrFieldsToDisplay)
  92. {
  93. var tr = new TableRow();
  94. tbl.Rows.Add(tr);
  95. var objVal = field.GetValue(entity);
  96. //field name
  97. tr.Cells.Add(new TableCell { Text = field.DisplayName + ":" });
  98. //field control
  99. var td = new TableCell();
  100. tr.Cells.Add(td);
  101. td.VisualText(objVal.ToDisplayString());
  102. //fk
  103. if (field.IsFK && objVal != null)
  104. {
  105. lstFKs.Add(new FieldFK { Field = field, Td = td, FKVal = objVal });
  106. }
  107. }
  108. if (fkQuery != null)
  109. {
  110. ProcTdFK(fkQuery, lstFKs);
  111. }
  112. return tbl;
  113. }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. /// <param name="container">容器, 要求已经添加到Page</param>
  118. /// <param name="dataSource">数据源</param>
  119. /// <param name="arrSkipFields">跳过的字段(如密码)</param>
  120. /// <param name="fkQuery">外键查询回调</param>
  121. /// <param name="urlEdit">{0}=ID 默认 ?op=edit&amp;arg={0}</param>
  122. /// <param name="urlDelete">{0}=ID 默认 ?op=delete&amp;arg={0}</param>
  123. /// <param name="pageIndex"></param>
  124. /// <param name="pageSize"></param>
  125. public void RenderGrid(Control container, IEnumerable dataSource, string[] arrSkipFields
  126. , Func<Type, object[], object[]> fkQuery = null
  127. , string urlEdit = null
  128. , string urlDelete = null
  129. , int pageIndex = 0, int pageSize = 0)
  130. {
  131. //TODO: should allow optional columns to show
  132. //init vars
  133. urlEdit = urlEdit ?? "?op=edit&arg={0}";
  134. urlDelete = urlDelete ?? "?op=delete&arg={0}";
  135. //init
  136. var tbl = new Table();
  137. container.Controls.Add(tbl);
  138. tbl.Attributes.Add("border", "1");
  139. tbl.Style[HtmlTextWriterStyle.BorderCollapse] = "Collapse";
  140. tbl.CellSpacing = 1;
  141. tbl.CellPadding = 2;
  142. arrSkipFields = arrSkipFields ?? new string[] { };
  143. var arrFieldsToDisplay = _allField
  144. .Where(p => arrSkipFields.Contains(p.FieldName) == false)
  145. .ToArray();
  146. //header
  147. var th = new TableRow();
  148. th.Cells.Add(new TableHeaderCell { Text = "#" });
  149. th.Cells.AddRange(
  150. arrFieldsToDisplay
  151. .SelectArray(p => (TableCell)new TableHeaderCell { Text = p.DisplayName })
  152. );
  153. th.Cells.Add(new TableHeaderCell { Text = "操作" });
  154. tbl.Rows.Add(th);
  155. //coll fk td for batch process
  156. var lstFKs = new List<FieldFK>();
  157. var rowNumber = pageIndex * pageSize;
  158. //rows
  159. foreach (var item in dataSource)
  160. {
  161. //TODO: optimize, use pure html tag(not server control)
  162. var tr = new TableRow();
  163. tbl.Rows.Add(tr);
  164. tr.Cells.Add(new TableCell { Text = (++rowNumber).ToString() });
  165. //entity data
  166. var item1 = item;
  167. tr.Cells.AddRange(
  168. arrFieldsToDisplay
  169. .SelectArray(p =>
  170. {
  171. var objVal = p.GetValue(item1);
  172. var strVal = objVal.ToDisplayString();
  173. var td = new TableCell();
  174. td.VisualText(strVal);
  175. if (p.IsFK && objVal != null)
  176. {
  177. lstFKs.Add(new FieldFK { Field = p, Td = td, FKVal = objVal });
  178. }
  179. return td;
  180. })
  181. );
  182. //opt.
  183. var tdOpt = new TableCell();
  184. tr.Cells.Add(tdOpt);
  185. var key = _pkField.GetValue(item);
  186. tdOpt.Controls.Add(new HyperLink { Text = "修改", NavigateUrl = urlEdit.Format(key) });
  187. tdOpt.Controls.Add(new Literal { Text = " " });
  188. tdOpt.Controls.Add(new HyperLink { Text = "删除", NavigateUrl = urlDelete.Format(key) });
  189. }//end foreach
  190. if (fkQuery != null)
  191. {
  192. ProcTdFK(fkQuery, lstFKs);
  193. }
  194. }
  195. public void RenderInputForm(WebControl container, Action actOnSubmit, bool isAdd = true)
  196. {
  197. //init
  198. var tbl = new Table();
  199. container.Controls.Add(tbl);
  200. tbl.Attributes.Add("border", "1");
  201. tbl.Style[HtmlTextWriterStyle.BorderCollapse] = "Collapse";
  202. tbl.CellSpacing = 1;
  203. tbl.CellPadding = 2;
  204. //generate fields of form
  205. foreach (var field in _allField)
  206. {
  207. if (isAdd && field.IsPK)
  208. continue;
  209. var validation = field.Validation;
  210. var tr = new TableRow();
  211. tbl.Rows.Add(tr);
  212. //field name
  213. var tdName = tr.AddCell(new TableCell { Text = field.DisplayName + ":" });
  214. if (field.AllowNull == false)
  215. {
  216. tdName.Controls.Add(new Literal { Text = "[" });
  217. tdName.Controls.Add(new Label { Text = "*", ForeColor = Color.Red });
  218. tdName.Controls.Add(new Literal { Text = "]" });
  219. tdName.Controls.Add(new Literal { Text = field.DisplayName + ":" });
  220. }
  221. //field control
  222. var tdCtl = new TableCell();
  223. tr.Cells.Add(tdCtl);
  224. var ctl = field.CreateControl();
  225. if (field.IsPK)
  226. {
  227. var box = ctl as TextBox;
  228. if (box != null)
  229. {
  230. var tc = box;
  231. tc.ReadOnly = true;
  232. }
  233. }
  234. if (field.HasDefaultValueGenerator)
  235. DymWebFormControlMappnig.GetInst(field.DataType, field.ValidationType).ControlSetValue(ctl, field.GenerateDefaultValue());
  236. tdCtl.Controls.Add(ctl);
  237. ctl.ID = field.ControlID;
  238. //hint
  239. if (!isAdd && validation is DynWebFormPasswordValidation)
  240. {
  241. tdCtl.Controls.Add(new Literal { Text = "<br>*留空保持不变" });
  242. tdName.VerticalAlign = VerticalAlign.Top;
  243. }
  244. //field validate
  245. var tdVal = new TableCell();
  246. tr.Cells.Add(tdVal);
  247. if (field.IsPK == false
  248. && field.AllowNull == false
  249. && ctl is CheckBox == false
  250. && (
  251. (isAdd)
  252. || (false == validation is DynWebFormPasswordValidation)
  253. )
  254. )
  255. {
  256. var rfv = new RequiredFieldValidator();
  257. tdVal.Controls.Add(rfv);
  258. rfv.ValidationGroup = "fc" + TableType.Name;
  259. rfv.ControlToValidate = ctl.ID;
  260. rfv.Display = ValidatorDisplay.Dynamic;
  261. rfv.ForeColor = Color.Red;
  262. rfv.Text = "*";
  263. rfv.ErrorMessage = "必填字段:<b>" + field.DisplayName + "</b>";
  264. rfv.ID = "rfv" + ctl.ID;
  265. }
  266. if (validation != null)
  267. {
  268. BaseValidator vc = validation.CreateValidator(container);
  269. tdVal.Controls.Add(vc);
  270. vc.ValidationGroup = "fc" + TableType.Name;
  271. vc.ControlToValidate = ctl.ID;
  272. vc.Display = ValidatorDisplay.Dynamic;
  273. vc.ForeColor = Color.Red;
  274. vc.Text = "▲";
  275. vc.ErrorMessage = "无效输入:<b>" + field.DisplayName + "</b>";
  276. vc.ID = "cv" + ctl.ID;
  277. }
  278. }
  279. //the submit button
  280. var trSubmit = new TableRow();
  281. tbl.Rows.Add(trSubmit);
  282. var tdSubmit = new TableCell();
  283. trSubmit.Cells.Add(tdSubmit);
  284. tdSubmit.ColumnSpan = 3;
  285. tdSubmit.HorizontalAlign = HorizontalAlign.Right;
  286. tdSubmit.AddControl(new Button
  287. {
  288. Text = "提交",
  289. ValidationGroup = "fc" + TableType.Name
  290. }).Click += (ss, ee) =>
  291. {
  292. if (((Page)HttpContext.Current.Handler).IsValid)
  293. actOnSubmit();
  294. };
  295. //the summary
  296. var trSummarny = new TableRow();
  297. tbl.Rows.Add(trSummarny);
  298. var tdSummary = new TableCell();
  299. trSummarny.Cells.Add(tdSummary);
  300. tdSummary.ColumnSpan = 3;
  301. tdSummary.Controls.Add(new ValidationSummary { ValidationGroup = "fc" + TableType.Name, ForeColor = Color.Red });
  302. }
  303. public void BindForm(WebControl container, object entity, bool isAdd = false)
  304. {
  305. _allField.ForEach(p =>
  306. {
  307. var val = p.GetValue(entity);
  308. var octl = container.FindControl(p.ControlID);
  309. DymWebFormControlMappnig.GetInst(p.DataType, p.ValidationType).ControlSetValue(octl, val);
  310. });
  311. }
  312. public void BindEntity(WebControl container, object entity)
  313. {
  314. if (container == null)
  315. throw new ArgumentNullException("container");
  316. _allField
  317. .Where(p => p.IsPK == false)
  318. .ForEach(p =>
  319. {
  320. var octl = container.FindControl(p.ControlID);
  321. var val = DymWebFormControlMappnig.GetInst(p.DataType, p.ValidationType).ControlGetValue(octl);
  322. if (p.Validation is DynWebFormPasswordValidation
  323. && (val == null || val as string == "")
  324. )
  325. return;
  326. p.SetValue(entity, val);
  327. });
  328. }
  329. public void RenderDetails(WebControl container, object entity, Func<Type, object[], object[]> fkQuery = null, string[] arrSkipFields = null)
  330. {
  331. RenderDetailsInternal(container, entity, fkQuery, arrSkipFields);
  332. }
  333. public void RenderDelete(WebControl container, object entity, Action actOnDeleteSubmit, Func<Type, object[], object[]> fkQuery = null, string[] skipFields = null)
  334. {
  335. var tbl = RenderDetailsInternal(container, entity, fkQuery, skipFields);
  336. //the submit button
  337. var trSubmit = new TableRow();
  338. tbl.Rows.Add(trSubmit);
  339. var tdSubmit = new TableCell();
  340. trSubmit.Cells.Add(tdSubmit);
  341. tdSubmit.ColumnSpan = 3;
  342. tdSubmit.HorizontalAlign = HorizontalAlign.Right;
  343. tdSubmit.AddControl(new Button
  344. {
  345. Text = "删除",
  346. }).Click += (ss, ee) =>
  347. {
  348. if (((Page)HttpContext.Current.Handler).IsValid)
  349. actOnDeleteSubmit();
  350. };
  351. }
  352. public Dictionary<string, string> GetFieldNamesForGrid()
  353. {
  354. return _allField
  355. .Where(p => false == p.Validation is DynWebFormPasswordValidation)
  356. .ToDictionary(p => p.FieldName, p => p.DisplayName);
  357. }
  358. public PropertyInfo GetFieldInfoForGrid(string name)
  359. {
  360. return _allField
  361. .Where(p => p.FieldName == name && false == p.Validation is DynWebFormPasswordValidation)
  362. .Select(p => p.PropertyInfo)
  363. .FirstOrDefault();
  364. }
  365. }
  366. }