PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Source/nHydrate.Generator.EFCodeFirst/Generators/ViewEntity/ViewEntityGeneratedTemplate.cs

#
C# | 819 lines | 694 code | 63 blank | 62 comment | 46 complexity | 27a0e37f90469288649c5685f7f878ab 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 Widgetsphere.Generator.Common.GeneratorFramework;
  41. using Widgetsphere.Generator.Models;
  42. using System.Text;
  43. using Widgetsphere.Generator.Common.Util;
  44. using System.Collections.Generic;
  45. namespace Widgetsphere.Generator.EFCodeFirst.Generators.EFCSDL
  46. {
  47. public class ViewEntityGeneratedTemplate : EFCodeFirstBaseTemplate
  48. {
  49. private StringBuilder sb = new StringBuilder();
  50. private CustomView _currentView;
  51. public ViewEntityGeneratedTemplate(ModelRoot model, CustomView currentTable)
  52. {
  53. _model = model;
  54. _currentView = currentTable;
  55. }
  56. #region BaseClassTemplate overrides
  57. public override string FileName
  58. {
  59. get { return string.Format("{0}.Generated.cs", _currentView.PascalName); }
  60. }
  61. public string ParentItemName
  62. {
  63. get { return string.Format("{0}.cs", _currentView.PascalName); }
  64. }
  65. public override string FileContent
  66. {
  67. get
  68. {
  69. try
  70. {
  71. sb = new StringBuilder();
  72. this.GenerateContent();
  73. return sb.ToString();
  74. }
  75. catch (Exception ex)
  76. {
  77. throw;
  78. }
  79. }
  80. }
  81. #endregion
  82. #region GenerateContent
  83. private void GenerateContent()
  84. {
  85. try
  86. {
  87. ValidationHelper.AppendCopyrightInCode(sb, _model);
  88. this.AppendUsingStatements();
  89. sb.AppendLine("namespace " + this.GetLocalNamespace() + ".Entity");
  90. sb.AppendLine("{");
  91. this.AppendEntityClass();
  92. this.AppendPagingClass();
  93. sb.AppendLine("}");
  94. sb.AppendLine();
  95. }
  96. catch (Exception ex)
  97. {
  98. throw;
  99. }
  100. }
  101. private void AppendUsingStatements()
  102. {
  103. sb.AppendLine("using System;");
  104. sb.AppendLine("using System.Linq;");
  105. sb.AppendLine("using System.Runtime.Serialization;");
  106. sb.AppendLine("using System.Data.Objects.DataClasses;");
  107. sb.AppendLine("using System.Xml.Serialization;");
  108. sb.AppendLine("using System.ComponentModel;");
  109. sb.AppendLine("using System.Collections.Generic;");
  110. sb.AppendLine("using System.Data.Objects;");
  111. sb.AppendLine("using System.Text;");
  112. sb.AppendLine("using " + this.GetLocalNamespace() + ";");
  113. sb.AppendLine("using System.Text.RegularExpressions;");
  114. sb.AppendLine("using System.Linq.Expressions;");
  115. sb.AppendLine("using System.Data.Linq;");
  116. sb.AppendLine();
  117. }
  118. private void AppendEntityClass()
  119. {
  120. sb.AppendLine(" /// <summary>");
  121. sb.AppendLine(" /// The collection to hold '" + _currentView.PascalName + "' entities");
  122. if (!string.IsNullOrEmpty(_currentView.Description))
  123. sb.AppendLine(" /// " + _currentView.Description);
  124. sb.AppendLine(" /// </summary>");
  125. sb.AppendLine(" [EdmEntityTypeAttribute(NamespaceName = \"" + this.GetLocalNamespace() + ".Entity" + "\", Name = \"" + _currentView.PascalName + "\")]");
  126. sb.AppendLine(" [Serializable()]");
  127. sb.AppendLine(" [DataContractAttribute(IsReference = true)]");
  128. //sb.AppendLine(" [Widgetsphere.EFCore.Attributes.FieldNameConstantsAttribute(typeof(" + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ".FieldNameConstants))]");
  129. if (!string.IsNullOrEmpty(_currentView.Description))
  130. sb.AppendLine(" [System.ComponentModel.Description(\"" + _currentView.Description + "\")]");
  131. sb.AppendLine(" [System.ComponentModel.ImmutableObject(true)]");
  132. //sb.AppendLine(" public partial class " + _currentView.PascalName + " : Widgetsphere.EFCore.DataAccess.NHEntityObject, Widgetsphere.EFCore.DataAccess.IBusinessObject");
  133. sb.AppendLine(" {");
  134. this.AppendedFieldEnum();
  135. this.AppendConstructors();
  136. this.AppendProperties();
  137. this.AppendGenerateEvents();
  138. this.AppendRegionGetMaxLength();
  139. this.AppendParented();
  140. this.AppendIsEquivalent();
  141. this.AppendRegionGetDatabaseFieldName();
  142. sb.AppendLine(" }");
  143. sb.AppendLine();
  144. }
  145. private void AppendConstructors()
  146. {
  147. string scope = "protected internal";
  148. sb.AppendLine(" #region Constructors");
  149. sb.AppendLine();
  150. sb.AppendLine(" /// <summary>");
  151. sb.AppendLine(" /// Initializes a new instance of the " + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + " class");
  152. sb.AppendLine(" /// </summary>");
  153. sb.AppendLine(" " + scope + " " + _currentView.PascalName + "()");
  154. sb.AppendLine(" {");
  155. if (_currentView.PrimaryKeyColumns.Count == 1 && _currentView.PrimaryKeyColumns[0].DataType == System.Data.SqlDbType.UniqueIdentifier)
  156. sb.AppendLine(" this." + _currentView.PrimaryKeyColumns[0].PascalName + " = Guid.NewGuid();");
  157. sb.AppendLine(" }");
  158. sb.AppendLine();
  159. sb.AppendLine(" #endregion");
  160. sb.AppendLine();
  161. }
  162. private void AppendParented()
  163. {
  164. sb.AppendLine(" #region IsParented");
  165. sb.AppendLine();
  166. sb.AppendLine(" /// <summary>");
  167. sb.AppendLine(" /// Determines if this object is part of a collection or is detached");
  168. sb.AppendLine(" /// </summary>");
  169. sb.AppendLine(" [System.ComponentModel.Browsable(false)]");
  170. sb.AppendLine(" public virtual bool IsParented");
  171. sb.AppendLine(" {");
  172. sb.AppendLine(" get { return (this.EntityState != System.Data.EntityState.Detached); }");
  173. sb.AppendLine(" }");
  174. sb.AppendLine();
  175. sb.AppendLine(" #endregion");
  176. sb.AppendLine();
  177. }
  178. private void AppendIsEquivalent()
  179. {
  180. sb.AppendLine(" #region IsEquivalent");
  181. sb.AppendLine();
  182. sb.AppendLine(" /// <summary>");
  183. sb.AppendLine(" /// Determines if all of the fields for the specified object exactly matches the current object.");
  184. sb.AppendLine(" /// </summary>");
  185. sb.AppendLine(" /// <param name=\"item\">The object to compare</param>");
  186. //sb.AppendLine(" public override bool IsEquivalent(Widgetsphere.EFCore.DataAccess.NHEntityObject item)");
  187. sb.AppendLine(" {");
  188. sb.AppendLine(" if (item == null) return false;");
  189. sb.AppendLine(" if (!(item is " + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ")) return false;");
  190. sb.AppendLine(" " + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + " o = item as " + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ";");
  191. sb.AppendLine(" return (");
  192. IEnumerable<CustomViewColumn> allColumns = _currentView.GetColumns().Where(x => x.Generated);
  193. int index = 0;
  194. foreach (CustomViewColumn column in allColumns)
  195. {
  196. #if notimplementedyet
  197. Table typeTable = _currentView.GetRelatedTypeTableByColumn(column, true);
  198. if (typeTable != null)
  199. {
  200. sb.Append(" o." + typeTable.PascalName + " == this." + typeTable.PascalName);
  201. }
  202. else
  203. {
  204. sb.Append(" o." + column.PascalName + " == this." + column.PascalName);
  205. }
  206. #else
  207. sb.Append(" o." + column.PascalName + " == this." + column.PascalName);
  208. #endif
  209. if (index < allColumns.Count() - 1) sb.Append(" &&");
  210. sb.AppendLine();
  211. index++;
  212. }
  213. sb.AppendLine(" );");
  214. sb.AppendLine(" }");
  215. sb.AppendLine();
  216. sb.AppendLine(" #endregion");
  217. sb.AppendLine();
  218. }
  219. private void AppendedFieldEnum()
  220. {
  221. IEnumerable<CustomViewColumn> imageColumnList = _currentView.GetColumns().Where(x => x.DataType == System.Data.SqlDbType.Image).OrderBy(x=>x.Name);
  222. if (imageColumnList.Count() != 0)
  223. {
  224. sb.AppendLine(" #region FieldNameConstants Enumeration");
  225. sb.AppendLine();
  226. sb.AppendLine(" /// <summary>");
  227. sb.AppendLine(" /// An enumeration of this object's image type fields");
  228. sb.AppendLine(" /// </summary>");
  229. sb.AppendLine(" public enum FieldImageConstants");
  230. sb.AppendLine(" {");
  231. foreach (var column in imageColumnList)
  232. {
  233. sb.AppendLine(" /// <summary>");
  234. sb.AppendLine(" /// Field mapping for the image column '" + column.PascalName + "' property");
  235. sb.AppendLine(" /// </summary>");
  236. sb.AppendLine(" [System.ComponentModel.Description(\"Field mapping for the image column '" + column.PascalName + "' property\")]");
  237. sb.AppendLine(" " + column.PascalName + ",");
  238. }
  239. sb.AppendLine(" }");
  240. sb.AppendLine();
  241. sb.AppendLine(" #endregion");
  242. sb.AppendLine();
  243. }
  244. sb.AppendLine(" #region FieldNameConstants Enumeration");
  245. sb.AppendLine();
  246. sb.AppendLine(" /// <summary>");
  247. sb.AppendLine(" /// Enumeration to define each property that maps to a database field for the '" + _currentView.PascalName + "' customView.");
  248. sb.AppendLine(" /// </summary>");
  249. sb.AppendLine(" public enum FieldNameConstants");
  250. sb.AppendLine(" {");
  251. foreach (var column in _currentView.GetColumns().OrderBy(x=>x.Name))
  252. {
  253. sb.AppendLine(" /// <summary>");
  254. sb.AppendLine(" /// Field mapping for the '" + column.PascalName + "' property");
  255. sb.AppendLine(" /// </summary>");
  256. sb.AppendLine(" [System.ComponentModel.ReadOnlyAttribute(true)]");
  257. sb.AppendLine(" [System.ComponentModel.Description(\"Field mapping for the '" + column.PascalName + "' property\")]");
  258. sb.AppendLine(" " + column.PascalName + ",");
  259. }
  260. sb.AppendLine(" }");
  261. sb.AppendLine(" #endregion");
  262. sb.AppendLine();
  263. }
  264. private void AppendProperties()
  265. {
  266. sb.AppendLine(" #region Properties");
  267. sb.AppendLine();
  268. //Fake Primary Key
  269. sb.AppendLine(" [System.ComponentModel.Browsable(true)]");
  270. sb.AppendLine(" [EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]");
  271. sb.AppendLine(" [DataMemberAttribute()]");
  272. sb.AppendLine(" [System.ComponentModel.DisplayName(\"pk\")]");
  273. sb.AppendLine(" [System.ComponentModel.ReadOnly(true)]");
  274. sb.AppendLine(" private Guid pk");
  275. sb.AppendLine(" {");
  276. sb.AppendLine(" get { return _pk; }");
  277. sb.AppendLine(" set { _pk = value; }");
  278. sb.AppendLine(" }");
  279. sb.AppendLine(" private Guid _pk;");
  280. foreach (var column in _currentView.GetColumns().Where(x => x.Generated).OrderBy(x => x.Name))
  281. {
  282. #if notimplementedtyet
  283. if (_currentView.IsColumnRelatedToTypeTable(column))
  284. {
  285. Table typeTable = _currentView.GetRelatedTypeTableByColumn(column);
  286. //If this column is a type table column then generate a special property
  287. sb.AppendLine(" /// <summary>");
  288. if (!string.IsNullOrEmpty(column.Description))
  289. sb.AppendLine(" /// " + column.Description + "");
  290. else
  291. sb.AppendLine(" /// The property that maps back to the database '" + (column.ParentTableRef.Object as Table).DatabaseName + "." + column.DatabaseName + "' field");
  292. sb.AppendLine(" /// </summary>");
  293. sb.AppendLine(" /// <remarks>" + column.GetIntellisenseRemarks() + "</remarks>");
  294. sb.AppendLine(" [EdmComplexPropertyAttribute()]");
  295. //sb.AppendLine(" [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]");
  296. //sb.AppendLine(" [XmlElement(IsNullable=" + column.AllowNull.ToString().ToLower() + ")]");
  297. //sb.AppendLine(" [SoapElement(IsNullable=" + column.AllowNull.ToString().ToLower() + ")]");
  298. sb.AppendLine(" [DataMemberAttribute()]");
  299. if (column.PrimaryKey)
  300. sb.AppendLine(" [System.ComponentModel.DataAnnotations.Key()]");
  301. if (column.PrimaryKey || _currentView.Immutable || column.ComputedColumn)
  302. sb.AppendLine(" [System.ComponentModel.ReadOnly(true)]");
  303. if (!string.IsNullOrEmpty(column.Description))
  304. sb.AppendLine(" [System.ComponentModel.Description(\"" + StringHelper.ConvertTextToSingleLineCodeString(column.Description) + "\")]");
  305. sb.AppendLine(" [Widgetsphere.EFCore.Attributes.EntityFieldMetadata(\"" + column.PascalName + "\", " + column.SortOrder + ", " + column.UIVisible.ToString().ToLower() + ", " + column.Length + ", \"" + column.Mask + "\", \"" + column.GetFriendlyName() + "\", \"" + column.Default + "\", " + column.AllowNull.ToString().ToLower() + ", \"" + column.Description + "\", " + column.ComputedColumn.ToString().ToLower() + ", " + column.IsUnique.ToString().ToLower() + ", " + (double.IsNaN(column.Min) ? "double.NaN" : column.Min.ToString()) + ", " + (double.IsNaN(column.Max) ? "double.NaN" : column.Max.ToString()) + ", " + column.PrimaryKey.ToString().ToLower() + ")]");
  306. sb.AppendLine(" public virtual " + typeTable.PascalName + "Wrapper " + typeTable.PascalName);
  307. sb.AppendLine(" {");
  308. sb.AppendLine(" get { return _" + column.CamelName + "; }");
  309. //OLD CODE - we tried to hide the setter but serialization hates this
  310. //sb.AppendLine(" " + (column.PrimaryKey || _currentTable.Immutable ? "protected " : "") + "set");
  311. sb.AppendLine(" set");
  312. sb.AppendLine(" {");
  313. if (column.PrimaryKey)
  314. {
  315. sb.AppendLine(" ReportPropertyChanging(\"" + column.PascalName + "\");");
  316. sb.AppendLine(" this.OnPropertyChanging(\"" + column.PascalName + "\");");
  317. //sb.AppendLine(" _" + column.CamelName + " = StructuralObject.SetValidValue(value);");
  318. sb.AppendLine(" _" + column.CamelName + " = value;");
  319. sb.AppendLine(" ReportPropertyChanged(\"" + column.PascalName + "\");");
  320. sb.AppendLine(" this.OnPropertyChanged(\"" + column.PascalName + "\");");
  321. }
  322. else
  323. {
  324. sb.AppendLine(" Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + typeTable.PascalName + "Constants> eventArg = new Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + typeTable.PascalName + "Constants>(value, \"" + typeTable.PascalName + "\");");
  325. if (_model.EnableCustomChangeEvents)
  326. sb.AppendLine(" this.On" + typeTable.PascalName + "Changing(eventArg);");
  327. sb.AppendLine(" if (eventArg.Cancel) return;");
  328. sb.AppendLine(" ReportPropertyChanging(\"" + typeTable.PascalName + "\");");
  329. sb.AppendLine(" this.OnPropertyChanging(\"" + column.PascalName + "\");");
  330. //sb.AppendLine(" _" + column.CamelName + " = StructuralObject.SetValidValue(value);");
  331. sb.AppendLine(" _" + column.CamelName + " = eventArg.Value;");
  332. sb.AppendLine(" ReportPropertyChanged(\"" + typeTable.PascalName + "\");");
  333. sb.AppendLine(" this.OnPropertyChanged(\"" + column.PascalName + "\");");
  334. if (_model.EnableCustomChangeEvents)
  335. sb.AppendLine(" this.On" + typeTable.PascalName + "Changed(eventArg);");
  336. }
  337. sb.AppendLine(" }");
  338. sb.AppendLine(" }");
  339. sb.AppendLine();
  340. }
  341. else
  342. {
  343. #endif
  344. sb.AppendLine(" /// <summary>");
  345. if (!string.IsNullOrEmpty(column.Description))
  346. sb.AppendLine(" /// " + column.Description + "");
  347. sb.AppendLine(" /// </summary>");
  348. sb.AppendLine(" [System.ComponentModel.Browsable(true)]");
  349. //sb.AppendLine(" [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]");
  350. //sb.AppendLine(" [XmlElement(IsNullable=" + column.AllowNull.ToString().ToLower() + ")]");
  351. //sb.AppendLine(" [SoapElement(IsNullable=" + column.AllowNull.ToString().ToLower() + ")]");
  352. sb.AppendLine(" [EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = " + (column.AllowNull ? "true" : "false") + ")]");
  353. sb.AppendLine(" [DataMemberAttribute()]");
  354. sb.AppendLine(" [System.ComponentModel.DisplayName(\"" + column.GetFriendlyName() + "\")]");
  355. sb.AppendLine(" [System.ComponentModel.ReadOnly(true)]");
  356. if (!string.IsNullOrEmpty(column.Description))
  357. sb.AppendLine(" [System.ComponentModel.Description(\"" + StringHelper.ConvertTextToSingleLineCodeString(column.Description) + "\")]");
  358. sb.AppendLine(" public virtual " + column.GetCodeType() + " " + column.PascalName);
  359. sb.AppendLine(" {");
  360. sb.AppendLine(" get { return _" + column.CamelName + "; }");
  361. //OLD CODE - we tried to hide the setter but serialization hates this
  362. //sb.AppendLine(" " + (column.PrimaryKey || _currentTable.Immutable ? "protected " : "") + "set");
  363. sb.AppendLine(" set");
  364. sb.AppendLine(" {");
  365. //Error Check for field size
  366. if (ModelHelper.IsTextType(column.DataType))
  367. {
  368. sb.Append(" if ((value != null) && (value.Length > GetMaxLength(FieldNameConstants." + column.PascalName + ")))");
  369. sb.AppendLine(" throw new Exception(string.Format(GlobalValues.ERROR_DATA_TOO_BIG, value, \"" + _currentView.PascalName + "." + column.PascalName + "\", GetMaxLength(FieldNameConstants." + column.PascalName + ")));");
  370. }
  371. else if (column.DataType == System.Data.SqlDbType.DateTime)
  372. {
  373. //Error check date value
  374. sb.AppendLine(" if (" + (column.AllowNull ? "(value != null) && " : "") + "(value < GlobalValues.MIN_DATETIME)) throw new Exception(\"The DateTime value '" + column.PascalName + "' (\" + value" + (column.AllowNull ? ".Value" : "") + ".ToString(\"yyyy-MM-dd HH:mm:ss\") + \") cannot be less than \" + GlobalValues.MIN_DATETIME.ToString());");
  375. sb.AppendLine(" if (" + (column.AllowNull ? "(value != null) && " : "") + "(value > GlobalValues.MAX_DATETIME)) throw new Exception(\"The DateTime value '" + column.PascalName + "' (\" + value" + (column.AllowNull ? ".Value" : "") + ".ToString(\"yyyy-MM-dd HH:mm:ss\") + \") cannot be greater than \" + GlobalValues.MAX_DATETIME.ToString());");
  376. }
  377. #if notdone
  378. if (column.PrimaryKey)
  379. {
  380. sb.AppendLine(" ReportPropertyChanging(\"" + column.PascalName + "\");");
  381. sb.AppendLine(" this.OnPropertyChanging(\"" + column.PascalName + "\");");
  382. //sb.AppendLine(" _" + column.CamelName + " = StructuralObject.SetValidValue(value);");
  383. sb.AppendLine(" _" + column.CamelName + " = value;");
  384. sb.AppendLine(" ReportPropertyChanged(\"" + column.PascalName + "\");");
  385. sb.AppendLine(" this.OnPropertyChanged(\"" + column.PascalName + "\");");
  386. }
  387. else
  388. {
  389. #endif
  390. sb.AppendLine(" Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + column.GetCodeType() + "> eventArg = new Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + column.GetCodeType() + ">(, \"" + column.PascalName + "\");");
  391. if (_model.EnableCustomChangeEvents)
  392. sb.AppendLine(" this.On" + column.PascalName + "Changing(eventArg);");
  393. sb.AppendLine(" if (eventArg.Cancel) return;");
  394. sb.AppendLine(" ReportPropertyChanging(\"" + column.PascalName + "\");");
  395. sb.AppendLine(" this.OnPropertyChanging(\"" + column.PascalName + "\");");
  396. //sb.AppendLine(" _" + column.CamelName + " = StructuralObject.SetValidValue(value);");
  397. sb.AppendLine(" _" + column.CamelName + " = eventArg.Value;");
  398. sb.AppendLine(" ReportPropertyChanged(\"" + column.PascalName + "\");");
  399. sb.AppendLine(" this.OnPropertyChanged(\"" + column.PascalName + "\");");
  400. if (_model.EnableCustomChangeEvents)
  401. sb.AppendLine(" this.On" + column.PascalName + "Changed(eventArg);");
  402. #if notdone
  403. }
  404. #endif
  405. sb.AppendLine(" }");
  406. sb.AppendLine(" }");
  407. sb.AppendLine();
  408. }
  409. #if notimplementedyet
  410. }
  411. #endif
  412. sb.AppendLine(" #endregion");
  413. sb.AppendLine();
  414. }
  415. private void AppendGenerateEvents()
  416. {
  417. sb.AppendLine(" #region Events");
  418. sb.AppendLine();
  419. foreach (var column in _currentView.GetColumns().Where(x => x.Generated).OrderBy(x => x.Name))
  420. {
  421. #if notdone
  422. if (_currentView.IsColumnRelatedToTypeTable(column))
  423. {
  424. Table typeTable = _currentView.GetRelatedTypeTableByColumn(column);
  425. //If this column is a type table column then generate a special property
  426. sb.AppendLine(" /// <summary>");
  427. sb.AppendLine(" /// The internal reference variable for the '" + column.PascalName + "' property");
  428. sb.AppendLine(" /// </summary>");
  429. sb.AppendLine(" protected " + typeTable.PascalName + "Wrapper _" + column.CamelName + ";");
  430. sb.AppendLine();
  431. this.AppendPropertyEventDeclarations(column, typeTable.PascalName + "Constants");
  432. }
  433. else
  434. {
  435. #endif
  436. sb.AppendLine(" /// <summary>");
  437. sb.AppendLine(" /// The internal reference variable for the '" + column.PascalName + "' property");
  438. sb.AppendLine(" /// </summary>");
  439. sb.AppendLine(" protected " + column.GetCodeType() + " _" + column.CamelName + ";");
  440. sb.AppendLine();
  441. this.AppendPropertyEventDeclarations(column, column.GetCodeType());
  442. }
  443. #if notdone
  444. }
  445. #endif
  446. sb.AppendLine(" #endregion");
  447. sb.AppendLine();
  448. }
  449. private void AppendPropertyEventDeclarations(CustomViewColumn column, string codeType)
  450. {
  451. #if notdone
  452. CustomView typetable = _currentView.GetRelatedTypeTableByColumn(column);
  453. if (typetable != null)
  454. {
  455. this.AppendPropertyEventDeclarations(typetable.PascalName, codeType);
  456. }
  457. else
  458. {
  459. #endif
  460. this.AppendPropertyEventDeclarations(column.PascalName, codeType);
  461. #if notdone
  462. }
  463. #endif
  464. }
  465. private void AppendPropertyEventDeclarations(string columnName, string codeType)
  466. {
  467. //Do not support custom events
  468. if (!_model.EnableCustomChangeEvents) return;
  469. sb.AppendLine(" /// <summary>");
  470. sb.AppendLine(" /// Occurs when the '" + columnName + "' property value change is a pending.");
  471. sb.AppendLine(" /// </summary>");
  472. sb.AppendLine(" public event EventHandler<Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + codeType + ">> " + columnName + "Changing;");
  473. sb.AppendLine();
  474. sb.AppendLine(" /// <summary>");
  475. sb.AppendLine(" /// Raises the On" + columnName + "Changing event.");
  476. sb.AppendLine(" /// </summary>");
  477. sb.AppendLine(" protected virtual void On" + columnName + "Changing(Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + codeType + "> e)");
  478. sb.AppendLine(" {");
  479. sb.AppendLine(" if (this." + columnName + "Changing != null)");
  480. sb.AppendLine(" this." + columnName + "Changing(this, e);");
  481. sb.AppendLine(" }");
  482. sb.AppendLine();
  483. sb.AppendLine(" /// <summary>");
  484. sb.AppendLine(" /// Occurs when the '" + columnName + "' property value has changed.");
  485. sb.AppendLine(" /// </summary>");
  486. sb.AppendLine(" public event EventHandler<ChangedEventArgs<" + codeType + ">> " + columnName + "Changed;");
  487. sb.AppendLine();
  488. sb.AppendLine(" /// <summary>");
  489. sb.AppendLine(" /// Raises the On" + columnName + "Changed event.");
  490. sb.AppendLine(" /// </summary>");
  491. sb.AppendLine(" protected virtual void On" + columnName + "Changed(ChangedEventArgs<" + codeType + "> e)");
  492. sb.AppendLine(" {");
  493. sb.AppendLine(" if (this." + columnName + "Changed != null)");
  494. sb.AppendLine(" this." + columnName + "Changed(this, e);");
  495. sb.AppendLine(" }");
  496. sb.AppendLine();
  497. }
  498. private void AppendRegionGetMaxLength()
  499. {
  500. sb.AppendLine(" #region GetMaxLength");
  501. sb.AppendLine();
  502. sb.AppendLine(" /// <summary>");
  503. sb.AppendLine(" /// Gets the maximum size of the field value.");
  504. sb.AppendLine(" /// </summary>");
  505. sb.AppendLine(" public static int GetMaxLength(FieldNameConstants field)");
  506. sb.AppendLine(" {");
  507. sb.AppendLine(" switch (field)");
  508. sb.AppendLine(" {");
  509. foreach (var column in _currentView.GetColumns().Where(x => x.Generated).OrderBy(x => x.Name))
  510. {
  511. sb.AppendLine(" case FieldNameConstants." + column.PascalName + ":");
  512. if (_currentView.GeneratedColumns.Contains(column))
  513. {
  514. //This is an actual column in this table
  515. switch (column.DataType)
  516. {
  517. case System.Data.SqlDbType.Text:
  518. sb.AppendLine(" return int.MaxValue;");
  519. break;
  520. case System.Data.SqlDbType.NText:
  521. sb.AppendLine(" return int.MaxValue;");
  522. break;
  523. case System.Data.SqlDbType.Image:
  524. sb.AppendLine(" return int.MaxValue;");
  525. break;
  526. case System.Data.SqlDbType.Char:
  527. case System.Data.SqlDbType.NChar:
  528. case System.Data.SqlDbType.NVarChar:
  529. case System.Data.SqlDbType.VarChar:
  530. if ((column.Length == 0) && (ModelHelper.SupportsMax(column.DataType)))
  531. sb.AppendLine(" return int.MaxValue;");
  532. else
  533. sb.AppendLine(" return " + column.Length + ";");
  534. break;
  535. default:
  536. sb.AppendLine(" return 0;");
  537. break;
  538. }
  539. }
  540. }
  541. sb.AppendLine(" }");
  542. sb.AppendLine(" return 0;");
  543. sb.AppendLine(" }");
  544. sb.AppendLine();
  545. sb.AppendLine(" int Widgetsphere.EFCore.DataAccess.IBusinessObject.GetMaxLength(Enum field)");
  546. sb.AppendLine(" {");
  547. sb.AppendLine(" return GetMaxLength((FieldNameConstants)field);");
  548. sb.AppendLine(" }");
  549. sb.AppendLine();
  550. sb.AppendLine(" System.Type Widgetsphere.EFCore.DataAccess.IBusinessObject.GetFieldNameConstants()");
  551. sb.AppendLine(" {");
  552. sb.AppendLine(" return typeof(FieldNameConstants);");
  553. sb.AppendLine(" }");
  554. sb.AppendLine();
  555. //If this is not derived then add the Primary key stuff
  556. sb.AppendLine(" /// <summary>");
  557. sb.AppendLine(" /// Hold the primary key for this object");
  558. sb.AppendLine(" /// </summary>");
  559. sb.AppendLine(" protected Widgetsphere.EFCore.DataAccess.IPrimaryKey _primaryKey = null;");
  560. sb.AppendLine(" Widgetsphere.EFCore.DataAccess.IPrimaryKey Widgetsphere.EFCore.DataAccess.IBusinessObject.PrimaryKey");
  561. sb.AppendLine(" {");
  562. sb.AppendLine(" get");
  563. sb.AppendLine(" {");
  564. sb.AppendLine(" return null;");
  565. sb.AppendLine(" }");
  566. sb.AppendLine(" }");
  567. sb.AppendLine();
  568. sb.AppendLine(" #endregion");
  569. sb.AppendLine();
  570. }
  571. private void GenerateAuditField(string columnName, string codeType, string description, string propertyScope)
  572. {
  573. if (!string.IsNullOrEmpty(description))
  574. {
  575. sb.AppendLine(" /// <summary>");
  576. sb.AppendLine(" /// " + description);
  577. sb.AppendLine(" /// </summary>");
  578. }
  579. sb.AppendLine(" [EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]");
  580. sb.AppendLine(" [DataMemberAttribute()]");
  581. sb.AppendLine(" " + propertyScope + " virtual " + codeType + " " + columnName);
  582. sb.AppendLine(" {");
  583. sb.AppendLine(" get { return _" + StringHelper.DatabaseNameToCamelCase(columnName) + "; }");
  584. if (propertyScope == "public")
  585. {
  586. //OLD CODE - we tried to hide the setter but serialization hates this
  587. //sb.AppendLine(" protected internal set");
  588. sb.AppendLine(" set");
  589. }
  590. else
  591. {
  592. sb.AppendLine(" set");
  593. }
  594. sb.AppendLine(" {");
  595. sb.AppendLine(" Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + codeType + "> eventArg = new Widgetsphere.EFCore.EventArgs.ChangingEventArgs<" + codeType + ">(value, \"" + columnName + "\");");
  596. if (_model.EnableCustomChangeEvents)
  597. sb.AppendLine(" On" + columnName + "Changing(eventArg);");
  598. sb.AppendLine(" if (eventArg.Cancel) return;");
  599. sb.AppendLine(" ReportPropertyChanging(\"" + columnName + "\");");
  600. sb.AppendLine(" this.OnPropertyChanging(\"" + columnName + "\");");
  601. sb.AppendLine(" _" + StringHelper.DatabaseNameToCamelCase(columnName) + " = eventArg.Value;");
  602. sb.AppendLine(" ReportPropertyChanged(\"" + columnName + "\");");
  603. sb.AppendLine(" this.OnPropertyChanged(\"" + columnName + "\");");
  604. if (_model.EnableCustomChangeEvents)
  605. sb.AppendLine(" On" + columnName + "Changed(eventArg);");
  606. sb.AppendLine(" }");
  607. sb.AppendLine(" }");
  608. sb.AppendLine();
  609. sb.AppendLine(" /// <summary>");
  610. sb.AppendLine(" /// The internal reference variable for the '" + StringHelper.DatabaseNameToCamelCase(columnName) + "' property");
  611. sb.AppendLine(" /// </summary>");
  612. sb.AppendLine(" protected " + codeType + " _" + StringHelper.DatabaseNameToCamelCase(columnName) + ";");
  613. sb.AppendLine();
  614. }
  615. private void AppendPagingClass()
  616. {
  617. sb.AppendLine(" #region " + _currentView.PascalName + "Paging");
  618. sb.AppendLine();
  619. sb.AppendLine(" /// <summary>");
  620. sb.AppendLine(" /// A field sort object for the " + _currentView.PascalName + "Paging object.");
  621. sb.AppendLine(" /// </summary>");
  622. sb.AppendLine(" [Serializable]");
  623. sb.AppendLine(" internal class " + _currentView.PascalName + "PagingFieldItem : IPagingFieldItem");
  624. sb.AppendLine(" {");
  625. sb.AppendLine(" /// <summary>");
  626. sb.AppendLine(" /// Determines the direction of the sort.");
  627. sb.AppendLine(" /// </summary>");
  628. sb.AppendLine(" public bool Ascending { get; set; }");
  629. sb.AppendLine();
  630. sb.AppendLine(" /// <summary>");
  631. sb.AppendLine(" /// Determines the field on which to sort.");
  632. sb.AppendLine(" /// </summary>");
  633. sb.AppendLine(" public " + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ".FieldNameConstants Field { get; set; }");
  634. sb.AppendLine();
  635. sb.AppendLine(" /// <summary>");
  636. sb.AppendLine(" /// Create a sorting field object for the " + _currentView.PascalName + "Paging object.");
  637. sb.AppendLine(" /// </summary>");
  638. sb.AppendLine(" /// <param name=\"field\">The field on which to sort.</param>");
  639. sb.AppendLine(" public " + _currentView.PascalName + "PagingFieldItem(" + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ".FieldNameConstants field)");
  640. sb.AppendLine(" {");
  641. sb.AppendLine(" this.Field = field;");
  642. sb.AppendLine(" }");
  643. sb.AppendLine();
  644. sb.AppendLine(" /// <summary>");
  645. sb.AppendLine(" /// Create a sorting field object for the " + _currentView.PascalName + "Paging object.");
  646. sb.AppendLine(" /// </summary>");
  647. sb.AppendLine(" /// <param name=\"field\">The field on which to sort.</param>");
  648. sb.AppendLine(" /// <param name=\"ascending\">Determines the direction of the sort.</param>");
  649. sb.AppendLine(" public " + _currentView.PascalName + "PagingFieldItem(" + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ".FieldNameConstants field, bool ascending)");
  650. sb.AppendLine(" : this(field)");
  651. sb.AppendLine(" {");
  652. sb.AppendLine(" this.Ascending = ascending;");
  653. sb.AppendLine(" }");
  654. sb.AppendLine();
  655. sb.AppendLine(" #region IPagingFieldItem Members");
  656. sb.AppendLine();
  657. sb.AppendLine(" Enum IPagingFieldItem.GetField()");
  658. sb.AppendLine(" {");
  659. sb.AppendLine(" return this.Field;");
  660. sb.AppendLine(" }");
  661. sb.AppendLine();
  662. sb.AppendLine(" #endregion");
  663. sb.AppendLine();
  664. sb.AppendLine(" }");
  665. sb.AppendLine();
  666. sb.AppendLine(" /// <summary>");
  667. sb.AppendLine(" /// The paging object for the " + _currentView.PascalName + " collection");
  668. sb.AppendLine(" /// </summary>");
  669. sb.AppendLine(" [Serializable]");
  670. sb.AppendLine(" internal class " + _currentView.PascalName + "Paging : Widgetsphere.EFCore.DataAccess.PagingBase<" + _currentView.PascalName + "PagingFieldItem>");
  671. sb.AppendLine(" {");
  672. sb.AppendLine();
  673. sb.AppendLine(" #region Constructors");
  674. sb.AppendLine();
  675. sb.AppendLine(" /// <summary>");
  676. sb.AppendLine(" /// Creates a paging object");
  677. sb.AppendLine(" /// </summary>");
  678. sb.AppendLine(" public " + _currentView.PascalName + "Paging()");
  679. sb.AppendLine(" : this(1, 10, null)");
  680. sb.AppendLine(" {");
  681. sb.AppendLine(" }");
  682. sb.AppendLine();
  683. sb.AppendLine(" /// <summary>");
  684. sb.AppendLine(" /// Creates a paging object");
  685. sb.AppendLine(" /// </summary>");
  686. sb.AppendLine(" /// <param name=\"pageIndex\">The page number to load</param>");
  687. sb.AppendLine(" /// <param name=\"recordsperPage\">The number of records per page.</param>");
  688. sb.AppendLine(" public " + _currentView.PascalName + "Paging(int pageIndex, int recordsperPage)");
  689. sb.AppendLine(" : this(pageIndex, recordsperPage, null)");
  690. sb.AppendLine(" {");
  691. sb.AppendLine(" }");
  692. sb.AppendLine();
  693. sb.AppendLine(" /// <summary>");
  694. sb.AppendLine(" /// Creates a paging object");
  695. sb.AppendLine(" /// </summary>");
  696. sb.AppendLine(" /// <param name=\"pageIndex\">The page number to load</param>");
  697. sb.AppendLine(" /// <param name=\"recordsperPage\">The number of records per page.</param>");
  698. sb.AppendLine(" /// <param name=\"field\">The field on which to sort.</param>");
  699. sb.AppendLine(" /// <param name=\"ascending\">Determines the sorted direction.</param>");
  700. sb.AppendLine(" public " + _currentView.PascalName + "Paging(int pageIndex, int recordsperPage, " + this.GetLocalNamespace() + ".Entity." + _currentView.PascalName + ".FieldNameConstants field, bool ascending)");
  701. sb.AppendLine(" : this(pageIndex, recordsperPage, new " + _currentView.PascalName + "PagingFieldItem(field, ascending))");
  702. sb.AppendLine(" {");
  703. sb.AppendLine(" }");
  704. sb.AppendLine();
  705. sb.AppendLine(" /// <summary>");
  706. sb.AppendLine(" /// Creates a paging object");
  707. sb.AppendLine(" /// </summary>");
  708. sb.AppendLine(" /// <param name=\"pageIndex\">The page number to load</param>");
  709. sb.AppendLine(" /// <param name=\"recordsperPage\">The number of items per page.</param>");
  710. sb.AppendLine(" /// <param name=\"orderByField\">The field on which to sort.</param>");
  711. sb.AppendLine(" public " + _currentView.PascalName + "Paging(int pageIndex, int recordsperPage, " + _currentView.PascalName + "PagingFieldItem orderByField)");
  712. sb.AppendLine(" : base(pageIndex, recordsperPage, orderByField)");
  713. sb.AppendLine(" {");
  714. sb.AppendLine(" }");
  715. sb.AppendLine();
  716. sb.AppendLine(" #endregion");
  717. sb.AppendLine();
  718. sb.AppendLine(" }");
  719. sb.AppendLine();
  720. sb.AppendLine(" #endregion");
  721. sb.AppendLine();
  722. }
  723. private void AppendRegionGetDatabaseFieldName()
  724. {
  725. sb.AppendLine(" #region GetDatabaseFieldName");
  726. sb.AppendLine();
  727. sb.AppendLine(" /// <summary>");
  728. sb.AppendLine(" /// Returns the actual database name of the specified field.");
  729. sb.AppendLine(" /// </summary>");
  730. sb.AppendLine(" internal static string GetDatabaseFieldName(" + _currentView.PascalName + ".FieldNameConstants field)");
  731. sb.AppendLine(" {");
  732. sb.AppendLine(" return GetDatabaseFieldName(field.ToString());");
  733. sb.AppendLine(" }");
  734. sb.AppendLine();
  735. sb.AppendLine(" /// <summary>");
  736. sb.AppendLine(" /// Returns the actual database name of the specified field.");
  737. sb.AppendLine(" /// </summary>");
  738. sb.AppendLine(" internal static string GetDatabaseFieldName(string field)");
  739. sb.AppendLine(" {");
  740. sb.AppendLine(" switch (field)");
  741. sb.AppendLine(" {");
  742. foreach (var column in _currentView.GeneratedColumns)
  743. {
  744. if (column.Generated)
  745. sb.AppendLine(" case \"" + column.PascalName + "\": return \"" + column.Name + "\";");
  746. }
  747. sb.AppendLine(" }");
  748. sb.AppendLine(" return string.Empty;");
  749. sb.AppendLine(" }");
  750. sb.AppendLine();
  751. sb.AppendLine(" #endregion");
  752. sb.AppendLine();
  753. }
  754. #endregion
  755. }
  756. }