TicketImporter.TfsProject.EndImport C# (CSharp) Method

EndImport() public method

public EndImport ( ) : void
return void
        public void EndImport()
        {
            var batchFile = "";
            if (newlyImported.Count > 0)
            {
                var progressNotifer = new ProgressNotifier(OnPercentComplete, (newlyImported.Count + 3));

                // Batch save WorkItems to TFS
                progressNotifer.UpdateProgress();
                var workItemStore = (WorkItemStore) tfs.GetService(typeof (WorkItemStore));

                var batch = new WorkItem[newlyImported.Count];
                newlyImported.Values.CopyTo(batch, 0);

                // Unfortunately once created we need to revisit & update TFS ticket state.
                // (Seemingly you cannot create a new TFS ticket with a status of "Closed", "In-progress" etc).
                foreach (var item in newlyImported)
                {
                    updateWorkItem(item.Key, item.Value);
                    progressNotifer.UpdateProgress();
                }
                progressNotifer.UpdateProgress();

                batchFile = generateBatchFile();
                Users.ReleaseImpersonations();
                progressNotifer.UpdateProgress();
            }

            if (tfsUsers.CanImpersonate == false || tfsUsers.FailedImpersonations.Count > 0)
            {
                if (importSummary.Warnings.Count > 0)
                {
                    importSummary.Warnings.Add(Environment.NewLine);
                }
                importSummary.Warnings.Add("Impersonation");
                importSummary.Warnings.Add("-------------");
            }

            if (tfsUsers.CanImpersonate == false)
            {
                importSummary.Warnings.Add(
                    "You don't have impersonation rights enabled. Hence all tickets were created under your name.");
            }
            else if (tfsUsers.FailedImpersonations.Count > 0)
            {
                importSummary.Warnings.Add(string.Format("TFS failed to impersonate the following {0} users.",
                    externalReferenceTag));
                foreach (var failedUser in tfsUsers.FailedImpersonations)
                {
                    importSummary.Warnings.Add(string.Format("* {0}", failedUser.DisplayName));
                }
            }

            if (tfsUsers.NoRightsToAdd.Count > 0)
            {
                if (importSummary.Warnings.Count > 0)
                {
                    importSummary.Warnings.Add(Environment.NewLine);
                }
                importSummary.Warnings.Add("User Rights");
                importSummary.Warnings.Add("-----------");
                importSummary.Warnings.Add(
                    string.Format("The following {0} users do not have rights to add tickets in {1}.",
                        externalReferenceTag, Target));
                importSummary.Warnings.Add(string.Format("Default ticket creator '{0}' used instead.",
                    tfsUsers.CurrentDefaultCreator));
                foreach (var failedUser in tfsUsers.NoRightsToAdd)
                {
                    importSummary.Warnings.Add(string.Format("* {0}", failedUser.DisplayName));
                }
            }

            importSummary.End = DateTime.Now;
            importSummary.Imported = newlyImported.Count;
            importSummary.PreviouslyImported = previouslyImported.Count;
            if (failedAttachments)
            {
                importSummary.Notes.Add(
                    "Default max. attachment size in TFS is 4MB. Contact your TFS Administrator to expand this limit if required.");
            }
            if (string.IsNullOrWhiteSpace(batchFile) == false)
            {
                importSummary.Notes.Add(string.Format("To undo import run the following batch file: '{0}'.", batchFile));
            }
        }