ReportBuilderBase.cs 13 KB

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