PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/DataGeneration/Domain/CustomProcessors/NonKeyColumnProcessor.cs

#
C# | 225 lines | 208 code | 8 blank | 9 comment | 22 complexity | 0993e2861b4bd0ce2db9b3f43bc6c12b MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Xml.Linq;
  4. using DataGeneration.Domain.Helpers;
  5. namespace DataGeneration.Domain.CustomProcessors
  6. {
  7. public class NonKeyColumnProcessor
  8. {
  9. /// <summary>
  10. /// Appends the non key property value.
  11. /// </summary>
  12. /// <param name="propertyValues">The property values.</param>
  13. /// <param name="property">The property.</param>
  14. /// <param name="tableName">Name of the table.</param>
  15. /// <param name="entityKeyFilename">The entity key filename.</param>
  16. /// <param name="dataAccess">The data access.</param>
  17. /// <param name="currentIteration">The current iteration.</param>
  18. public static void AppendNonKeyPropertyValue(StringBuilder propertyValues, XElement property, string tableName, string entityKeyFilename, DataAccessLayer.DataAccess dataAccess, int currentIteration)
  19. {
  20. propertyValues.AppendFormat("{0},", GeneratePropertyValue(property, tableName, entityKeyFilename, dataAccess,currentIteration));
  21. }
  22. private static string GeneratePropertyValue(XElement property, string tableName, string entityKeyFilename, DataAccessLayer.DataAccess dataAccess, int currentIteration)
  23. {
  24. string returnValue;
  25. if (property.ContainsAttributeWithoutValue(Tokens.ActualValue, ""))
  26. {
  27. returnValue = property.ReadAttributeValue(Tokens.ActualValue);
  28. }
  29. else
  30. {
  31. string hintValue = "";
  32. if (property.ContainsAttribute(Tokens.Hint))
  33. {
  34. hintValue = property.ReadAttributeValue(Tokens.Hint);
  35. }
  36. switch (property.ReadAttributeValue(Tokens.Type).Trim().ToLower())
  37. {
  38. case "guid":
  39. returnValue = string.Format("'{0}'", Guid.NewGuid());
  40. break;
  41. case "bit":
  42. returnValue = RandomMizer.GetNextValue(0, 1).ToString();
  43. break;
  44. case "smallinteger":
  45. returnValue = RandomMizer.GetNextValue(1, 255).ToString();
  46. break;
  47. case "money":
  48. returnValue = RandomMizer.GetNextValue(1, 255).ToString();
  49. break;
  50. case "decimal":
  51. returnValue =Convert.ToDecimal(RandomMizer.GetNextValue(1, 255)).ToString();
  52. break;
  53. case "float":
  54. case "double":
  55. returnValue = RandomMizer.GetNextValue(1, 10000000).ToString();
  56. break;
  57. case "int32":
  58. case "int":
  59. case "integer":
  60. returnValue = RandomMizer.GetNextValue(1, 23000).ToString();
  61. break;
  62. case "xml":
  63. returnValue = "null";// string.Format("'<?xml version=\"1.0\" encoding=\"utf-8\" ?><DataLoad cleanDB=\"false\"></DataLoad>'");
  64. break;
  65. case "datetime":
  66. returnValue = DateTimeProcessor.GenerateValue(hintValue.Trim().ToLower());
  67. break;
  68. default:
  69. returnValue = ApplyDefaultStringProcessor(property, hintValue, entityKeyFilename, tableName, dataAccess,currentIteration);
  70. break;
  71. }
  72. }
  73. return returnValue;
  74. }
  75. protected static string ApplyDefaultStringProcessor(XElement property, string hintValue, string entityKeyFilename, string tableName, DataAccessLayer.DataAccess dataAccess, int currentIteration)
  76. {
  77. string[] entries = hintValue.Trim().ToLower().Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  78. string returnValue;
  79. int maxCharacters;
  80. switch (entries[0].Trim())
  81. {
  82. case "title":
  83. returnValue = HumanNameGenerator.HumanTitle;
  84. break;
  85. case "surname":
  86. returnValue = HumanNameGenerator.HumanName;
  87. break;
  88. case "firstname":
  89. returnValue = HumanNameGenerator.HumanName;
  90. break;
  91. case "languagecode":
  92. returnValue = HumanNameGenerator.LanguageCode;
  93. break;
  94. case "language":
  95. returnValue = HumanNameGenerator.Language;
  96. break;
  97. case "abbrvhumangender":
  98. returnValue = HumanNameGenerator.AbbrevatedHumanGender;
  99. break;
  100. case "humangender":
  101. returnValue = HumanNameGenerator.HumanGender;
  102. break;
  103. case "telephone":
  104. returnValue = TelephoneNumberGenerator.GenerateTelephoneNumber();
  105. break;
  106. case "emailaddress":
  107. returnValue = EmailAddressGenerator.GenerateEmailAddress();
  108. break;
  109. case "regex":
  110. returnValue = RegexProcessor.GenerateValue(property.ReadAttributeValue(Tokens.RegexPattern));
  111. break;
  112. case "postcode":
  113. returnValue = AddressGenerator.Postcode;
  114. break;
  115. case "town":
  116. returnValue = AddressGenerator.TownName;
  117. break;
  118. case "addressline1":
  119. if (entries.Length > 1 && int.TryParse(entries[1], out maxCharacters))
  120. {
  121. returnValue = AddressGenerator.Addressline1(maxCharacters);
  122. }
  123. else
  124. {
  125. throw new Exception("The value for the addressline1 hint must follow the following pattern addressline1-5 Where the number signifies the maximum string length.");
  126. }
  127. break;
  128. case "addressline2":
  129. if (entries.Length > 1 && int.TryParse(entries[1], out maxCharacters))
  130. {
  131. returnValue = AddressGenerator.Addressline2(maxCharacters);
  132. }
  133. else
  134. {
  135. throw new Exception("The value for the addressline2 hint must follow the following pattern addressline2-5 Where the number signifies the maximum string length.");
  136. }
  137. break;
  138. case "addressline3":
  139. if (entries.Length > 1 && int.TryParse(entries[1], out maxCharacters))
  140. { returnValue = AddressGenerator.Addressline3(maxCharacters); }
  141. else
  142. {
  143. throw new Exception("The value for the addressline3 hint must follow the following pattern addressline3-5 Where the number signifies the maximum string length.");
  144. }
  145. break;
  146. case "country":
  147. returnValue = AddressGenerator.CountryName;
  148. break;
  149. case "max":
  150. if (entries.Length > 1 && int.TryParse(entries[1], out maxCharacters))
  151. {
  152. returnValue = StringProcessor.GenerateValue(maxCharacters, IsUpperCaseRequired(entries));
  153. }
  154. else
  155. {
  156. throw new Exception("The value for the max hint must follow the following pattern max-5 Where the number signifies the maximum string length.");
  157. }
  158. break;
  159. case "username":
  160. if (entries.Length > 1 && int.TryParse(entries[1], out maxCharacters))
  161. {
  162. returnValue = StringProcessor.GenerateUniqueUsername(maxCharacters, entityKeyFilename, tableName, property.ReadAttributeValue(Tokens.PropertyName), IsUpperCaseRequired(entries), dataAccess);
  163. }
  164. else
  165. {
  166. throw new Exception("The value for the username hint must follow the following pattern username-5 Where the number signifies the maximum string length.");
  167. }
  168. break;
  169. case "incrementpassword":
  170. int passwordStartValue;
  171. if (entries.Length > 2 && int.TryParse(entries[2], out passwordStartValue))
  172. {
  173. returnValue = StringProcessor.GeneratePatternedPassword(entries[1], currentIteration, passwordStartValue);
  174. }
  175. else
  176. {
  177. throw new Exception("The value for the incrementpassword hint must follow the following pattern incrementpassword-sometext-1 Where the sometext signifies the string to which a number will be appended and 1 indicates the value to start counting from.");
  178. }
  179. break;
  180. case "uniqueincrementusername":
  181. int usernameStartValue;
  182. if (entries.Length > 2 && int.TryParse(entries[2], out usernameStartValue))
  183. {
  184. returnValue = StringProcessor.GeneratePatternedUsername(entries[1], entityKeyFilename, tableName, property.ReadAttributeValue(Tokens.PropertyName), dataAccess, currentIteration, usernameStartValue);
  185. }
  186. else
  187. {
  188. throw new Exception("The value for the uniqueincrementusername hint must follow the following pattern uniqueincrementusername-sometext-1 Where the sometext signifies the string to which a number will be appended and 1 indicates the value to start counting from.");
  189. }
  190. break;
  191. case "unique":
  192. if (entries.Length > 1 && int.TryParse(entries[1], out maxCharacters))
  193. {
  194. returnValue = StringProcessor.GenerateUniqueValue(maxCharacters, entityKeyFilename, tableName, property.ReadAttributeValue(Tokens.PropertyName), IsUpperCaseRequired(entries), dataAccess);
  195. }
  196. else
  197. {
  198. throw new Exception("The value for the unique hint must follow the following pattern unique-5 Where the number signifies the maximum string length.");
  199. }
  200. break;
  201. default:
  202. returnValue = property.ReadAttributeValue(Tokens.PropertyName);
  203. break;
  204. }
  205. return string.Format("'{0}'", returnValue.Replace("'", "''"));
  206. }
  207. private static bool IsUpperCaseRequired(string[] entries)
  208. {
  209. var upperCase = false;
  210. if (entries.Length > 2 && entries[2].Trim().ToLower().Equals("uppercase"))
  211. {
  212. upperCase = true;
  213. }
  214. return upperCase;
  215. }
  216. }
  217. }