private GroupFilterRepository()
 {
     EndSaveCallback = (obj) =>
     {
         Types[obj.GroupFilterID] = obj.Types;
         Changes.AddOrUpdate(obj.GroupFilterID);
     };
     EndDeleteCallback = (obj) =>
     {
         Types.Remove(obj.GroupFilterID);
         Changes.Remove(obj.GroupFilterID);
     };
 }
        /// <summary>
        /// Inserts a batch of <see cref="SVR_AnimeGroup_User"/> into the database.
        /// </summary>
        /// <remarks>
        /// <para>It is up to the caller of this method to manage transactions, etc.</para>
        /// <para>Group Filters, etc. will not be updated by this method.</para>
        /// </remarks>
        /// <param name="session">The NHibernate session.</param>
        /// <param name="groupUsers">The batch of <see cref="SVR_AnimeGroup_User"/> to insert into the database.</param>
        /// <exception cref="ArgumentNullException"><paramref name="session"/> or <paramref name="groupUsers"/> is <c>null</c>.</exception>
        public void UpdateBatch(ISessionWrapper session, IEnumerable <SVR_AnimeGroup_User> groupUsers)
        {
            if (session == null)
            {
                throw new ArgumentNullException(nameof(session));
            }
            if (groupUsers == null)
            {
                throw new ArgumentNullException(nameof(groupUsers));
            }

            foreach (SVR_AnimeGroup_User groupUser in groupUsers)
            {
                lock (globalDBLock)
                {
                    session.Update(groupUser);
                    lock (Cache)
                    {
                        Cache.Update(groupUser);
                    }
                }

                if (!Changes.TryGetValue(groupUser.JMMUserID, out ChangeTracker <int> changeTracker))
                {
                    changeTracker = new ChangeTracker <int>();
                    Changes[groupUser.JMMUserID] = changeTracker;


                    changeTracker.AddOrUpdate(groupUser.AnimeGroupID);
                }
            }
        }
Esempio n. 3
0
 internal override void EndSave(SVR_GroupFilter entity, object returnFromBeginSave, bool onlyconditions)
 {
     lock (Types)
     {
         lock (Changes)
         {
             Types[entity.GroupFilterID] = entity.Types;
             Changes.AddOrUpdate(entity.GroupFilterID);
         }
     }
 }
        public void Save(SVR_AnimeGroup grp, bool updategrpcontractstats, bool recursive,
                         bool verifylockedFilters = true)
        {
            using (var session = DatabaseFactory.SessionFactory.OpenSession())
            {
                ISessionWrapper sessionWrapper = session.Wrap();
                lock (globalDBLock)
                {
                    lock (grp)
                    {
                        if (grp.AnimeGroupID == 0)
                        //We are creating one, and we need the AnimeGroupID before Update the contracts
                        {
                            grp.Contract = null;
                            using (var transaction = session.BeginTransaction())
                            {
                                session.SaveOrUpdate(grp);
                                transaction.Commit();
                            }
                        }
                        var types = grp.UpdateContract(sessionWrapper, updategrpcontractstats);
                        //Types will contains the affected GroupFilterConditionTypes
                        using (var transaction = session.BeginTransaction())
                        {
                            SaveWithOpenTransaction(session, grp);
                            transaction.Commit();
                        }
                        lock (Changes)
                        {
                            Changes.AddOrUpdate(grp.AnimeGroupID);
                        }

                        if (verifylockedFilters)
                        {
                            RepoFactory.GroupFilter.CreateOrVerifyDirectoryFilters(false, grp.Contract.Stat_AllTags,
                                                                                   grp.Contract.Stat_AllYears, grp.Contract.Stat_AllSeasons);
                            //This call will create extra years or tags if the Group have a new year or tag
                            grp.UpdateGroupFilters(types, null);
                        }
                    }
                }
                if (grp.AnimeGroupParentID.HasValue && recursive)
                {
                    SVR_AnimeGroup pgroup = GetByID(grp.AnimeGroupParentID.Value);
                    // This will avoid the recursive error that would be possible, it won't update it, but that would be
                    // the least of the issues
                    if (pgroup != null && pgroup.AnimeGroupParentID == grp.AnimeGroupID)
                    {
                        Save(pgroup, updategrpcontractstats, true, verifylockedFilters);
                    }
                }
            }
        }
