PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/Last2008/Source/Widgetsphere.Generator.DALProxy/ProjectItemGenerators/DataTransferViewExtensionObject/DTOExtensionViewGeneratedTemplate.cs

#
C# | 367 lines | 297 code | 32 blank | 38 comment | 1 complexity | 1b30a1389fb10f1689d314cdb92e21c6 MD5 | raw file
Possible License(s): JSON, CC-BY-SA-3.0
  1. #region Copyright (c) 2006-2010 Widgetsphere LLC, All Rights Reserved
  2. //--------------------------------------------------------------------- *
  3. // Widgetsphere LLC *
  4. // Copyright (c) 2006-2010 All Rights reserved *
  5. // *
  6. // *
  7. //This file and its contents are protected by United States and *
  8. //International copyright laws. Unauthorized reproduction and/or *
  9. //distribution of all or any portion of the code contained herein *
  10. //is strictly prohibited and will result in severe civil and criminal *
  11. //penalties. Any violations of this copyright will be prosecuted *
  12. //to the fullest extent possible under law. *
  13. // *
  14. //THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED *
  15. //TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND *
  16. //TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
  17. //CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY *
  18. //THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC *
  19. // *
  20. //UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN *
  21. //PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR *
  22. //SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT. *
  23. // *
  24. //THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE *
  25. //CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE, *
  26. //INC. THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO *
  27. //INSURE ITS CONFIDENTIALITY. *
  28. // *
  29. //THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT *
  30. //PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN *
  31. //EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING *
  32. //THE PRODUCT. IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE *
  33. //SOURCE CODE CONTAINED HEREIN. *
  34. // *
  35. //THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE. *
  36. //--------------------------------------------------------------------- *
  37. #endregion
  38. using System;
  39. using System.Collections.Generic;
  40. using System.Text;
  41. using System.Linq;
  42. using Widgetsphere.Generator.Models;
  43. using Widgetsphere.Generator.Common.Util;
  44. using System.Collections;
  45. using System.Collections.ObjectModel;
  46. using Widgetsphere.Generator.ProjectItemGenerators;
  47. namespace Widgetsphere.Generator.DALProxy.ProjectItemGenerators.DataTransferViewExtensionObject
  48. {
  49. class DTOExtensionViewGeneratedTemplate : BaseDALProxyTemplate
  50. {
  51. private StringBuilder sb = new StringBuilder();
  52. private CustomView _currentView;
  53. public DTOExtensionViewGeneratedTemplate(ModelRoot model, CustomView view)
  54. {
  55. _model = model;
  56. _currentView = view;
  57. }
  58. #region BaseClassTemplate overrides
  59. public override string FileName
  60. {
  61. get { return string.Format("{0}.Generated.cs", _currentView.PascalName); }
  62. }
  63. public string ParentItemName
  64. {
  65. get { return string.Format("{0}.cs", _currentView.PascalName); }
  66. }
  67. public override string FileContent
  68. {
  69. get
  70. {
  71. GenerateContent();
  72. return sb.ToString();
  73. }
  74. }
  75. #endregion
  76. #region GenerateContent
  77. private void GenerateContent()
  78. {
  79. try
  80. {
  81. ValidationHelper.AppendCopyrightInCode(sb, _model);
  82. this.AppendUsingStatements();
  83. sb.AppendLine("namespace " + DefaultNamespace + ".Extensions");
  84. sb.AppendLine("{");
  85. this.AppendClass();
  86. sb.AppendLine("}");
  87. }
  88. catch (Exception ex)
  89. {
  90. throw;
  91. }
  92. }
  93. #endregion
  94. #region namespace / objects
  95. public void AppendUsingStatements()
  96. {
  97. sb.AppendLine("using System;");
  98. sb.AppendLine("using System.Collections.Generic;");
  99. sb.AppendLine("using System.Linq;");
  100. sb.AppendLine("using System.Text;");
  101. sb.AppendLine("using System.Xml;");
  102. sb.AppendLine("using System.ComponentModel;");
  103. sb.AppendLine("using System.Data;");
  104. sb.AppendLine("using " + _model.CompanyName + "." + _model.ProjectName + ".DataTransfer;");
  105. sb.AppendLine("using " + _model.CompanyName + "." + _model.ProjectName + ".Business.Objects;");
  106. sb.AppendLine("using Widgetsphere.Core.DataAccess;");
  107. sb.AppendLine("using System.Linq.Expressions;");
  108. sb.AppendLine("using " + _model.CompanyName + "." + _model.ProjectName + ".Business.LINQ;");
  109. sb.AppendLine("using Widgetsphere.Core.Util;");
  110. sb.AppendLine("using " + _model.CompanyName + "." + _model.ProjectName + ".Business.Views;");
  111. sb.AppendLine();
  112. }
  113. private void AppendClass()
  114. {
  115. try
  116. {
  117. sb.AppendLine(" /// <summary>");
  118. sb.AppendLine(" /// This is a set of extensions for the " + _currentView.PascalName + " DAL/DTO object");
  119. sb.AppendLine(" /// </summary>");
  120. sb.AppendLine(" public static partial class " + _currentView.PascalName + "Extension");
  121. sb.AppendLine(" {");
  122. #region Fill DAL
  123. sb.AppendLine(" #region Fill DAL");
  124. sb.AppendLine();
  125. sb.AppendLine(" /// <summary>");
  126. sb.AppendLine(" /// Populate a DAL object from its corresponding DTO.");
  127. sb.AppendLine(" /// </summary>");
  128. sb.AppendLine(" /// <param name=\"item\">The empty DAL object.</param>");
  129. sb.AppendLine(" /// <param name=\"dtoItem\">The source DTO to load.</param>");
  130. sb.AppendLine(" public static void Fill(this " + _currentView.PascalName + " item, " + _currentView.PascalName + "DTO dtoItem)");
  131. sb.AppendLine(" {");
  132. sb.AppendLine(" if (dtoItem == null) throw new Exception(\"The " + _currentView.PascalName + " DTO cannot be null.\");");
  133. sb.AppendLine(" if (item == null) throw new Exception(\"The " + _currentView.PascalName + " item cannot be null.\");");
  134. sb.AppendLine(" DataRow dr = ((DataRow)((IWrappingClass)item).WrappedClass);");
  135. foreach (CustomViewColumn column in _currentView.GetColumns().Where(x => x.Generated))
  136. {
  137. if (column.AllowNull)
  138. {
  139. sb.AppendLine(" if (dtoItem." + column.PascalName + " == null) dr[\"" + column.DatabaseName + "\"] = System.DBNull.Value;");
  140. sb.AppendLine(" else dr[\"" + column.DatabaseName + "\"] = dtoItem." + column.PascalName + ";");
  141. }
  142. else
  143. {
  144. sb.AppendLine(" dr[\"" + column.DatabaseName + "\"] = dtoItem." + column.PascalName + ";");
  145. }
  146. }
  147. sb.AppendLine(" }");
  148. sb.AppendLine();
  149. sb.AppendLine(" #endregion");
  150. sb.AppendLine();
  151. #endregion
  152. #region Fill DTO
  153. sb.AppendLine(" #region Fill DTO");
  154. sb.AppendLine();
  155. //Fill for DTO collection
  156. sb.AppendLine(" /// <summary>");
  157. sb.AppendLine(" /// Populate a DTO object collection from its corresponding DAL object collection.");
  158. sb.AppendLine(" /// </summary>");
  159. sb.AppendLine(" /// <param name=\"list\">The destination DTO collection.</param>");
  160. sb.AppendLine(" /// <param name=\"" + _currentView.CamelName + "Collection\">The source DAL collection.</param>");
  161. sb.AppendLine(" public static void Fill(this List<" + _currentView.PascalName + "DTO> list, " + _currentView.PascalName + "Collection " + _currentView.CamelName + "Collection)");
  162. sb.AppendLine(" {");
  163. sb.AppendLine(" if (" + _currentView.CamelName + "Collection == null) throw new Exception(\"The " + _currentView.PascalName + " collection cannot be null.\");");
  164. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  165. sb.AppendLine(" foreach (" + _currentView.PascalName + " item in " + _currentView.CamelName + "Collection)");
  166. sb.AppendLine(" {");
  167. sb.AppendLine(" " + _currentView.PascalName + "DTO newItem = new " + _currentView.PascalName + "DTO();");
  168. sb.AppendLine(" newItem.Fill(item);");
  169. sb.AppendLine(" list.Add(newItem);");
  170. sb.AppendLine(" }");
  171. sb.AppendLine(" }");
  172. sb.AppendLine();
  173. sb.AppendLine(" /// <summary>");
  174. sb.AppendLine(" /// Populate a DTO object from its corresponding DAL object.");
  175. sb.AppendLine(" /// </summary>");
  176. sb.AppendLine(" /// <param name=\"item\">The source DAL object.</param>");
  177. sb.AppendLine(" /// <param name=\"dtoItem\">The empty DTO to load.</param>");
  178. sb.AppendLine(" public static void Fill(this " + _currentView.PascalName + "DTO dtoItem, " + _currentView.PascalName + " item)");
  179. sb.AppendLine(" {");
  180. sb.AppendLine(" if (dtoItem == null) throw new Exception(\"The " + _currentView.PascalName + " DTO cannot be null.\");");
  181. sb.AppendLine(" if (item == null) throw new Exception(\"The " + _currentView.PascalName + " item cannot be null.\");");
  182. foreach (CustomViewColumn column in _currentView.GetColumns().Where(x => x.Generated).OrderBy(x => x.DatabaseName))
  183. {
  184. //Normal property
  185. sb.AppendLine(" dtoItem." + column.PascalName + " = item." + column.PascalName + ";");
  186. }
  187. sb.AppendLine(" }");
  188. sb.AppendLine();
  189. sb.AppendLine(" #endregion");
  190. sb.AppendLine();
  191. #endregion
  192. #region RunSelect
  193. sb.AppendLine(" #region RunSelect");
  194. sb.AppendLine();
  195. sb.AppendLine(" /// <summary>");
  196. sb.AppendLine(" /// Select all objects from store.");
  197. sb.AppendLine(" /// </summary>");
  198. sb.AppendLine(" public static void RunSelect(this List<" + _currentView.PascalName + "DTO> list)");
  199. sb.AppendLine(" {");
  200. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  201. sb.AppendLine(" " + _currentView.PascalName + "Collection retval = " + _currentView.PascalName + "Collection.RunSelect();");
  202. sb.AppendLine(" foreach (" + _currentView.PascalName + " item in retval)");
  203. sb.AppendLine(" {");
  204. sb.AppendLine(" " + _currentView.PascalName + "DTO newItem = new " + _currentView.PascalName + "DTO();");
  205. sb.AppendLine(" newItem.Fill(item);");
  206. sb.AppendLine(" list.Add(newItem);");
  207. sb.AppendLine(" }");
  208. sb.AppendLine(" }");
  209. sb.AppendLine();
  210. //RunSelect LINQ
  211. sb.AppendLine(" /// <summary>");
  212. sb.AppendLine(" /// Using the specified Where expression, execute a query against the database to return a result set.");
  213. sb.AppendLine(" /// </summary>");
  214. sb.AppendLine(" public static void RunSelect(this List<" + _currentView.PascalName + "DTO> list, Expression<Func<" + _currentView.PascalName + "Query, bool>> where)");
  215. sb.AppendLine(" {");
  216. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  217. sb.AppendLine(" list.Fill(" + _currentView.PascalName + "Collection.RunSelect(where));");
  218. sb.AppendLine(" }");
  219. sb.AppendLine();
  220. sb.AppendLine(" #endregion");
  221. sb.AppendLine();
  222. #endregion
  223. #region RunSelect Paged
  224. sb.AppendLine(" #region RunSelect Paged");
  225. sb.AppendLine();
  226. sb.AppendLine(" /// <summary>");
  227. sb.AppendLine(" /// Using the specified Where expression, execute a query against the database to return a paged result set.");
  228. sb.AppendLine(" /// </summary>");
  229. sb.AppendLine(" public static PagedQueryResults<" + _currentView.PascalName + "DTO> RunSelect(this List<" + _currentView.PascalName + "DTO> list, string pageSize, string page, string sortOrder, string sortColumn, string linq)");
  230. sb.AppendLine(" {");
  231. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  232. sb.AppendLine(" #region Setup Variables and Error Check");
  233. sb.AppendLine(" int po;");
  234. sb.AppendLine(" int rpp;");
  235. sb.AppendLine();
  236. sb.AppendLine(" if (!int.TryParse(page, out po)) throw new Exception(\"The page number number is not a valid integer!\");");
  237. sb.AppendLine(" if (po < 1) po = 1;");
  238. sb.AppendLine();
  239. sb.AppendLine(" if (!int.TryParse(pageSize, out rpp)) throw new Exception(\"The page size is not a valid integer!\");");
  240. sb.AppendLine(" if (rpp < 1) rpp = 1;");
  241. sb.AppendLine();
  242. sb.AppendLine(" " + _currentView.PascalName + ".FieldNameConstants sortField;");
  243. sb.AppendLine(" try");
  244. sb.AppendLine(" {");
  245. sb.AppendLine(" sortField = (" + _currentView.PascalName + ".FieldNameConstants)Enum.Parse(typeof(" + _currentView.PascalName + ".FieldNameConstants), sortColumn);");
  246. sb.AppendLine(" }");
  247. sb.AppendLine(" catch");
  248. sb.AppendLine(" {");
  249. sb.AppendLine(" throw new Exception(\"The sort field is not valid!\");");
  250. sb.AppendLine(" }");
  251. sb.AppendLine();
  252. sb.AppendLine(" bool ascending = (sortOrder.ToLower() == \"asc\" || sortOrder.ToLower() == \"ascending\");");
  253. sb.AppendLine(" #endregion");
  254. sb.AppendLine();
  255. sb.AppendLine(" PagedQueryResults<" + _currentView.PascalName + "DTO> retVal = new PagedQueryResults<" + _currentView.PascalName + "DTO>();");
  256. sb.AppendLine(" " + _currentView.PascalName + "Collection." + _currentView.PascalName + "Paging paging = new " + _currentView.PascalName + "Collection." + _currentView.PascalName + "Paging();");
  257. sb.AppendLine(" paging.RecordsperPage = rpp;");
  258. sb.AppendLine(" paging.PageIndex = po;");
  259. sb.AppendLine(" paging.OrderByList.Add(new " + _currentView.PascalName + "Collection." + _currentView.PascalName + "PagingFieldItem(sortField, ascending));");
  260. sb.AppendLine();
  261. sb.AppendLine(" " + _currentView.PascalName + "Collection dalList = null;");
  262. sb.AppendLine(" if (string.IsNullOrEmpty(linq))");
  263. sb.AppendLine(" {");
  264. sb.AppendLine(" dalList = " + _currentView.PascalName + "Collection.RunSelect(x => true, paging);");
  265. sb.AppendLine(" }");
  266. sb.AppendLine(" else");
  267. sb.AppendLine(" {");
  268. sb.AppendLine(" System.Reflection.MethodInfo method = LINQDynamicCompile.GetLINQMethodPaged(linq, \"" + _model.CompanyName + "." + _model.ProjectName + ".Business\", \"" + _currentView.PascalName + "\");");
  269. sb.AppendLine(" dalList = (" + _currentView.PascalName + "Collection)method.Invoke(null, new object[] { paging });");
  270. sb.AppendLine(" }");
  271. sb.AppendLine();
  272. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  273. sb.AppendLine(" foreach (" + _currentView.PascalName + " item in dalList)");
  274. sb.AppendLine(" {");
  275. sb.AppendLine(" " + _currentView.PascalName + "DTO newItem = new " + _currentView.PascalName + "DTO();");
  276. sb.AppendLine(" newItem.Fill(item);");
  277. sb.AppendLine(" list.Add(newItem);");
  278. sb.AppendLine(" }");
  279. sb.AppendLine();
  280. sb.AppendLine(" retVal.CurrentPage = paging.PageIndex;");
  281. sb.AppendLine(" double totalPages = Math.Ceiling((double)paging.RecordCount / (double)paging.RecordsperPage);");
  282. sb.AppendLine(" retVal.TotalPages = (int)totalPages;");
  283. sb.AppendLine(" retVal.TotalRecords = paging.RecordCount;");
  284. sb.AppendLine(" retVal.GridData = list;");
  285. sb.AppendLine();
  286. sb.AppendLine(" return retVal;");
  287. sb.AppendLine(" }");
  288. sb.AppendLine();
  289. sb.AppendLine(" #endregion");
  290. sb.AppendLine();
  291. #endregion
  292. #region RunSelect Paged
  293. sb.AppendLine(" #region RunSelect Paged");
  294. sb.AppendLine();
  295. sb.AppendLine(" /// <summary>");
  296. sb.AppendLine(" /// Using the specified Where expression, execute a query against the database to return a result set.");
  297. sb.AppendLine(" /// </summary>");
  298. sb.AppendLine(" public static void RunSelect(this List<" + _currentView.PascalName + "DTO> list, string linq)");
  299. sb.AppendLine(" {");
  300. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  301. sb.AppendLine();
  302. sb.AppendLine(" " + _currentView.PascalName + "Collection dalList = null;");
  303. sb.AppendLine(" if (string.IsNullOrEmpty(linq))");
  304. sb.AppendLine(" {");
  305. sb.AppendLine(" dalList = " + _currentView.PascalName + "Collection.RunSelect(x => true);");
  306. sb.AppendLine(" }");
  307. sb.AppendLine(" else");
  308. sb.AppendLine(" {");
  309. sb.AppendLine(" System.Reflection.MethodInfo method = LINQDynamicCompile.GetLINQMethodAll(linq, \"" + _model.CompanyName + "." + _model.ProjectName + ".Business\", \"" + _currentView.PascalName + "\");");
  310. sb.AppendLine(" dalList = (" + _currentView.PascalName + "Collection)method.Invoke(null, new object[] { });");
  311. sb.AppendLine(" }");
  312. sb.AppendLine();
  313. sb.AppendLine(" if (list == null) list = new List<" + _currentView.PascalName + "DTO>();");
  314. sb.AppendLine(" foreach (" + _currentView.PascalName + " item in dalList)");
  315. sb.AppendLine(" {");
  316. sb.AppendLine(" " + _currentView.PascalName + "DTO newItem = new " + _currentView.PascalName + "DTO();");
  317. sb.AppendLine(" newItem.Fill(item);");
  318. sb.AppendLine(" list.Add(newItem);");
  319. sb.AppendLine(" }");
  320. sb.AppendLine(" }");
  321. sb.AppendLine();
  322. sb.AppendLine(" #endregion");
  323. sb.AppendLine();
  324. #endregion
  325. sb.AppendLine(" }");
  326. sb.AppendLine();
  327. }
  328. catch (Exception ex)
  329. {
  330. throw;
  331. }
  332. }
  333. #endregion
  334. }
  335. }