PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/BaliEnterpriseSystems/BaliEnterpriseSystems/BestObjects/BestTable.cs

https://github.com/sirivedula/BEST
C# | 516 lines | 455 code | 56 blank | 5 comment | 47 complexity | 4c007eb804fdf8afe0327f1aadea2a99 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. namespace BaliEnterpriseSystems.BestObjects
  17. {
  18. public class BestTable : BestDatabase
  19. {
  20. private BestFields _fields = new BestFields();
  21. private BestRows _rows = new BestRows();
  22. public BestTable()
  23. {
  24. this.currentRowId = 0;
  25. }
  26. public string SecurityPage
  27. {
  28. get;
  29. set;
  30. }
  31. public string TableName
  32. {
  33. get;
  34. set;
  35. }
  36. public BestFields TableFields
  37. {
  38. get { return _fields; }
  39. set { _fields = value; }
  40. }
  41. public BestRows TableRows
  42. {
  43. get
  44. {
  45. return _rows;
  46. }
  47. }
  48. public bool HasRows
  49. {
  50. get
  51. {
  52. return (TableRows.Count > 0);
  53. }
  54. }
  55. private BestRow NewRow()
  56. {
  57. BestRow brow = new BestRow();
  58. brow.bestTable = this;
  59. brow.IsNew = true;
  60. brow.Fields = CopyFields();
  61. return brow;
  62. }
  63. private BestFields CopyFields()
  64. {
  65. BestFields bfields = new BestFields();
  66. for (int i = 0; i < TableFields.Count; i++)
  67. {
  68. bfields.Add(TableFields[i].Clone());
  69. }
  70. return bfields;
  71. }
  72. public int currentRowId
  73. {
  74. get;
  75. set;
  76. }
  77. public BestRow CurrentRow
  78. {
  79. get
  80. {
  81. if (! HasRows)
  82. {
  83. TableRows.Add(NewRow());
  84. }
  85. return TableRows[currentRowId];
  86. }
  87. }
  88. private void ConstructRow(BestRow brow, OleDbDataReader objDataReader)
  89. {
  90. brow.IsNew = false;
  91. for (int i = 0; i < brow.Fields.Count; i++ )
  92. {
  93. if (brow.Fields[i].fieldType.Equals("System.Byte"))
  94. {
  95. brow.Fields[i].fieldValue = objDataReader[brow.Fields[i].fieldName].ToString().Equals("True")?"1":"0";
  96. brow.Fields[i].OldValue = brow.Fields[i].fieldValue;
  97. }
  98. else
  99. {
  100. brow.Fields[i].fieldValue = objDataReader[brow.Fields[i].fieldName].ToString();
  101. brow.Fields[i].OldValue = brow.Fields[i].fieldValue;
  102. }
  103. }
  104. }
  105. private string fieldCSV
  106. {
  107. get
  108. {
  109. StringBuilder sb = new StringBuilder();
  110. for (int i = 0; i < TableFields.Count; i++)
  111. {
  112. sb.Append(TableFields[i].fieldName);
  113. if (i < TableFields.Count - 1) sb.Append(",");
  114. }
  115. return sb.ToString();
  116. }
  117. }
  118. public void LoadRows(string orderby)
  119. {
  120. OleDbCommand myCmd = this.dbCmd;
  121. string sql = "select " + fieldCSV + " from " + this.TableName;
  122. if (! string.IsNullOrEmpty(orderby))
  123. {
  124. sql += " order by " + orderby.Replace("'","''");
  125. }
  126. myCmd.CommandText = sql;
  127. OleDbDataReader tblReader = myCmd.ExecuteReader();
  128. while (tblReader.Read())
  129. {
  130. BestRow tempRow = NewRow();
  131. this.ConstructRow(tempRow, tblReader);
  132. this.TableRows.Add(tempRow);
  133. }
  134. tblReader.Close();
  135. }
  136. public void LoadRows()
  137. {
  138. OleDbCommand myCmd = this.dbCmd;
  139. myCmd.CommandText = "select " + fieldCSV + " from " + this.TableName;
  140. OleDbDataReader tblReader = myCmd.ExecuteReader();
  141. while (tblReader.Read())
  142. {
  143. BestRow tempRow = NewRow();
  144. this.ConstructRow(tempRow, tblReader);
  145. this.TableRows.Add(tempRow);
  146. }
  147. tblReader.Close();
  148. }
  149. public void LoadRows(string whereclause, List<BestField> cmdParams, string orderby)
  150. {
  151. OleDbCommand myCmd = this.dbCmd;
  152. string sql = "select " + fieldCSV + " from " + this.TableName + " where " + whereclause;
  153. if(!string.IsNullOrEmpty(orderby))
  154. {
  155. sql += " order by " + orderby.Replace("'","''");
  156. }
  157. myCmd.CommandText = sql;
  158. if (cmdParams != null)
  159. {
  160. foreach (BestField cmdParam in cmdParams)
  161. {
  162. myCmd.Parameters.Add(cmdParam.Param);
  163. }
  164. }
  165. OleDbDataReader tblReader = myCmd.ExecuteReader();
  166. while (tblReader.Read())
  167. {
  168. BestRow tempRow = NewRow();
  169. this.ConstructRow(tempRow, tblReader);
  170. this.TableRows.Add(tempRow);
  171. }
  172. tblReader.Close();
  173. }
  174. public void LoadRows(string whereclause, List<BestField> cmdParams)
  175. {
  176. OleDbCommand myCmd = this.dbCmd;
  177. myCmd.CommandText = "select " + fieldCSV + " from " + this.TableName + " where " + whereclause;
  178. if (cmdParams != null)
  179. {
  180. foreach (BestField cmdParam in cmdParams)
  181. {
  182. myCmd.Parameters.Add(cmdParam.Param);
  183. }
  184. }
  185. OleDbDataReader tblReader = myCmd.ExecuteReader();
  186. while (tblReader.Read())
  187. {
  188. BestRow tempRow = NewRow();
  189. this.ConstructRow(tempRow, tblReader);
  190. this.TableRows.Add(tempRow);
  191. }
  192. tblReader.Close();
  193. }
  194. public void LoadRows(string whereclause, string param1, string value1, string orderby)
  195. {
  196. BestField bfld = new BestField() { fieldName = param1, fieldType="System.String", paramOledbType = OleDbType.VarChar, fieldSize = 50 };
  197. bfld.fieldValue = value1;
  198. List<BestField> bparam = new List<BestField>();
  199. bparam.Add(bfld);
  200. this.LoadRows(whereclause, bparam, orderby);
  201. }
  202. public void LoadRows(string whereclause, string param1, Guid value1, string orderby)
  203. {
  204. BestField bfld = new BestField() { fieldName = param1, fieldType ="System.Guid", paramOledbType = OleDbType.Guid, fieldSize = 50 };
  205. bfld.fieldValue = value1.ToString();
  206. List<BestField> bparam = new List<BestField>();
  207. bparam.Add(bfld);
  208. this.LoadRows(whereclause, bparam, orderby);
  209. }
  210. public bool allowAdd
  211. {
  212. get
  213. {
  214. if (overrideAdd.HasValue) return overrideAdd.Value;
  215. if (Utils.User != null)
  216. {
  217. return Utils.User.UserRoleByName(this.SecurityPage).allowAdd;
  218. }
  219. return false;
  220. }
  221. }
  222. public bool allowEdit
  223. {
  224. get
  225. {
  226. if (overrideEdit.HasValue) return overrideEdit.Value;
  227. if (Utils.User != null)
  228. {
  229. return Utils.User.UserRoleByName(this.SecurityPage).allowEdit;
  230. }
  231. return false;
  232. }
  233. }
  234. public bool allowDelete
  235. {
  236. get
  237. {
  238. if (overrideDelete.HasValue) return overrideDelete.Value;
  239. if (Utils.User != null)
  240. {
  241. return Utils.User.UserRoleByName(this.SecurityPage).allowDelete;
  242. }
  243. return false;
  244. }
  245. }
  246. public bool? overrideAdd
  247. {
  248. get;
  249. set;
  250. }
  251. public bool? overrideEdit
  252. {
  253. get;
  254. set;
  255. }
  256. public bool? overrideDelete
  257. {
  258. get;
  259. set;
  260. }
  261. }
  262. /* Table Rows */
  263. public class BestRows
  264. {
  265. public List<BestRow> Rows = new List<BestRow>();
  266. public BestRow this[int i]
  267. {
  268. get
  269. {
  270. return Rows[i];
  271. }
  272. }
  273. public void Add(BestRow field)
  274. {
  275. Rows.Add(field);
  276. }
  277. public void Clear()
  278. {
  279. Rows.Clear();
  280. }
  281. public int Count
  282. {
  283. get
  284. {
  285. return Rows.Count;
  286. }
  287. }
  288. }
  289. /* Row */
  290. public class BestRow
  291. {
  292. public BestRow()
  293. {
  294. lastError = "";
  295. }
  296. public BestFields Fields
  297. {
  298. get;
  299. set;
  300. }
  301. public bool IsNew
  302. {
  303. get;
  304. set;
  305. }
  306. public bool IsDelete
  307. {
  308. get;
  309. set;
  310. }
  311. public BestTable bestTable
  312. {
  313. get;
  314. set;
  315. }
  316. private bool HasChanges
  317. {
  318. get
  319. {
  320. bool changed = false;
  321. for (int i = 0; i < Fields.Count; i++)
  322. {
  323. changed = changed || (Fields[i].hasChange);
  324. }
  325. return changed;
  326. }
  327. }
  328. private string _fieldQues;
  329. private string fieldQues
  330. {
  331. get
  332. {
  333. return _fieldQues;
  334. }
  335. }
  336. private List<OleDbParameter> _Params;
  337. private List<OleDbParameter> Params
  338. {
  339. get
  340. {
  341. if (_Params == null)
  342. {
  343. _Params = new List<OleDbParameter>();
  344. }
  345. return _Params;
  346. }
  347. }
  348. private string InsfieldCSV()
  349. {
  350. string fieldList = "";
  351. _fieldQues = "";
  352. Params.Clear();
  353. for (int i = 0; i < Fields.Count; i++)
  354. {
  355. if (Fields[i].fieldValue != null)
  356. {
  357. fieldList += Fields[i].fieldName + ",";
  358. _fieldQues += "?,";
  359. Params.Add(Fields[i].Param);
  360. }
  361. }
  362. if (fieldList.Length > 0)
  363. {
  364. fieldList = fieldList.Substring(0, fieldList.Length - 1);
  365. _fieldQues = _fieldQues.Substring(0, _fieldQues.Length - 1);
  366. }
  367. return fieldList;
  368. }
  369. public string lastError
  370. {
  371. get;
  372. set;
  373. }
  374. private string updfieldSQL()
  375. {
  376. string fieldList = "";
  377. Params.Clear();
  378. for (int i = 0; i < Fields.Count; i++)
  379. {
  380. if (!(Fields[i].fieldName.Equals("guidfield")))
  381. {
  382. if (Fields[i].hasChange)
  383. {
  384. fieldList += Fields[i].fieldName + " = ? ,";
  385. Params.Add(Fields[i].Param);
  386. }
  387. }
  388. }
  389. if (fieldList.Length > 0)
  390. {
  391. fieldList = fieldList.Substring(0, fieldList.Length - 1);
  392. }
  393. return fieldList;
  394. }
  395. public virtual bool Save()
  396. {
  397. bool result = true;
  398. if (IsDelete && bestTable.allowDelete)
  399. {
  400. //Delete
  401. OleDbCommand myCmd = bestTable.dbCmd;
  402. myCmd.CommandText = "delete from " + bestTable.TableName + " where guidfield = ?";
  403. myCmd.Parameters.Add(Fields["guidfield"].Param);
  404. try
  405. {
  406. myCmd.ExecuteNonQuery();
  407. }
  408. catch (Exception ex)
  409. {
  410. lastError += ex.Message;
  411. result = false;
  412. }
  413. }
  414. else if (IsNew && bestTable.allowAdd)
  415. {
  416. //Insert
  417. OleDbCommand myCmd = bestTable.dbCmd;
  418. myCmd.CommandText = "insert into " + bestTable.TableName + "(" + this.InsfieldCSV() + ") values (" + fieldQues + ")";
  419. for(int i=0; i<Params.Count;i++)
  420. {
  421. myCmd.Parameters.Add(Params[i]);
  422. }
  423. try
  424. {
  425. myCmd.ExecuteNonQuery();
  426. }
  427. catch(Exception ex)
  428. {
  429. lastError += ex.Message;
  430. result = false;
  431. }
  432. }
  433. else if (HasChanges && bestTable.allowEdit)
  434. {
  435. //Update
  436. OleDbCommand myCmd = bestTable.dbCmd;
  437. myCmd.CommandText = "update " + bestTable.TableName + " set " + updfieldSQL() + " where guidfield = ?";
  438. for (int i = 0; i < Params.Count; i++)
  439. {
  440. myCmd.Parameters.Add(Params[i]);
  441. }
  442. myCmd.Parameters.Add(Fields["guidfield"].Param);
  443. try
  444. {
  445. myCmd.ExecuteNonQuery();
  446. }
  447. catch (Exception ex)
  448. {
  449. lastError += ex.Message;
  450. result = false;
  451. }
  452. }
  453. return result;
  454. }
  455. public virtual bool canDelete()
  456. {
  457. return true;
  458. }
  459. }
  460. }