Esempio n. 5
0
        public void Save(SVR_AnimeSeries obj, bool updateGroups, bool onlyupdatestats, bool skipgroupfilters = false,
                         bool alsoupdateepisodes = false)
        {
            bool           newSeries   = false;
            SVR_AnimeGroup oldGroup    = null;
            bool           isMigrating = false;

            lock (obj)
            {
                if (obj.AnimeSeriesID == 0)
                {
                    newSeries = true; // a new series
                }
                else
                {
                    // get the old version from the DB
                    SVR_AnimeSeries oldSeries;
                    using (var session = DatabaseFactory.SessionFactory.OpenSession())
                    {
                        lock (globalDBLock)
                        {
                            oldSeries = session.Get <SVR_AnimeSeries>(obj.AnimeSeriesID);
                        }
                    }
                    if (oldSeries != null)
                    {
                        // means we are moving series to a different group
                        if (oldSeries.AnimeGroupID != obj.AnimeGroupID)
                        {
                            oldGroup = RepoFactory.AnimeGroup.GetByID(oldSeries.AnimeGroupID);
                            SVR_AnimeGroup newGroup = RepoFactory.AnimeGroup.GetByID(obj.AnimeGroupID);
                            if (newGroup != null && newGroup.GroupName.Equals("AAA Migrating Groups AAA"))
                            {
                                isMigrating = true;
                            }
                            newSeries = true;
                        }
                    }
                }
                if (newSeries && !isMigrating)
                {
                    obj.Contract = null;
                    base.Save(obj);
                }
                HashSet <GroupFilterConditionType> types = obj.UpdateContract(onlyupdatestats);
                base.Save(obj);

                if (updateGroups && !isMigrating)
                {
                    logger.Trace("Updating group stats by series from AnimeSeriesRepository.Save: {0}", obj.AnimeSeriesID);
                    SVR_AnimeGroup grp = RepoFactory.AnimeGroup.GetByID(obj.AnimeGroupID);
                    if (grp != null)
                    {
                        RepoFactory.AnimeGroup.Save(grp, true, true);
                    }

                    if (oldGroup != null)
                    {
                        logger.Trace("Updating group stats by group from AnimeSeriesRepository.Save: {0}",
                                     oldGroup.AnimeGroupID);
                        RepoFactory.AnimeGroup.Save(oldGroup, true, true);
                    }
                }
                if (!skipgroupfilters && !isMigrating)
                {
                    int endyear = obj.Contract?.AniDBAnime?.AniDBAnime?.EndYear ?? 0;
                    if (endyear == 0)
                    {
                        endyear = DateTime.Today.Year;
                    }
                    HashSet <int> allyears = null;
                    if ((obj.Contract?.AniDBAnime?.AniDBAnime?.BeginYear ?? 0) != 0)
                    {
                        allyears = new HashSet <int>(Enumerable.Range(obj.Contract.AniDBAnime.AniDBAnime.BeginYear,
                                                                      endyear - obj.Contract.AniDBAnime.AniDBAnime.BeginYear + 1));
                    }
                    //This call will create extra years or tags if the Group have a new year or tag
                    RepoFactory.GroupFilter.CreateOrVerifyDirectoryFilters(false,
                                                                           obj.Contract?.AniDBAnime?.AniDBAnime?.GetAllTags(), allyears,
                                                                           obj.Contract?.AniDBAnime?.Stat_AllSeasons);

                    // Update other existing filters
                    obj.UpdateGroupFilters(types, null);
                }
                lock (Changes)
                {
                    Changes.AddOrUpdate(obj.AnimeSeriesID);
                }
            }
            if (alsoupdateepisodes)
            {
                List <SVR_AnimeEpisode> eps = RepoFactory.AnimeEpisode.GetBySeriesID(obj.AnimeSeriesID);
                RepoFactory.AnimeEpisode.Save(eps);
            }
        }
