/// <summary>
        /// Creates <see cref="SVR_AnimeGroup"/> that contain <see cref="SVR_AnimeSeries"/> that appear to be related.
        /// </summary>
        /// <remarks>
        /// This method assumes that there are no active transactions on the specified <paramref name="session"/>.
        /// </remarks>
        /// <param name="session">The NHibernate session.</param>
        /// <param name="seriesList">The list of <see cref="SVR_AnimeSeries"/> to create groups for.</param>
        /// <returns>A sequence of the created <see cref="SVR_AnimeGroup"/>s.</returns>
        private IEnumerable <SVR_AnimeGroup> AutoCreateGroupsWithRelatedSeries(ISessionWrapper session,
                                                                               IReadOnlyCollection <SVR_AnimeSeries> seriesList)
        {
            ServerState.Instance.DatabaseBlocked = new ServerState.DatabaseBlockedInfo {
                Blocked = true, Status = "Auto-generating Groups based on Relation Trees"
            };
            _log.Info("Auto-generating AnimeGroups for {0} AnimeSeries based on aniDB relationships", seriesList.Count);

            DateTime now           = DateTime.Now;
            var      grpCalculator = AutoAnimeGroupCalculator.CreateFromServerSettings(session);

            _log.Info(
                "The following exclusions will be applied when generating the groups: " + grpCalculator.Exclusions);

            // Group all of the specified series into their respective groups (keyed by the groups main anime ID)
            var seriesByGroup     = seriesList.ToLookup(s => grpCalculator.GetGroupAnimeId(s.AniDB_ID));
            var newGroupsToSeries =
                new List <Tuple <SVR_AnimeGroup, IReadOnlyCollection <SVR_AnimeSeries> > >(seriesList.Count);

            foreach (var groupAndSeries in seriesByGroup)
            {
                int             mainAnimeId = groupAndSeries.Key;
                SVR_AnimeSeries mainSeries  = groupAndSeries.FirstOrDefault(series => series.AniDB_ID == mainAnimeId);
                SVR_AnimeGroup  animeGroup  = CreateAnimeGroup(mainSeries, mainAnimeId, now);

                newGroupsToSeries.Add(
                    new Tuple <SVR_AnimeGroup, IReadOnlyCollection <SVR_AnimeSeries> >(animeGroup,
                                                                                       groupAndSeries.AsReadOnlyCollection()));
            }

            using (ITransaction trans = session.BeginTransaction())
            {
                _animeGroupRepo.InsertBatch(session, newGroupsToSeries.Select(gts => gts.Item1).AsReadOnlyCollection());
                trans.Commit();
            }

            // Anime groups should have IDs now they've been inserted. Now assign the group ID's to their respective series
            // (The caller of this method will be responsible for saving the AnimeSeries)
            foreach (var groupAndSeries in newGroupsToSeries)
            {
                foreach (SVR_AnimeSeries series in groupAndSeries.Item2)
                {
                    series.AnimeGroupID = groupAndSeries.Item1.AnimeGroupID;
                }
            }

            _log.Info("Generated {0} AnimeGroups", newGroupsToSeries.Count);

            return(newGroupsToSeries.Select(gts => gts.Item1));
        }
