| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 | using MediaBrowser.Controller.Entities;using MediaBrowser.Controller.Entities.Audio;using MediaBrowser.Controller.Providers;using MediaBrowser.Model.Entities;using System;using System.Collections.Generic;namespace MediaBrowser.Providers.Manager{    public static class ProviderUtils    {        public static void MergeBaseItemData<T>(MetadataResult<T> sourceResult,            MetadataResult<T> targetResult,            List<MetadataFields> lockedFields,            bool replaceData,            bool mergeMetadataSettings)            where T : BaseItem        {            var source = sourceResult.Item;            var target = targetResult.Item;            if (source == null)            {                throw new ArgumentNullException("source");            }            if (target == null)            {                throw new ArgumentNullException("target");            }            if (!lockedFields.Contains(MetadataFields.Name))            {                if (replaceData || string.IsNullOrEmpty(target.Name))                {                    // Safeguard against incoming data having an emtpy name                    if (!string.IsNullOrWhiteSpace(source.Name))                    {                        target.Name = source.Name;                    }                }            }            if (replaceData || !target.CommunityRating.HasValue)            {                target.CommunityRating = source.CommunityRating;            }            if (replaceData || !target.EndDate.HasValue)            {                target.EndDate = source.EndDate;            }            if (!lockedFields.Contains(MetadataFields.Genres))            {                if (replaceData || target.Genres.Count == 0)                {                    target.Genres = source.Genres;                }            }            if (replaceData || string.IsNullOrEmpty(target.HomePageUrl))            {                target.HomePageUrl = source.HomePageUrl;                if (!string.IsNullOrWhiteSpace(target.HomePageUrl) && target.HomePageUrl.IndexOf("http", StringComparison.OrdinalIgnoreCase) != 0)                {                    target.HomePageUrl = "http://" + target.HomePageUrl;                }            }            if (replaceData || !target.IndexNumber.HasValue)            {                target.IndexNumber = source.IndexNumber;            }            if (!lockedFields.Contains(MetadataFields.OfficialRating))            {                if (replaceData || string.IsNullOrEmpty(target.OfficialRating))                {                    target.OfficialRating = source.OfficialRating;                }            }            if (replaceData || string.IsNullOrEmpty(target.OfficialRatingDescription))            {                target.OfficialRatingDescription = source.OfficialRatingDescription;            }            if (replaceData || string.IsNullOrEmpty(target.CustomRating))            {                target.CustomRating = source.CustomRating;            }            if (!lockedFields.Contains(MetadataFields.Overview))            {                if (replaceData || string.IsNullOrEmpty(target.Overview))                {                    target.Overview = source.Overview;                }            }            if (replaceData || !target.ParentIndexNumber.HasValue)            {                target.ParentIndexNumber = source.ParentIndexNumber;            }            if (!lockedFields.Contains(MetadataFields.Cast))            {                if (replaceData || targetResult.People == null || targetResult.People.Count == 0)                {                    targetResult.People = sourceResult.People;                }            }            if (replaceData || !target.PremiereDate.HasValue)            {                target.PremiereDate = source.PremiereDate;            }            if (replaceData || !target.ProductionYear.HasValue)            {                target.ProductionYear = source.ProductionYear;            }            if (!lockedFields.Contains(MetadataFields.Runtime))            {                if (replaceData || !target.RunTimeTicks.HasValue)                {                    if (!(target is Audio) && !(target is Video))                    {                        target.RunTimeTicks = source.RunTimeTicks;                    }                }            }            if (!lockedFields.Contains(MetadataFields.Studios))            {                if (replaceData || target.Studios.Count == 0)                {                    target.Studios = source.Studios;                }            }            if (!lockedFields.Contains(MetadataFields.Tags))            {                var sourceHasTags = source as IHasTags;                var targetHasTags = target as IHasTags;                if (sourceHasTags != null && targetHasTags != null)                {                    if (replaceData || targetHasTags.Tags.Count == 0)                    {                        targetHasTags.Tags = sourceHasTags.Tags;                    }                }            }            if (!lockedFields.Contains(MetadataFields.Keywords))            {                var sourceHasKeywords = source as IHasKeywords;                var targetHasKeywords = target as IHasKeywords;                if (sourceHasKeywords != null && targetHasKeywords != null)                {                    if (replaceData || targetHasKeywords.Keywords.Count == 0)                    {                        targetHasKeywords.Keywords = sourceHasKeywords.Keywords;                    }                }            }            if (!lockedFields.Contains(MetadataFields.ProductionLocations))            {                var sourceHasProductionLocations = source as IHasProductionLocations;                var targetHasProductionLocations = target as IHasProductionLocations;                if (sourceHasProductionLocations != null && targetHasProductionLocations != null)                {                    if (replaceData || targetHasProductionLocations.ProductionLocations.Count == 0)                    {                        targetHasProductionLocations.ProductionLocations = sourceHasProductionLocations.ProductionLocations;                    }                }            }            if (replaceData || !target.VoteCount.HasValue)            {                target.VoteCount = source.VoteCount;            }            foreach (var id in source.ProviderIds)            {                var key = id.Key;                // Don't replace existing Id's.                if (replaceData || !target.ProviderIds.ContainsKey(key))                {                    target.ProviderIds[key] = id.Value;                }            }            MergeAlbumArtist(source, target, lockedFields, replaceData);            MergeBudget(source, target, lockedFields, replaceData);            MergeMetascore(source, target, lockedFields, replaceData);            MergeCriticRating(source, target, lockedFields, replaceData);            MergeAwards(source, target, lockedFields, replaceData);            MergeTaglines(source, target, lockedFields, replaceData);            MergeTrailers(source, target, lockedFields, replaceData);            MergeShortOverview(source, target, lockedFields, replaceData);            if (mergeMetadataSettings)            {                MergeMetadataSettings(source, target);            }        }        public static void MergeMetadataSettings(BaseItem source,           BaseItem target)        {            target.ForcedSortName = source.ForcedSortName;            target.LockedFields = source.LockedFields;            target.IsLocked = source.IsLocked;            target.DisplayMediaType = source.DisplayMediaType;            // Grab the value if it's there, but if not then don't overwrite the default            if (source.DateCreated != default(DateTime))            {                target.DateCreated = source.DateCreated;            }            target.PreferredMetadataCountryCode = source.PreferredMetadataCountryCode;            target.PreferredMetadataLanguage = source.PreferredMetadataLanguage;            var sourceHasDisplayOrder = source as IHasDisplayOrder;            var targetHasDisplayOrder = target as IHasDisplayOrder;            if (sourceHasDisplayOrder != null && targetHasDisplayOrder != null)            {                targetHasDisplayOrder.DisplayOrder = sourceHasDisplayOrder.DisplayOrder;            }        }        private static void MergeShortOverview(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceHasShortOverview = source as IHasShortOverview;            var targetHasShortOverview = target as IHasShortOverview;            if (sourceHasShortOverview != null && targetHasShortOverview != null)            {                if (replaceData || string.IsNullOrEmpty(targetHasShortOverview.ShortOverview))                {                    targetHasShortOverview.ShortOverview = sourceHasShortOverview.ShortOverview;                }            }        }        private static void MergeAlbumArtist(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceHasAlbumArtist = source as IHasAlbumArtist;            var targetHasAlbumArtist = target as IHasAlbumArtist;            if (sourceHasAlbumArtist != null && targetHasAlbumArtist != null)            {                if (replaceData || targetHasAlbumArtist.AlbumArtists.Count == 0)                {                    targetHasAlbumArtist.AlbumArtists = sourceHasAlbumArtist.AlbumArtists;                }            }        }        private static void MergeBudget(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceHasBudget = source as IHasBudget;            var targetHasBudget = target as IHasBudget;            if (sourceHasBudget != null && targetHasBudget != null)            {                if (replaceData || !targetHasBudget.Budget.HasValue)                {                    targetHasBudget.Budget = sourceHasBudget.Budget;                }                if (replaceData || !targetHasBudget.Revenue.HasValue)                {                    targetHasBudget.Revenue = sourceHasBudget.Revenue;                }            }        }        private static void MergeMetascore(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceCast = source as IHasMetascore;            var targetCast = target as IHasMetascore;            if (sourceCast != null && targetCast != null)            {                if (replaceData || !targetCast.Metascore.HasValue)                {                    targetCast.Metascore = sourceCast.Metascore;                }            }        }        private static void MergeAwards(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceCast = source as IHasAwards;            var targetCast = target as IHasAwards;            if (sourceCast != null && targetCast != null)            {                if (replaceData || string.IsNullOrEmpty(targetCast.AwardSummary))                {                    targetCast.AwardSummary = sourceCast.AwardSummary;                }            }        }        private static void MergeCriticRating(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceCast = source as IHasCriticRating;            var targetCast = target as IHasCriticRating;            if (sourceCast != null && targetCast != null)            {                if (replaceData || !targetCast.CriticRating.HasValue)                {                    targetCast.CriticRating = sourceCast.CriticRating;                }                if (replaceData || string.IsNullOrEmpty(targetCast.CriticRatingSummary))                {                    targetCast.CriticRatingSummary = sourceCast.CriticRatingSummary;                }            }        }        private static void MergeTaglines(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceCast = source as IHasTaglines;            var targetCast = target as IHasTaglines;            if (sourceCast != null && targetCast != null)            {                if (replaceData || targetCast.Taglines.Count == 0)                {                    targetCast.Taglines = sourceCast.Taglines;                }            }        }        private static void MergeTrailers(BaseItem source, BaseItem target, List<MetadataFields> lockedFields, bool replaceData)        {            var sourceCast = source as IHasTrailers;            var targetCast = target as IHasTrailers;            if (sourceCast != null && targetCast != null)            {                if (replaceData || targetCast.RemoteTrailers.Count == 0)                {                    targetCast.RemoteTrailers = sourceCast.RemoteTrailers;                }            }        }    }}
 |