PageRenderTime 1425ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/TFSHelperPackage/CopyWorkItemAdvancedForm.cs

http://tfshelperforvs2010.codeplex.com
C# | 265 lines | 128 code | 75 blank | 62 comment | 15 complexity | 68203df85843579a2d86bd3a1189f874 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Microsoft.VisualStudio.TeamFoundation;
  10. using Microsoft.TeamFoundation.Client;
  11. using Microsoft.TeamFoundation.WorkItemTracking.Client;
  12. using Microsoft.VisualStudio.Shell;
  13. using Microsoft.TeamFoundation.Framework.Client;
  14. using System.Collections.ObjectModel;
  15. using Microsoft.TeamFoundation.Framework.Common;
  16. using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;
  17. using System.IO;
  18. namespace VS2010.TFSHelperPackage
  19. {
  20. public partial class CopyWorkItemAdvancedForm : Form
  21. {
  22. private IServiceProvider _serviceProvider;
  23. private IWorkItemTrackingDocument _currentDocument;
  24. private WorkItem _currentWorkItem;
  25. private WorkItem _newWorkItem;
  26. private WorkItemStore _workItemStore;
  27. public CopyWorkItemAdvancedForm(IServiceProvider serviceProvider)
  28. {
  29. InitializeComponent();
  30. _serviceProvider = serviceProvider;
  31. LoadControls();
  32. }
  33. private void LoadControls()
  34. {
  35. this.cmbProjects.Items.Clear();
  36. this.cmbWorkItemTypes.Items.Clear();
  37. //Load source workitem
  38. _currentDocument = WitDocumentHelper.GetCurrentWitDocument(_serviceProvider);
  39. string selectedItemId = string.Empty;
  40. if (_currentDocument is IResultsDocument)
  41. {
  42. int cnt = ((IResultsDocument)_currentDocument).SelectedItemIds.Length;
  43. if (cnt > 0)
  44. {
  45. IResultsDocument resultDocument = (IResultsDocument)_currentDocument;
  46. selectedItemId = resultDocument.SelectedItemIds[0].ToString();
  47. //int[] itemIds = {resultDocument.SelectedItemIds[0]};
  48. //_currentWorkItem = resultDocument.ResultListDataProvider.PageItems(itemIds)[0];
  49. _currentWorkItem = resultDocument.ResultListDataProvider.GetItem(0);
  50. //_currentWorkItem = resultDocument.ResultListDataProvider.GetItem(resultDocument.SelectedItemIds[0]);
  51. //_currentWorkItem = resultDocument.WorkItems[0];
  52. }
  53. }
  54. else if (_currentDocument is IWorkItemDocument)
  55. {
  56. // To be completed
  57. }
  58. else if (_currentDocument is IQueryDocument)
  59. {
  60. // To be completed
  61. }
  62. else
  63. {
  64. // To be completed
  65. }
  66. this.Text = "Create Copy of Work Item with options ... [ " + selectedItemId+"]";
  67. //Load dropdowns
  68. var dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
  69. TeamFoundationServerExt ext = dte.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt") as TeamFoundationServerExt;
  70. TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(ext.ActiveProjectContext.DomainUri));
  71. // Refer to http://msdn.microsoft.com/en-us/library/bb286958.aspx for list of types available with GetService
  72. _workItemStore = teamProjectCollection.GetService<WorkItemStore>();
  73. if (_workItemStore != null)
  74. {
  75. if (_workItemStore.Projects.Count > 0)
  76. {
  77. foreach (Project prj in _workItemStore.Projects)
  78. {
  79. this.cmbProjects.Items.Add(prj.Name);
  80. if (this.cmbWorkItemTypes.Items.Count == 0)
  81. {
  82. foreach (WorkItemType wtype in prj.WorkItemTypes)
  83. {
  84. string wtypeName = wtype.Name;
  85. if (!this.cmbWorkItemTypes.Items.Contains(wtypeName))
  86. this.cmbWorkItemTypes.Items.Add(wtypeName);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. this.cmbWorkItemTypes.SelectedIndex = this.cmbWorkItemTypes.Items.IndexOf(_currentWorkItem.Type.Name);
  93. }
  94. private void btnOk_Click(object sender, EventArgs e)
  95. {
  96. if (_currentWorkItem != null)
  97. {
  98. CreateNewWorkItem();
  99. string message = string.Format("Copied Item Successfully !!! \r\n Source Item ID {0} has {1} Attachments and {2} Links \r\n\r\n Target Item ID {3} has {4} Attachments and {5} Links",
  100. _currentWorkItem.Id,
  101. _currentWorkItem.Attachments.Count,
  102. _currentWorkItem.Links.Count,
  103. _newWorkItem.Id,
  104. _newWorkItem.Attachments.Count,
  105. _newWorkItem.Links.Count);
  106. MessageBox.Show(message,this.Text,MessageBoxButtons.OK);
  107. }
  108. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  109. }
  110. private void CreateNewWorkItem()
  111. {
  112. Project targetProject = _workItemStore.Projects[this.cmbProjects.SelectedItem.ToString()];
  113. WorkItemType wit = targetProject.WorkItemTypes[this.cmbWorkItemTypes.SelectedItem.ToString()];
  114. _newWorkItem = _currentWorkItem.Copy(wit);
  115. RelatedLink source2Target = new RelatedLink(targetProject.Id);
  116. _newWorkItem.Links.Add(source2Target);
  117. System.Net.WebClient request = new System.Net.WebClient();
  118. request.Credentials = System.Net.CredentialCache.DefaultCredentials;
  119. string tempPath = System.IO.Path.GetTempPath();
  120. if (_currentWorkItem.Attachments.Count > 0)
  121. {
  122. foreach (Attachment attach in _currentWorkItem.Attachments)
  123. {
  124. string attachmentPath = Path.Combine(tempPath, attach.Name);
  125. request.DownloadFile(attach.Uri, attachmentPath);
  126. _newWorkItem.Attachments.Add(new Attachment(attachmentPath));
  127. }
  128. }
  129. _newWorkItem.Save(SaveFlags.MergeLinks);
  130. //_newWorkItem.Save();
  131. #region comments
  132. //MessageBox.Show("Saved Successfully");
  133. // Create New WorkIten
  134. //WorkItemType wiType = proj.WorkItemTypes["Bug Fix"];
  135. //// Populate New WorkItem
  136. //WorkItem wi = new WorkItem(wiType);
  137. //wi.Fields["FCS.Common.ProductionSystem"].Value = getProductionSystem(wiIn.Fields["System.TeamProject"].Value.ToString());
  138. //wi.Fields["System.Title"].Value = wiIn.Fields["System.Title"].Value;
  139. //wi.Fields["System.Description"].Value = wiIn.Fields["System.Description"].Value;
  140. //wi.Fields["System.AssignedTo"].Value = wiIn.Fields["System.AssignedTo"].Value;
  141. //wi.Fields["FCS.Common.StatusCode"].Value = wiIn.Fields["FCS.Common.StatusCode"].Value;
  142. //wi.Fields["FCS.Common.Top10Priority"].Value = wiIn.Fields["FCS.Common.Top10Priority"].Value;
  143. //if (Convert.IsDBNull(wiIn.Fields["FCS.Common.DetectedDate"]))
  144. //{
  145. //wi.Fields["FCS.Common.DateRequested"].Value = wiIn.Fields["System.DateEntered"].Value;
  146. //}
  147. //else
  148. //{
  149. //wi.Fields["FCS.Common.DateRequested"].Value = wiIn.Fields["FCS.Common.DetectedDate"].Value;
  150. //}
  151. //wi.Fields["FCS.Common.ScheduledRelease"].Value = wiIn.Fields["FCS.Common.ScheduledRelease"].Value;
  152. //wi.Fields["FCS.Common.AssignedBA"].Value = wiIn.Fields["FCS.Common.AssignedBA"].Value;
  153. ////wi.Fields["FCS.Common.Owner1"].Value = wiIn.Fields["FCS.Common.Owner1"].Value;
  154. //wi.Fields["FCS.Common.BAHoursProjected"].Value = wiIn.Fields["FCS.Common.BAHoursProjected"].Value;
  155. //wi.Fields["FCS.Common.BAHoursActual"].Value = wiIn.Fields["FCS.Common.BAHoursActual"].Value;
  156. //wi.Fields["FCS.Common.DevHoursProjected"].Value = wiIn.Fields["FCS.Common.DevHoursProjected"].Value;
  157. //wi.Fields["FCS.Common.DevHoursActual"].Value = wiIn.Fields["FCS.Common.DevHoursActual"].Value;
  158. //wi.Fields["System.History"].Value = wiIn.Fields["System.History"].Value;
  159. ////wi.Fields["System.Attachments"].Value = wiIn.Fields["System.Attachments"].Value;
  160. //// Check for Attachments
  161. //if (wiIn.Attachments.Count > 0)
  162. //{
  163. //foreach (Attachment att in wiIn.Attachments)
  164. //{
  165. //wi.Attachments.Add(att);
  166. //}
  167. //}
  168. //// Check for Links
  169. //foreach (Link lnk in wiIn.Links)
  170. //{
  171. //wi.Links.Add(lnk);
  172. //}
  173. //// Check Validity
  174. //if (!FieldValidityCheck(wi))
  175. //{
  176. //Console.WriteLine("Validity Check Failed!");
  177. //}
  178. //else
  179. //{
  180. //// Save New WorkItem
  181. ////wi.Save();
  182. //}
  183. #endregion comments
  184. }
  185. private void btnCancel_Click(object sender, EventArgs e)
  186. {
  187. this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  188. }
  189. }
  190. }