private void DbClasses_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { DbHelper.Instance.IsChanged = true; if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { if (sender is ObservableCollection <Player> ) { PlayersById.Add(((Player)e.NewItems[0]).Id, (Player)e.NewItems[0]); m_Logger.Debug("Added a player: " + (Player)e.NewItems[0]); NotifyPropertyChanged("PlayersSorted"); } else if (sender is ObservableCollection <GameFamily> ) { GameFamiliesById.Add(((GameFamily)e.NewItems[0]).Id, (GameFamily)e.NewItems[0]); m_Logger.Debug("Added a game family: " + (GameFamily)e.NewItems[0]); NotifyPropertyChanged("GameFamiliesFiltered"); } else if (sender is ObservableCollection <Location> ) { LocationsById.Add(((Location)e.NewItems[0]).Id, (Location)e.NewItems[0]); m_Logger.Debug("Added a Location: " + (Location)e.NewItems[0]); NotifyPropertyChanged("LocationsSorted"); } else if (sender is ObservableCollection <Game> ) { GamesById.Add(((Game)e.NewItems[0]).Id, (Game)e.NewItems[0]); m_Logger.Debug("Added a Game: " + (Game)e.NewItems[0]); NotifyPropertyChanged("GamesPointBased"); NotifyPropertyChanged("GamesSorted"); } else if (sender is ObservableCollection <Result> ) { Result v_Result = (Result)e.NewItems[0]; // Hooking up the notifications for the new object. v_Result.PropertyChanged += Result_PropertyChanged; foreach (Score i_Score in v_Result.Scores) { i_Score.PropertyChanged += Result_PropertyChanged; } NotifyPropertyChanged("ResultsOrdered"); } else { throw new NotImplementedException(String.Format("Adding of [{0}] is not supported.", sender.GetType())); } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { if (sender is ObservableCollection <Player> ) { PlayersById.Remove(((Player)e.OldItems[0]).Id); NotifyPropertyChanged("PlayersSorted"); } else if (sender is ObservableCollection <GameFamily> ) { GameFamiliesById.Remove(((GameFamily)e.OldItems[0]).Id); NotifyPropertyChanged("GameFamiliesFiltered"); } else if (sender is ObservableCollection <Location> ) { LocationsById.Remove(((Location)e.OldItems[0]).Id); NotifyPropertyChanged("LocationsSorted"); } else if (sender is ObservableCollection <Game> ) { GamesById.Remove(((Game)e.OldItems[0]).Id); NotifyPropertyChanged("GamesPointBased"); NotifyPropertyChanged("GamesSorted"); } else if (sender is ObservableCollection <Result> ) { NotifyPropertyChanged("ResultsOrdered"); } else { throw new NotImplementedException(String.Format("Removing of [{0}] is not supported.", sender.GetType())); } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { m_Logger.Debug("Resetting collection for [" + sender.GetType() + "]."); if (sender is ObservableCollection <Player> ) { PlayersById.Clear(); NotifyPropertyChanged("PlayersSorted"); } else if (sender is ObservableCollection <GameFamily> ) { GameFamiliesById.Clear(); NotifyPropertyChanged("GameFamiliesFiltered"); } else if (sender is ObservableCollection <Location> ) { LocationsById.Clear(); NotifyPropertyChanged("LocationsSorted"); } else if (sender is ObservableCollection <Game> ) { GamesById.Clear(); NotifyPropertyChanged("GamesPointBased"); NotifyPropertyChanged("GamesSorted"); } else if (sender is ObservableCollection <Result> ) { NotifyPropertyChanged("ResultsOrdered"); } else { throw new NotImplementedException(String.Format("Resetting of [{0}] is not supported.", sender.GetType())); } } else { throw new NotImplementedException(String.Format("Action {0} not supported on collection.", e.Action)); } }
/// <summary> /// Any database object should be only removed through this method. It encapsulates sanity testing and only deletes /// an entity if it is not referenced in any other entity. /// </summary> /// <param name="a_EntityToRemove">The entity will be tried to converted into a known object.</param> /// <returns>Invalid if something went wrong, Removed if the entity was removed and NotRemoved if the entity is still referenced.</returns> public EntityInteractionStatus RemoveEntity(object a_EntityToRemove) { EntityInteractionStatus v_ActualStatus = EntityInteractionStatus.Invalid; if (a_EntityToRemove is null) { return(v_ActualStatus); } else if (a_EntityToRemove is Player) { Player v_PlayerToRemove = a_EntityToRemove as Player; var v_ReferencedPlayer = Results.SelectMany(p => p.Scores).Where(p => p.IdPlayer == v_PlayerToRemove.Id); if (v_ReferencedPlayer.ToList().Count == 0) { PlayersById.Remove(v_PlayerToRemove.Id); Players.Remove(v_PlayerToRemove); m_Logger.Info(String.Format("Removed Player [{0}].", v_PlayerToRemove)); v_ActualStatus = EntityInteractionStatus.Removed; } else { if (v_PlayerToRemove.Gender == Player.Genders.Male) { m_Logger.Error(String.Format(v_RemovalMessage, v_PlayerToRemove.Name, "he", v_ReferencedPlayer.ToList().Count)); } else if (v_PlayerToRemove.Gender == Player.Genders.Female) { m_Logger.Error(String.Format(v_RemovalMessage, v_PlayerToRemove.Name, "she", v_ReferencedPlayer.ToList().Count)); } else { // Just for the case someone adds another gender... m_Logger.Error(String.Format(v_RemovalMessage, v_PlayerToRemove.Name, "insert pronoun here", v_ReferencedPlayer.ToList().Count)); } v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else if (a_EntityToRemove is GameFamily) { GameFamily v_GameFamilyToRemove = a_EntityToRemove as GameFamily; var v_GamesWithFamilyToRemove = Games.Where(p => p.IdGamefamilies.Contains(v_GameFamilyToRemove.Id)); if (v_GamesWithFamilyToRemove.ToList().Count == 0) { GameFamilies.Remove(v_GameFamilyToRemove); m_Logger.Info(String.Format("Removed GameFamily [{0}].", v_GameFamilyToRemove)); v_ActualStatus = EntityInteractionStatus.Removed; } else { m_Logger.Error(String.Format("Cannot remove [{0}] because it is is referenced in {1} games.", v_GameFamilyToRemove.Name, v_GamesWithFamilyToRemove.ToList().Count)); v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else if (a_EntityToRemove is Game) { Game v_GameToRemove = a_EntityToRemove as Game; var v_ReferencesToGame = Results.Where(p => p.IdGame == v_GameToRemove.Id); if (v_ReferencesToGame.ToList().Count == 0) { Games.Remove(v_GameToRemove); m_Logger.Info(String.Format("Removed Game [{0}].", v_GameToRemove.Name)); v_ActualStatus = EntityInteractionStatus.Removed; } else { m_Logger.Error(String.Format("Cannot remove [{0}] because it is is referenced in {1} results.", v_GameToRemove.Name, v_ReferencesToGame.ToList().Count)); v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else if (a_EntityToRemove is Location) { Location v_LocationToRemove = a_EntityToRemove as Location; var v_ReferencesToLocation = Results.Where(p => p.IdLocation == v_LocationToRemove.Id); if (v_ReferencesToLocation.ToList().Count == 0) { Locations.Remove(v_LocationToRemove); m_Logger.Info(String.Format("Removed Location [{0}].", v_LocationToRemove.Name)); v_ActualStatus = EntityInteractionStatus.Removed; } else { m_Logger.Error(String.Format("Cannot remove {0} because it is is referenced in {1} results.", v_LocationToRemove.Name, v_ReferencesToLocation.ToList().Count)); v_ActualStatus = EntityInteractionStatus.NotRemoved; } } else { throw new NotImplementedException(String.Format("Removing entities of type [{0}] is not supported.", a_EntityToRemove.GetType())); } return(v_ActualStatus); }