Esempio n. 6
0
        public void Save(AnimeSeries obj, bool updateGroups, bool onlyupdatestats, bool skipgroupfilters = false, bool alsoupdateepisodes = false)
        {
            bool       newSeries   = false;
            AnimeGroup oldGroup    = null;
            bool       isMigrating = false;

            lock (obj)
            {
                if (obj.AnimeSeriesID == 0)
                {
                    newSeries = true; // a new series
                }
                else
                {
                    // get the old version from the DB
                    AnimeSeries oldSeries;
                    using (var session = DatabaseFactory.SessionFactory.OpenSession())
                    {
                        oldSeries = session.Get <AnimeSeries>(obj.AnimeSeriesID);
                    }
                    if (oldSeries != null)
                    {
                        // means we are moving series to a different group
                        if (oldSeries.AnimeGroupID != obj.AnimeGroupID)
                        {
                            oldGroup = RepoFactory.AnimeGroup.GetByID(oldSeries.AnimeGroupID);
                            AnimeGroup newGroup = RepoFactory.AnimeGroup.GetByID(obj.AnimeGroupID);
                            if (newGroup != null && newGroup.GroupName.Equals("AAA Migrating Groups AAA"))
                            {
                                isMigrating = true;
                            }
                            newSeries = true;
                        }
                    }
                }
                if (newSeries && !isMigrating)
                {
                    obj.Contract = null;
                    base.Save(obj);
                }
                HashSet <GroupFilterConditionType> types = obj.UpdateContract(onlyupdatestats);
                base.Save(obj);
                if (!skipgroupfilters && !isMigrating)
                {
                    RepoFactory.GroupFilter.CreateOrVerifyTagsAndYearsFilters(false,
                                                                              obj.Contract?.AniDBAnime?.AniDBAnime?.AllTags, obj.Contract?.AniDBAnime?.AniDBAnime?.AirDate);
                    //This call will create extra years or tags if the Group have a new year or tag
                    obj.UpdateGroupFilters(types, null);
                }
                Changes.AddOrUpdate(obj.AnimeSeriesID);
            }
            if (updateGroups && !isMigrating)
            {
                logger.Trace("Updating group stats by series from AnimeSeriesRepository.Save: {0}", obj.AnimeSeriesID);
                AnimeGroup grp = RepoFactory.AnimeGroup.GetByID(obj.AnimeGroupID);
                if (grp != null)
                {
                    RepoFactory.AnimeGroup.Save(grp, true, true);
                }

                if (oldGroup != null)
                {
                    logger.Trace("Updating group stats by group from AnimeSeriesRepository.Save: {0}", oldGroup.AnimeGroupID);
                    RepoFactory.AnimeGroup.Save(oldGroup, true, true);
                }
            }
            if (alsoupdateepisodes)
            {
                List <AnimeEpisode> eps = RepoFactory.AnimeEpisode.GetBySeriesID(obj.AnimeSeriesID);
                RepoFactory.AnimeEpisode.Save(eps);
            }
        }
