using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using Com.Piwigo.Lib.DTO.Helper; using System.Xml; using Com.Piwigo.Lib.DTO; namespace Com.Piwigo.Lib.Proxy.Helper { internal static class PwgProxyReponseHelper { internal static void buildDicFieldFromArray(List values, String paramName, ref Dictionary dcFields) { String FormatString = String.Empty; String ParamValue = String.Empty; Boolean firstOcc = true; if (values != null) { foreach (T occurance in values) { if (firstOcc == true) { firstOcc = false; FormatString = "{0}"; } else { FormatString = ",{0}"; } ParamValue += String.Format(FormatString,GetString(occurance)); } dcFields.Add(paramName, ParamValue); } } internal static void buildDicFieldFromValue(Nullable nullableValue, String paramName, ref Dictionary dcFields) where T : struct { if (nullableValue.HasValue) { buildDicFieldFromValue(nullableValue.Value, paramName, ref dcFields); } } internal static void buildDicFieldFromValue(T value, String paramName, ref Dictionary dcFields) { String FormatString = String.Empty; String ParamValue = String.Empty; FormatString = "{0}"; ParamValue = GetString(value); dcFields.Add(paramName, ParamValue); } internal static void buildQueryFromArray(List values, String paramName, ref Boolean firstOcc, StringBuilder queryStringBuilder) { String FormatString = String.Empty; if (values != null) { if (values.Count == 1) { FormatString = "{0}={1}"; } else { FormatString = "{0}[]={1}"; } if (firstOcc != true) { FormatString = String.Format("&{0}", FormatString); } foreach (T occurance in values) { queryStringBuilder.AppendFormat(FormatString, paramName, Uri.EscapeUriString(GetString(occurance))); firstOcc = false; } } } internal static void buildQueryFromValue(Nullable nullableValue, String paramName, ref Boolean firstOcc, StringBuilder queryStringBuilder) where T : struct { if (nullableValue.HasValue) { buildQueryFromValue(nullableValue.Value, paramName, ref firstOcc, queryStringBuilder); } } internal static void buildQueryFromValue(T value, String paramName, ref Boolean firstOcc, StringBuilder queryStringBuilder) { String FormatString = String.Empty; String ParamValue = String.Empty; FormatString = "{0}={1}"; if (firstOcc != true) { FormatString = String.Format("&{0}", FormatString); } ParamValue = Uri.EscapeUriString(GetString(value)); queryStringBuilder.AppendFormat(FormatString, paramName, ParamValue); firstOcc = false; } public static String GetString(T value) { String strRet = String.Empty; if (value is String) { strRet = (value as String); } else if (typeof(T).IsEnum) { strRet = PwgEnumHelper.StringValueOf(value); } else if (value is PwgTag) { strRet = (value as PwgTag).Name; } else { strRet = (value as Object).ToString().ToLowerInvariant(); } return strRet; } } }