Ejemplo n.º 1
0
		public void Save(GroupFilter obj)
		{
			using (var session = JMMService.SessionFactory.OpenSession())
			{
				// populate the database
				using (var transaction = session.BeginTransaction())
				{
					session.SaveOrUpdate(obj);
					transaction.Commit();
				}
			}
            logger.Trace("Updating group filter stats by groupfilter from GroupFilterRepository.Save: {0}", obj.GroupFilterID);
            StatsCache.Instance.UpdateGroupFilterUsingGroupFilter(obj.GroupFilterID);
		}
Ejemplo n.º 2
0
 public static GroupFilter FromContract(Contract_GroupFilter gfc)
 {
     GroupFilter gf = new GroupFilter();
     gf.GroupFilterID = gfc.GroupFilterID ?? 0;
     gf.GroupFilterName = gfc.GroupFilterName;
     gf.ApplyToSeries = gfc.ApplyToSeries;
     gf.BaseCondition = gfc.BaseCondition;
     gf.SortingCriteria = gfc.SortingCriteria;
     gf.Locked = gfc.Locked;
     gf.InvisibleInClients = gfc.InvisibleInClients;
     gf.ParentGroupFilterID = gfc.ParentGroupFilterID;
     gf.FilterType = gfc.FilterType;
     List<GroupFilterCondition> conds = new List<GroupFilterCondition>();
     foreach (Contract_GroupFilterCondition c in gfc.FilterConditions)
     {
         GroupFilterCondition cc = new GroupFilterCondition();
         cc.ConditionType = c.ConditionType;
         cc.ConditionOperator = c.ConditionOperator;
         cc.ConditionParameter = c.ConditionParameter;
         conds.Add(cc);
     }
     gf.Conditions = conds;
     gf.GroupsIds = gfc.Groups ?? new Dictionary<int, HashSet<int>>();
     gf.SeriesIds = gfc.Series ?? new Dictionary<int, HashSet<int>>();
     return gf;
 }
Ejemplo n.º 3
0
        public Contract_GroupFilter_SaveResponse SaveGroupFilter(Contract_GroupFilter contract)
        {
            Contract_GroupFilter_SaveResponse response = new Contract_GroupFilter_SaveResponse();
            response.ErrorMessage = string.Empty;
            response.GroupFilter = null;

            GroupFilterRepository repGF = new GroupFilterRepository();
            GroupFilterConditionRepository repGFC = new GroupFilterConditionRepository();

            // Process the group
            GroupFilter gf = null;
            if (contract.GroupFilterID.HasValue)
            {
                gf = repGF.GetByID(contract.GroupFilterID.Value);
                if (gf == null)
                {
                    response.ErrorMessage = "Could not find existing Group Filter with ID: " + contract.GroupFilterID.Value.ToString();
                    return response;
                }
            }
            else
                gf = new GroupFilter();

            gf.GroupFilterName = contract.GroupFilterName;
            gf.ApplyToSeries = contract.ApplyToSeries;
            gf.BaseCondition = contract.BaseCondition;
            gf.SortingCriteria = contract.SortingCriteria;

            if (string.IsNullOrEmpty(gf.GroupFilterName))
            {
                response.ErrorMessage = "Must specify a group filter name";
                return response;
            }

            repGF.Save(gf);

            // Process the filter conditions

            // check for any that have been deleted
            foreach (GroupFilterCondition gfc in gf.FilterConditions)
            {
                bool gfcExists = false;
                foreach (Contract_GroupFilterCondition gfc_con in contract.FilterConditions)
                {
                    if (gfc_con.GroupFilterConditionID.HasValue && gfc_con.GroupFilterConditionID.Value == gfc.GroupFilterConditionID)
                    {
                        gfcExists = true;
                        break;
                    }
                }
                if (!gfcExists)
                    repGFC.Delete(gfc.GroupFilterConditionID);
            }

            // save newly added or modified ones
            foreach (Contract_GroupFilterCondition gfc_con in contract.FilterConditions)
            {
                GroupFilterCondition gfc = null;
                if (gfc_con.GroupFilterConditionID.HasValue)
                {
                    gfc = repGFC.GetByID(gfc_con.GroupFilterConditionID.Value);
                    if (gfc == null)
                    {
                        response.ErrorMessage = "Could not find existing Group Filter Condition with ID: " + gfc_con.GroupFilterConditionID.ToString();
                        return response;
                    }
                }
                else
                    gfc = new GroupFilterCondition();

                gfc.ConditionOperator = gfc_con.ConditionOperator;
                gfc.ConditionParameter = gfc_con.ConditionParameter;
                gfc.ConditionType = gfc_con.ConditionType;
                gfc.GroupFilterID = gf.GroupFilterID;

                repGFC.Save(gfc);
            }

            response.GroupFilter = gf.ToContract();

            return response;
        }
