From 1e07dbec63bced51857e67b00941b91ca86a7f77 Mon Sep 17 00:00:00 2001 From: Tavares André Date: Thu, 11 Jun 2015 20:25:12 +0200 Subject: Reports - Add Users activities --- .../Reports/Common/ReportBuilderBase.cs | 537 +++++++++++++-------- 1 file changed, 325 insertions(+), 212 deletions(-) (limited to 'MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs') diff --git a/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs b/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs index af6dc997c..939bf280a 100644 --- a/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs +++ b/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs @@ -13,217 +13,330 @@ using System.Threading.Tasks; namespace MediaBrowser.Api.Reports { - /// A report builder base. - public class ReportBuilderBase - { - /// - /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilderBase class. - /// Manager for library. - public ReportBuilderBase(ILibraryManager libraryManager) - { - _libraryManager = libraryManager; - } - - /// Manager for library. - protected readonly ILibraryManager _libraryManager; - - /// Gets audio stream. - /// The item. - /// The audio stream. - protected string GetAudioStream(BaseItem item) - { - var stream = GetStream(item, MediaStreamType.Audio); - if (stream != null) - return stream.Codec.ToUpper() == "DCA" ? stream.Profile : stream.Codec. - ToUpper(); - - return string.Empty; - } - - /// Gets an episode. - /// The item. - /// The episode. - protected string GetEpisode(BaseItem item) - { - - if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null) - return "Season " + item.ParentIndexNumber; - else - return item.Name; - } - - /// Gets a genre. - /// The name. - /// The genre. - protected Genre GetGenre(string name) - { - if (string.IsNullOrEmpty(name)) - return null; - return _libraryManager.GetGenre(name); - } - - /// Gets genre identifier. - /// The name. - /// The genre identifier. - protected string GetGenreID(string name) - { - if (string.IsNullOrEmpty(name)) - return string.Empty; - return string.Format("{0:N}", - GetGenre(name).Id); - } - - /// Gets list as string. - /// The items. - /// The list as string. - protected string GetListAsString(List items) - { - return String.Join("; ", items); - } - - /// Gets media source information. - /// The item. - /// The media source information. - protected MediaSourceInfo GetMediaSourceInfo(BaseItem item) - { - var mediaSource = item as IHasMediaSources; - if (mediaSource != null) - return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default); - - return null; - } - - /// Gets an object. - /// Generic type parameter. - /// Type of the r. - /// The item. - /// The function. - /// The default value. - /// The object. - protected R GetObject(BaseItem item, Func function, R defaultValue = default(R)) where T : class - { - var value = item as T; - if (value != null && function != null) - return function(value); - else - return defaultValue; - } - - /// Gets a person. - /// The name. - /// The person. - protected Person GetPerson(string name) - { - if (string.IsNullOrEmpty(name)) - return null; - return _libraryManager.GetPerson(name); - } - - /// Gets person identifier. - /// The name. - /// The person identifier. - protected string GetPersonID(string name) - { - if (string.IsNullOrEmpty(name)) - return string.Empty; - return string.Format("{0:N}", - GetPerson(name).Id); - } - - /// Gets runtime date time. - /// The runtime. - /// The runtime date time. - protected double? GetRuntimeDateTime(long? runtime) - { - if (runtime.HasValue) + /// A report builder base. + public abstract class ReportBuilderBase + { + + #region [Constructors] + + /// + /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilderBase class. + /// Manager for library. + public ReportBuilderBase(ILibraryManager libraryManager) + { + _libraryManager = libraryManager; + } + + #endregion + + #region [Protected Fields] + + /// Manager for library. + protected readonly ILibraryManager _libraryManager; ///< Manager for library + + protected Func GetBoolString = s => s == true ? "x" : ""; ///< . + + #endregion + + #region [Protected Internal Methods] + + /// Gets the headers. + /// Type of the header. + /// The request. + /// The headers. + protected internal abstract List GetHeaders(H request) where H : IReportsHeader; + + #endregion + + #region [Protected Methods] + + /// Gets active headers. + /// Generic type parameter. + /// Options for controlling the operation. + /// The active headers. + protected List GetActiveHeaders(List> options) + { + List headers = new List(); + foreach (ReportOptions option in options.Where(x => x.Header.Visible == true)) + { + headers.Add(option.Header); + } + + return headers; + } + + /// Gets audio stream. + /// The item. + /// The audio stream. + protected string GetAudioStream(BaseItem item) + { + var stream = GetStream(item, MediaStreamType.Audio); + if (stream != null) + return stream.Codec.ToUpper() == "DCA" ? stream.Profile : stream.Codec. + ToUpper(); + + return string.Empty; + } + + /// Gets an episode. + /// The item. + /// The episode. + protected string GetEpisode(BaseItem item) + { + + if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null) + return "Season " + item.ParentIndexNumber; + else + return item.Name; + } + + /// Gets a genre. + /// The name. + /// The genre. + protected Genre GetGenre(string name) + { + if (string.IsNullOrEmpty(name)) + return null; + return _libraryManager.GetGenre(name); + } + + /// Gets genre identifier. + /// The name. + /// The genre identifier. + protected string GetGenreID(string name) + { + if (string.IsNullOrEmpty(name)) + return string.Empty; + return string.Format("{0:N}", + GetGenre(name).Id); + } + + /// Gets the headers. + /// Generic type parameter. + /// Options for controlling the operation. + /// The headers. + protected List GetHeaders(List> options) + { + List headers = new List(); + foreach (ReportOptions option in options) + { + headers.Add(option.Header); + } + + return headers; + } + + /// Gets the headers. + /// Generic type parameter. + /// The request. + /// The get headers metadata. + /// Options for controlling the get. + /// The headers. + protected List GetHeaders(IReportsHeader request, Func> getHeadersMetadata, Func> getOptions) + { + List> options = this.GetReportOptions(request, getHeadersMetadata, getOptions); + return this.GetHeaders(options); + } + + /// Gets list as string. + /// The items. + /// The list as string. + protected string GetListAsString(List items) + { + return String.Join("; ", items); + } + + /// Gets localized header. + /// The internal header. + /// The localized header. + protected static string GetLocalizedHeader(HeaderMetadata internalHeader) + { + string headerName = ""; + if (internalHeader != HeaderMetadata.None) + { + string localHeader = "Header" + internalHeader.ToString(); + headerName = internalHeader != HeaderMetadata.None ? ReportHelper.GetJavaScriptLocalizedString(localHeader) : ""; + if (string.Compare(localHeader, headerName, StringComparison.CurrentCultureIgnoreCase) == 0) + headerName = ReportHelper.GetServerLocalizedString(localHeader); + } + return headerName; + } + + /// Gets media source information. + /// The item. + /// The media source information. + protected MediaSourceInfo GetMediaSourceInfo(BaseItem item) + { + var mediaSource = item as IHasMediaSources; + if (mediaSource != null) + return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default); + + return null; + } + + /// Gets an object. + /// Generic type parameter. + /// Type of the r. + /// The item. + /// The function. + /// The default value. + /// The object. + protected R GetObject(BaseItem item, Func function, R defaultValue = default(R)) where T : class + { + var value = item as T; + if (value != null && function != null) + return function(value); + else + return defaultValue; + } + + /// Gets a person. + /// The name. + /// The person. + protected Person GetPerson(string name) + { + if (string.IsNullOrEmpty(name)) + return null; + return _libraryManager.GetPerson(name); + } + + /// Gets person identifier. + /// The name. + /// The person identifier. + protected string GetPersonID(string name) + { + if (string.IsNullOrEmpty(name)) + return string.Empty; + return string.Format("{0:N}", + GetPerson(name).Id); + } + + /// Gets report options. + /// Generic type parameter. + /// The request. + /// The get headers metadata. + /// Options for controlling the get. + /// The report options. + protected List> GetReportOptions(IReportsHeader request, Func> getHeadersMetadata, Func> getOptions) + { + List headersMetadata = getHeadersMetadata(); + List> options = new List>(); + foreach (HeaderMetadata header in headersMetadata) + { + options.Add(getOptions(header)); + } + + if (request != null && !string.IsNullOrEmpty(request.ReportColumns)) + { + List headersMetadataFiltered = ReportHelper.GetFilteredReportHeaderMetadata(request.ReportColumns, () => headersMetadata); + foreach (ReportHeader header in options.Select(x => x.Header)) + { + if (!headersMetadataFiltered.Contains(header.FieldName)) + { + header.Visible = false; + } + } + } + + return options; + } + + /// Gets runtime date time. + /// The runtime. + /// The runtime date time. + protected double? GetRuntimeDateTime(long? runtime) + { + if (runtime.HasValue) return Math.Ceiling(new TimeSpan(runtime.Value).TotalMinutes); - return null; - } - - /// Gets series production year. - /// The item. - /// The series production year. - protected string GetSeriesProductionYear(BaseItem item) - { - - string productionYear = item.ProductionYear.ToString(); - var series = item as Series; - if (series == null) - { - if (item.ProductionYear == null || item.ProductionYear == 0) - return string.Empty; - return productionYear; - } - - if (series.Status == SeriesStatus.Continuing) - return productionYear += "-Present"; - - if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear) - return productionYear += "-" + series.EndDate.Value.Year; - - return productionYear; - } - - /// Gets a stream. - /// The item. - /// Type of the stream. - /// The stream. - protected MediaStream GetStream(BaseItem item, MediaStreamType streamType) - { - var itemInfo = GetMediaSourceInfo(item); - if (itemInfo != null) - return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType); - - return null; - } - - /// Gets a studio. - /// The name. - /// The studio. - protected Studio GetStudio(string name) - { - if (string.IsNullOrEmpty(name)) - return null; - return _libraryManager.GetStudio(name); - } - - /// Gets studio identifier. - /// The name. - /// The studio identifier. - protected string GetStudioID(string name) - { - if (string.IsNullOrEmpty(name)) - return string.Empty; - return string.Format("{0:N}", - GetStudio(name).Id); - } - - /// Gets video resolution. - /// The item. - /// The video resolution. - protected string GetVideoResolution(BaseItem item) - { - var stream = GetStream(item, - MediaStreamType.Video); - if (stream != null && stream.Width != null) - return string.Format("{0} * {1}", - stream.Width, - (stream.Height != null ? stream.Height.ToString() : "-")); - - return string.Empty; - } - - /// Gets video stream. - /// The item. - /// The video stream. - protected string GetVideoStream(BaseItem item) - { - var stream = GetStream(item, MediaStreamType.Video); - if (stream != null) - return stream.Codec.ToUpper(); - - return string.Empty; - } - - } + return null; + } + + /// Gets series production year. + /// The item. + /// The series production year. + protected string GetSeriesProductionYear(BaseItem item) + { + + string productionYear = item.ProductionYear.ToString(); + var series = item as Series; + if (series == null) + { + if (item.ProductionYear == null || item.ProductionYear == 0) + return string.Empty; + return productionYear; + } + + if (series.Status == SeriesStatus.Continuing) + return productionYear += "-Present"; + + if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear) + return productionYear += "-" + series.EndDate.Value.Year; + + return productionYear; + } + + /// Gets a stream. + /// The item. + /// Type of the stream. + /// The stream. + protected MediaStream GetStream(BaseItem item, MediaStreamType streamType) + { + var itemInfo = GetMediaSourceInfo(item); + if (itemInfo != null) + return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType); + + return null; + } + + /// Gets a studio. + /// The name. + /// The studio. + protected Studio GetStudio(string name) + { + if (string.IsNullOrEmpty(name)) + return null; + return _libraryManager.GetStudio(name); + } + + /// Gets studio identifier. + /// The name. + /// The studio identifier. + protected string GetStudioID(string name) + { + if (string.IsNullOrEmpty(name)) + return string.Empty; + return string.Format("{0:N}", + GetStudio(name).Id); + } + + /// Gets video resolution. + /// The item. + /// The video resolution. + protected string GetVideoResolution(BaseItem item) + { + var stream = GetStream(item, + MediaStreamType.Video); + if (stream != null && stream.Width != null) + return string.Format("{0} * {1}", + stream.Width, + (stream.Height != null ? stream.Height.ToString() : "-")); + + return string.Empty; + } + + /// Gets video stream. + /// The item. + /// The video stream. + protected string GetVideoStream(BaseItem item) + { + var stream = GetStream(item, MediaStreamType.Video); + if (stream != null) + return stream.Codec.ToUpper(); + + return string.Empty; + } + + #endregion + + } } -- cgit v1.2.3