source: extensions/PiwigoLib/PiwigoLib/Proxy/Helper/PwgProxyReponseHelper.cs @ 11872

Last change on this file since 11872 was 11872, checked in by bayral, 13 years ago
File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Web;
6
7using Com.Piwigo.Lib.DTO.Helper;
8using System.Xml;
9using Com.Piwigo.Lib.DTO;
10
11namespace Com.Piwigo.Lib.Proxy.Helper
12{
13    internal static class PwgProxyReponseHelper
14    {
15        internal static void buildDicFieldFromArray<T>(List<T> values, String paramName, ref Dictionary<String, String> dcFields)
16        {
17            String FormatString = String.Empty;
18            String ParamValue = String.Empty;
19
20            Boolean firstOcc = true;
21            if (values != null)
22            {
23                foreach (T occurance in values)
24                {
25                    if (firstOcc == true)
26                    {
27                        firstOcc = false;
28                        FormatString = "{0}";
29                    }
30                    else
31                    {
32                        FormatString = ",{0}";
33                    }
34
35                    ParamValue += String.Format(FormatString,GetString(occurance));
36                }
37
38                dcFields.Add(paramName, ParamValue);
39            }
40        }
41
42        internal static void buildDicFieldFromValue<T>(Nullable<T> nullableValue, String paramName, ref Dictionary<String, String> dcFields) where T : struct
43        {
44            if (nullableValue.HasValue)
45            {
46                buildDicFieldFromValue<T>(nullableValue.Value, paramName, ref dcFields);
47            }
48        }
49
50        internal static void buildDicFieldFromValue<T>(T value, String paramName, ref Dictionary<String, String> dcFields)
51        {
52            String FormatString = String.Empty;
53            String ParamValue = String.Empty;
54
55            FormatString = "{0}";
56
57            ParamValue = GetString(value);
58
59            dcFields.Add(paramName, ParamValue);
60        }
61
62        internal static void buildQueryFromArray<T>(List<T> values, String paramName, ref Boolean firstOcc, StringBuilder queryStringBuilder)
63        {
64            String FormatString = String.Empty;
65            if (values != null)
66            {
67                if (values.Count == 1)
68                {
69                    FormatString = "{0}={1}";
70                }
71                else
72                {
73                    FormatString = "{0}[]={1}";
74                }
75                if (firstOcc != true)
76                {
77                    FormatString = String.Format("&{0}", FormatString);
78                }
79
80                foreach (T occurance in values)
81                {
82                    queryStringBuilder.AppendFormat(FormatString, paramName, HttpUtility.UrlEncode(GetString(occurance)));
83                    firstOcc = false;
84                }
85
86            }
87        }
88
89        internal static void buildQueryFromValue<T>(Nullable<T> nullableValue, String paramName, ref Boolean firstOcc, StringBuilder queryStringBuilder) where T : struct
90        {
91            if (nullableValue.HasValue)
92            {
93                buildQueryFromValue<T>(nullableValue.Value, paramName, ref firstOcc, queryStringBuilder);
94            }
95        }
96
97        internal static void buildQueryFromValue<T>(T value, String paramName, ref Boolean firstOcc, StringBuilder queryStringBuilder)
98        {
99            String FormatString = String.Empty;
100            String ParamValue = String.Empty;
101           
102                FormatString = "{0}={1}";
103                if (firstOcc != true)
104                {
105                    FormatString = String.Format("&{0}", FormatString);
106                }
107
108                ParamValue = HttpUtility.UrlEncode(GetString(value));
109               
110                queryStringBuilder.AppendFormat(FormatString, paramName, ParamValue);
111               
112                firstOcc = false;
113        }
114
115        public static String GetString<T>(T value)
116        {
117            String strRet = String.Empty;
118            if (value is String)
119            {
120                strRet = (value as String);
121            }
122            else if (typeof(T).IsEnum)
123            {
124                strRet = PwgEnumHelper<T>.StringValueOf(value);
125            }
126            else if (value is PwgTag)
127            {
128
129                strRet = (value as PwgTag).Name;
130            }
131            else
132            {
133
134                strRet = (value as Object).ToString().ToLowerInvariant();
135            }
136
137            return strRet;
138        }
139    }
140}
Note: See TracBrowser for help on using the repository browser.