Ejemplo n.º 4
0
        public List<Contract_AnimeGroup> GetAnimeGroupsForFilter(int groupFilterID, int userID, bool getSingleSeriesGroups)
        {
            List<Contract_AnimeGroup> retGroups = new List<Contract_AnimeGroup>();
            try
            {
                using (var session = JMMService.SessionFactory.OpenSession())
                {
                    DateTime start = DateTime.Now;
                    GroupFilterRepository repGF = new GroupFilterRepository();

                    JMMUserRepository repUsers = new JMMUserRepository();
                    JMMUser user = repUsers.GetByID(session, userID);
                    if (user == null) return retGroups;

                    GroupFilter gf = null;

                    if (groupFilterID == -999)
                    {
                        // all groups
                        gf = new GroupFilter();
                        gf.GroupFilterName = "All";
                    }
                    else
                    {
                        gf = repGF.GetByID(session, groupFilterID);
                        if (gf == null) return retGroups;
                    }

                    //Contract_GroupFilterExtended contract = gf.ToContractExtended(user);

                    AnimeGroupRepository repGroups = new AnimeGroupRepository();
                    List<AnimeGroup> allGrps = repGroups.GetAll(session);

                    AnimeGroup_UserRepository repUserRecords = new AnimeGroup_UserRepository();
                    List<AnimeGroup_User> userRecords = repUserRecords.GetByUserID(session, userID);
                    Dictionary<int, AnimeGroup_User> dictUserRecords = new Dictionary<int, AnimeGroup_User>();
                    foreach (AnimeGroup_User userRec in userRecords)
                        dictUserRecords[userRec.AnimeGroupID] = userRec;

                    TimeSpan ts = DateTime.Now - start;
                    string msg = string.Format("Got groups for filter DB: {0} - {1} in {2} ms", gf.GroupFilterName, allGrps.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    start = DateTime.Now;

                    AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
                    List<AnimeSeries> allSeries = new List<AnimeSeries>();
                    if (getSingleSeriesGroups)
                        allSeries = repSeries.GetAll(session);
                    if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(user.JMMUserID)) && (StatsCache.Instance.StatUserGroupFilter[user.JMMUserID].ContainsKey(gf.GroupFilterID)))
                    {
                        HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[user.JMMUserID][gf.GroupFilterID];

                        foreach (AnimeGroup grp in allGrps)
                        {
                            AnimeGroup_User userRec = null;
                            if (dictUserRecords.ContainsKey(grp.AnimeGroupID))
                                userRec = dictUserRecords[grp.AnimeGroupID];
                            if (groups.Contains(grp.AnimeGroupID))
                            {
                                Contract_AnimeGroup contractGrp = grp.ToContract(userRec);
                                if (getSingleSeriesGroups)
                                {
                                    if (contractGrp.Stat_SeriesCount == 1)
                                    {
                                        AnimeSeries ser = GetSeriesForGroup(grp.AnimeGroupID, allSeries);
                                        if (ser != null)
                                            contractGrp.SeriesForNameOverride = ser.ToContract(ser.GetUserRecord(session, userID));

                                    }
                                }
                                retGroups.Add(contractGrp);
                            }
                        }
                    }
                    ts = DateTime.Now - start;
                    msg = string.Format("Got groups for filter EVAL: {0} - {1} in {2} ms", gf.GroupFilterName, retGroups.Count, ts.TotalMilliseconds);
                    logger.Info(msg);

                    return retGroups;
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
            }
            return retGroups;
        }
Ejemplo n.º 5
0
		public static List<SortPropOrFieldAndDirection> GetSortDescriptions(GroupFilter gf)
		{
			List<SortPropOrFieldAndDirection> sortlist = new List<SortPropOrFieldAndDirection>();
			foreach (GroupFilterSortingCriteria gfsc in gf.SortCriteriaList)
			{
				sortlist.Add(GetSortDescription(gfsc.SortType, gfsc.SortDirection));
			}
			return sortlist;
		}
Ejemplo n.º 6
0
		private static void CreateContinueWatchingGroupFilter()
		{
			// group filters
			GroupFilterRepository repFilters = new GroupFilterRepository();
			GroupFilterConditionRepository repGFC = new GroupFilterConditionRepository();

			using (var session = JMMService.SessionFactory.OpenSession())
			{
				// check if it already exists
				List<GroupFilter> lockedGFs = repFilters.GetLockedGroupFilters(session);

				if (lockedGFs != null)
				{
					// if it already exists we can leave
					foreach (GroupFilter gfTemp in lockedGFs)
						if (gfTemp.GroupFilterName.Equals(Constants.GroupFilterName.ContinueWatching, StringComparison.InvariantCultureIgnoreCase)) return;
				}

				GroupFilter gf = new GroupFilter();
				gf.GroupFilterName = Constants.GroupFilterName.ContinueWatching;
				gf.Locked = 1;
				gf.SortingCriteria = "4;2"; // by last watched episode desc
				gf.ApplyToSeries = 0;
				gf.BaseCondition = 1; // all

				repFilters.Save(gf);

				GroupFilterCondition gfc = new GroupFilterCondition();
				gfc.ConditionType = (int)GroupFilterConditionType.HasWatchedEpisodes;
				gfc.ConditionOperator = (int)GroupFilterOperator.Include;
				gfc.ConditionParameter = "";
				gfc.GroupFilterID = gf.GroupFilterID;
				repGFC.Save(gfc);

				gfc = new GroupFilterCondition();
				gfc.ConditionType = (int)GroupFilterConditionType.HasUnwatchedEpisodes;
				gfc.ConditionOperator = (int)GroupFilterOperator.Include;
				gfc.ConditionParameter = "";
				gfc.GroupFilterID = gf.GroupFilterID;
				repGFC.Save(gfc);
			}
		}
Ejemplo n.º 7
0
		private static void CreateInitialGroupFilters()
		{
			// group filters
			GroupFilterRepository repFilters = new GroupFilterRepository();
			GroupFilterConditionRepository repGFC = new GroupFilterConditionRepository();

			if (repFilters.GetAll().Count() > 0) return;

			// Favorites
			GroupFilter gf = new GroupFilter();
			gf.GroupFilterName = "Favorites";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			GroupFilterCondition gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.Favourite;
			gfc.ConditionOperator = (int)GroupFilterOperator.Include;
			gfc.ConditionParameter = "";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);



			// Missing Episodes
			gf = new GroupFilter();
			gf.GroupFilterName = "Missing Episodes";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.MissingEpisodesCollecting;
			gfc.ConditionOperator = (int)GroupFilterOperator.Include;
			gfc.ConditionParameter = "";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			// Newly Added Series
			gf = new GroupFilter();
			gf.GroupFilterName = "Newly Added Series";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.SeriesCreatedDate;
			gfc.ConditionOperator = (int)GroupFilterOperator.LastXDays;
			gfc.ConditionParameter = "10";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			// Newly Airing Series
			gf = new GroupFilter();
			gf.GroupFilterName = "Newly Airing Series";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.AirDate;
			gfc.ConditionOperator = (int)GroupFilterOperator.LastXDays;
			gfc.ConditionParameter = "30";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			// Votes Needed
			gf = new GroupFilter();
			gf.GroupFilterName = "Votes Needed";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.CompletedSeries;
			gfc.ConditionOperator = (int)GroupFilterOperator.Include;
			gfc.ConditionParameter = "";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.HasUnwatchedEpisodes;
			gfc.ConditionOperator = (int)GroupFilterOperator.Exclude;
			gfc.ConditionParameter = "";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.UserVotedAny;
			gfc.ConditionOperator = (int)GroupFilterOperator.Exclude;
			gfc.ConditionParameter = "";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			// Recently Watched
			gf = new GroupFilter();
			gf.GroupFilterName = "Recently Watched";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.EpisodeWatchedDate;
			gfc.ConditionOperator = (int)GroupFilterOperator.LastXDays;
			gfc.ConditionParameter = "10";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);

			// TvDB/MovieDB Link Missing
			gf = new GroupFilter();
			gf.GroupFilterName = "TvDB/MovieDB Link Missing";
			gf.ApplyToSeries = 0;
			gf.BaseCondition = 1;

			repFilters.Save(gf);

			gfc = new GroupFilterCondition();
			gfc.ConditionType = (int)GroupFilterConditionType.AssignedTvDBOrMovieDBInfo;
			gfc.ConditionOperator = (int)GroupFilterOperator.Exclude;
			gfc.ConditionParameter = "";
			gfc.GroupFilterID = gf.GroupFilterID;
			repGFC.Save(gfc);
		}
Ejemplo n.º 8
0
        public void UpdateGroupFilterUsingGroup(int groupid)
        {
            AnimeGroupRepository repGroups = new AnimeGroupRepository();
            AnimeGroup_UserRepository repUserGroups = new AnimeGroup_UserRepository();
            JMMUserRepository repUser = new JMMUserRepository();
            GroupFilterRepository repGrpFilter = new GroupFilterRepository();

            AnimeGroup grp = repGroups.GetByID(groupid);
            if (grp.AnimeGroupParentID.HasValue)
                return;
            foreach (JMMUser user in repUser.GetAll())
            {
                AnimeGroup_User userRec = repUserGroups.GetByUserAndGroupID(user.JMMUserID, groupid);
            
                Dictionary<int, HashSet<int>> groupfilters;
                if (StatUserGroupFilter.ContainsKey(user.JMMUserID))
                    groupfilters = StatUserGroupFilter[user.JMMUserID];
                else
                {
                    groupfilters = new Dictionary<int, HashSet<int>>();
                    StatUserGroupFilter.Add(user.JMMUserID, groupfilters);
                }
                List<GroupFilter> gfs = repGrpFilter.GetAll();
                GroupFilter gfgf = new GroupFilter();
                gfgf.GroupFilterName = "All";
                gfs.Add(gfgf);

                foreach (GroupFilter gf in gfs)
                {
                    HashSet<int> groups;
                    if (groupfilters.ContainsKey(gf.GroupFilterID))
                        groups = groupfilters[gf.GroupFilterID];
                    else
                    {
                        groups = new HashSet<int>();
                        groupfilters.Add(gf.GroupFilterID, groups);
                    }
                    if (groups.Contains(groupid))
                        groups.Remove(groupid);
                    if (EvaluateGroupFilter(gf, grp, user, userRec))
                        groups.Add(grp.AnimeGroupID);
                }
            }
        }
Ejemplo n.º 9
0
        public void UpdateGroupFilterUsingUser(int userid)
        {
            AnimeGroupRepository repGroups = new AnimeGroupRepository();
            AnimeGroup_UserRepository repUserGroups = new AnimeGroup_UserRepository();
            JMMUserRepository repUser = new JMMUserRepository();
            GroupFilterRepository repGrpFilter = new GroupFilterRepository();
            JMMUser user = repUser.GetByID(userid);
            if (user == null)
                return;
            Dictionary<int, HashSet<int>> groupfilters;
            if (StatUserGroupFilter.ContainsKey(user.JMMUserID))
                groupfilters = StatUserGroupFilter[user.JMMUserID];
            else
            {
                groupfilters = new Dictionary<int, HashSet<int>>();
                StatUserGroupFilter.Add(user.JMMUserID, groupfilters);
            }
            List<GroupFilter> gfs = repGrpFilter.GetAll();
		    GroupFilter gfgf = new GroupFilter();
			gfgf.GroupFilterName = "All";
            gfs.Add(gfgf);
            foreach(GroupFilter gf in gfs)
            {
                HashSet<int> groups;
                if (groupfilters.ContainsKey(gf.GroupFilterID))
                    groups = groupfilters[gf.GroupFilterID];
                else
                {
                    groups = new HashSet<int>();
                    groupfilters.Add(gf.GroupFilterID, groups);
                }
                groups.Clear();
                List<AnimeGroup> allGrps = repGroups.GetAllTopLevelGroups(); // No Need of subgroups

                foreach (AnimeGroup grp in allGrps)
                {
                    AnimeGroup_User userRec = repUserGroups.GetByUserAndGroupID(user.JMMUserID, grp.AnimeGroupID);
                    if (EvaluateGroupFilter(gf, grp, user, userRec))
                        groups.Add(grp.AnimeGroupID);
                }
            }
        }
Ejemplo n.º 10
0
		public bool EvaluateGroupFilter(GroupFilter gf, AnimeGroup grp, JMMUser curUser, AnimeGroup_User userRec)
		{
			// sub groups don't count
			if (grp.AnimeGroupParentID.HasValue) return false;

			// make sure the user has not filtered this out
			if (!curUser.AllowedGroup(grp, userRec)) return false;

			// first check for anime groups which are included exluded every time
			foreach (GroupFilterCondition gfc in gf.FilterConditions)
			{
				if (gfc.ConditionTypeEnum != GroupFilterConditionType.AnimeGroup) continue;

				int groupID = 0;
				int.TryParse(gfc.ConditionParameter, out groupID);
				if (groupID == 0) break;

				if (gfc.ConditionOperatorEnum == GroupFilterOperator.Equals)
					if (groupID == grp.AnimeGroupID) return true;

				if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotEquals)
					if (groupID == grp.AnimeGroupID) return false;
			}

			NumberStyles style = NumberStyles.Number;
			CultureInfo culture = CultureInfo.CreateSpecificCulture("en-GB");

			if (gf.BaseCondition == (int)GroupFilterBaseCondition.Exclude) return false;

			Contract_AnimeGroup contractGroup = grp.ToContract(userRec);

			// now check other conditions
			foreach (GroupFilterCondition gfc in gf.FilterConditions)
			{
				switch (gfc.ConditionTypeEnum)
				{
					case GroupFilterConditionType.Favourite:
						if (userRec == null) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && userRec.IsFave == 0) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && userRec.IsFave == 1) return false;
						break;

					case GroupFilterConditionType.MissingEpisodes:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && grp.HasMissingEpisodesAny == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && grp.HasMissingEpisodesAny == true) return false;
						break;

					case GroupFilterConditionType.MissingEpisodesCollecting:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && grp.HasMissingEpisodesGroups == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && grp.HasMissingEpisodesGroups == true) return false;
						break;

						case GroupFilterConditionType.HasWatchedEpisodes:
						if (userRec == null) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && userRec.AnyFilesWatched == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && userRec.AnyFilesWatched == true) return false;
						break;

					case GroupFilterConditionType.HasUnwatchedEpisodes:
						if (userRec == null) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && userRec.HasUnwatchedFiles == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && userRec.HasUnwatchedFiles == true) return false;
						break;

					case GroupFilterConditionType.AssignedTvDBInfo:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_HasTvDBLink == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_HasTvDBLink == true) return false;
						break;

					case GroupFilterConditionType.AssignedMALInfo:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_HasMALLink == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_HasMALLink == true) return false;
						break;

					case GroupFilterConditionType.AssignedMovieDBInfo:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_HasMovieDBLink == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_HasMovieDBLink == true) return false;
						break;

					case GroupFilterConditionType.AssignedTvDBOrMovieDBInfo:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_HasMovieDBOrTvDBLink == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_HasMovieDBOrTvDBLink == true) return false;
						break;

					case GroupFilterConditionType.CompletedSeries:

						/*if (grp.IsComplete != grp.Stat_IsComplete)
						{
							Debug.Print("IsComplete DIFF  {0}", grp.GroupName);
						}*/

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_IsComplete == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_IsComplete == true) return false;
						break;

					case GroupFilterConditionType.FinishedAiring:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_HasFinishedAiring == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_IsCurrentlyAiring == false) return false;
						break;

					case GroupFilterConditionType.UserVoted:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_UserVotePermanent.HasValue == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_UserVotePermanent.HasValue == true) return false;
						break;

					case GroupFilterConditionType.UserVotedAny:
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Include && contractGroup.Stat_UserVoteOverall.HasValue == false) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.Exclude && contractGroup.Stat_UserVoteOverall.HasValue == true) return false;
						break;

					case GroupFilterConditionType.AirDate:
						DateTime filterDate;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							int days = 0;
							int.TryParse(gfc.ConditionParameter, out days);
							filterDate = DateTime.Today.AddDays(0 - days);
						}
						else
							filterDate = GetDateFromString(gfc.ConditionParameter);

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan || gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							if (!contractGroup.Stat_AirDate_Min.HasValue || !contractGroup.Stat_AirDate_Max.HasValue) return false;
							if (contractGroup.Stat_AirDate_Max.Value < filterDate) return false;
						}
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
						{
							if (!contractGroup.Stat_AirDate_Min.HasValue || !contractGroup.Stat_AirDate_Max.HasValue) return false;
							if (contractGroup.Stat_AirDate_Min.Value > filterDate) return false;
						}
						break;

					case GroupFilterConditionType.SeriesCreatedDate:
						DateTime filterDateSeries;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							int days = 0;
							int.TryParse(gfc.ConditionParameter, out days);
							filterDateSeries = DateTime.Today.AddDays(0 - days);
						}
						else
							filterDateSeries = GetDateFromString(gfc.ConditionParameter);

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan || gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							if (!contractGroup.Stat_SeriesCreatedDate.HasValue) return false;
							if (contractGroup.Stat_SeriesCreatedDate.Value < filterDateSeries) return false;
						}
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
						{
							if (!contractGroup.Stat_SeriesCreatedDate.HasValue) return false;
							if (contractGroup.Stat_SeriesCreatedDate.Value > filterDateSeries) return false;
						}
						break;

					case GroupFilterConditionType.EpisodeWatchedDate:
						DateTime filterDateEpsiodeWatched;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							int days = 0;
							int.TryParse(gfc.ConditionParameter, out days);
							filterDateEpsiodeWatched = DateTime.Today.AddDays(0 - days);
						}
						else
							filterDateEpsiodeWatched = GetDateFromString(gfc.ConditionParameter);

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan || gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							if (userRec == null) return false;
							if (!userRec.WatchedDate.HasValue) return false;
							if (userRec.WatchedDate.Value < filterDateEpsiodeWatched) return false;
						}
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
						{
							if (userRec == null) return false;
							if (!userRec.WatchedDate.HasValue) return false;
							if (userRec.WatchedDate.Value > filterDateEpsiodeWatched) return false;
						}
						break;

					case GroupFilterConditionType.EpisodeAddedDate:
						DateTime filterDateEpisodeAdded;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							int days = 0;
							int.TryParse(gfc.ConditionParameter, out days);
							filterDateEpisodeAdded = DateTime.Today.AddDays(0 - days);
						}
						else
							filterDateEpisodeAdded = GetDateFromString(gfc.ConditionParameter);

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan || gfc.ConditionOperatorEnum == GroupFilterOperator.LastXDays)
						{
							if (!grp.EpisodeAddedDate.HasValue) return false;
							if (grp.EpisodeAddedDate.Value < filterDateEpisodeAdded) return false;
						}
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan)
						{
							if (!grp.EpisodeAddedDate.HasValue) return false;
							if (grp.EpisodeAddedDate.Value > filterDateEpisodeAdded) return false;
						}
						break;

					case GroupFilterConditionType.EpisodeCount:

						int epCount = -1;
						int.TryParse(gfc.ConditionParameter, out epCount);

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan && contractGroup.Stat_EpisodeCount < epCount) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan && contractGroup.Stat_EpisodeCount > epCount) return false;
						break;

					case GroupFilterConditionType.AniDBRating:

						decimal dRating = -1;
						decimal.TryParse(gfc.ConditionParameter, style, culture, out dRating);

						decimal thisRating = contractGroup.Stat_AniDBRating / (decimal)100;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan && thisRating < dRating) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan && thisRating > dRating) return false;
						break;

					case GroupFilterConditionType.UserRating:

						if (!contractGroup.Stat_UserVoteOverall.HasValue) return false;

						decimal dUserRating = -1;
						decimal.TryParse(gfc.ConditionParameter, style, culture, out dUserRating);

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.GreaterThan && contractGroup.Stat_UserVoteOverall.Value < dUserRating) return false;
						if (gfc.ConditionOperatorEnum == GroupFilterOperator.LessThan && contractGroup.Stat_UserVoteOverall.Value > dUserRating) return false;
						break;

					case GroupFilterConditionType.Category:

						string filterParm = gfc.ConditionParameter.Trim();

						string[] cats = filterParm.Split(',');
						bool foundCat = false;
						int index = 0;
						foreach (string cat in cats)
						{
							if (cat.Trim().Length == 0) continue;
							if (cat.Trim() == ",") continue;

							index = contractGroup.Stat_AllCategories.IndexOf(cat, 0, StringComparison.InvariantCultureIgnoreCase);
							if (index > -1)
							{
								foundCat = true;
								break;
							}
						}

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.In)
							if (!foundCat) return false;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn)
							if (foundCat) return false;
						break;

					case GroupFilterConditionType.AnimeType:

						filterParm = gfc.ConditionParameter.Trim();
						List<string> grpTypeList = grp.AnimeTypesList;

						string[] atypes = filterParm.Split(',');
						bool foundAnimeType = false;
						index = 0;
						foreach (string atype in atypes)
						{
							if (atype.Trim().Length == 0) continue;
							if (atype.Trim() == ",") continue;

							foreach (string thisAType in grpTypeList)
							{
								if (string.Equals(thisAType, atype, StringComparison.InvariantCultureIgnoreCase))
								{
									foundAnimeType = true;
									break;
								}
							}
						}

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.In)
							if (!foundAnimeType) return false;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn)
							if (foundAnimeType) return false;
						break;



					case GroupFilterConditionType.VideoQuality:

						filterParm = gfc.ConditionParameter.Trim();

						string[] vidQuals = filterParm.Split(',');
						bool foundVid = false;
						bool foundVidAllEps = false;
						index = 0;
						foreach (string vidq in vidQuals)
						{
							if (vidq.Trim().Length == 0) continue;
							if (vidq.Trim() == ",") continue;

							index = contractGroup.Stat_AllVideoQuality.IndexOf(vidq, 0, StringComparison.InvariantCultureIgnoreCase);
							if (index > -1) foundVid = true;

							index = contractGroup.Stat_AllVideoQuality_Episodes.IndexOf(vidq, 0, StringComparison.InvariantCultureIgnoreCase);
							if (index > -1) foundVidAllEps = true;

						}

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.In)
							if (!foundVid) return false;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn)
							if (foundVid) return false;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.InAllEpisodes)
							if (!foundVidAllEps) return false;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotInAllEpisodes)
							if (foundVidAllEps) return false;

						break;

					case GroupFilterConditionType.AudioLanguage:
					case GroupFilterConditionType.SubtitleLanguage:

						filterParm = gfc.ConditionParameter.Trim();

						string[] languages = filterParm.Split(',');
						bool foundLan = false;
						index = 0;
						foreach (string lanName in languages)
						{
							if (lanName.Trim().Length == 0) continue;
							if (lanName.Trim() == ",") continue;

							if (gfc.ConditionTypeEnum == GroupFilterConditionType.AudioLanguage)
								index = contractGroup.Stat_AudioLanguages.IndexOf(lanName, 0, StringComparison.InvariantCultureIgnoreCase);

							if (gfc.ConditionTypeEnum == GroupFilterConditionType.SubtitleLanguage)
								index = contractGroup.Stat_SubtitleLanguages.IndexOf(lanName, 0, StringComparison.InvariantCultureIgnoreCase);

							if (index > -1) foundLan = true;

						}

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.In)
							if (!foundLan) return false;

						if (gfc.ConditionOperatorEnum == GroupFilterOperator.NotIn)
							if (foundLan) return false;

						break;
				}
			}

			return true;
		}
