Exemple #1
0
        /// <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);
        }
Exemple #2
0
        public void Init()
        {
            m_Logger.Info("Init Database.");
            PlayersById = new Dictionary <Guid, Player>();

            foreach (Player i_Player in Players)
            {
                PlayersById.Add(i_Player.Id, i_Player);
            }

            m_Logger.Info(String.Format("[{0}] Players loaded.", Players.Count));
            GameFamiliesById = new Dictionary <Guid, GameFamily>();

            foreach (GameFamily i_Family in GameFamilies)
            {
                GameFamiliesById.Add(i_Family.Id, i_Family);
                m_Logger.Debug("GF: " + i_Family.Id + " " + i_Family.Name);
            }

            m_Logger.Info(String.Format("[{0}] Game Families loaded.", GameFamilies.Count));
            GamesById = new Dictionary <Guid, Game>();

            foreach (Game i_Game in Games)
            {
                //i_Game.Family = GameFamiliesById[i_Game.IdGamefamily];
                GamesById.Add(i_Game.Id, i_Game);
            }

            m_Logger.Info(String.Format("[{0}] Games loaded.", Games.Count));
            LocationsById = new Dictionary <Guid, Location>();

            foreach (Location i_Location in Locations)
            {
                LocationsById.Add(i_Location.Id, i_Location);
            }

            m_Logger.Info(String.Format("[{0}] Locations loaded.", Locations.Count));

            foreach (Result i_Result in Results)
            {
                i_Result.Init();
                i_Result.PropertyChanged += Result_PropertyChanged;

                foreach (Score i_Score in i_Result.Scores)
                {
                    i_Score.PropertyChanged += Result_PropertyChanged;
                }
            }

            Players      = new ObservableCollection <Player>(Players.OrderBy(p => p.Name));
            GameFamilies = new ObservableCollection <GameFamily>(GameFamilies.OrderBy(p => p.Name));
            Locations    = new ObservableCollection <Location>(Locations.OrderBy(p => p.Name));
            Games        = new ObservableCollection <Game>(Games.OrderBy(p => p.Name));
            Results      = new ObservableCollection <Result>(Results.OrderByDescending(p => p.Date));

            Players.CollectionChanged      += DbClasses_CollectionChanged;
            GameFamilies.CollectionChanged += DbClasses_CollectionChanged;
            Locations.CollectionChanged    += DbClasses_CollectionChanged;
            Games.CollectionChanged        += DbClasses_CollectionChanged;
            Results.CollectionChanged      += DbClasses_CollectionChanged;

            m_Logger.Info(String.Format("[{0}] Results loaded.", Results.Count));
            m_Logger.Info("Init Database completed.");
        }