PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/IntelWebSite/WebAppCode/WebGrid.cs

https://github.com/sirivedula/IntelWebPro
C# | 534 lines | 496 code | 38 blank | 0 comment | 59 complexity | a72a15ff4818a4e96abf50c67a0ccf38 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Text;
  7. using System.Web.UI;
  8. using System.Globalization;
  9. using IntelWeb;
  10. using IntelWeb.IntelWebObjects;
  11. namespace IntelWebSite
  12. {
  13. public class WebGrid
  14. {
  15. private List<IntelWebObject> _data;
  16. private NameValueCollection _form;
  17. private List<ApplicationException> _loadErrors = new List<ApplicationException>();
  18. public string afterSaveErrors { get; set; }
  19. public string updateDataErrors { get; set; }
  20. private CurrentUser _user;
  21. private IntelWebObject _protoObject;
  22. private IntelWebField _pkfield;
  23. private List<DisplayField> _fields = new List<DisplayField>();
  24. private string _gridName;
  25. private WebGridTable _gridTable = new WebGridTable();
  26. private string _uiFormShowJS;
  27. private string _uiFormName;
  28. private string _ajaxURL = "SaveGrid.aspx";
  29. private string _objectId;
  30. private string _afterSubmitJS;
  31. private string _onAddJS;
  32. public List<IntelWebObject> deleteQueue = new List<IntelWebObject>();
  33. public List<IntelWebField> serverOnlyFields = new List<IntelWebField>();
  34. public string objectType { get; set; }
  35. public ControlCollection controls { get; set; }
  36. public string onAddJS
  37. {
  38. get { return _onAddJS; }
  39. set { _onAddJS = value; }
  40. }
  41. public string afterSubmitJS
  42. {
  43. get { return _afterSubmitJS; }
  44. set { _afterSubmitJS = value; }
  45. }
  46. public string objectId
  47. {
  48. get { return _objectId; }
  49. set { _objectId = value; }
  50. }
  51. public string ajaxURL
  52. {
  53. get { return _ajaxURL; }
  54. set { _ajaxURL = value; }
  55. }
  56. public string uiFormName
  57. {
  58. get { return _uiFormName; }
  59. set { _uiFormName = value; }
  60. }
  61. public string uiFormShowJS
  62. {
  63. get { return _uiFormShowJS; }
  64. set { _uiFormShowJS = value; }
  65. }
  66. public IntelWebObject protoObject
  67. {
  68. get { return _protoObject; }
  69. set { _protoObject = value; }
  70. }
  71. public List<DisplayField> headerFields()
  72. {
  73. var result = new List<DisplayField>();
  74. result.AddRange(this.fields.FindAll(x => x.isHeaderField));
  75. return result;
  76. }
  77. public WebGridTable gridTable
  78. {
  79. get
  80. {
  81. return _gridTable;
  82. }
  83. set
  84. {
  85. _gridTable = value;
  86. }
  87. }
  88. public string gridName
  89. {
  90. get { return _gridName; }
  91. set { _gridName = value; }
  92. }
  93. public List<DisplayField> fields
  94. {
  95. get { return _fields; }
  96. set { _fields = value; }
  97. }
  98. public List<ApplicationException> loadErrors
  99. {
  100. get { return _loadErrors; }
  101. }
  102. public CurrentUser User
  103. {
  104. get { return _user; }
  105. set { _user = value; }
  106. }
  107. public NameValueCollection form
  108. {
  109. get
  110. {
  111. return _form;
  112. }
  113. set
  114. {
  115. _form = value;
  116. }
  117. }
  118. public List<IntelWebObject> data
  119. {
  120. get { return _data; }
  121. set { _data = value; }
  122. }
  123. public WebGrid(string type, CurrentUser myUser, string myGridName)
  124. {
  125. this.User = myUser;
  126. this.objectType = type;
  127. _protoObject = this.User.protoObject(this.objectType);
  128. _pkfield = _protoObject.primaryKey;
  129. _gridName = myGridName;
  130. }
  131. public List<DisplayField> fieldsFromList(List<string> fieldNames)
  132. {
  133. var result = new List<DisplayField>();
  134. foreach (var fieldName in fieldNames)
  135. {
  136. var fld = _protoObject.Field(fieldName);
  137. if (fld == null)
  138. {
  139. throw new ApplicationException("There is no field in the " + _protoObject.tableName + " named [" + fieldName + "]");
  140. }
  141. DisplayField df = new DisplayField(this, fld.name.ToLower(), fld.name, null, false, fld.name.ToLower());
  142. result.Add(df);
  143. }
  144. return result;
  145. }
  146. public int submittedRecordCount
  147. {
  148. get
  149. {
  150. decimal decimalresult = 0;
  151. if (this.form.Get(this._gridName + "_recordCount") != null)
  152. {
  153. decimal.TryParse(this.form.Get(this._gridName + "_recordCount"), out decimalresult);
  154. }
  155. return Convert.ToInt32(decimalresult);
  156. }
  157. }
  158. public IntelWebField primaryKey
  159. {
  160. get
  161. {
  162. return _pkfield;
  163. }
  164. }
  165. public object convertStringToType(string value, Type type)
  166. {
  167. var convertedVal = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
  168. if (DBNull.Equals(DBNull.Value, convertedVal))
  169. {
  170. return null;
  171. }
  172. return convertedVal;
  173. }
  174. public bool UpdateData()
  175. {
  176. this.updateDataErrors = "";
  177. int recCnt = this.submittedRecordCount;
  178. var myType = _protoObject.GetType();
  179. for (int counter = 0; counter < recCnt; counter++)
  180. {
  181. string keyvalue = this.form.Get(this._gridName + "_pk_" + counter.ToString());
  182. string isNewString = this.form.Get(this._gridName + "_isNew_" + counter.ToString());
  183. string isDeletedString = this.form.Get(this._gridName + "_isDeleted_" + counter.ToString());
  184. IntelWebObject tpObj = null;
  185. if (!String.IsNullOrEmpty(isNewString) && isNewString == "true")
  186. {
  187. string tempPK = this.form.Get(this._gridName + "_temppk_" + counter.ToString());
  188. tpObj = this.User.protoObject(this.objectType);
  189. if (_data == null) { _data = new List<IntelWebObject>(); }
  190. _data.Add(tpObj);
  191. }
  192. else
  193. {
  194. if (_data != null)
  195. {
  196. tpObj = _data.Find(x => x.primaryKey.fieldValue.Equals(WebDataConverter.toType(keyvalue, typeof(Int32))));
  197. }
  198. }
  199. if (tpObj != null)
  200. {
  201. if (!String.IsNullOrEmpty(isDeletedString) && isDeletedString == "true")
  202. {
  203. deleteQueue.Add(tpObj);
  204. }
  205. foreach (DisplayField field in this.fields)
  206. {
  207. if (field.submitField)
  208. {
  209. string formFieldName = this._gridName + "_r" + counter.ToString() + "_" + field.fieldName;
  210. var fieldValue = this.form.Get(formFieldName);
  211. var tProp = myType.GetProperties().FirstOrDefault(x => x.Name.Equals(field.fieldName, StringComparison.InvariantCultureIgnoreCase));
  212. try
  213. {
  214. var convertedVal = WebDataConverter.toType(fieldValue, tProp.PropertyType);
  215. tProp.SetValue(tpObj, convertedVal, null);
  216. }
  217. catch (Exception)
  218. {
  219. updateDataErrors += "The value [" + fieldValue + "] that was submitted for the field [" + tProp.Name + "] was not a valid.\n";
  220. }
  221. }
  222. }
  223. foreach (IntelWebField wpField in this.serverOnlyFields)
  224. {
  225. var tProp = myType.GetProperties().FirstOrDefault(x => x.Name.Equals(wpField.name, StringComparison.InvariantCultureIgnoreCase));
  226. tProp.SetValue(tpObj, wpField.fieldValue, null);
  227. }
  228. }
  229. else
  230. {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. public string gridScript()
  237. {
  238. StringBuilder sb = new StringBuilder();
  239. sb.AppendLine("");
  240. sb.Append("webGrid.grids.push({name:'" + JSUtil.EnquoteJS(this.gridName) + "'");
  241. sb.Append(",objectId:'" + JSUtil.EnquoteJS(this.objectId) + "'");
  242. sb.Append(",tableId:''");
  243. sb.Append(",ajaxURL:'" + JSUtil.EnquoteJS(this.ajaxURL) + "'");
  244. sb.Append(",fieldDefs:" + this.gridFieldsJS());
  245. sb.Append(",extraColumns:[]");
  246. sb.Append(",data: " + this.dataJS() + "");
  247. if (!String.IsNullOrEmpty(this.afterSubmitJS))
  248. {
  249. sb.Append(",afterSubmit: " + this.afterSubmitJS);
  250. }
  251. sb.Append(",showUIForm: " + ((this.uiFormShowJS == null) ? "undefined" : this.uiFormShowJS) + "});");
  252. return sb.ToString();
  253. }
  254. public string dataJS()
  255. {
  256. StringBuilder sb = new StringBuilder();
  257. int counter = -1;
  258. if (this.data != null)
  259. {
  260. foreach (IntelWebObject wbObj in this.data)
  261. {
  262. counter++;
  263. sb.Append("{_id:" + counter.ToString() + ",_pk:" + JSUtil.SerializeJSProperty(wbObj.primaryKey));
  264. sb.Append(",fields:{");
  265. foreach (DisplayField field in this.fields)
  266. {
  267. var myValue = wbObj.Field(field.fieldName);
  268. if (myValue != null)
  269. {
  270. if (field.formatFunction != null)
  271. {
  272. sb.Append("'" + JSUtil.EnquoteJS(field.fieldName) + "':" + JSUtil.SerializeJSProperty(field.formatFunction.Invoke(wbObj)));
  273. }
  274. else
  275. {
  276. if (myValue.fieldValue == null)
  277. {
  278. sb.Append("'" + JSUtil.EnquoteJS(field.fieldName) + "':null");
  279. }
  280. else
  281. {
  282. sb.Append("'" + JSUtil.EnquoteJS(field.fieldName) + "':" + JSUtil.SerializeJSProperty(myValue));
  283. }
  284. }
  285. sb.Append(",");
  286. }
  287. }
  288. sb.Remove(sb.Length - 1, 1);
  289. sb.AppendLine("}}");
  290. sb.Append(",");
  291. }
  292. if (sb.Length > 1)
  293. {
  294. sb.Remove(sb.Length - 1, 1);
  295. }
  296. }
  297. return "[" + sb.ToString() + "]";
  298. }
  299. public bool LoadData()
  300. {
  301. int myCount = this.submittedRecordCount;
  302. List<IntelWebField> primaryKeys = new List<IntelWebField>();
  303. StringBuilder sb = new StringBuilder();
  304. int recCnt = this.submittedRecordCount;
  305. for (int counter = 0; counter < recCnt; counter++)
  306. {
  307. string isNewString = this.form.Get(this._gridName + "_isNew_" + counter.ToString());
  308. if (String.IsNullOrEmpty(isNewString) || isNewString != "true")
  309. {
  310. sb.Append("?,");
  311. string keyvalue = this.form.Get(this._gridName + "_pk_" + counter.ToString());
  312. object convertedKey;
  313. switch (this.primaryKey.type)
  314. {
  315. case System.Data.OleDb.OleDbType.Guid:
  316. convertedKey = new Guid(keyvalue);
  317. break;
  318. case System.Data.OleDb.OleDbType.Integer:
  319. convertedKey = int.Parse(keyvalue);
  320. break;
  321. default:
  322. convertedKey = keyvalue;
  323. break;
  324. }
  325. IntelWebField paramFld = new IntelWebField(this._gridName + "_pk_" + counter.ToString(), this.primaryKey.type, this.primaryKey.size, 0, 0, System.Data.DataRowVersion.Current, false, convertedKey);
  326. primaryKeys.Add(paramFld);
  327. }
  328. }
  329. try
  330. {
  331. if (sb.Length > 0)
  332. {
  333. sb.Remove(sb.Length - 1, 1);
  334. IntelWebObject tobj = this.User.protoObject(objectType);
  335. _data = tobj.Load(this.primaryKey.name + " in (" + sb.ToString() + ")", "", primaryKeys);
  336. }
  337. }
  338. catch (ApplicationException ex)
  339. {
  340. this.loadErrors.Add(ex);
  341. return false;
  342. }
  343. return true;
  344. }
  345. public string gridFieldsJS()
  346. {
  347. StringBuilder sb = new StringBuilder();
  348. foreach (DisplayField df in this.fields)
  349. {
  350. sb.Append(df.toJS());
  351. sb.Append(",");
  352. }
  353. if (sb.Length > 0) { sb.Remove(sb.Length - 1, 1); }
  354. return "[" + sb.ToString() + "]";
  355. }
  356. }
  357. public class DisplayField
  358. {
  359. private Func<IntelWebObject, object> _formatFunction;
  360. private string _displayName;
  361. private string _fieldName;
  362. private string _jsFormatFunction;
  363. private string _jsValidationFunction;
  364. private string _defaultValue;
  365. private int _maxLength;
  366. private bool _allowNull;
  367. private bool _isHeaderField;
  368. private bool _submitField = true;
  369. private string _calculation;
  370. private IntelWebField _defaultField;
  371. private string _formFieldName;
  372. public DisplayField()
  373. {
  374. }
  375. public DisplayField(WebGrid myGrid, string fieldName, string myDisplayName, string myJSFormatFunction, bool myIsHeaderField, string myFormFieldName)
  376. {
  377. if (myGrid.controls != null)
  378. {
  379. var control = GridDefUtil.getControl(myGrid.controls, myFormFieldName);
  380. if (control == null)
  381. {
  382. _formFieldName = myFormFieldName;
  383. }
  384. else
  385. {
  386. _formFieldName = control.ClientID;
  387. }
  388. }
  389. else
  390. {
  391. _formFieldName = myFormFieldName;
  392. }
  393. _jsFormatFunction = myJSFormatFunction;
  394. _displayName = myDisplayName;
  395. var myField = myGrid.protoObject.Field(fieldName);
  396. if (myField == null)
  397. {
  398. throw new ApplicationException("The field [" + fieldName + "] does not exist as a field in the prototype for [" + myGrid.protoObject.GetType().Name + "]");
  399. }
  400. _fieldName = fieldName;
  401. _allowNull = myField.nullable;
  402. if (myField.type == System.Data.OleDb.OleDbType.VarChar || myField.type == System.Data.OleDb.OleDbType.VarWChar || myField.type == System.Data.OleDb.OleDbType.Char || myField.type == System.Data.OleDb.OleDbType.LongVarChar || myField.type == System.Data.OleDb.OleDbType.LongVarWChar)
  403. {
  404. _maxLength = myField.parameter.Size;
  405. }
  406. else
  407. {
  408. _maxLength = 0;
  409. }
  410. _isHeaderField = myIsHeaderField;
  411. if (myField.version == System.Data.DataRowVersion.Default)
  412. {
  413. _defaultField = myField;
  414. }
  415. }
  416. public string formFieldName
  417. {
  418. get { return _formFieldName; }
  419. set { _formFieldName = value; }
  420. }
  421. public bool submitField
  422. {
  423. get { return _submitField; }
  424. set { _submitField = value; }
  425. }
  426. public string calculation
  427. {
  428. get { return _calculation; }
  429. set { _calculation = value; }
  430. }
  431. public bool isHeaderField
  432. {
  433. get { return _isHeaderField; }
  434. set { _isHeaderField = value; }
  435. }
  436. public string jsValidationFunction
  437. {
  438. get { return _jsValidationFunction; }
  439. set { _jsValidationFunction = value; }
  440. }
  441. public string defaultValue
  442. {
  443. get { return _defaultValue; }
  444. set { _defaultValue = value; }
  445. }
  446. public int maxLength
  447. {
  448. get { return _maxLength; }
  449. set { _maxLength = value; }
  450. }
  451. public bool allowNull
  452. {
  453. get { return _allowNull; }
  454. set { _allowNull = value; }
  455. }
  456. public string jsFormatFunction
  457. {
  458. get { return _jsFormatFunction; }
  459. set { _jsFormatFunction = value; }
  460. }
  461. public string displayname
  462. {
  463. get { return _displayName; }
  464. set { _displayName = value; }
  465. }
  466. public string fieldName
  467. {
  468. get { return _fieldName; }
  469. set { _fieldName = value; }
  470. }
  471. public Func<IntelWebObject, object> formatFunction
  472. {
  473. get { return _formatFunction; }
  474. set { _formatFunction = value; }
  475. }
  476. public string toJS(string extraPropertyName, string javaEscapedExtraPropertyValue)
  477. {
  478. return "{name:'" + JSUtil.EnquoteJS(this.fieldName) +
  479. "',displayName:'" + JSUtil.EnquoteJS(this.displayname) +
  480. "',formatter:" + ((this.jsFormatFunction != null) ? "function(obj){" + this.jsFormatFunction + "}" : "null") +
  481. ",maxLength:" + this.maxLength.ToString() +
  482. ",allowBlank:" + (this.allowNull ? "true" : "false") +
  483. ",defaultValue:" + ((_defaultField != null) ? JSUtil.SerializeJSProperty(_defaultField) : "'" + JSUtil.EnquoteJS(this.defaultValue) + "'") +
  484. ",validation:" + ((this.jsValidationFunction != null) ? "function(obj){" + this.jsValidationFunction + "}" : "null") +
  485. ",isHeaderField:" + (this.isHeaderField ? "true" : "false") +
  486. ",submitField:" + (this.submitField ? "true" : "false") +
  487. ",formFieldName:'" + JSUtil.EnquoteJS(this.formFieldName) + "'" +
  488. ((extraPropertyName != null) ? "," + extraPropertyName + ":" + javaEscapedExtraPropertyValue : "") +
  489. "}";
  490. }
  491. public string toJS()
  492. {
  493. return this.toJS(null, null);
  494. }
  495. }
  496. }