public AddEditMilestoneDelayDialog(IssueMilestone milestone)
        {
            InitializeComponent();

            CompositionInitializer.SatisfyImports(this);

            AddEditMilestoneDelayViewModel viewModel = new AddEditMilestoneDelayViewModel(milestone) {View = this};
            viewModel.Loaded += () =>{DataContext = viewModel;};
        }
        /// <summary>
        ///     EDIT
        /// </summary>
        public AddEditIssueMilestoneViewModel(List<IssueMilestone> milestones, IssueMilestone editedMilestone, int issueTypeId)
        {
            CompositionInitializer.SatisfyImports(this);

            mEditedMilestone = editedMilestone;
            mMilestones = milestones;

            LoadLists(issueTypeId);

            SetOrdinal();

            InitaliseCommands();
        }
        public AddEditMilestoneDelayViewModel(IssueMilestone milestone)
        {
            mInAddMode = true;

            mMilestone = milestone;

            mIssueMilestoneDelay = new IssueMilestoneDelay { IssueMilestone = mMilestone };

            CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint);
            cmsWebServiceClient.GetIssueMilestoneDelayTypesCompleted += cmsWebServiceClient_GetIssueMilestoneDelayTypesCompleted;
            cmsWebServiceClient.GetIssueMilestoneDelayTypesAsync();

            OkButtonCommand = new DelegateCommand<object>(OkButtonHandler, CanExecuteOkButtonHandler);
            CancelButtonCommand = new DelegateCommand<object>(CanelButtonHandler, CanExecuteOkButtonHandler);

            AreAllValid();
        }
        /// <summary>
        ///     CREATE
        /// </summary>
        /// <param name="issue"> The issue. </param>
        public AddEditIssueMilestoneViewModel(Issue issue)
        {
            CompositionInitializer.SatisfyImports(this);

            mMilestones = issue.IssueMilestones;

            mEditedMilestone = new IssueMilestone { Description = string.Empty };
            issue.IssueMilestones.Add(mEditedMilestone);

            mEditedMilestone.IssueId = issue.Id;

            LoadLists(issue.IssueTypeId);

            SetOrdinal();

            InitaliseCommands();

            //AreAllValid();
        }
        /// <summary>
        /// </summary>
        /// <param name="selectedMilestone">milestone the user has selected in the XAML</param>
        /// <param name="otherMilestone">milestone that gets itself swapped with selectedMilestone</param>
        /// <param name="sortDirection">SortDirection.Up or SortDirection.Down</param>
        private void SwapLinkedMilestones(IssueMilestone selectedMilestone, IssueMilestone otherMilestone, SortDirection sortDirection)
        {
            if (selectedMilestone.Id == 0 || otherMilestone.Id == 0)
            {
                PopupDialog warningDialog = new PopupDialog(PopupDialogType.Warning, "Please Save first before re-ordering.", "Please Save");
                warningDialog.Show();
                return;
            }

            if (sortDirection == SortDirection.Up)
            {
                selectedMilestone.Ordinal -= 1; //move up
                otherMilestone.Ordinal += 1; //move down
            }
            else
            {
                selectedMilestone.Ordinal += 1; //move down
                otherMilestone.Ordinal -= 1; //move up
            }

            FixupPostSort();
        }
        private static void SaveMilestones(Issue issue, CmsEntities cee)
        {
            log.Verbose("SaveMilestones()");
            try
            {
                var originalMilestones = (from x in cee.IssueMilestones.Include("IssueMilestoneDelays") where x.IssueId == issue.Id select x).ToList();

                //gather db delays
                List<IssueMilestoneDelay> dbIssueMilestoneDelays = new List<IssueMilestoneDelay>();
                foreach (var original in originalMilestones)
                {
                    if (original.IssueMilestoneDelays != null && original.IssueMilestoneDelays.Any())
                    {
                        dbIssueMilestoneDelays.AddRange(original.IssueMilestoneDelays);
                    }
                }

                //gather memory delays
                List<IssueMilestoneDelay> memoryMilestoneDelays = new List<IssueMilestoneDelay>();
                foreach (var mem in issue.IssueMilestones)
                {
                    if (mem.IssueMilestoneDelays != null && mem.IssueMilestoneDelays.Any())
                    {
                        memoryMilestoneDelays.AddRange(mem.IssueMilestoneDelays);
                    }
                }

                foreach (var delay in dbIssueMilestoneDelays)
                {
                    var match = (from x in memoryMilestoneDelays where x.Id == delay.Id select x).FirstOrDefault();

                    if (match == null)
                    {
                        //DELETE delay
                        cee.IssueMilestoneDelays.Remove(delay);
                        cee.SaveChanges();
                    }
                }

                foreach (var original in originalMilestones)
                {
                    var milestone = (from x in issue.IssueMilestones where original.Id == x.Id select x).FirstOrDefault();

                    if (milestone == null)
                    {
                        //delete MILESTONE
                        cee.IssueMilestones.Remove(original);
                        cee.SaveChanges();
                    }
                }

                foreach (IssueMilestone milestone in issue.IssueMilestones.OrderBy(x => x.Ordinal))
                {

                    var matchMilestone = (from x in cee.IssueMilestones where x.Id == milestone.Id select x).FirstOrDefault();
                    bool isNewMilestone = false;
                    if (matchMilestone == null)
                    {
                        isNewMilestone = true;
                        matchMilestone = new IssueMilestone { IssueId = issue.Id };
                        cee.IssueMilestones.Add(matchMilestone);
                    }

                    matchMilestone.Ordinal = milestone.Ordinal;
                    matchMilestone.AccumulatedDelay = milestone.AccumulatedDelay;
                    matchMilestone.ActualStart = milestone.ActualStart;
                    matchMilestone.ActualEnd = milestone.ActualEnd;
                    matchMilestone.AssignedToId = milestone.AssignedToId;

                    if (isNewMilestone)
                    {
                        if (milestone.PlannedPreviousMilestone != null)
                        {
                            //get previous milestone based on the Ordinal
                            var previousMilestone = (from x in cee.IssueMilestones
                                                     where
                                                         x.IssueId == milestone.PlannedPreviousMilestone.IssueId &&
                                                         x.Ordinal == milestone.PlannedPreviousMilestone.Ordinal &&
                                                         x.Description == milestone.PlannedPreviousMilestone.Description &&
                                                         x.AssignedToId == milestone.PlannedPreviousMilestone.AssignedToId
                                                     select x).FirstOrDefault();
                            if (previousMilestone != null)
                            {
                                matchMilestone.PlannedPreviousMilestoneId = previousMilestone.Id;
                            }
                        }

                        if (milestone.ActualPreviousMilestone != null)
                        {
                            //get previous milestone based on the Ordinal
                            var previousMilestone = (from x in cee.IssueMilestones
                                                     where
                                                         x.IssueId == milestone.ActualPreviousMilestone.IssueId &&
                                                         x.Ordinal == milestone.ActualPreviousMilestone.Ordinal &&
                                                         x.Description == milestone.ActualPreviousMilestone.Description &&
                                                         x.AssignedToId == milestone.ActualPreviousMilestone.AssignedToId
                                                     select x).FirstOrDefault();
                            if (previousMilestone != null)
                            {
                                matchMilestone.ActualPreviousMilestoneId = previousMilestone.Id;
                            }
                        }
                    }
                    else
                    {
                        matchMilestone.PlannedPreviousMilestoneId = milestone.PlannedPreviousMilestoneId;
                        matchMilestone.ActualPreviousMilestoneId = milestone.ActualPreviousMilestoneId;
                    }

                    matchMilestone.Description = milestone.Description;
                    matchMilestone.PlannedStart = milestone.PlannedStart;
                    matchMilestone.PlannedEnd = milestone.PlannedEnd;
                    matchMilestone.MilestoneTypeId = milestone.MilestoneTypeId;
                    matchMilestone.PercentageComplete = milestone.PercentageComplete;
                    cee.SaveChanges();

                    //save / update delays
                    foreach (IssueMilestoneDelay delay in milestone.IssueMilestoneDelays)
                    {
                        var matchDelay = (from x in cee.IssueMilestoneDelays where x.Id == delay.Id select x).FirstOrDefault();

                        if (matchDelay == null)
                        {
                            matchDelay = new IssueMilestoneDelay();
                            cee.IssueMilestoneDelays.Add(matchDelay);
                        }

                        matchDelay.IssueMilestoneId = matchMilestone.Id;
                        matchDelay.Delay = delay.Delay;
                        matchDelay.IssueMilestoneDelayTypeId = delay.IssueMilestoneDelayTypeId;
                        matchDelay.DelayCause = delay.DelayCause;
                        matchDelay.CorrectiveAction = delay.CorrectiveAction;
                    } //end for loop on milestones
                    cee.SaveChanges();
                }
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var validationErrors in ex.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        log.Error("", ex, "Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }