using MediaBrowser.Controller.Entities; using System; using System.Collections.Generic; using System.Linq; namespace MediaBrowser.Api.Reports { /// A report helper. public class ReportHelper { #region [Public Methods] /// Convert field to string. /// Generic type parameter. /// The value. /// Type of the field. /// The field converted to string. public static string ConvertToString(T value, ReportFieldType fieldType) { if (value == null) return ""; switch (fieldType) { case ReportFieldType.String: return value.ToString(); case ReportFieldType.Boolean: return value.ToString(); case ReportFieldType.Date: return string.Format("{0:d}", value); case ReportFieldType.Time: return string.Format("{0:t}", value); case ReportFieldType.DateTime: return string.Format("{0:d}", value); case ReportFieldType.Minutes: return string.Format("{0}mn", value); case ReportFieldType.Int: return string.Format("", value); default: if (value is Guid) return string.Format("{0:N}", value); return value.ToString(); } } /// Gets filtered report header metadata. /// The report columns. /// The default return value. /// The filtered report header metadata. public static List GetFilteredReportHeaderMetadata(string reportColumns, Func> defaultReturnValue = null) { if (!string.IsNullOrEmpty(reportColumns)) { var s = reportColumns.Split('|').Select(x => ReportHelper.GetHeaderMetadataType(x)).Where(x => x != HeaderMetadata.None); return s.ToList(); } else if (defaultReturnValue != null) return defaultReturnValue(); else return new List(); } /// Gets header metadata type. /// The header. /// The header metadata type. public static HeaderMetadata GetHeaderMetadataType(string header) { if (string.IsNullOrEmpty(header)) return HeaderMetadata.None; HeaderMetadata rType; if (!Enum.TryParse(header, out rType)) return HeaderMetadata.None; return rType; } /// Gets java script localized string. /// The phrase. /// The java script localized string. public static string GetJavaScriptLocalizedString(string phrase) { return BaseItem.LocalizationManager.GetLocalizedString(phrase); } /// Gets report view type. /// The type. /// The report view type. public static ReportViewType GetReportViewType(string rowType) { if (string.IsNullOrEmpty(rowType)) return ReportViewType.ReportData; ReportViewType rType; if (!Enum.TryParse(rowType, out rType)) return ReportViewType.ReportData; return rType; } /// Gets row type. /// The type. /// The row type. public static ReportIncludeItemTypes GetRowType(string rowType) { if (string.IsNullOrEmpty(rowType)) return ReportIncludeItemTypes.BaseItem; ReportIncludeItemTypes rType; if (!Enum.TryParse(rowType, out rType)) return ReportIncludeItemTypes.BaseItem; return rType; } /// Gets server localized string. /// The phrase. /// The server localized string. public static string GetServerLocalizedString(string phrase) { return BaseItem.LocalizationManager.GetLocalizedString(phrase); } #endregion } }