void SaveChanges()
        {
            MessageBoxVM = new MessageBoxModel()
            {
                Message = "Выполнить сохранение всех внесённых изменений?",
                Caption = "сотрудники",
                Buttons = MessageBoxButton.YesNo
            };
            if (MessageBoxVM.Result == MessageBoxResult.Yes)
            {
                List <Employee> emplsToAdd    = Mapper.Map <IEnumerable <EmployeeView>, List <Employee> >(EmployeeViews.Where(eV => eV.IsAdded));
                List <Employee> emplsToUpdate = Mapper.Map <IEnumerable <EmployeeView>, List <Employee> >(EmployeeViews.Where(eV => eV.IsChanged));
                List <Employee> emplsToDelete = Mapper.Map <IEnumerable <EmployeeView>, List <Employee> >(EmployeeViews.Where(eV => eV.IsDeleted));

                bool IsChangesSaved = false;
                try
                {
                    IsChangesSaved = EmployeeLogic.SetEmployees(emplsToAdd, emplsToUpdate, emplsToDelete);
                }
                catch (Exception ex)
                {
                    MessageBoxVM = new MessageBoxModel()
                    {
                        Message = ErrorMessage.MakingMessageForMessageBox(ex),
                        Caption = errCaption
                    };
                    Log.WriteLogAsync(ex);
                }
                if (IsChangesSaved)
                {
                    if (emplsToDelete.Count > 0)
                    {
                        while (EmployeeViews.Where(eV => eV.IsDeleted).Count() > 0)
                        {
                            EmployeeView emplView = EmployeeViews.First(eV => eV.IsDeleted);
                            foreach (var pV in ViewModelsContainer.ProjectsViewModel.ProjectViews)
                            {
                                pV.ProjEmplViews.Remove(Mapper.Map <ProjEmplView>(emplView));
                                if (pV.ProjLeadView != null && pV.ProjLeadView.Id == emplView.Id)
                                {
                                    pV.ProjLeadView = null;
                                }
                            }
                            EmployeeViews.Remove(emplView);
                        }
                    }

                    if (emplsToAdd.Count > 0)
                    {
                        IEnumerator <Employee> emplEnumerator = EmployeeLogic.GetEmplIDs().GetEnumerator();
                        foreach (var eV in EmployeeViews)
                        {
                            emplEnumerator.MoveNext();
                            eV.Id = emplEnumerator.Current.Id;
                            if (eV.IsAdded == true)
                            {
                                eV.LeadProjViews = new ObservableCollection <EmplProjView>();
                                eV.EmplProjViews = new ObservableCollection <EmplProjView>();
                            }
                            eV.IsAdded = false;
                        }
                    }

                    if (emplsToUpdate.Count > 0)
                    {
                        foreach (var eV in EmployeeViews)
                        {
                            eV.IsChanged = false;
                            eV.BackupClear();
                        }
                    }
                    IsEmployeeViewsChanged = false;
                }
            }
        }
Ejemplo n.º 2
0
 public void BackupClear()
 {
     Backup = null;
 }