PageRenderTime 616ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/NBi.UI.Genbi/Presenter/TestCasesPresenter.cs

http://nbi.codeplex.com
C# | 282 lines | 237 code | 40 blank | 5 comment | 8 complexity | e0f496881c5470c51c8295b82ad1f15b MD5 | raw file
Possible License(s): LGPL-2.1, CC-BY-SA-3.0, MIT, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using NBi.Service;
  7. using NBi.UI.Genbi.Command;
  8. using NBi.UI.Genbi.Command.TestCases;
  9. using NBi.UI.Genbi.View.TestSuiteGenerator;
  10. namespace NBi.UI.Genbi.Presenter
  11. {
  12. class TestCasesPresenter : PresenterBase
  13. {
  14. private readonly TestCaseCollectionManager testCaseCollectionManager;
  15. public TestCasesPresenter(RenameVariableWindow renameVariablewindow, FilterWindow filterWindow, ConnectionStringWindow connectionStringWindow, TestCaseCollectionManager testCaseCollectionManager, DataTable testCases, BindingList<string> variables, BindingList<string> connectionStringNames)
  16. {
  17. this.OpenTestCasesCommand = new OpenTestCasesCommand(this);
  18. this.OpenTestCasesQueryCommand = new OpenTestCasesQueryCommand(this);
  19. this.RenameVariableCommand = new RenameVariableCommand(this, renameVariablewindow);
  20. this.RemoveVariableCommand = new RemoveVariableCommand(this);
  21. this.MoveLeftVariableCommand = new MoveLeftVariableCommand(this);
  22. this.MoveRightVariableCommand = new MoveRightVariableCommand(this);
  23. this.FilterCommand = new FilterCommand(this, filterWindow);
  24. this.FilterDistinctCommand = new FilterDistinctCommand(this);
  25. this.AddConnectionStringCommand = new AddConnectionStringCommand(this, connectionStringWindow);
  26. this.RemoveConnectionStringCommand = new RemoveConnectionStringCommand(this);
  27. this.EditConnectionStringCommand = new EditConnectionStringCommand(this, connectionStringWindow);
  28. this.RunQueryCommand = new RunQueryCommand(this);
  29. this.testCaseCollectionManager = testCaseCollectionManager;
  30. TestCases = testCases;
  31. Variables = variables;
  32. ConnectionStringNames = connectionStringNames;
  33. ConnectionStringSelectedIndex = -1;
  34. VariableSelectedIndex = -1;
  35. }
  36. public ICommand OpenTestCasesCommand { get; private set; }
  37. public ICommand OpenTestCasesQueryCommand { get; private set; }
  38. public ICommand RenameVariableCommand { get; private set; }
  39. public ICommand RemoveVariableCommand { get; private set; }
  40. public ICommand MoveLeftVariableCommand { get; private set; }
  41. public ICommand MoveRightVariableCommand { get; private set; }
  42. public ICommand FilterCommand { get; private set; }
  43. public ICommand FilterDistinctCommand { get; private set; }
  44. public ICommand AddConnectionStringCommand { get; private set; }
  45. public ICommand RemoveConnectionStringCommand { get; private set; }
  46. public ICommand EditConnectionStringCommand { get; private set; }
  47. public ICommand RunQueryCommand { get; private set; }
  48. #region Bindable properties
  49. public DataTable TestCases
  50. {
  51. get { return GetValue<DataTable>("TestCases"); }
  52. set { SetValue("TestCases", value); }
  53. }
  54. public BindingList<string> Variables
  55. {
  56. get { return GetValue<BindingList<string>>("Variables"); }
  57. set { SetValue("Variables", value); }
  58. }
  59. public BindingList<string> ConnectionStringNames
  60. {
  61. get { return GetValue<BindingList<string>>("ConnectionStringNames"); }
  62. set { SetValue("ConnectionStringNames", value); }
  63. }
  64. public int ConnectionStringSelectedIndex
  65. {
  66. get { return GetValue<int>("ConnectionStringSelectedIndex"); }
  67. set { SetValue<int>("ConnectionStringSelectedIndex", value); }
  68. }
  69. public string ConnectionStringSelectedName
  70. {
  71. get { return ConnectionStringNames[ConnectionStringSelectedIndex]; }
  72. }
  73. public string ConnectionStringSelectedValue
  74. {
  75. get { return testCaseCollectionManager.ConnectionStrings[ConnectionStringSelectedName]; }
  76. }
  77. public string Query
  78. {
  79. get { return this.GetValue<string>("Query"); }
  80. set { this.SetValue("Query", value); }
  81. }
  82. public string NewVariableName
  83. {
  84. get { return this.GetValue<string>("NewVariableName"); }
  85. set { this.SetValue("NewVariableName", value); }
  86. }
  87. public int VariableSelectedIndex
  88. {
  89. get { return GetValue<int>("VariableSelectedIndex"); }
  90. set { SetValue<int>("VariableSelectedIndex", value); }
  91. }
  92. #endregion
  93. protected override void OnPropertyChanged(string propertyName)
  94. {
  95. base.OnPropertyChanged(propertyName);
  96. switch (propertyName)
  97. {
  98. case "TestCases":
  99. this.FilterDistinctCommand.Refresh();
  100. break;
  101. case "Variables":
  102. this.RenameVariableCommand.Refresh();
  103. this.RemoveVariableCommand.Refresh();
  104. this.MoveLeftVariableCommand.Refresh();
  105. this.MoveRightVariableCommand.Refresh();
  106. this.FilterCommand.Refresh();
  107. break;
  108. case "VariableSelectedIndex":
  109. this.RenameVariableCommand.Refresh();
  110. this.RemoveVariableCommand.Refresh();
  111. this.MoveLeftVariableCommand.Refresh();
  112. this.MoveRightVariableCommand.Refresh();
  113. break;
  114. case "ConnectionStringNames":
  115. ReloadConnectionStrings();
  116. this.RemoveConnectionStringCommand.Refresh();
  117. this.EditConnectionStringCommand.Refresh();
  118. break;
  119. case "ConnectionStringSelectedIndex":
  120. this.RemoveConnectionStringCommand.Refresh();
  121. this.EditConnectionStringCommand.Refresh();
  122. this.RunQueryCommand.Refresh();
  123. break;
  124. case "Query":
  125. this.RunQueryCommand.Refresh();
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. internal void LoadCsv(string fullPath)
  132. {
  133. testCaseCollectionManager.Scope.ReadFromCsv(fullPath);
  134. Reload();
  135. OnPropertyChanged("Variables");
  136. }
  137. internal void LoadQuery(string fullPath)
  138. {
  139. Query = testCaseCollectionManager.Scope.GetQueryFileContent(fullPath);
  140. OnPropertyChanged("Query");
  141. }
  142. private void Reload()
  143. {
  144. var dtReader = new DataTableReader(testCaseCollectionManager.Scope.Content);
  145. //Reset the state of the DataTable
  146. //Remove the Sort Order or you'll be in troubles when loading the datatable
  147. TestCases.DefaultView.Sort = String.Empty;
  148. TestCases.Rows.Clear();
  149. TestCases.Columns.Clear();
  150. TestCases.RejectChanges();
  151. //Load it
  152. TestCases.Load(dtReader, LoadOption.PreserveChanges);
  153. OnPropertyChanged("TestCases");
  154. //Take care of variables
  155. Variables.Clear();
  156. foreach (var v in testCaseCollectionManager.Scope.Variables)
  157. Variables.Add(v);
  158. if (VariableSelectedIndex < 0 && Variables.Count > 0)
  159. VariableSelectedIndex = 0;
  160. }
  161. private void ReloadConnectionStrings()
  162. {
  163. //Take care of variables
  164. ConnectionStringNames.Clear();
  165. foreach (var connStr in testCaseCollectionManager.ConnectionStringNames)
  166. ConnectionStringNames.Add(connStr);
  167. if (ConnectionStringSelectedIndex < 0 && ConnectionStringNames.Count > 0)
  168. ConnectionStringSelectedIndex = 0;
  169. }
  170. internal void Rename(int index, string newName)
  171. {
  172. testCaseCollectionManager.Scope.RenameVariable(index, newName);
  173. Reload();
  174. OnPropertyChanged("Variables");
  175. }
  176. internal void Remove(int index)
  177. {
  178. Variables.RemoveAt(index);
  179. OnPropertyChanged("Variables");
  180. TestCases.Columns.RemoveAt(index);
  181. }
  182. internal bool IsRenamable()
  183. {
  184. return Variables.Count > 0;
  185. }
  186. internal bool IsDeletable()
  187. {
  188. return Variables.Count > 1;
  189. }
  190. internal bool IsValidVariableName(string variableName)
  191. {
  192. return !string.IsNullOrEmpty(variableName) && Variables.Contains(variableName);
  193. }
  194. internal bool IsFirst()
  195. {
  196. return VariableSelectedIndex == 0;
  197. }
  198. internal bool IsLast()
  199. {
  200. return VariableSelectedIndex == Variables.Count - 1;
  201. }
  202. internal void Move(int selectedIndex, int newPosition)
  203. {
  204. testCaseCollectionManager.Scope.MoveVariable(Variables[VariableSelectedIndex], newPosition);
  205. Reload();
  206. VariableSelectedIndex = newPosition;
  207. OnPropertyChanged("Variables");
  208. }
  209. internal void Filter(int selectedIndex, Operator @operator, bool negation, string text)
  210. {
  211. testCaseCollectionManager.Scope.Filter(Variables[VariableSelectedIndex], @operator, negation, text);
  212. Reload();
  213. OnPropertyChanged("TestCases");
  214. }
  215. internal void FilterDistinct()
  216. {
  217. testCaseCollectionManager.Scope.FilterDistinct();
  218. Reload();
  219. OnPropertyChanged("TestCases");
  220. }
  221. internal void AddConnectionString(string name, string value)
  222. {
  223. testCaseCollectionManager.AddConnectionStrings(name, value);
  224. OnPropertyChanged("ConnectionStringNames");
  225. }
  226. internal void RemoveConnectionString()
  227. {
  228. testCaseCollectionManager.RemoveConnectionStrings(ConnectionStringSelectedName);
  229. OnPropertyChanged("ConnectionStringNames");
  230. }
  231. internal void EditConnectionString(string newValue)
  232. {
  233. testCaseCollectionManager.EditConnectionStrings(ConnectionStringSelectedName, newValue);
  234. OnPropertyChanged("ConnectionStringNames");
  235. }
  236. internal void RunQuery()
  237. {
  238. testCaseCollectionManager.Scope.ReadFromQuery(Query, ConnectionStringSelectedValue);
  239. Reload();
  240. }
  241. }
  242. }