PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/BaliEnterpriseSystems/BaliEnterpriseSystems/Utils.cs

https://github.com/sirivedula/BEST
C# | 371 lines | 359 code | 11 blank | 1 comment | 30 complexity | 651cff1975f2469fcf2569ba47d43915 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Reflection;
  15. using System.ComponentModel;
  16. using BaliEnterpriseSystems.BestObjects;
  17. namespace BaliEnterpriseSystems
  18. {
  19. public static class Utils
  20. {
  21. public static CurrentUser User
  22. {
  23. get
  24. {
  25. if (HttpContext.Current.Session != null)
  26. {
  27. var cuser = HttpContext.Current.Session["CurrentUser"];
  28. if (cuser != null)
  29. {
  30. return (CurrentUser)cuser;
  31. }
  32. }
  33. return null;
  34. }
  35. }
  36. public static bool IsGuid(string guid)
  37. {
  38. bool result=true;
  39. if (!string.IsNullOrEmpty(guid))
  40. {
  41. try
  42. {
  43. Guid tmp = new Guid(guid);
  44. }
  45. catch (Exception ex)
  46. {
  47. result = false;
  48. }
  49. }
  50. else
  51. {
  52. result = false;
  53. }
  54. return result;
  55. }
  56. public static Guid? getNullableGuid(string guid)
  57. {
  58. if (string.IsNullOrEmpty(guid))
  59. {
  60. return null;
  61. }
  62. try
  63. {
  64. Guid result = new Guid(guid);
  65. return result;
  66. }
  67. catch (Exception)
  68. {
  69. return null;
  70. }
  71. }
  72. public static string WarningMessage(string warning)
  73. {
  74. string result = warning;
  75. if (result.Contains("IX_"))
  76. {
  77. result = "Can not add duplicate record, because it is already exists.";
  78. }
  79. return @"<div class=""ui-widget"">
  80. <div class=""ui-state-error ui-corner-all"" style=""width:70%;margin-left:200px;margin-top: 5px; padding: 0 .7em;"">
  81. <p><span class=""ui-icon ui-icon-alert"" style=""float: left; margin-right: .3em;""></span>
  82. <strong>Alert:</strong>" + HttpUtility.HtmlEncode(result) + @"</p>
  83. </div>
  84. </div>";
  85. }
  86. public static string InfoMessage(string warning)
  87. {
  88. string result = warning;
  89. if (result.Contains("IX_"))
  90. {
  91. result = "Can not add duplicate record, because it is already exists.";
  92. }
  93. return @"<div class=""ui-widget"">
  94. <div class=""ui-state-highlight ui-corner-all"" style=""width:70%;margin-left:200px;margin-top: 5px; padding: 0 .7em;"">
  95. <p><span class=""ui-icon ui-icon-info"" style=""float: left; margin-right: .3em;""></span>
  96. " + HttpUtility.HtmlEncode(result).Replace("\n", "<br />") + @"</p>
  97. </div>
  98. </div>";
  99. }
  100. public static string GetMD5Hash(string input)
  101. {
  102. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  103. byte[] bs = Encoding.UTF8.GetBytes(input + "874987^&*Masala Hash To Password Best75938*&*$");
  104. bs = md5.ComputeHash(bs);
  105. StringBuilder sb = new StringBuilder();
  106. foreach (byte b in bs)
  107. {
  108. sb.Append(b.ToString("x2").ToLower());
  109. }
  110. string password = sb.ToString();
  111. return password;
  112. }
  113. public static string EnquoteJS(string s)
  114. {
  115. if (s == null || s.Length == 0)
  116. {
  117. return "\"\"";
  118. }
  119. char c;
  120. int i;
  121. int len = s.Length;
  122. StringBuilder sb = new StringBuilder(len + 4);
  123. string t;
  124. sb.Append('"');
  125. for (i = 0; i < len; i += 1)
  126. {
  127. c = s[i];
  128. if ((c == '\\') || (c == '"') || (c == '>'))
  129. {
  130. sb.Append('\\');
  131. sb.Append(c);
  132. }
  133. else if (c == '\b')
  134. sb.Append("\\b");
  135. else if (c == '\t')
  136. sb.Append("\\t");
  137. else if (c == '\n')
  138. sb.Append("\\n");
  139. else if (c == '\f')
  140. sb.Append("\\f");
  141. else if (c == '\r')
  142. sb.Append("\\r");
  143. else
  144. {
  145. if (c < ' ')
  146. {
  147. //t = "000" + Integer.toHexString(c);
  148. string tmp = new string(c, 1);
  149. t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
  150. sb.Append("\\u" + t.Substring(t.Length - 4));
  151. }
  152. else
  153. {
  154. sb.Append(c);
  155. }
  156. }
  157. }
  158. sb.Append('"');
  159. return sb.ToString();
  160. }
  161. public static bool IsNullableType(Type theType)
  162. {
  163. return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
  164. }
  165. public static string GenericArgType(Type theType)
  166. {
  167. Type[] t = theType.GetGenericArguments();
  168. if (t.Length > 0)
  169. {
  170. return t[0].Name;
  171. }
  172. return "";
  173. }
  174. public static string ToTimeFormat(string num)
  175. {
  176. string result = num;
  177. if (!string.IsNullOrEmpty(num))
  178. {
  179. int temp = int.Parse(num);
  180. if (temp > 1230)
  181. {
  182. temp = temp - 1200;
  183. }
  184. result = (temp > 930) ? temp.ToString().Substring(0, 2) + ":" + temp.ToString().Substring(2, 2) : temp.ToString().Substring(0, 1) + ":" + temp.ToString().Substring(1, 2);
  185. }
  186. return result;
  187. }
  188. public static object ToType(string val, Type ty)
  189. {
  190. string myTypeName = ty.Name.ToUpper();
  191. Type ovType = ty;
  192. if (IsNullableType(ty))
  193. {
  194. myTypeName = GenericArgType(ty).ToUpper();
  195. }
  196. if (IsNullableType(ty))
  197. {
  198. if (String.IsNullOrEmpty(val))
  199. {
  200. return null;
  201. }
  202. NullableConverter nc = new NullableConverter(ty);
  203. ovType = nc.UnderlyingType;
  204. }
  205. switch (myTypeName)
  206. {
  207. case "BOOLEAN":
  208. switch (val.ToUpper())
  209. {
  210. case "1":
  211. case "-1":
  212. case "T":
  213. case "TRUE":
  214. case "Y":
  215. case "YES":
  216. return true;
  217. default:
  218. return false;
  219. }
  220. case "DATETIME":
  221. if ((val??"").Equals(""))
  222. {
  223. return DateTime.MinValue;
  224. }
  225. break;
  226. case "BYTE":
  227. case "DECIMAL":
  228. case "INT32":
  229. case "INT16":
  230. case "SBYTE":
  231. case "SHORT":
  232. case "USHORT":
  233. case "FLOAT":
  234. case "DOUBLE":
  235. case "LONG":
  236. case "ULONG":
  237. if (val.Equals(""))
  238. {
  239. return Convert.ChangeType("0", ovType);
  240. }
  241. break;
  242. case "GUID":
  243. return new Guid(val);
  244. }
  245. return Convert.ChangeType(val, ovType);
  246. }
  247. public static string MenuSelectScript(string ms)
  248. {
  249. string result = "";
  250. if (!string.IsNullOrEmpty(ms))
  251. {
  252. result = "<script type=\"text/javascript\"> $().ready(function(){ $('#liMenu" + ms + "').addClass('Selected');";
  253. result += "$('#bleft" + ms + "').addClass(\"L\");";
  254. result += "$('#bright" + ms + "').addClass(\"R\");";
  255. result += "}); </script>";
  256. }
  257. return result;
  258. }
  259. public static string StateOptions()
  260. {
  261. StringBuilder sb = new StringBuilder();
  262. sb.Append("<option value=\"\"></option>");
  263. sb.Append("<option value=\"AK\">AK (Alaska)</option>");
  264. sb.Append("<option value=\"AL\">AL (Alabama)</option>");
  265. sb.Append("<option value=\"AR\">AR (Arkansas)</option>");
  266. sb.Append("<option value=\"AZ\">AZ (Arizona)</option>");
  267. sb.Append("<option value=\"CA\" selected>CA (California)</option>");
  268. sb.Append("<option value=\"CO\">CO (Colorado)</option>");
  269. sb.Append("<option value=\"CT\">CT (Connecticut)</option>");
  270. sb.Append("<option value=\"DC\">DC (Washington DC)</option>");
  271. sb.Append("<option value=\"DE\">DE (Delaware)</option>");
  272. sb.Append("<option value=\"FL\">FL (Florida)</option>");
  273. sb.Append("<option value=\"GA\">GA (Georgia)</option>");
  274. sb.Append("<option value=\"HI\">HI (Hawaii)</option>");
  275. sb.Append("<option value=\"IA\">IA (Iowa)</option>");
  276. sb.Append("<option value=\"ID\">ID (Idaho)</option>");
  277. sb.Append("<option value=\"IL\">IL (Illinois)</option>");
  278. sb.Append("<option value=\"IN\">IN (Indiana)</option>");
  279. sb.Append("<option value=\"KS\">KS (Kansas)</option>");
  280. sb.Append("<option value=\"KY\">KY (Kentucky)</option>");
  281. sb.Append("<option value=\"LA\">LA (Lousiana)</option>");
  282. sb.Append("<option value=\"MA\">MA (Massachusetts)</option>");
  283. sb.Append("<option value=\"MD\">MD (Maryland)</option>");
  284. sb.Append("<option value=\"ME\">ME (Maine)</option>");
  285. sb.Append("<option value=\"MI\">MI (Michigan)</option>");
  286. sb.Append("<option value=\"MN\">MN (Minnesota)</option>");
  287. sb.Append("<option value=\"MO\">MO (Missouri)</option>");
  288. sb.Append("<option value=\"MS\">MS (Mississippi)</option>");
  289. sb.Append("<option value=\"MT\">MT (Montana)</option>");
  290. sb.Append("<option value=\"NC\">NC (North Carolina)</option>");
  291. sb.Append("<option value=\"ND\">ND (North Dakota)</option>");
  292. sb.Append("<option value=\"NE\">NE (Nebraska)</option>");
  293. sb.Append("<option value=\"NH\">NH (New Hampshire)</option>");
  294. sb.Append("<option value=\"NJ\">NJ (New Jersey)</option>");
  295. sb.Append("<option value=\"NM\">NM (New Mexico)</option>");
  296. sb.Append("<option value=\"NV\">NV (Nevada)</option>");
  297. sb.Append("<option value=\"NY\">NY (New York)</option>");
  298. sb.Append("<option value=\"OH\">OH (Ohio)</option>");
  299. sb.Append("<option value=\"OK\">OK (Oklahoma)</option>");
  300. sb.Append("<option value=\"OR\">OR (Oregon)</option>");
  301. sb.Append("<option value=\"PA\">PA (Pennsylvania)</option>");
  302. sb.Append("<option value=\"PR\">PR (Puerto Rico)</option>");
  303. sb.Append("<option value=\"RI\">RI (Rhode Island)</option>");
  304. sb.Append("<option value=\"SC\">SC (South Carolina)</option>");
  305. sb.Append("<option value=\"SD\">SD (South Dakota)</option>");
  306. sb.Append("<option value=\"TN\">TN (Tennessee)</option>");
  307. sb.Append("<option value=\"TX\">TX (Texas)</option>");
  308. sb.Append("<option value=\"UT\">UT (Utah)</option>");
  309. sb.Append("<option value=\"VA\">VA (Virginia)</option>");
  310. sb.Append("<option value=\"VI\">VI (Virgin Islands)</option>");
  311. sb.Append("<option value=\"VT\">VT (Vermont)</option>");
  312. sb.Append("<option value=\"WA\">WA (Washington)</option>");
  313. sb.Append("<option value=\"WI\">WI (Wisconsin)</option>");
  314. sb.Append("<option value=\"WV\">WV (West Virginia)</option>");
  315. sb.Append("<option value=\"WY\">WY (Wyoming)</option>");
  316. return sb.ToString();
  317. }
  318. public static string RelationshipOptions()
  319. {
  320. return "<option value=\"\"></option>" +
  321. "<option value=\"Adopted Child\">Adopted Child</option>" +
  322. "<option value=\"Brother or Sister\">Brother or Sister</option>" +
  323. "<option value=\"Brother/Sister-in-law\">Brother/Sister-in-law</option>" +
  324. "<option value=\"Child\">Child</option>" +
  325. "<option value=\"Collateral Dependent\">Collateral Dependent</option>" +
  326. "<option value=\"Court Appointed Guardian\">Court Appointed Guardian</option>" +
  327. "<option value=\"Cousin\">Cousin</option>" +
  328. "<option value=\"Dependent of a Minor Dependent\">Dependent of a Minor Dependent</option>" +
  329. "<option value=\"Ex-spouse\">Ex-spouse</option>" +
  330. "<option value=\"Father\">Father</option>" +
  331. "<option value=\"Father or Mother\">Father or Mother</option>" +
  332. "<option value=\"Foster Child\">Foster Child</option>" +
  333. "<option value=\"Grandfather or Grandmother\">Grandfather or Grandmother</option>" +
  334. "<option value=\"Grandson or Granddaughter\">Grandson or Granddaughter</option>" +
  335. "<option value=\"Guardian\">Guardian</option>" +
  336. "<option value=\"Life Partner\">Life Partner</option>" +
  337. "<option value=\"Mother\">Mother</option>" +
  338. "<option value=\"Mother/Father-in-law\">Mother/Father-in-law</option>" +
  339. "<option value=\"Nephew or Niece\">Nephew or Niece</option>" +
  340. "<option value=\"Other\">Other</option>" +
  341. "<option value=\"Son/Daughter-in-law\">Son/Daughter-in-law</option>" +
  342. "<option value=\"Sponsored Dependent\">Sponsored Dependent</option>" +
  343. "<option value=\"Spouse\">Spouse</option>" +
  344. "<option value=\"Stepfather\">Stepfather</option>" +
  345. "<option value=\"Stepmother\">Stepmother</option>" +
  346. "<option value=\"Stepson or Stepdaughter\">Stepson or Stepdaughter</option>" +
  347. "<option value=\"Uncle or Aunt\">Uncle or Aunt</option>" +
  348. "<option value=\"Ward\">Ward</option>";
  349. }
  350. }
  351. }