using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MediaBrowser.Api.Reports
{
/// 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, ReportDisplayType displayType)
{
List headers = new List();
foreach (ReportOptions option in options.Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType)))
{
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>();
ReportDisplayType displayType = ReportHelper.GetReportDisplayType(request.DisplayType);
foreach (HeaderMetadata header in headersMetadata)
{
ReportOptions headerOptions = getOptions(header);
if (this.DisplayTypeVisible(headerOptions.Header.DisplayType, displayType))
options.Add(headerOptions);
}
if (request != null && !string.IsNullOrEmpty(request.ReportColumns))
{
List headersMetadataFiltered = ReportHelper.GetFilteredReportHeaderMetadata(request.ReportColumns, () => headersMetadata);
foreach (ReportHeader header in options.Select(x => x.Header))
{
if (this.DisplayTypeVisible(header.DisplayType, displayType))
{
if (!headersMetadataFiltered.Contains(header.FieldName) && displayType != ReportDisplayType.Export)
{
header.DisplayType = ReportDisplayType.None;
}
}
else
header.DisplayType = ReportDisplayType.None;
}
}
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;
}
/// Displays a type visible.
/// Type of the header display.
/// Type of the display.
/// true if it succeeds, false if it fails.
protected bool DisplayTypeVisible(ReportDisplayType headerDisplayType, ReportDisplayType displayType)
{
if (headerDisplayType == ReportDisplayType.None)
return false;
bool rval = headerDisplayType == displayType || headerDisplayType == ReportDisplayType.ScreenExport && (displayType == ReportDisplayType.Screen || displayType == ReportDisplayType.Export);
return rval;
}
#endregion
}
}