PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v41/Source/Widgetsphere.Generator.DAL/ProjectItemGenerators/BusinessObjectLINQ/BusinessObjectLINQGeneratedTemplate.cs

#
C# | 287 lines | 209 code | 37 blank | 41 comment | 17 complexity | daf6d6e71c2d38b9c75392ef30b26b9d MD5 | raw file
Possible License(s): JSON, CC-BY-SA-3.0
  1. #region Copyright (c) 2006-2011 Widgetsphere LLC, All Rights Reserved
  2. //--------------------------------------------------------------------- *
  3. // Widgetsphere LLC *
  4. // Copyright (c) 2006-2011 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.Linq;
  40. using System.Text;
  41. using Widgetsphere.Generator.Models;
  42. namespace Widgetsphere.Generator.DAL.ProjectItemGenerators.BusinessObjectLINQ
  43. {
  44. class BusinessObjectLINQGeneratedTemplate : DomainProjectTemplate
  45. {
  46. private readonly StringBuilder sb = new StringBuilder();
  47. private readonly Table _currentTable;
  48. public BusinessObjectLINQGeneratedTemplate(ModelRoot model, Table currentTable)
  49. {
  50. _model = model;
  51. _currentTable = currentTable;
  52. }
  53. #region BaseClassTemplate overrides
  54. public override string FileName
  55. {
  56. get { return string.Format("{0}.Generated.cs", _currentTable.PascalName); }
  57. }
  58. public string ParentItemName
  59. {
  60. get { return string.Format("{0}.cs", _currentTable.PascalName); }
  61. }
  62. public override string FileContent
  63. {
  64. get
  65. {
  66. GenerateContent();
  67. return sb.ToString();
  68. }
  69. }
  70. #endregion
  71. #region GenerateContent
  72. private void GenerateContent()
  73. {
  74. try
  75. {
  76. Widgetsphere.Generator.GenerationHelper.AppendCopyrightInCode(sb, _model);
  77. this.AppendUsingStatements();
  78. sb.AppendLine("namespace " + this.GetLocalNamespace() + ".Business.LINQ");
  79. sb.AppendLine("{");
  80. this.AppendClass();
  81. sb.AppendLine("}");
  82. }
  83. catch (Exception ex)
  84. {
  85. throw;
  86. }
  87. }
  88. #endregion
  89. #region namespace / objects
  90. public void AppendUsingStatements()
  91. {
  92. sb.AppendLine("using System;");
  93. sb.AppendLine("using System.Data;");
  94. sb.AppendLine("using System.Linq;");
  95. sb.AppendLine("using System.Data.Linq;");
  96. sb.AppendLine("using System.Linq.Expressions;");
  97. sb.AppendLine("using System.Data.Linq.Mapping;");
  98. sb.AppendLine("using System.Collections;");
  99. sb.AppendLine("using System.Collections.Generic;");
  100. sb.AppendLine("using Widgetsphere.Core.DataAccess;");
  101. sb.AppendLine();
  102. }
  103. private void AppendClass()
  104. {
  105. try
  106. {
  107. sb.AppendLine(" /// <summary>");
  108. sb.AppendLine(" /// This is a helper object for running LINQ queries on the " + _currentTable.PascalName + " collection.");
  109. sb.AppendLine(" /// </summary>");
  110. sb.AppendLine(" [Serializable()]");
  111. sb.AppendLine(" [Table(Name = \"" + _currentTable.DatabaseName + "\")]");
  112. sb.AppendLine(" public partial class " + _currentTable.PascalName + "Query : Widgetsphere.Core.DataAccess.IBusinessObjectLINQQuery");
  113. sb.AppendLine(" {");
  114. sb.AppendLine(" #region Properties");
  115. var allTables = _currentTable.GetTableHierarchy();
  116. var columnList = _currentTable.GetColumnsFullHierarchy().Where(x => x.Generated).ToList();
  117. foreach (var c in columnList.OrderBy(x => x.Name))
  118. {
  119. var description = c.Description.Trim();
  120. if (!string.IsNullOrEmpty(description)) description += "\r\n ///";
  121. description += "(Maps to the '" + _currentTable.DatabaseName + "." + c.DatabaseName + "' database field)";
  122. sb.AppendLine(" /// <summary>");
  123. sb.AppendLine(" /// " + description);
  124. sb.AppendLine(" /// </summary>");
  125. sb.Append(" [Column(");
  126. sb.Append("Name = \"" + c.DatabaseName + "\", ");
  127. sb.Append("DbType = \"" + c.DatabaseTypeRaw + "\", ");
  128. sb.Append("CanBeNull = " + c.AllowNull.ToString().ToLower() + ", ");
  129. sb.Append("IsPrimaryKey = " + _currentTable.PrimaryKeyColumns.Contains(c).ToString().ToLower());
  130. sb.AppendLine(")]");
  131. sb.AppendLine(" public virtual " + c.GetCodeType(true) + " " + c.PascalName + " { get; set; }");
  132. }
  133. if (_currentTable.AllowCreateAudit)
  134. {
  135. sb.AppendLine(" /// <summary>");
  136. sb.AppendLine(" /// The date of creation");
  137. sb.AppendLine(" /// </summary>");
  138. sb.AppendLine(" [Column(Name = \"" + _model.Database.CreatedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
  139. sb.AppendLine(" public virtual DateTime? " + _model.Database.CreatedDatePascalName + " { get; set; }");
  140. sb.AppendLine(" /// <summary>");
  141. sb.AppendLine(" /// The name of the creating entity");
  142. sb.AppendLine(" /// </summary>");
  143. sb.AppendLine(" [Column(Name = \"" + _model.Database.CreatedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
  144. sb.AppendLine(" public virtual string " + _model.Database.CreatedByPascalName + " { get; set; }");
  145. }
  146. if (_currentTable.AllowModifiedAudit)
  147. {
  148. sb.AppendLine(" /// <summary>");
  149. sb.AppendLine(" /// The date of last modification");
  150. sb.AppendLine(" /// </summary>");
  151. sb.AppendLine(" [Column(Name = \"" + _model.Database.ModifiedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
  152. sb.AppendLine(" public virtual DateTime? " + _model.Database.ModifiedDatePascalName + " { get; set; }");
  153. sb.AppendLine(" /// <summary>");
  154. sb.AppendLine(" /// The name of the last modifing entity");
  155. sb.AppendLine(" /// </summary>");
  156. sb.AppendLine(" [Column(Name = \"" + _model.Database.ModifiedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
  157. sb.AppendLine(" public virtual string " + _model.Database.ModifiedByPascalName + " { get; set; }");
  158. }
  159. if (_currentTable.AllowTimestamp)
  160. {
  161. sb.AppendLine(" /// <summary>");
  162. sb.AppendLine(" /// This is an internal field and is not to be used.");
  163. sb.AppendLine(" /// </summary>");
  164. sb.AppendLine(" [Column(Name = \"" + _model.Database.TimestampColumnName + "\", DbType = \"Binary\", CanBeNull = false)]");
  165. sb.AppendLine(" public virtual byte[] " + _model.Database.TimestampPascalName + " { get; set; }");
  166. }
  167. sb.AppendLine();
  168. //Add child relationships
  169. foreach (var relation in _model.Database.Relations.FindByParentTable(_currentTable, true).Where(x => x.IsGenerated))
  170. {
  171. //Relation relation = (Relation)reference.Object;
  172. var childTable = (Table)relation.ChildTableRef.Object;
  173. //Column pkColumn = (Column)relation.ColumnRelationships[0].ChildColumnRef.Object;
  174. var thisKey = string.Empty;
  175. var otherKey = string.Empty;
  176. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  177. {
  178. thisKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  179. otherKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  180. }
  181. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  182. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  183. if ((childTable.Generated) && (!allTables.Contains(childTable)))
  184. {
  185. sb.AppendLine(" /// <summary>");
  186. sb.AppendLine(" /// This is a mapping of the relationship with the " + childTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  187. sb.AppendLine(" /// </summary>");
  188. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  189. sb.AppendLine(" public " + this.GetLocalNamespace() + ".Business.LINQ." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + " { get; private set; }");
  190. }
  191. }
  192. //Add parent relationships
  193. foreach (var relation in _model.Database.Relations.FindByChildTable(_currentTable, true).Where(x => x.IsGenerated))
  194. {
  195. var parentTable = (Table)relation.ParentTableRef.Object;
  196. //Do not process self-referencing relationships
  197. if (parentTable != _currentTable)
  198. {
  199. var thisKey = string.Empty;
  200. var otherKey = string.Empty;
  201. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  202. {
  203. thisKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  204. otherKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  205. }
  206. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  207. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  208. if ((parentTable.Generated) && (!allTables.Contains(parentTable)))
  209. {
  210. sb.AppendLine(" /// <summary>");
  211. sb.AppendLine(" /// This is a mapping of the relationship with the " + parentTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  212. sb.AppendLine(" /// </summary>");
  213. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  214. sb.AppendLine(" public " + this.GetLocalNamespace() + ".Business.LINQ." + parentTable.PascalName + "Query " + relation.PascalRoleName + parentTable.PascalName + " { get; private set; }");
  215. }
  216. }
  217. }
  218. sb.AppendLine();
  219. sb.AppendLine(" #endregion");
  220. sb.AppendLine();
  221. //Add helper method to mask primary key
  222. sb.AppendLine(" #region SinglePrimaryKeyValue");
  223. sb.AppendLine(" internal string SinglePrimaryKeyValue()");
  224. sb.AppendLine(" {");
  225. sb.AppendLine(" var retval = string.Empty;");
  226. sb.Append(" retval += ");
  227. var index = 0;
  228. foreach (var pk in _currentTable.PrimaryKeyColumns.OrderBy(x => x.Name))
  229. {
  230. sb.Append("this." + pk.PascalName + ".ToString()");
  231. if (index < _currentTable.PrimaryKeyColumns.Count - 1)
  232. sb.Append(" + \"|~\" + ");
  233. index++;
  234. }
  235. sb.AppendLine(";");
  236. sb.AppendLine(" return retval;");
  237. sb.AppendLine(" }");
  238. sb.AppendLine(" #endregion");
  239. sb.AppendLine();
  240. sb.AppendLine(" }");
  241. sb.AppendLine();
  242. }
  243. catch (Exception ex)
  244. {
  245. throw;
  246. }
  247. }
  248. #endregion
  249. }
  250. }