void CancelChanges() { if (EmployeeViews.Where(e => e.IsAdded).Count() > 0 || EmployeeViews.Where(e => e.IsChanged).Count() > 0 || EmployeeViews.Where(e => e.IsDeleted).Count() > 0) { MessageBoxVM = new MessageBoxModel() { Message = "Все внесённые изменения будут удалены. Продолжить?", Caption = "сотрудники", Buttons = MessageBoxButton.YesNo }; if (MessageBoxVM.Result == MessageBoxResult.Yes) { int numOfAdded = 0; foreach (var eV in EmployeeViews) { if (eV.IsAdded) { numOfAdded++; } if (eV.IsChanged) { eV.Name = eV.Backup.Name; if (eV.IsChanged) { eV.Surname = eV.Backup.Surname; } if (eV.IsChanged) { eV.MiddleName = eV.Backup.MiddleName; } if (eV.IsChanged) { eV.EMail = eV.Backup.EMail; } } if (eV.IsDeleted) { eV.IsDeleted = false; } } while (numOfAdded > 0) { EmployeeViews.Remove(EmployeeViews.First(eV => eV.IsAdded)); numOfAdded--; } if (EmployeeViews.FirstOrDefault(eV => eV.IsAdded || eV.IsDeleted) != null) { IsEmployeeViewsChanged = true; } else { IsEmployeeViewsChanged = false; } } } }
void FilterViewFromDB() { if (LoadFromDb == true) { List <Employee> employees = new List <Employee>(); try { if (EmployeeViews.Where(eV => eV.IsAdded).Count() > 0 || EmployeeViews.Where(eV => eV.IsChanged).Count() > 0 || EmployeeViews.Where(eV => eV.IsDeleted).Count() > 0) { MessageBoxVM = new MessageBoxModel() { Message = "Все внесённые изменения будут удалены. Продолжить?", Caption = "сотрудники", Buttons = MessageBoxButton.YesNo }; if (MessageBoxVM.Result == MessageBoxResult.Yes) { while (EmployeeViews.Count > 0) { EmployeeViews.Remove(EmployeeViews.First()); } employees = LoadFilteredDataFromDb(FilterControlName); } } else { while (EmployeeViews.Count > 0) { EmployeeViews.Remove(EmployeeViews.First()); } employees = LoadFilteredDataFromDb(FilterControlName); } } catch (Exception ex) { MessageBoxVM = new MessageBoxModel() { Message = ErrorMessage.MakingMessageForMessageBox(ex), Caption = errCaption }; Log.WriteLogAsync(ex); } Mapper.Map <List <EmployeeView> >(employees) .ForEach(e => { e.PropertyChanged += (s, eArgs) => OnEmployeeViewChnaged(e, eArgs.PropertyName); e.EmplProjViews.CollectionChanged += (s, eArgs) => OnEmplProjViewsCountChanged(e, eArgs.NewItems, eArgs.OldItems); e.LeadProjViews.CollectionChanged += (s, eArgs) => OnLeadProjViewsCountChanged(e, eArgs.NewItems, eArgs.OldItems); EmployeeViews.Add(e); }); } }
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; } } }