Exemple #2
0
        /// <summary>
        /// Gets or creates an <see cref="SVR_AnimeGroup"/> for the specified series.
        /// </summary>
        /// <param name="session">The NHibernate session.</param>
        /// <param name="series">The series for which the group is to be created/retrieved (Must be initialised first).</param>
        /// <returns>The <see cref="SVR_AnimeGroup"/> to use for the specified series.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="session"/> or <paramref name="series"/> is <c>null</c>.</exception>
        public SVR_AnimeGroup GetOrCreateSingleGroupForSeries(ISessionWrapper session, SVR_AnimeSeries series)
        {
            if (session == null)
            {
                throw new ArgumentNullException(nameof(session));
            }
            if (series == null)
            {
                throw new ArgumentNullException(nameof(series));
            }

            SVR_AnimeGroup animeGroup = null;

            if (_autoGroupSeries)
            {
                var grpCalculator = AutoAnimeGroupCalculator.CreateFromServerSettings(session);
                IReadOnlyList <int> grpAnimeIds = grpCalculator.GetIdsOfAnimeInSameGroup(series.AniDB_ID);
                // Try to find an existing AnimeGroup to add the series to
                // We basically pick the first group that any of the related series belongs to already
                animeGroup = grpAnimeIds.Where(id => id != series.AniDB_ID)
                             .Select(id => RepoFactory.AnimeSeries.GetByAnimeID(id))
                             .Where(s => s != null)
                             .Select(s => RepoFactory.AnimeGroup.GetByID(s.AnimeGroupID))
                             .FirstOrDefault(s => s != null);

                if (animeGroup == null)
                {
                    // No existing group was found, so create a new one
                    int             mainAnimeId = grpCalculator.GetGroupAnimeId(series.AniDB_ID);
                    SVR_AnimeSeries mainSeries  = _animeSeriesRepo.GetByAnimeID(mainAnimeId);

                    animeGroup = CreateAnimeGroup(mainSeries, mainAnimeId, DateTime.Now);
                    RepoFactory.AnimeGroup.Save(animeGroup, true, true);
                }
            }
            else // We're not auto grouping (e.g. we're doing group per series)
            {
                animeGroup = new SVR_AnimeGroup();
                animeGroup.Populate(series, DateTime.Now);
                RepoFactory.AnimeGroup.Save(animeGroup, true, true);
            }

            return(animeGroup);
        }
        /// <summary>
        /// Gets or creates an <see cref="SVR_AnimeGroup"/> for the specified series.
        /// </summary>
        /// <param name="session">The NHibernate session.</param>
        /// <param name="series">The series for which the group is to be created/retrieved (Must be initialised first).</param>
        /// <returns>The <see cref="SVR_AnimeGroup"/> to use for the specified series.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="session"/> or <paramref name="series"/> is <c>null</c>.</exception>
        public SVR_AnimeGroup GetOrCreateSingleGroupForSeries(SVR_AnimeSeries series)
        {
            if (series == null)
            {
                throw new ArgumentNullException(nameof(series));
            }

            SVR_AnimeGroup animeGroup;

            if (_autoGroupSeries)
            {
                var grpCalculator = AutoAnimeGroupCalculator.CreateFromServerSettings();
                IReadOnlyList <int> grpAnimeIds = grpCalculator.GetIdsOfAnimeInSameGroup(series.AniDB_ID);
                // Try to find an existing AnimeGroup to add the series to
                // We basically pick the first group that any of the related series belongs to already
                animeGroup = grpAnimeIds.Where(id => id != series.AniDB_ID)
                             .Select(id => Repo.Instance.AnimeSeries.GetByAnimeID(id))
                             .Where(s => s != null)
                             .Select(s => Repo.Instance.AnimeGroup.GetByID(s.AnimeGroupID))
                             .FirstOrDefault(s => s != null);

                if (animeGroup == null)
                {
                    // No existing group was found, so create a new one
                    int             mainAnimeId = grpCalculator.GetGroupAnimeId(series.AniDB_ID);
                    SVR_AnimeSeries mainSeries  = Repo.Instance.AnimeSeries.GetByAnimeID(mainAnimeId);

                    animeGroup = Repo.Instance.AnimeGroup.BeginAdd(CreateAnimeGroup(mainSeries, mainAnimeId, DateTime.Now)).Commit((true, true, true));
                }
            }
            else // We're not auto grouping (e.g. we're doing group per series)
            {
                using (var upd = Repo.Instance.AnimeGroup.BeginAdd())
                {
                    upd.Entity.Populate_RA(series, DateTime.Now);
                    animeGroup = upd.Commit((true, true, true));
                }
            }

            return(animeGroup);
        }
        /// <summary>
        /// Creates <see cref="SVR_AnimeGroup"/> that contain <see cref="SVR_AnimeSeries"/> that appear to be related.
        /// </summary>
        /// <remarks>
        /// This method assumes that there are no active transactions on the specified <paramref name="session"/>.
        /// </remarks>
        /// <param name="session">The NHibernate session.</param>
        /// <param name="seriesList">The list of <see cref="SVR_AnimeSeries"/> to create groups for.</param>
        /// <returns>A sequence of the created <see cref="SVR_AnimeGroup"/>s.</returns>
        private IEnumerable <SVR_AnimeGroup> AutoCreateGroupsWithRelatedSeries(IReadOnlyCollection <SVR_AnimeSeries> seriesList)
        {
            _log.Info("Auto-generating AnimeGroups for {0} AnimeSeries based on aniDB relationships", seriesList.Count);

            DateTime now           = DateTime.Now;
            var      grpCalculator = AutoAnimeGroupCalculator.CreateFromServerSettings();

            _log.Info("The following exclusions will be applied when generating the groups: " + grpCalculator.Exclusions);

            // Group all of the specified series into their respective groups (keyed by the groups main anime ID)



            var seriesByGroup          = seriesList.ToLookup(s => grpCalculator.GetGroupAnimeId(s.AniDB_ID));
            List <SVR_AnimeGroup> grps = new List <SVR_AnimeGroup>();

            foreach (var groupAndSeries in seriesByGroup)
            {
                int             mainAnimeId = groupAndSeries.Key;
                SVR_AnimeSeries mainSeries  = groupAndSeries.FirstOrDefault(series => series.AniDB_ID == mainAnimeId);
                SVR_AnimeGroup  grp;
                using (var upd = Repo.Instance.AnimeGroup.BeginAdd())
                {
                    CreateAnimeGroup_RA(upd.Entity, mainSeries, mainAnimeId, now);
                    grp = upd.Commit((false, false, false));
                }
                using (var upd = Repo.Instance.AnimeSeries.BeginAddOrUpdate(mainSeries))
                {
                    upd.Entity.AnimeGroupID = grp.AnimeGroupID;
                    upd.Commit((false, false, true, false));
                }
                grps.Add(grp);
            }

            return(grps);
        }