Esempio n. 7
0
        public void Save(SVR_AnimeSeries obj, bool updateGroups, bool onlyupdatestats, bool skipgroupfilters = false,
                         bool alsoupdateepisodes = false)
        {
            DateTime       start;
            TimeSpan       ts;
            bool           newSeries = false;
            SVR_AnimeGroup oldGroup  = null;

            // Updated Now
            obj.DateTimeUpdated = DateTime.Now;
            bool isMigrating = false;

            lock (obj)
            {
                if (obj.AnimeSeriesID == 0)
                {
                    newSeries = true; // a new series
                }
                else
                {
                    // get the old version from the DB
                    SVR_AnimeSeries oldSeries;
                    start = DateTime.Now;
                    using (var session = DatabaseFactory.SessionFactory.OpenSession())
                    {
                        lock (globalDBLock)
                        {
                            oldSeries = session.Get <SVR_AnimeSeries>(obj.AnimeSeriesID);
                        }
                    }

                    ts = DateTime.Now - start;
                    logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Got existing record from database in {ts.Milliseconds}ms");

                    if (oldSeries != null)
                    {
                        // means we are moving series to a different group
                        if (oldSeries.AnimeGroupID != obj.AnimeGroupID)
                        {
                            oldGroup = RepoFactory.AnimeGroup.GetByID(oldSeries.AnimeGroupID);
                            SVR_AnimeGroup newGroup = RepoFactory.AnimeGroup.GetByID(obj.AnimeGroupID);
                            if (newGroup != null && newGroup.GroupName.Equals("AAA Migrating Groups AAA"))
                            {
                                isMigrating = true;
                            }
                            newSeries = true;
                        }
                    }
                    else
                    {
                        // should not happen, but if it does, recover
                        newSeries = true;
                        logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Failed to get valid record from database, making a new one");
                    }
                }
                if (newSeries && !isMigrating)
                {
                    obj.Contract = null;
                    base.Save(obj);
                }
                var seasons = obj.Contract?.AniDBAnime?.Stat_AllSeasons;
                if (seasons == null || seasons.Count == 0)
                {
                    start = DateTime.Now;
                    SVR_AniDB_Anime anime = obj.GetAnime();
                    if (anime != null)
                    {
                        RepoFactory.AniDB_Anime.Save(anime, true);
                    }
                    ts = DateTime.Now - start;
                    logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Regenerated AniDB_Anime contract in {ts.Milliseconds}ms");
                }

                start = DateTime.Now;
                HashSet <GroupFilterConditionType> types = obj.UpdateContract(onlyupdatestats);
                ts = DateTime.Now - start;
                logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updated Contract in {ts.Milliseconds}ms");
                start = DateTime.Now;
                base.Save(obj);
                ts = DateTime.Now - start;
                logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Saved to Database in {ts.Milliseconds}ms");

                if (updateGroups && !isMigrating)
                {
                    start = DateTime.Now;
                    logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updating Group");
                    SVR_AnimeGroup grp = RepoFactory.AnimeGroup.GetByID(obj.AnimeGroupID);
                    if (grp != null)
                    {
                        RepoFactory.AnimeGroup.Save(grp, true, true);
                    }

                    // Last ditch to make sure we aren't just updating the same group twice (shouldn't be)
                    if (oldGroup != null && grp.AnimeGroupID != oldGroup.AnimeGroupID)
                    {
                        logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updating Previous Group (moved series)");
                        RepoFactory.AnimeGroup.Save(oldGroup, true, true);
                    }

                    ts = DateTime.Now - start;
                    logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updated Group Stats in {ts.Milliseconds}ms");
                }
                if (!skipgroupfilters && !isMigrating)
                {
                    start = DateTime.Now;
                    int endyear = obj.Contract?.AniDBAnime?.AniDBAnime?.EndYear ?? 0;
                    if (endyear == 0)
                    {
                        endyear = DateTime.Today.Year;
                    }
                    int startyear = obj.Contract?.AniDBAnime?.AniDBAnime?.BeginYear ?? 0;
                    if (endyear < startyear)
                    {
                        endyear = startyear;
                    }
                    HashSet <int> allyears = null;
                    if (startyear != 0)
                    {
                        allyears = startyear == endyear
                            ? new HashSet <int> {
                            startyear
                        }
                            : new HashSet <int>(Enumerable.Range(startyear, endyear - startyear + 1));
                    }

                    // Reinit this in case it was updated in the contract
                    seasons = obj.Contract?.AniDBAnime?.Stat_AllSeasons;
                    //This call will create extra years or tags if the Group have a new year or tag
                    RepoFactory.GroupFilter.CreateOrVerifyDirectoryFilters(false,
                                                                           obj.Contract?.AniDBAnime?.AniDBAnime?.GetAllTags(), allyears, seasons);

                    // Update other existing filters
                    obj.UpdateGroupFilters(types);
                    ts = DateTime.Now - start;
                    logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updated GroupFilters in {ts.Milliseconds}ms");
                }
                Changes.AddOrUpdate(obj.AnimeSeriesID);
            }
            if (alsoupdateepisodes)
            {
                logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updating Episodes");
                start = DateTime.Now;
                List <SVR_AnimeEpisode> eps = RepoFactory.AnimeEpisode.GetBySeriesID(obj.AnimeSeriesID);
                RepoFactory.AnimeEpisode.Save(eps);
                ts = DateTime.Now - start;
                logger.Trace($"While Saving SERIES {obj.GetAnime()?.MainTitle ?? obj.AniDB_ID.ToString()}, Updated Episodes in {ts.Milliseconds}ms");
            }
        }