aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Reports/Common/ReportHelper.cs
blob: 1de0190cf82cf9e860218cb4c9b56e61a41def0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using MediaBrowser.Controller.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MediaBrowser.Api.Reports
{
    /// <summary> A report helper. </summary>
    public class ReportHelper
    {
        #region [Public Methods]

        /// <summary> Convert field to string. </summary>
        /// <typeparam name="T"> Generic type parameter. </typeparam>
        /// <param name="value"> The value. </param>
        /// <param name="fieldType"> Type of the field. </param>
        /// <returns> The field converted to string. </returns>
        public static string ConvertToString<T>(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();
            }
        }

        /// <summary> Gets filtered report header metadata. </summary>
        /// <param name="reportColumns"> The report columns. </param>
        /// <param name="defaultReturnValue"> The default return value. </param>
        /// <returns> The filtered report header metadata. </returns>
        public static List<HeaderMetadata> GetFilteredReportHeaderMetadata(string reportColumns, Func<List<HeaderMetadata>> 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<HeaderMetadata>();
        }

        /// <summary> Gets header metadata type. </summary>
        /// <param name="header"> The header. </param>
        /// <returns> The header metadata type. </returns>
        public static HeaderMetadata GetHeaderMetadataType(string header)
        {
            if (string.IsNullOrEmpty(header))
                return HeaderMetadata.None;

            HeaderMetadata rType;

            if (!Enum.TryParse<HeaderMetadata>(header, out rType))
                return HeaderMetadata.None;

            return rType;
        }

        /// <summary> Gets java script localized string. </summary>
        /// <param name="phrase"> The phrase. </param>
        /// <returns> The java script localized string. </returns>
        public static string GetJavaScriptLocalizedString(string phrase)
        {
            var dictionary = BaseItem.LocalizationManager.GetJavaScriptLocalizationDictionary(BaseItem.ConfigurationManager.Configuration.UICulture);

            string value;

            if (dictionary.TryGetValue(phrase, out value))
            {
                return value;
            }

            return phrase;
        }

        /// <summary> Gets report view type. </summary>
        /// <param name="rowType"> The type. </param>
        /// <returns> The report view type. </returns>
        public static ReportViewType GetReportViewType(string rowType)
        {
            if (string.IsNullOrEmpty(rowType))
                return ReportViewType.ReportData;

            ReportViewType rType;

            if (!Enum.TryParse<ReportViewType>(rowType, out rType))
                return ReportViewType.ReportData;

            return rType;
        }

        /// <summary> Gets row type. </summary>
        /// <param name="rowType"> The type. </param>
        /// <returns> The row type. </returns>
        public static ReportIncludeItemTypes GetRowType(string rowType)
        {
            if (string.IsNullOrEmpty(rowType))
                return ReportIncludeItemTypes.BaseItem;

            ReportIncludeItemTypes rType;

            if (!Enum.TryParse<ReportIncludeItemTypes>(rowType, out rType))
                return ReportIncludeItemTypes.BaseItem;

            return rType;
        }

        /// <summary> Gets server localized string. </summary>
        /// <param name="phrase"> The phrase. </param>
        /// <returns> The server localized string. </returns>
        public static string GetServerLocalizedString(string phrase)
        {
            return BaseItem.LocalizationManager.GetLocalizedString(phrase, BaseItem.ConfigurationManager.Configuration.UICulture);
        }

        #endregion

    }
}