public static IEnumerable <MediaItem> ProcessMediaItems(IEnumerable <MediaItem> mediaItems)
        {
            UserProfile     userProfile               = null;
            bool            applyUserRestrictions     = false;
            IUserManagement userProfileDataManagement = ServiceRegistration.Get <IUserManagement>();

            if (userProfileDataManagement != null && userProfileDataManagement.IsValidUser)
            {
                userProfile           = userProfileDataManagement.CurrentUser;
                applyUserRestrictions = userProfileDataManagement.ApplyUserRestriction;
            }

            int  allowedAge = 5;
            bool includeParentalGuidedContent = false;
            bool includeUnratedContent        = false;
            bool allowAllAges = true;

            if (userProfile != null && applyUserRestrictions)
            {
                foreach (var key in userProfile.AdditionalData)
                {
                    foreach (var val in key.Value)
                    {
                        if (key.Key == UserDataKeysKnown.KEY_ALLOW_ALL_AGES)
                        {
                            string allow = val.Value;
                            if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                            {
                                allowAllAges = Convert.ToInt32(allow) > 0;
                            }
                        }
                        else if (key.Key == UserDataKeysKnown.KEY_ALLOWED_AGE)
                        {
                            string age = val.Value;
                            if (!string.IsNullOrEmpty(age) && Convert.ToInt32(age) >= 0)
                            {
                                allowedAge = Convert.ToInt32(age);
                            }
                        }
                        else if (key.Key == UserDataKeysKnown.KEY_INCLUDE_PARENT_GUIDED_CONTENT)
                        {
                            string allow = val.Value;
                            if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                            {
                                includeParentalGuidedContent = Convert.ToInt32(allow) > 0;
                            }
                        }
                        else if (key.Key == UserDataKeysKnown.KEY_INCLUDE_UNRATED_CONTENT)
                        {
                            string allow = val.Value;
                            if (!string.IsNullOrEmpty(allow) && Convert.ToInt32(allow) >= 0)
                            {
                                includeUnratedContent = Convert.ToInt32(allow) > 0;
                            }
                        }
                    }
                }
            }

            List <MediaItem> allowedMedia = new List <MediaItem>();

            foreach (var mediaItem in mediaItems)
            {
                string certification            = null;
                CertificationMapping bestMatch  = null;
                CertificationMapping mediaMatch = null;
                if (mediaItem.Aspects.ContainsKey(MovieAspect.ASPECT_ID))
                {
                    if (MediaItemAspect.TryGetAttribute(mediaItem.Aspects, MovieAspect.ATTR_CERTIFICATION, out certification))
                    {
                        if (applyUserRestrictions && !allowAllAges)
                        {
                            if (CertificationMapper.TryFindMovieCertification(certification, out mediaMatch))
                            {
                                if (!CertificationMapper.IsAgeAllowed(mediaMatch, allowedAge, includeParentalGuidedContent))
                                {
                                    continue;
                                }
                            }
                            if (certification == null && !includeUnratedContent)
                            {
                                continue;
                            }
                        }
                        if (certification != null && !string.IsNullOrEmpty(DisplayMovieCertificationCountry))
                        {
                            bestMatch = CertificationMapper.FindMatchingMovieCertification(DisplayMovieCertificationCountry, certification);
                        }
                    }
                }
                if (mediaItem.Aspects.ContainsKey(SeriesAspect.ASPECT_ID))
                {
                    if (MediaItemAspect.TryGetAttribute(mediaItem.Aspects, SeriesAspect.ATTR_CERTIFICATION, out certification))
                    {
                        if (applyUserRestrictions && !allowAllAges)
                        {
                            if (CertificationMapper.TryFindSeriesCertification(certification, out mediaMatch))
                            {
                                if (!CertificationMapper.IsAgeAllowed(mediaMatch, allowedAge, includeParentalGuidedContent))
                                {
                                    continue;
                                }
                            }
                            if (certification == null && !includeUnratedContent)
                            {
                                continue;
                            }
                        }
                        if (certification != null && !string.IsNullOrEmpty(DisplaySeriesCertificationCountry))
                        {
                            bestMatch = CertificationMapper.FindMatchingSeriesCertification(DisplaySeriesCertificationCountry, certification);
                        }
                    }
                }

                //Assign new certification value
                if (bestMatch != null)
                {
                    if (mediaItem.Aspects.ContainsKey(MovieAspect.ASPECT_ID))
                    {
                        MediaItemAspect.SetAttribute <string>(mediaItem.Aspects, MovieAspect.ATTR_CERTIFICATION, bestMatch.CertificationId);
                    }
                    else if (mediaItem.Aspects.ContainsKey(SeriesAspect.ASPECT_ID))
                    {
                        MediaItemAspect.SetAttribute <string>(mediaItem.Aspects, SeriesAspect.ATTR_CERTIFICATION, bestMatch.CertificationId);
                    }
                }
                else
                {
                    if (applyUserRestrictions && !allowAllAges && !includeUnratedContent)
                    {
                        continue;
                    }

                    if (mediaItem.Aspects.ContainsKey(MovieAspect.ASPECT_ID))
                    {
                        MediaItemAspect.SetAttribute <string>(mediaItem.Aspects, MovieAspect.ATTR_CERTIFICATION, null);
                    }
                    else if (mediaItem.Aspects.ContainsKey(SeriesAspect.ASPECT_ID))
                    {
                        MediaItemAspect.SetAttribute <string>(mediaItem.Aspects, SeriesAspect.ATTR_CERTIFICATION, null);
                    }
                }
                allowedMedia.Add(mediaItem);
            }

            return(allowedMedia);
        }
Exemple #2
0
        public override async Task <ICollection <FilterValue> > GetAvailableValuesAsync(IEnumerable <Guid> necessaryMIATypeIds, IFilter selectAttributeFilter, IFilter filter)
        {
            IContentDirectory cd = ServiceRegistration.Get <IServerConnectionManager>().ContentDirectory;

            if (cd == null)
            {
                throw new NotConnectedException("The MediaLibrary is not connected");
            }

            bool showVirtual = VirtualMediaHelper.ShowVirtualMedia(necessaryMIATypeIds);

            if (string.IsNullOrEmpty(CertificationHelper.DisplaySeriesCertificationCountry))
            {
                HomogenousMap valueGroups = await cd.GetValueGroupsAsync(SeriesAspect.ATTR_CERTIFICATION, null, ProjectionFunction.None,
                                                                         necessaryMIATypeIds, filter, true, showVirtual);

                IList <FilterValue> result = new List <FilterValue>(valueGroups.Count);
                int numEmptyEntries        = 0;
                foreach (KeyValuePair <object, object> group in valueGroups)
                {
                    string certification = (string)group.Key;
                    if (!string.IsNullOrEmpty(certification))
                    {
                        CertificationMapping cert;
                        if (CertificationMapper.TryFindSeriesCertification(certification, out cert))
                        {
                            result.Add(new FilterValue(cert.CertificationId, cert.Name,
                                                       new RelationalFilter(SeriesAspect.ATTR_CERTIFICATION, RelationalOperator.EQ, certification), null, (int)group.Value, this));
                        }
                    }
                    else
                    {
                        numEmptyEntries += (int)group.Value;
                    }
                }
                if (numEmptyEntries > 0)
                {
                    result.Insert(0, new FilterValue("UR", Consts.RES_VALUE_UNRATED_TITLE, new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION), null, numEmptyEntries, this));
                }
                return(result);
            }
            else
            {
                IList <FilterValue> result      = new List <FilterValue>();
                IFilter             emptyFilter = new EmptyFilter(SeriesAspect.ATTR_CERTIFICATION);
                int numEmptyItems = await cd.CountMediaItemsAsync(necessaryMIATypeIds, BooleanCombinationFilter.CombineFilters(BooleanOperator.And, filter, emptyFilter), true, showVirtual);

                if (numEmptyItems > 0)
                {
                    result.Add(new FilterValue("UR", Consts.RES_VALUE_UNRATED_TITLE, emptyFilter, null, numEmptyItems, this));
                }
                List <string> usedFilters = new List <string>();
                foreach (var cert in CertificationMapper.GetSeriesCertificationsForCountry(CertificationHelper.DisplaySeriesCertificationCountry))
                {
                    IEnumerable <CertificationMapping> certs = CertificationMapper.FindAllAllowedSeriesCertifications(cert.CertificationId);
                    if (certs.Count() > 0)
                    {
                        List <string> certList = new List <string>(certs.Select(c => c.CertificationId).Except(usedFilters));
                        usedFilters.AddRange(certList);
                        IFilter certFilter = new InFilter(SeriesAspect.ATTR_CERTIFICATION, certList);
                        int     numItems   = await cd.CountMediaItemsAsync(necessaryMIATypeIds, BooleanCombinationFilter.CombineFilters(BooleanOperator.And, filter, certFilter), true, showVirtual);

                        result.Add(new FilterValue(cert.CertificationId, cert.Name, certFilter, null, numItems, this));
                    }
                }
                return(result);
            }
        }