PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v41/Source/Widgetsphere.Generator.DAL/ProjectItemGenerators/BusinessComponentLINQ/BusinessComponentLINQGeneratedTemplate.cs

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