Ejemplo n.º 11
0
        private System.IO.Stream GetGroupsFromFilter(int userid, string GroupFilterId)
        {
            KodiObject ret=new KodiObject(KodiHelper.NewMediaContainer("Filters",true));
            if (!ret.Init())
                return new MemoryStream();
            //List<Joint> retGroups = new List<Joint>();
            List<Video> retGroups=new List<Video>();
            try
            {
                int groupFilterID ;
                int.TryParse(GroupFilterId, out groupFilterID);
                using (var session = JMMService.SessionFactory.OpenSession())
                {
                    if (groupFilterID == -1)
                        return new MemoryStream();
                    DateTime start = DateTime.Now;
                    GroupFilterRepository repGF = new GroupFilterRepository();

                    GroupFilter gf;

                    if (groupFilterID == -999)
                    {
                        // all groups
                        gf = new GroupFilter();
                        gf.GroupFilterName = "All";
                    }
                    else
                    {
                        gf = repGF.GetByID(session, groupFilterID);
                        if (gf == null) return new MemoryStream();
                    }
                    ret.MediaContainer.Title2 = ret.MediaContainer.Title1 = gf.GroupFilterName;
                    //Contract_GroupFilterExtended contract = gf.ToContractExtended(user);

                    AnimeGroupRepository repGroups = new AnimeGroupRepository();
                    List<AnimeGroup> allGrps = repGroups.GetAll(session);

                    TimeSpan ts = DateTime.Now - start;
                    string msg = string.Format("Got groups for filter DB: {0} - {1} in {2} ms", gf.GroupFilterName,
                        allGrps.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    start = DateTime.Now;

                    if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(userid)) &&
                        (StatsCache.Instance.StatUserGroupFilter[userid].ContainsKey(gf.GroupFilterID)))
                    {
                        HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[userid][gf.GroupFilterID];
                        var tas = StatsCache.Instance.StatKodiGroupsCache;
                        foreach (AnimeGroup grp in allGrps)
                        {
                            if (groups.Contains(grp.AnimeGroupID))
                            {
                                try {
                                    //if (grp.GroupName == "Rockman.EXE")
                                    //{
                                    //    int x = grp.MissingEpisodeCount;
                                    //}
                                    Video v = StatsCache.Instance.StatKodiGroupsCache[userid][grp.AnimeGroupID];
                                    if (v != null)
                                    {
                                        //proper naming
                                        AniDB_Anime anim = grp.Anime[0];
                                        v.OriginalTitle = "";
                                        foreach (AniDB_Anime_Title title in anim.GetTitles())
                                        {
                                            if (title.TitleType == "official" || title.TitleType == "main")
                                            {
                                                v.OriginalTitle += "{" + title.TitleType + ":" + title.Language + "}" + title.Title + "|";
                                            }
                                        }
                                        v.OriginalTitle = v.OriginalTitle.Substring(0, v.OriginalTitle.Length - 1);
                                        //proper naming end

                                        retGroups.Add(v.Clone());
                                    }
                                }
                                catch(Exception e)
                                {
                                    int x = retGroups.Count;
                                }
                            }
                        }
                    }
                    ts = DateTime.Now - start;
                    msg = string.Format("Got groups for filter EVAL: {0} - {1} in {2} ms", gf.GroupFilterName,
                        retGroups.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    if ((groupFilterID == -999) || (gf.SortCriteriaList.Count == 0))
                    {
                        ret.Childrens = retGroups.OrderBy(a => a.Group.SortName).ToList();
                        return ret.GetStream();
                    }
                    List<Contract_AnimeGroup> grps = retGroups.Select(a => a.Group).ToList();
                    List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
                    foreach (GroupFilterSortingCriteria g in gf.SortCriteriaList)
                    {
                        sortCriteria.Add(GroupFilterHelper.GetSortDescription(g.SortType, g.SortDirection));
                    }
                    grps = Sorting.MultiSort(grps, sortCriteria);
                    List<Video> joints2 = new List<Video>();
                    foreach (Contract_AnimeGroup gr in grps)
                    {
                        foreach (Video j in retGroups)
                        {
                            if (j.Group == gr)
                            {
                                //experiment
                                AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository();
                                AnimeSeriesRepository repSeries = new AnimeSeriesRepository();
                                AnimeGroup ag = repGroups.GetByID(gr.AnimeGroupID);
                                List<AnimeSeries> sers = ag.GetAllSeries();
                                AnimeSeries ser = sers[0];
                                AniDB_Anime anim = ser.GetAnime();

                                j.CharactersList = new List<Characters>();
                                Characters c = new Characters();
                                c.CharactersList = GetCharactersFromAniDB(anim);
                                j.CharactersList.Add(c);
                                //experimentEND

                                //proper naming
                                j.OriginalTitle = "";
                                foreach (AniDB_Anime_Title title in anim.GetTitles())
                                {
                                    if (title.TitleType == "official" || title.TitleType == "main")
                                    {
                                        j.OriginalTitle += "{" + title.TitleType + ":" + title.Language + "}" + title.Title + "|";
                                    }
                                }
                                j.OriginalTitle = j.OriginalTitle.Substring(0, j.OriginalTitle.Length - 1);
                                //proper naming end

                                //community support

                                //CrossRef_AniDB_TraktV2Repository repCrossRef = new CrossRef_AniDB_TraktV2Repository();
                                //List<CrossRef_AniDB_TraktV2> Trakt = repCrossRef.GetByAnimeID(anim.AnimeID);
                                //if (Trakt != null)
                                //{
                                //    if (Trakt.Count > 0)
                                //    {
                                //        j.Trakt = Trakt[0].TraktID;
                                //    }
                                //}

                                //CrossRef_AniDB_TvDBV2Repository repCrossRefV2 = new CrossRef_AniDB_TvDBV2Repository();
                                //List<CrossRef_AniDB_TvDBV2> TvDB = repCrossRefV2.GetByAnimeID(anim.AnimeID);
                                //if (TvDB != null)
                                //{
                                //    if (TvDB.Count > 0)
                                //    {
                                //        j.TvDB = TvDB[0].TvDBID.ToString();
                                //    }
                                //}

                                //community support END

                                joints2.Add(j);
                                retGroups.Remove(j);
                                break;
                            }
                        }
                    }
                    ret.Childrens = joints2;
                    ret.MediaContainer.Art = KodiHelper.GetRandomFanartFromVideoList(ret.Childrens);
                    ts = DateTime.Now - start;
                    msg = string.Format("Got groups final: {0} - {1} in {2} ms", gf.GroupFilterName,
                        retGroups.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    return ret.GetStream();

                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
            }
            return new MemoryStream();
        }
Ejemplo n.º 12
0
        private System.IO.Stream GetGroupsFromFilter(int userid, string GroupFilterId, HistoryInfo info)
        {
            PlexObject ret=new PlexObject(PlexHelper.NewMediaContainer(MediaContainerTypes.Show,info,false));
            if (!ret.Init())
                return new MemoryStream();

            //List<Joint> retGroups = new List<Joint>();
            List<Video> retGroups=new List<Video>();
            try
            {
                int groupFilterID ;
                int.TryParse(GroupFilterId, out groupFilterID);
                using (var session = JMMService.SessionFactory.OpenSession())
                {
                    if (groupFilterID == -1)
                        return new MemoryStream();
                    DateTime start = DateTime.Now;
                    GroupFilterRepository repGF = new GroupFilterRepository();

                    GroupFilter gf;

                    if (groupFilterID == -999)
                    {
                        // all groups
                        gf = new GroupFilter();
                        gf.GroupFilterName = "All";
                    }
                    else
                    {
                        gf = repGF.GetByID(session, groupFilterID);
                        if (gf == null) return new MemoryStream();
                    }
                    //Contract_GroupFilterExtended contract = gf.ToContractExtended(user);

                    AnimeGroupRepository repGroups = new AnimeGroupRepository();
                    List<AnimeGroup> allGrps = repGroups.GetAll(session);

                    TimeSpan ts = DateTime.Now - start;
                    string msg = string.Format("Got groups for filter DB: {0} - {1} in {2} ms", gf.GroupFilterName,
                        allGrps.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    start = DateTime.Now;

                    if ((StatsCache.Instance.StatUserGroupFilter.ContainsKey(userid)) &&
                        (StatsCache.Instance.StatUserGroupFilter[userid].ContainsKey(gf.GroupFilterID)))
                    {
                        HashSet<int> groups = StatsCache.Instance.StatUserGroupFilter[userid][gf.GroupFilterID];

                        foreach (AnimeGroup grp in allGrps)
                        {
                            if (groups.Contains(grp.AnimeGroupID))
                            {
                                Video v = StatsCache.Instance.StatPlexGroupsCache[userid][grp.AnimeGroupID];
                                if (v != null)
                                {
                                    v = v.Clone();

                                    retGroups.Add(v, info);
                                }
                            }
                        }
                    }
                    ts = DateTime.Now - start;
                    msg = string.Format("Got groups for filter EVAL: {0} - {1} in {2} ms", gf.GroupFilterName,
                        retGroups.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    if ((groupFilterID == -999) || (gf.SortCriteriaList.Count == 0))
                    {
                        ret.Childrens = PlexHelper.ConvertToDirectoryIfNotUnique(retGroups.OrderBy(a => a.Group.SortName).ToList());
                        return ret.GetStream();
                    }
                    List<Contract_AnimeGroup> grps = retGroups.Select(a => a.Group).ToList();
                    List<SortPropOrFieldAndDirection> sortCriteria = new List<SortPropOrFieldAndDirection>();
                    foreach (GroupFilterSortingCriteria g in gf.SortCriteriaList)
                    {
                        sortCriteria.Add(GroupFilterHelper.GetSortDescription(g.SortType, g.SortDirection));
                    }
                    grps = Sorting.MultiSort(grps, sortCriteria);
                    List<Video> joints2 = new List<Video>();
                    foreach (Contract_AnimeGroup gr in grps)
                    {
                        foreach (Video j in retGroups)
                        {
                            if (j.Group == gr)
                            {
                                joints2.Add(j);
                                retGroups.Remove(j);
                                break;
                            }
                        }
                    }
                    ret.Childrens = PlexHelper.ConvertToDirectoryIfNotUnique(joints2);
                    ts = DateTime.Now - start;
                    msg = string.Format("Got groups final: {0} - {1} in {2} ms", gf.GroupFilterName,
                        retGroups.Count, ts.TotalMilliseconds);
                    logger.Info(msg);
                    return ret.GetStream();

                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
            }
            return new MemoryStream();
        }
Ejemplo n.º 13
0
        private static void CreateContinueWatchingGroupFilter()
        {
            // group filters
            GroupFilterRepository repFilters = new GroupFilterRepository();
            GroupFilterConditionRepository repGFC = new GroupFilterConditionRepository();

            using (var session = JMMService.SessionFactory.OpenSession())
            {
                // check if it already exists
                List<GroupFilter> lockedGFs = repFilters.GetLockedGroupFilters(session);

                if (lockedGFs != null)
                {
                    // if it already exists we can leave
                    foreach (GroupFilter gfTemp in lockedGFs)
                    {
                        if (gfTemp.FilterType == (int)GroupFilterType.ContinueWatching)
                            return;
                    }

                    // the default value when the column was added to the database was '1'
                    // this is only needed for users of a migrated database
                    foreach (GroupFilter gfTemp in lockedGFs)
                    {
                        if (gfTemp.GroupFilterName.Equals(Constants.GroupFilterName.ContinueWatching, StringComparison.InvariantCultureIgnoreCase) &&
                            gfTemp.FilterType != (int)GroupFilterType.ContinueWatching)
                        {
                            FixContinueWatchingGroupFilter_20160406();
                            return;
                        }
                    }
                }

                GroupFilter gf = new GroupFilter();
                gf.GroupFilterName = Constants.GroupFilterName.ContinueWatching;
                gf.Locked = 1;
                gf.SortingCriteria = "4;2"; // by last watched episode desc
                gf.ApplyToSeries = 0;
                gf.BaseCondition = 1; // all
                gf.FilterType = (int)GroupFilterType.ContinueWatching;

                repFilters.Save(gf);

                GroupFilterCondition gfc = new GroupFilterCondition();
                gfc.ConditionType = (int)GroupFilterConditionType.HasWatchedEpisodes;
                gfc.ConditionOperator = (int)GroupFilterOperator.Include;
                gfc.ConditionParameter = "";
                gfc.GroupFilterID = gf.GroupFilterID;
                repGFC.Save(gfc);

                gfc = new GroupFilterCondition();
                gfc.ConditionType = (int)GroupFilterConditionType.HasUnwatchedEpisodes;
                gfc.ConditionOperator = (int)GroupFilterOperator.Include;
                gfc.ConditionParameter = "";
                gfc.GroupFilterID = gf.GroupFilterID;
                repGFC.Save(gfc);
            }
        }