Interaction logic for SelectWorkItemQueryControl.xaml
Inheritance: System.Windows.Window
        private void Shelve()
        {
            var pendingChanges = PendingChanges
                .Where(model => model.IncludeChange)
                .Select(model => model.Change)
                .ToArray();

            if (EvaluatePoliciesAndCheckinNotes)
            {
                var missingCheckinNotes = CheckinNotes
                    .Where(model => model.CheckinNoteFieldDefinition.Required && string.IsNullOrWhiteSpace(model.Value))
                    .Select(model => model.CheckinNoteFieldDefinition.Name).ToArray();

                if (missingCheckinNotes.Any())
                {
                    OnShowPendingChangesItem(ShowPendingChangesTabItemEnum.CheckinNotes);

                    MessageBox.Show(
                        string.Format("Check-in Validation\r\n\r\nEnter a value for {0}", string.Join(", ", missingCheckinNotes)),
                        "Team Pilgrim", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            var workItemInfo = WorkItems
                .Where(model => model.IsSelected)
                .Select(model => new WorkItemCheckinInfo(model.WorkItem, model.WorkItemCheckinAction.ToWorkItemCheckinAction()))
                .ToArray();

            var checkinNoteFieldValues =
                CheckinNotes
                    .Where(model => !string.IsNullOrWhiteSpace(model.Value))
                    .Select(model => new CheckinNoteFieldValue(model.CheckinNoteFieldDefinition.Name, model.Value))
                    .ToArray();

            var checkinNote = new CheckinNote(checkinNoteFieldValues);

            string policyOverrideComment = null;
            if (EvaluatePoliciesAndCheckinNotes)
            {
                CheckinEvaluationResult checkinEvaluationResult;
                if (teamPilgrimServiceModelProvider.TryEvaluateCheckin(out checkinEvaluationResult, _workspaceServiceModel.Workspace, pendingChanges, Comment, checkinNote, workItemInfo))
                {
                    if (!checkinEvaluationResult.IsValid())
                    {
                        OnShowPendingChangesItem(ShowPendingChangesTabItemEnum.PolicyWarnings);

                        var policyFailureModel = new PolicyFailureModel();
                        var policyFailureDialog = new PolicyFailureDialog()
                            {
                                DataContext = policyFailureModel
                            };

                        var dialogResult = policyFailureDialog.ShowDialog();
                        if (!dialogResult.HasValue || !dialogResult.Value || !policyFailureModel.Override)
                        {
                            CheckinEvaluationResult = checkinEvaluationResult;
                            return;
                        }

                        policyOverrideComment = policyFailureModel.Reason;
                    }
                }
                else
                {
                    return;
                }
            }

            var versionControlServer = _projectCollectionServiceModel.TfsTeamProjectCollection.GetVersionControlServer();
            var shelveset = new Shelveset(versionControlServer, ShelvesetName, _projectCollectionServiceModel.TfsTeamProjectCollection.AuthorizedIdentity.UniqueName)
                {
                    Comment = Comment,
                    ChangesExcluded = PendingChanges.Count() != pendingChanges.Count(),
                    WorkItemInfo = workItemInfo,
                    CheckinNote = checkinNote,
                    PolicyOverrideComment = policyOverrideComment
                };

            PendingSet[] pendingSets;
            if (teamPilgrimServiceModelProvider.TryWorkspaceQueryShelvedChanges(_workspaceServiceModel.Workspace, out pendingSets, ShelvesetName,
                                                                                _projectCollectionServiceModel.TfsTeamProjectCollection.AuthorizedIdentity.UniqueName, null))
            {
                bool overwrite = false;
                if (pendingSets != null && pendingSets.Any())
                {
                    if (MessageBox.Show(string.Format("Replace shelveset\r\n\r\nThe shelveset {0} already exists. Replace?", ShelvesetName),
                        "Team Pilgrim", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                    {
                        overwrite = true;
                    }
                    else
                    {
                        return;
                    }
                }

                var shelvingOptions = ShelvingOptions.None;

                if (!PreservePendingChangesLocally)
                    shelvingOptions |= ShelvingOptions.Move;

                if (overwrite)
                    shelvingOptions |= ShelvingOptions.Replace;

                if (teamPilgrimServiceModelProvider.TryShelve(_workspaceServiceModel.Workspace, shelveset, pendingChanges, shelvingOptions))
                {
                }
            }

            OnDismiss(true);
        }
        private void CheckIn()
        {
            this.Logger().Trace("CheckIn");

            var pendingChanges = PendingChanges
                .Where(model => model.IncludeChange)
                .Select(model => model.Change)
                .ToArray();

            var workItemChanges =
                WorkItems.Where(model => model.IsSelected)
                .Select(model => new WorkItemCheckinInfo(model.WorkItem, model.WorkItemCheckinAction.ToWorkItemCheckinAction())).ToArray();

            var missingCheckinNotes = CheckinNotes
                .Where(model => model.CheckinNoteFieldDefinition.Required && string.IsNullOrWhiteSpace(model.Value))
                .Select(model => model.CheckinNoteFieldDefinition.Name).ToArray();

            if (missingCheckinNotes.Any())
            {
                OnShowPendingChangesItem(ShowPendingChangesTabItemEnum.CheckinNotes);

                MessageBox.Show("Check-in Validation\r\n\r\nEnter a value for " + string.Join(", ", missingCheckinNotes), "Team Pilgrim", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var checkinNoteFieldValues =
                CheckinNotes
                .Where(model => !string.IsNullOrWhiteSpace(model.Value))
                .Select(model => new CheckinNoteFieldValue(model.CheckinNoteFieldDefinition.Name, model.Value))
                .ToArray();

            var checkinNote = new CheckinNote(checkinNoteFieldValues);

            CheckinEvaluationResult checkinEvaluationResult;
            if (teamPilgrimServiceModelProvider.TryEvaluateCheckin(out checkinEvaluationResult, Workspace, pendingChanges, Comment, checkinNote, workItemChanges))
            {
                this.Logger().Debug("CheckIn EvaluateCheckin: Valid:{0}", checkinEvaluationResult.IsValid());

                PolicyOverrideInfo policyOverrideInfo = null;

                if (!checkinEvaluationResult.IsValid())
                {
                    if (checkinEvaluationResult.Conflicts.Any())
                    {
                        MessageBox.Show(
                            "Check In\r\n\r\nNo files checked in due to conflicting changes. Please use Conflicts Manager to resolve conflicts and try again.",
                            "Team Pilgrim", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                        var conflictedServerItems = checkinEvaluationResult.Conflicts.Select(conflict => conflict.ServerItem).ToArray();
                        teamPilgrimVsService.ResolveConflicts(Workspace, conflictedServerItems, false, false);

                        return;
                    }

                    if (checkinEvaluationResult.PolicyFailures.Any())
                    {
                        OnShowPendingChangesItem(ShowPendingChangesTabItemEnum.PolicyWarnings);

                        var policyFailureModel = new PolicyFailureModel();
                        var policyFailureDialog = new PolicyFailureDialog()
                            {
                                DataContext = policyFailureModel
                            };

                        var dialogResult = policyFailureDialog.ShowDialog();
                        if (!dialogResult.HasValue || !dialogResult.Value || !policyFailureModel.Override)
                        {
                            CheckinEvaluationResult = checkinEvaluationResult;
                            return;
                        }

                        policyOverrideInfo = new PolicyOverrideInfo(policyFailureModel.Reason, checkinEvaluationResult.PolicyFailures);
                    }
                }

                if (teamPilgrimServiceModelProvider.TryCheckin(Workspace, pendingChanges, Comment, checkinNote, workItemChanges, policyOverrideInfo))
                {
                    Comment = string.Empty;
                    RefreshPendingChanges();

                    foreach (var workItem in WorkItems.Where(model => model.IsSelected))
                    {
                        workItem.IsSelected = false;
                    }

                    RefreshPendingChangesCommand.Execute(null);
                    RefreshSelectedDefinitionWorkItemsCommand.Execute(null);
                }
            }
        }