PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Condor.Core/PropertyObjects/BusinessObjectsPropertyRenderDataAnnotationsForDbContext.cs

http://github.com/kahanu/CondorXE
C# | 175 lines | 148 code | 10 blank | 17 comment | 31 complexity | 4185a6ef9c790d9d13f2c5ec7a5c4a84 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using MyMeta;
  4. namespace Condor.Core.PropertyObjects
  5. {
  6. public class BusinessObjectsPropertyRenderDataAnnotationsForDbContext : Property
  7. {
  8. protected CommonUtility _util;
  9. private readonly string[] _omitList;
  10. /// <summary>
  11. /// This renders short properties with DataAnnotations Attributes.
  12. /// </summary>
  13. /// <param name="column"></param>
  14. /// <param name="context"></param>
  15. /// <example>
  16. /// [Required(ErrorMessage = "Category Name is required.")]
  17. /// [StringLength(50, ErrorMessage = "Category Name must be between 1 and 50 characters.")]
  18. /// [DisplayName("Category Name")]
  19. /// public string CategoryName { get; set; }
  20. /// </example>
  21. public BusinessObjectsPropertyRenderDataAnnotationsForDbContext(MyMeta.IColumn column, RequestContext context)
  22. : base(column, context)
  23. {
  24. this._util = context.Utility;
  25. this._omitList = new string[0];
  26. }
  27. /// <summary>
  28. /// This renders short properties with DataAnnotations Attributes.
  29. /// </summary>
  30. /// <param name="column">The IColumn object</param>
  31. /// <param name="context">The RequestContext</param>
  32. /// <param name="omitList">Comma-delimited list of properties to omit</param>
  33. public BusinessObjectsPropertyRenderDataAnnotationsForDbContext(MyMeta.IColumn column, RequestContext context, string omitList)
  34. :this(column, context)
  35. {
  36. if (!string.IsNullOrEmpty(omitList))
  37. this._omitList = omitList.ToLower().Split(',');
  38. else
  39. this._omitList = new string[0];
  40. }
  41. public override void Render()
  42. {
  43. if (!_omitList.Where(o => o == this.Alias.ToLower()).Any())
  44. {
  45. IColumn column = Column;
  46. if (column.Name.ToLower() != _script.Settings.DataOptions.VersionColumnName.ToLower())
  47. {
  48. if (column.LanguageType.ToString().ToLower() == "string")
  49. {
  50. if (!column.IsNullable)
  51. {
  52. _output.autoTabLn("[Required(ErrorMessage = \"" + StringFormatter.NormalizePropertyName(column.Name) + " is required.\")]");
  53. if (column.Name.ToLower().Contains("phone"))
  54. {
  55. _output.autoTabLn("[StringLength(14, ErrorMessage=\"" + column.Name + " is invalid. Format: (###) ###-####.\")]");
  56. }
  57. else
  58. {
  59. if (!column.DataTypeName.Contains("text") && !column.DataTypeNameComplete.ToLower().Contains("nvarchar(max)"))
  60. {
  61. _output.autoTabLn("[StringLength(" + column.CharacterMaxLength + ", ErrorMessage=\"" + StringFormatter.NormalizePropertyName(column.Name) + " must be between 1 and " + column.CharacterMaxLength + " characters.\")]");
  62. }
  63. if (column.DataTypeName.Contains("text") || column.DataTypeNameComplete.ToLower().Contains("varchar(max)"))
  64. {
  65. _output.autoTabLn("[DataType(DataType.MultilineText)]");
  66. _output.autoTabLn("[UIHint(\"Editor\")]");
  67. }
  68. }
  69. }
  70. else
  71. {
  72. if (column.DataTypeName.Contains("text") || column.DataTypeNameComplete.ToLower().Contains("varchar(max)"))
  73. {
  74. _output.autoTabLn("[DataType(DataType.MultilineText)]");
  75. }
  76. }
  77. _output.autoTabLn(StringFormatter.CreateDisplayName(column.Name));
  78. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  79. _output.autoTabLn("");
  80. }
  81. else if (column.IsInPrimaryKey)
  82. {
  83. _output.autoTabLn("[Key]");
  84. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  85. _output.autoTabLn("");
  86. }
  87. else
  88. {
  89. _output.autoTabLn(StringFormatter.CreateDisplayName(column.Name));
  90. if (!column.IsNullable)
  91. {
  92. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  93. _output.autoTabLn("");
  94. }
  95. else
  96. {
  97. if (column.DataTypeName == "bit")
  98. {
  99. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  100. _output.autoTabLn("");
  101. }
  102. else
  103. {
  104. _output.autoTabLn("public " + column.LanguageType + "? " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  105. _output.autoTabLn("");
  106. }
  107. }
  108. }
  109. }
  110. else
  111. {
  112. if (!column.IsNullable)
  113. {
  114. if (column.DataTypeName == "timestamp")
  115. {
  116. }
  117. if (column.LanguageType == "byte[]")
  118. {
  119. if (column.Name.ToLower() == _script.Settings.DataOptions.VersionColumnName.ToLower())
  120. {
  121. _output.autoTabLn("[HiddenInput(DisplayValue=false)]");
  122. _output.autoTabLn("[Timestamp]");
  123. _output.autoTabLn("public byte[] " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  124. _output.autoTabLn("");
  125. }
  126. else
  127. {
  128. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  129. _output.autoTabLn("");
  130. }
  131. }
  132. else
  133. {
  134. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  135. _output.autoTabLn("");
  136. }
  137. }
  138. else
  139. {
  140. if (column.LanguageType == "byte[]")
  141. {
  142. if (column.Name.ToLower() == _script.Settings.DataOptions.VersionColumnName.ToLower())
  143. {
  144. _output.autoTabLn("[HiddenInput(DisplayValue=false)]");
  145. _output.autoTabLn("[Timestamp]");
  146. _output.autoTabLn("public byte[] " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  147. _output.autoTabLn("");
  148. }
  149. else
  150. {
  151. _output.autoTabLn("public " + column.LanguageType + " " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  152. _output.autoTabLn("");
  153. }
  154. }
  155. else
  156. {
  157. _output.autoTabLn("public " + column.LanguageType + "? " + _util.CleanUpProperty(column.Name) + " { get; set; }");
  158. _output.autoTabLn("");
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }