ReportBuilderBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. string headerName = "";
  127. if (internalHeader != HeaderMetadata.None)
  128. {
  129. string localHeader = "Header" + internalHeader.ToString();
  130. headerName = ReportHelper.GetCoreLocalizedString(localHeader);
  131. }
  132. return headerName;
  133. }
  134. /// <summary> Gets media source information. </summary>
  135. /// <param name="item"> The item. </param>
  136. /// <returns> The media source information. </returns>
  137. protected MediaSourceInfo GetMediaSourceInfo(BaseItem item)
  138. {
  139. var mediaSource = item as IHasMediaSources;
  140. if (mediaSource != null)
  141. return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default);
  142. return null;
  143. }
  144. /// <summary> Gets an object. </summary>
  145. /// <typeparam name="T"> Generic type parameter. </typeparam>
  146. /// <typeparam name="R"> Type of the r. </typeparam>
  147. /// <param name="item"> The item. </param>
  148. /// <param name="function"> The function. </param>
  149. /// <param name="defaultValue"> The default value. </param>
  150. /// <returns> The object. </returns>
  151. protected R GetObject<T, R>(BaseItem item, Func<T, R> function, R defaultValue = default(R)) where T : class
  152. {
  153. var value = item as T;
  154. if (value != null && function != null)
  155. return function(value);
  156. else
  157. return defaultValue;
  158. }
  159. /// <summary> Gets a person. </summary>
  160. /// <param name="name"> The name. </param>
  161. /// <returns> The person. </returns>
  162. protected Person GetPerson(string name)
  163. {
  164. if (string.IsNullOrEmpty(name))
  165. return null;
  166. return _libraryManager.GetPerson(name);
  167. }
  168. /// <summary> Gets person identifier. </summary>
  169. /// <param name="name"> The name. </param>
  170. /// <returns> The person identifier. </returns>
  171. protected string GetPersonID(string name)
  172. {
  173. if (string.IsNullOrEmpty(name))
  174. return string.Empty;
  175. return string.Format("{0:N}",
  176. GetPerson(name).Id);
  177. }
  178. /// <summary> Gets report options. </summary>
  179. /// <typeparam name="T"> Generic type parameter. </typeparam>
  180. /// <param name="request"> The request. </param>
  181. /// <param name="getHeadersMetadata"> The get headers metadata. </param>
  182. /// <param name="getOptions"> Options for controlling the get. </param>
  183. /// <returns> The report options. </returns>
  184. protected List<ReportOptions<T>> GetReportOptions<T>(IReportsHeader request, Func<List<HeaderMetadata>> getHeadersMetadata, Func<HeaderMetadata, ReportOptions<T>> getOptions)
  185. {
  186. List<HeaderMetadata> headersMetadata = getHeadersMetadata();
  187. List<ReportOptions<T>> options = new List<ReportOptions<T>>();
  188. ReportDisplayType displayType = ReportHelper.GetReportDisplayType(request.DisplayType);
  189. foreach (HeaderMetadata header in headersMetadata)
  190. {
  191. ReportOptions<T> headerOptions = getOptions(header);
  192. if (this.DisplayTypeVisible(headerOptions.Header.DisplayType, displayType))
  193. options.Add(headerOptions);
  194. }
  195. if (request != null && !string.IsNullOrEmpty(request.ReportColumns))
  196. {
  197. List<HeaderMetadata> headersMetadataFiltered = ReportHelper.GetFilteredReportHeaderMetadata(request.ReportColumns, () => headersMetadata);
  198. foreach (ReportHeader header in options.Select(x => x.Header))
  199. {
  200. if (this.DisplayTypeVisible(header.DisplayType, displayType))
  201. {
  202. if (!headersMetadataFiltered.Contains(header.FieldName) && displayType != ReportDisplayType.Export)
  203. {
  204. header.DisplayType = ReportDisplayType.None;
  205. }
  206. }
  207. else
  208. header.DisplayType = ReportDisplayType.None;
  209. }
  210. }
  211. return options;
  212. }
  213. /// <summary> Gets runtime date time. </summary>
  214. /// <param name="runtime"> The runtime. </param>
  215. /// <returns> The runtime date time. </returns>
  216. protected double? GetRuntimeDateTime(long? runtime)
  217. {
  218. if (runtime.HasValue)
  219. return Math.Ceiling(new TimeSpan(runtime.Value).TotalMinutes);
  220. return null;
  221. }
  222. /// <summary> Gets series production year. </summary>
  223. /// <param name="item"> The item. </param>
  224. /// <returns> The series production year. </returns>
  225. protected string GetSeriesProductionYear(BaseItem item)
  226. {
  227. string productionYear = item.ProductionYear.ToString();
  228. var series = item as Series;
  229. if (series == null)
  230. {
  231. if (item.ProductionYear == null || item.ProductionYear == 0)
  232. return string.Empty;
  233. return productionYear;
  234. }
  235. if (series.Status == SeriesStatus.Continuing)
  236. return productionYear += "-Present";
  237. if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear)
  238. return productionYear += "-" + series.EndDate.Value.Year;
  239. return productionYear;
  240. }
  241. /// <summary> Gets a stream. </summary>
  242. /// <param name="item"> The item. </param>
  243. /// <param name="streamType"> Type of the stream. </param>
  244. /// <returns> The stream. </returns>
  245. protected MediaStream GetStream(BaseItem item, MediaStreamType streamType)
  246. {
  247. var itemInfo = GetMediaSourceInfo(item);
  248. if (itemInfo != null)
  249. return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType);
  250. return null;
  251. }
  252. /// <summary> Gets a studio. </summary>
  253. /// <param name="name"> The name. </param>
  254. /// <returns> The studio. </returns>
  255. protected Studio GetStudio(string name)
  256. {
  257. if (string.IsNullOrEmpty(name))
  258. return null;
  259. return _libraryManager.GetStudio(name);
  260. }
  261. /// <summary> Gets studio identifier. </summary>
  262. /// <param name="name"> The name. </param>
  263. /// <returns> The studio identifier. </returns>
  264. protected string GetStudioID(string name)
  265. {
  266. if (string.IsNullOrEmpty(name))
  267. return string.Empty;
  268. return string.Format("{0:N}",
  269. GetStudio(name).Id);
  270. }
  271. /// <summary> Gets video resolution. </summary>
  272. /// <param name="item"> The item. </param>
  273. /// <returns> The video resolution. </returns>
  274. protected string GetVideoResolution(BaseItem item)
  275. {
  276. var stream = GetStream(item,
  277. MediaStreamType.Video);
  278. if (stream != null && stream.Width != null)
  279. return string.Format("{0} * {1}",
  280. stream.Width,
  281. stream.Height != null ? stream.Height.ToString() : "-");
  282. return string.Empty;
  283. }
  284. /// <summary> Gets video stream. </summary>
  285. /// <param name="item"> The item. </param>
  286. /// <returns> The video stream. </returns>
  287. protected string GetVideoStream(BaseItem item)
  288. {
  289. var stream = GetStream(item, MediaStreamType.Video);
  290. if (stream != null)
  291. return stream.Codec.ToUpper();
  292. return string.Empty;
  293. }
  294. /// <summary> Displays a type visible. </summary>
  295. /// <param name="headerDisplayType"> Type of the header display. </param>
  296. /// <param name="displayType"> Type of the display. </param>
  297. /// <returns> true if it succeeds, false if it fails. </returns>
  298. protected bool DisplayTypeVisible(ReportDisplayType headerDisplayType, ReportDisplayType displayType)
  299. {
  300. if (headerDisplayType == ReportDisplayType.None)
  301. return false;
  302. bool rval = headerDisplayType == displayType || headerDisplayType == ReportDisplayType.ScreenExport && (displayType == ReportDisplayType.Screen || displayType == ReportDisplayType.Export);
  303. return rval;
  304. }
  305. #endregion
  306. }
  307. }