/Dev/src/IterationManager/Actions.cs

http://TfsIterationManager.codeplex.com · C# · 334 lines · 233 code · 96 blank · 5 comment · 8 complexity · 4be4348caa80dca5bcc4cf495b18f84d MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Sogeti.QueryWrapper;
  6. using Microsoft.TeamFoundation.Client;
  7. using System.Xml;
  8. using Sogeti.VSExtention;
  9. using Sogeti.QueryWrapper;
  10. namespace Mskold.IterationManager
  11. {
  12. public interface IAction
  13. {
  14. string Title { get; }
  15. bool Execute(ref Dictionary<string, object> sharedVariables, ActionNotifyCallbackDelegate notifyCallback);
  16. }
  17. public class CreateIterationAction: IAction
  18. {
  19. string IAction.Title { get { return "Create Iteration node in Areas & Iterations"; } }
  20. bool IAction.Execute(ref Dictionary<string, object> sharedVariables,ActionNotifyCallbackDelegate notifyCallback)
  21. {
  22. try
  23. {
  24. string iterationName = GetSharedVariable<string>("IterationName", sharedVariables);
  25. XmlElement node = GetSharedVariable<XmlElement>("InsertMarkNode", sharedVariables);
  26. TeamExplorerIntergator teamExplorer = GetSharedVariable<TeamExplorerIntergator>("TeamExplorer", sharedVariables);
  27. XmlElement xParent = (XmlElement)node.ParentNode.ParentNode;
  28. CommonStructureWrapper csw = new CommonStructureWrapper(teamExplorer.tpCollection);
  29. XmlNode newIterationNode = csw.CreateNode(iterationName, new Uri(xParent.Attributes["NodeID"].Value.ToString()), GetIndexOfNodeInParent(node));
  30. notifyCallback(0.99, "Created iteration " + iterationName);
  31. sharedVariables.Add("NewIterationPath", newIterationNode.Attributes["Path"].Value.ToString());
  32. return true;
  33. }
  34. catch (Exception ex)
  35. {
  36. notifyCallback(0.99, "Unexpected error " + ex.Message);
  37. return false;
  38. }
  39. }
  40. private int GetIndexOfNodeInParent(XmlNode n)
  41. {
  42. int i = 0;
  43. while (n.PreviousSibling != null)
  44. {
  45. i++;
  46. n = n.PreviousSibling;
  47. }
  48. return i;
  49. }
  50. public T GetSharedVariable<T>(string name, Dictionary<string,object> sharedVariables )
  51. {
  52. object o;
  53. sharedVariables.TryGetValue(name, out o);
  54. return (T)o;
  55. }
  56. }
  57. public class CreateWorkItemAction : IAction
  58. {
  59. string IAction.Title { get { return "Create Sprint workitem "; } }
  60. bool IAction.Execute(ref Dictionary<string, object> sharedVariables, ActionNotifyCallbackDelegate notifyCallback)
  61. {
  62. try
  63. {
  64. string iterationName = GetSharedVariable<string>("IterationName", sharedVariables);
  65. string iterationPath = GetSharedVariable<string>("NewIterationPath", sharedVariables);
  66. TeamExplorerIntergator teamExplorer = GetSharedVariable<TeamExplorerIntergator>("TeamExplorer", sharedVariables);
  67. string sprintWIT = GetSharedVariable<string>("SprintWIT", sharedVariables);
  68. string startDateField = GetSharedVariable<string>("StartDateField", sharedVariables);
  69. string finishDateField = GetSharedVariable<string>("FinishDateField", sharedVariables);
  70. DateTime startDate = GetSharedVariable<DateTime>("StartDate", sharedVariables);
  71. DateTime finishDate = GetSharedVariable<DateTime>("FinishDate", sharedVariables);
  72. //TPWiWrapper wi = new TPWiWrapper(teamExplorer.tpCollection.Uri.ToString(), teamExplorer.tpName);
  73. TPWiWrapper wi = new TPWiWrapper(teamExplorer);
  74. notifyCallback(0.2, "Initialised ");
  75. Dictionary<string, object> fields = new Dictionary<string, object>();
  76. fields.Add(startDateField, startDate);
  77. fields.Add(finishDateField, finishDate);
  78. notifyCallback(0.3, "Setup fields, Creating new " + sprintWIT + "Workitem ");
  79. wi.CreateWorkItem(sprintWIT, iterationName, iterationPath, fields);
  80. notifyCallback(0.99, "Saved workitem ");
  81. return true;
  82. }
  83. catch (Exception ex)
  84. {
  85. notifyCallback(0.99, "Unexpected error " + ex.Message);
  86. return false;
  87. }
  88. }
  89. public T GetSharedVariable<T>(string name, Dictionary<string, object> sharedVariables)
  90. {
  91. object o;
  92. sharedVariables.TryGetValue(name, out o);
  93. return (T)o;
  94. }
  95. }
  96. public class CopyIterationFolderAction : IAction
  97. {
  98. string IAction.Title { get { return "Create new query older from template folder "; } }
  99. bool IAction.Execute(ref Dictionary<string, object> sharedVariables, ActionNotifyCallbackDelegate notifyCallback)
  100. {
  101. try
  102. {
  103. string iterationName = GetSharedVariable<string>("IterationName", sharedVariables);
  104. string newIterationPath = GetSharedVariable<string>("NewIterationPath", sharedVariables);
  105. TeamExplorerIntergator teamExplorer = GetSharedVariable<TeamExplorerIntergator>("TeamExplorer", sharedVariables);
  106. string masterFolder = GetSharedVariable<string>("TempalteWIQLFolder", sharedVariables);
  107. //TPQueryWrapper qw = new TPQueryWrapper(teamExplorer.tpCollection.Uri.ToString(), teamExplorer.tpName);
  108. TPQueryWrapper qw = new TPQueryWrapper(teamExplorer);
  109. notifyCallback(0.10, "Loading folder " + masterFolder);
  110. WiqlFolder qryFld = qw.FindWiqlFolder(masterFolder);
  111. string firstIterationPath = qw.GetUsedIterationsInFolder(qryFld).First();
  112. ActionNotifyCallback childCallback = new ActionNotifyCallback() { _delegate = notifyCallback, start = 0.3, range = 0.5 };
  113. if (qryFld != null)
  114. {
  115. qryFld.Path = qryFld.Path.Substring(0, qryFld.Path.LastIndexOf(@"/") + 1) + iterationName;
  116. qryFld.Name = iterationName;
  117. notifyCallback(0.3, "Replacing " + firstIterationPath + " with " + newIterationPath);
  118. if (qryFld != null)
  119. {
  120. qw.ReplaceIterationinFolder(ref qryFld, firstIterationPath, newIterationPath, childCallback);
  121. notifyCallback(0.8, "Saving query folder " + iterationName);
  122. qw.SaveQueryFolder(qryFld);
  123. notifyCallback(0.99, "Created query folder " + iterationName + " from template");
  124. }
  125. }
  126. return true;
  127. }
  128. catch (Exception ex)
  129. {
  130. notifyCallback(0.99, "Unexpected error " + ex.Message);
  131. return false;
  132. }
  133. }
  134. public T GetSharedVariable<T>(string name, Dictionary<string, object> sharedVariables)
  135. {
  136. object o;
  137. sharedVariables.TryGetValue(name, out o);
  138. return (T)o;
  139. }
  140. }
  141. public class ReplaceIterationInFolderAction : IAction
  142. {
  143. public string myTitle;
  144. public ReplaceIterationInFolderAction(string title)
  145. {
  146. myTitle = title;
  147. }
  148. public ReplaceIterationInFolderAction()
  149. {
  150. myTitle = "Create new query folder from template folder ";
  151. }
  152. string IAction.Title { get { return myTitle; } }
  153. bool IAction.Execute(ref Dictionary<string, object> sharedVariables, ActionNotifyCallbackDelegate notifyCallback)
  154. {
  155. try
  156. {
  157. TeamExplorerIntergator teamExplorer = GetSharedVariable<TeamExplorerIntergator>("TeamExplorer", sharedVariables);
  158. string currentIterationFolder = GetSharedVariable<string>("CurrentIterationFolder", sharedVariables);
  159. string fromIterationPath = GetSharedVariable<string>("FromIterationPath", sharedVariables);
  160. string toIterationPath = GetSharedVariable<string>("ToIterationPath", sharedVariables);
  161. //TPQueryWrapper qw = new TPQueryWrapper(teamExplorer.tpCollection.Uri.ToString(), teamExplorer.tpName);
  162. TPQueryWrapper qw = new TPQueryWrapper(teamExplorer);
  163. notifyCallback(0.10, "Loading folder " + currentIterationFolder);
  164. WiqlFolder currentFolder;
  165. currentFolder = qw.FindWiqlFolder(currentIterationFolder);
  166. ActionNotifyCallback childCallback = new ActionNotifyCallback() { _delegate = notifyCallback, start = 0.3, range = 0.5 };
  167. if (currentFolder != null)
  168. {
  169. notifyCallback(0.3, "Replacing " + fromIterationPath + " with " + toIterationPath);
  170. qw.ReplaceIterationinFolder(ref currentFolder, fromIterationPath, toIterationPath, childCallback);
  171. notifyCallback(0.8, "Saving query folder " + currentIterationFolder);
  172. qw.SaveQueryFolder(currentFolder);
  173. notifyCallback(0.99, "Replaced iteration in query folder " + currentIterationFolder);
  174. }
  175. return true;
  176. }
  177. catch (Exception ex)
  178. {
  179. notifyCallback(0.99, "Unexpected error " + ex.Message);
  180. return false;
  181. }
  182. }
  183. public T GetSharedVariable<T>(string name, Dictionary<string, object> sharedVariables)
  184. {
  185. object o;
  186. sharedVariables.TryGetValue(name, out o);
  187. return (T)o;
  188. }
  189. }
  190. #region "NotUSed???"
  191. public class CopyIterationDefautlWorkItemAction : IAction
  192. {
  193. protected string newIterationPath;
  194. protected string tpCollectionUrl;
  195. protected string teamProjectName;
  196. public CopyIterationDefautlWorkItemAction(string aTPCollectionUrl, string aTeamProjectName, string aTargetIterationPth)
  197. {
  198. tpCollectionUrl = aTPCollectionUrl;
  199. teamProjectName = aTeamProjectName;
  200. newIterationPath = aTargetIterationPth;
  201. }
  202. string IAction.Title { get { return "Copy Default WI to Iteration " + newIterationPath; }}
  203. bool IAction.Execute(ref Dictionary<string, object> sharedVariables, ActionNotifyCallbackDelegate notifyCallback)
  204. {
  205. newIterationPath = newIterationPath.Replace("\\Iteration\\", "\\").Substring(1);
  206. TPWiWrapper WIWrapper = new TPWiWrapper(tpCollectionUrl, teamProjectName);
  207. List<WI> lst = WIWrapper.GetInitialWorkItens();
  208. foreach (WI itm in lst)
  209. {
  210. itm.IterationPath = newIterationPath;
  211. }
  212. WIWrapper.CreatWorkItems(lst);
  213. return true;
  214. }
  215. }
  216. public class CreatePortalSubSiteAction : IAction
  217. {
  218. protected string subSiteName;
  219. protected string tpCollectionUrl;
  220. protected string teamProjectName;
  221. public CreatePortalSubSiteAction(string aTPCollectionUrl, string aTeamProjectName, string theSubSiteName)
  222. {
  223. tpCollectionUrl = aTPCollectionUrl;
  224. teamProjectName = aTeamProjectName;
  225. subSiteName = theSubSiteName;
  226. }
  227. string IAction.Title { get { return "Create subsite subsite " + subSiteName; } }
  228. bool IAction.Execute(ref Dictionary<string, object> sharedVariables, ActionNotifyCallbackDelegate notifyCallback)
  229. {
  230. RegistryWrapper rw = new RegistryWrapper(tpCollectionUrl, teamProjectName);
  231. string sPortalUrl = rw.GetPortalUrl(teamProjectName);
  232. //PortalWrapper.SpPortalWrapper sp= new PortalWrapper.SpPortalWrapper(theTpCollectionUrl);
  233. //sp.CreateSubSite(subSiteName);
  234. return true;
  235. }
  236. }
  237. }
  238. #endregion