ReportBuilderBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Channels;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Entities;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace MediaBrowser.Api.Reports
  11. {
  12. /// <summary> A report builder base. </summary>
  13. public abstract class ReportBuilderBase
  14. {
  15. #region [Constructors]
  16. /// <summary>
  17. /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilderBase class. </summary>
  18. /// <param name="libraryManager"> Manager for library. </param>
  19. public ReportBuilderBase(ILibraryManager libraryManager)
  20. {
  21. _libraryManager = libraryManager;
  22. }
  23. #endregion
  24. #region [Protected Fields]
  25. /// <summary> Manager for library. </summary>
  26. protected readonly ILibraryManager _libraryManager; ///< Manager for library
  27. protected Func<bool, string> GetBoolString = s => s == true ? "x" : ""; ///< .
  28. #endregion
  29. #region [Protected Internal Methods]
  30. /// <summary> Gets the headers. </summary>
  31. /// <typeparam name="H"> Type of the header. </typeparam>
  32. /// <param name="request"> The request. </param>
  33. /// <returns> The headers. </returns>
  34. protected internal abstract List<ReportHeader> GetHeaders<H>(H request) where H : IReportsHeader;
  35. #endregion
  36. #region [Protected Methods]
  37. /// <summary> Gets active headers. </summary>
  38. /// <typeparam name="T"> Generic type parameter. </typeparam>
  39. /// <param name="options"> Options for controlling the operation. </param>
  40. /// <returns> The active headers. </returns>
  41. protected List<ReportHeader> GetActiveHeaders<T>(List<ReportOptions<T>> options, ReportDisplayType displayType)
  42. {
  43. List<ReportHeader> headers = new List<ReportHeader>();
  44. foreach (ReportOptions<T> option in options.Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType)))
  45. {
  46. headers.Add(option.Header);
  47. }
  48. return headers;
  49. }
  50. /// <summary> Gets audio stream. </summary>
  51. /// <param name="item"> The item. </param>
  52. /// <returns> The audio stream. </returns>
  53. protected string GetAudioStream(BaseItem item)
  54. {
  55. var stream = GetStream(item, MediaStreamType.Audio);
  56. if (stream != null)
  57. return stream.Codec.ToUpper() == "DCA" ? stream.Profile : stream.Codec.
  58. ToUpper();
  59. return string.Empty;
  60. }
  61. /// <summary> Gets an episode. </summary>
  62. /// <param name="item"> The item. </param>
  63. /// <returns> The episode. </returns>
  64. protected string GetEpisode(BaseItem item)
  65. {
  66. if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null)
  67. return "Season " + item.ParentIndexNumber;
  68. else
  69. return item.Name;
  70. }
  71. /// <summary> Gets a genre. </summary>
  72. /// <param name="name"> The name. </param>
  73. /// <returns> The genre. </returns>
  74. protected Genre GetGenre(string name)
  75. {
  76. if (string.IsNullOrEmpty(name))
  77. return null;
  78. return _libraryManager.GetGenre(name);
  79. }
  80. /// <summary> Gets genre identifier. </summary>
  81. /// <param name="name"> The name. </param>
  82. /// <returns> The genre identifier. </returns>
  83. protected string GetGenreID(string name)
  84. {
  85. if (string.IsNullOrEmpty(name))
  86. return string.Empty;
  87. return string.Format("{0:N}",
  88. GetGenre(name).Id);
  89. }
  90. /// <summary> Gets the headers. </summary>
  91. /// <typeparam name="T"> Generic type parameter. </typeparam>
  92. /// <param name="options"> Options for controlling the operation. </param>
  93. /// <returns> The headers. </returns>
  94. protected List<ReportHeader> GetHeaders<T>(List<ReportOptions<T>> options)
  95. {
  96. List<ReportHeader> headers = new List<ReportHeader>();
  97. foreach (ReportOptions<T> option in options)
  98. {
  99. headers.Add(option.Header);
  100. }
  101. return headers;
  102. }
  103. /// <summary> Gets the headers. </summary>
  104. /// <typeparam name="T"> Generic type parameter. </typeparam>
  105. /// <param name="request"> The request. </param>
  106. /// <param name="getHeadersMetadata"> The get headers metadata. </param>
  107. /// <param name="getOptions"> Options for controlling the get. </param>
  108. /// <returns> The headers. </returns>
  109. protected List<ReportHeader> GetHeaders<T>(IReportsHeader request, Func<List<HeaderMetadata>> getHeadersMetadata, Func<HeaderMetadata, ReportOptions<T>> getOptions)
  110. {
  111. List<ReportOptions<T>> options = this.GetReportOptions(request, getHeadersMetadata, getOptions);
  112. return this.GetHeaders(options);
  113. }
  114. /// <summary> Gets list as string. </summary>
  115. /// <param name="items"> The items. </param>
  116. /// <returns> The list as string. </returns>
  117. protected string GetListAsString(List<string> items)
  118. {
  119. return String.Join("; ", items);
  120. }
  121. /// <summary> Gets localized header. </summary>
  122. /// <param name="internalHeader"> The internal header. </param>
  123. /// <returns> The localized header. </returns>
  124. protected static string GetLocalizedHeader(HeaderMetadata internalHeader)
  125. {
  126. if (internalHeader == HeaderMetadata.EpisodeNumber)
  127. {
  128. return "Episode";
  129. }
  130. string headerName = "";
  131. if (internalHeader != HeaderMetadata.None)
  132. {
  133. string localHeader = "Header" + internalHeader.ToString();
  134. headerName = ReportHelper.GetCoreLocalizedString(localHeader);
  135. }
  136. return headerName;
  137. }
  138. /// <summary> Gets media source information. </summary>
  139. /// <param name="item"> The item. </param>
  140. /// <returns> The media source information. </returns>
  141. protected MediaSourceInfo GetMediaSourceInfo(BaseItem item)
  142. {
  143. var mediaSource = item as IHasMediaSources;
  144. if (mediaSource != null)
  145. return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default);
  146. return null;
  147. }
  148. /// <summary> Gets an object. </summary>
  149. /// <typeparam name="T"> Generic type parameter. </typeparam>
  150. /// <typeparam name="R"> Type of the r. </typeparam>
  151. /// <param name="item"> The item. </param>
  152. /// <param name="function"> The function. </param>
  153. /// <param name="defaultValue"> The default value. </param>
  154. /// <returns> The object. </returns>
  155. protected R GetObject<T, R>(BaseItem item, Func<T, R> function, R defaultValue = default(R)) where T : class
  156. {
  157. var value = item as T;
  158. if (value != null && function != null)
  159. return function(value);
  160. else
  161. return defaultValue;
  162. }
  163. /// <summary> Gets a person. </summary>
  164. /// <param name="name"> The name. </param>
  165. /// <returns> The person. </returns>
  166. protected Person GetPerson(string name)
  167. {
  168. if (string.IsNullOrEmpty(name))
  169. return null;
  170. return _libraryManager.GetPerson(name);
  171. }
  172. /// <summary> Gets person identifier. </summary>
  173. /// <param name="name"> The name. </param>
  174. /// <returns> The person identifier. </returns>
  175. protected string GetPersonID(string name)
  176. {
  177. if (string.IsNullOrEmpty(name))
  178. return string.Empty;
  179. return string.Format("{0:N}",
  180. GetPerson(name).Id);
  181. }
  182. /// <summary> Gets report options. </summary>
  183. /// <typeparam name="T"> Generic type parameter. </typeparam>
  184. /// <param name="request"> The request. </param>
  185. /// <param name="getHeadersMetadata"> The get headers metadata. </param>
  186. /// <param name="getOptions"> Options for controlling the get. </param>
  187. /// <returns> The report options. </returns>
  188. protected List<ReportOptions<T>> GetReportOptions<T>(IReportsHeader request, Func<List<HeaderMetadata>> getHeadersMetadata, Func<HeaderMetadata, ReportOptions<T>> getOptions)
  189. {
  190. List<HeaderMetadata> headersMetadata = getHeadersMetadata();
  191. List<ReportOptions<T>> options = new List<ReportOptions<T>>();
  192. ReportDisplayType displayType = ReportHelper.GetReportDisplayType(request.DisplayType);
  193. foreach (HeaderMetadata header in headersMetadata)
  194. {
  195. ReportOptions<T> headerOptions = getOptions(header);
  196. if (this.DisplayTypeVisible(headerOptions.Header.DisplayType, displayType))
  197. options.Add(headerOptions);
  198. }
  199. if (request != null && !string.IsNullOrEmpty(request.ReportColumns))
  200. {
  201. List<HeaderMetadata> headersMetadataFiltered = ReportHelper.GetFilteredReportHeaderMetadata(request.ReportColumns, () => headersMetadata);
  202. foreach (ReportHeader header in options.Select(x => x.Header))
  203. {
  204. if (this.DisplayTypeVisible(header.DisplayType, displayType))
  205. {
  206. if (!headersMetadataFiltered.Contains(header.FieldName) && displayType != ReportDisplayType.Export)
  207. {
  208. header.DisplayType = ReportDisplayType.None;
  209. }
  210. }
  211. else
  212. header.DisplayType = ReportDisplayType.None;
  213. }
  214. }
  215. return options;
  216. }
  217. /// <summary> Gets runtime date time. </summary>
  218. /// <param name="runtime"> The runtime. </param>
  219. /// <returns> The runtime date time. </returns>
  220. protected double? GetRuntimeDateTime(long? runtime)
  221. {
  222. if (runtime.HasValue)
  223. return Math.Ceiling(new TimeSpan(runtime.Value).TotalMinutes);
  224. return null;
  225. }
  226. /// <summary> Gets series production year. </summary>
  227. /// <param name="item"> The item. </param>
  228. /// <returns> The series production year. </returns>
  229. protected string GetSeriesProductionYear(BaseItem item)
  230. {
  231. string productionYear = item.ProductionYear.ToString();
  232. var series = item as Series;
  233. if (series == null)
  234. {
  235. if (item.ProductionYear == null || item.ProductionYear == 0)
  236. return string.Empty;
  237. return productionYear;
  238. }
  239. if (series.Status == SeriesStatus.Continuing)
  240. return productionYear += "-Present";
  241. if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear)
  242. return productionYear += "-" + series.EndDate.Value.Year;
  243. return productionYear;
  244. }
  245. /// <summary> Gets a stream. </summary>
  246. /// <param name="item"> The item. </param>
  247. /// <param name="streamType"> Type of the stream. </param>
  248. /// <returns> The stream. </returns>
  249. protected MediaStream GetStream(BaseItem item, MediaStreamType streamType)
  250. {
  251. var itemInfo = GetMediaSourceInfo(item);
  252. if (itemInfo != null)
  253. return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType);
  254. return null;
  255. }
  256. /// <summary> Gets a studio. </summary>
  257. /// <param name="name"> The name. </param>
  258. /// <returns> The studio. </returns>
  259. protected Studio GetStudio(string name)
  260. {
  261. if (string.IsNullOrEmpty(name))
  262. return null;
  263. return _libraryManager.GetStudio(name);
  264. }
  265. /// <summary> Gets studio identifier. </summary>
  266. /// <param name="name"> The name. </param>
  267. /// <returns> The studio identifier. </returns>
  268. protected string GetStudioID(string name)
  269. {
  270. if (string.IsNullOrEmpty(name))
  271. return string.Empty;
  272. return string.Format("{0:N}",
  273. GetStudio(name).Id);
  274. }
  275. /// <summary> Gets video resolution. </summary>
  276. /// <param name="item"> The item. </param>
  277. /// <returns> The video resolution. </returns>
  278. protected string GetVideoResolution(BaseItem item)
  279. {
  280. var stream = GetStream(item,
  281. MediaStreamType.Video);
  282. if (stream != null && stream.Width != null)
  283. return string.Format("{0} * {1}",
  284. stream.Width,
  285. stream.Height != null ? stream.Height.ToString() : "-");
  286. return string.Empty;
  287. }
  288. /// <summary> Gets video stream. </summary>
  289. /// <param name="item"> The item. </param>
  290. /// <returns> The video stream. </returns>
  291. protected string GetVideoStream(BaseItem item)
  292. {
  293. var stream = GetStream(item, MediaStreamType.Video);
  294. if (stream != null)
  295. return stream.Codec.ToUpper();
  296. return string.Empty;
  297. }
  298. /// <summary> Displays a type visible. </summary>
  299. /// <param name="headerDisplayType"> Type of the header display. </param>
  300. /// <param name="displayType"> Type of the display. </param>
  301. /// <returns> true if it succeeds, false if it fails. </returns>
  302. protected bool DisplayTypeVisible(ReportDisplayType headerDisplayType, ReportDisplayType displayType)
  303. {
  304. if (headerDisplayType == ReportDisplayType.None)
  305. return false;
  306. bool rval = headerDisplayType == displayType || headerDisplayType == ReportDisplayType.ScreenExport && (displayType == ReportDisplayType.Screen || displayType == ReportDisplayType.Export);
  307. return rval;
  308. }
  309. #endregion
  310. }
  311. }