BEST /BaliEnterpriseSystems/BaliEnterpriseSystems/Utils.cs

Language C# Lines 372
MD5 Hash 651cff1975f2469fcf2569ba47d43915
Repository https://github.com/sirivedula/BEST.git View Raw File
  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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using BaliEnterpriseSystems.BestObjects;

namespace BaliEnterpriseSystems
{
    public static class Utils
    {
        public static CurrentUser User
        {
            get
            {
                if (HttpContext.Current.Session != null)
                {
                    var cuser = HttpContext.Current.Session["CurrentUser"];
                    if (cuser != null)
                    {
                        return (CurrentUser)cuser;
                    }
                }
                return null;
            }
        }

        public static bool IsGuid(string guid)
        {
            bool result=true;
            if (!string.IsNullOrEmpty(guid))
            {
                try
                {
                    Guid tmp = new Guid(guid);
                }
                catch (Exception ex)
                {
                    result = false;
                }
            }
            else
            {
                result = false;
            }
            return result;
        }

        public static Guid? getNullableGuid(string guid)
        {
            if (string.IsNullOrEmpty(guid))
            {
                return null;
            }
            try
            {
                Guid result = new Guid(guid);
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }


        public static string WarningMessage(string warning)
        {
            string result = warning;
            if (result.Contains("IX_"))
            {
                result = "Can not add duplicate record, because it is already exists.";
            }
                
            return @"<div class=""ui-widget"">
                <div class=""ui-state-error ui-corner-all"" style=""width:70%;margin-left:200px;margin-top: 5px; padding: 0 .7em;""> 
                <p><span class=""ui-icon ui-icon-alert"" style=""float: left; margin-right: .3em;""></span> 
                <strong>Alert:</strong>" + HttpUtility.HtmlEncode(result) + @"</p>
                </div>
            </div>";
        }

        public static string InfoMessage(string warning)
        {
            string result = warning;
            if (result.Contains("IX_"))
            {
                result = "Can not add duplicate record, because it is already exists.";
            }

            return @"<div class=""ui-widget"">
                <div class=""ui-state-highlight ui-corner-all"" style=""width:70%;margin-left:200px;margin-top: 5px; padding: 0 .7em;""> 
                <p><span class=""ui-icon ui-icon-info"" style=""float: left; margin-right: .3em;""></span> 
                " + HttpUtility.HtmlEncode(result).Replace("\n", "<br />") + @"</p>
                </div>
            </div>";
        }

        public static string GetMD5Hash(string input)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(input + "874987^&*Masala Hash To Password Best75938*&*$");
            bs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2").ToLower());
            }
            string password = sb.ToString();
            return password;
        }

        public static string EnquoteJS(string s)
        {
            if (s == null || s.Length == 0)
            {
                return "\"\"";
            }
            char c;
            int i;
            int len = s.Length;
            StringBuilder sb = new StringBuilder(len + 4);
            string t;

            sb.Append('"');
            for (i = 0; i < len; i += 1)
            {
                c = s[i];
                if ((c == '\\') || (c == '"') || (c == '>'))
                {
                    sb.Append('\\');
                    sb.Append(c);
                }
                else if (c == '\b')
                    sb.Append("\\b");
                else if (c == '\t')
                    sb.Append("\\t");
                else if (c == '\n')
                    sb.Append("\\n");
                else if (c == '\f')
                    sb.Append("\\f");
                else if (c == '\r')
                    sb.Append("\\r");
                else
                {
                    if (c < ' ')
                    {
                        //t = "000" + Integer.toHexString(c); 
                        string tmp = new string(c, 1);
                        t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
                        sb.Append("\\u" + t.Substring(t.Length - 4));
                    }
                    else
                    {
                        sb.Append(c);
                    }
                }
            }
            sb.Append('"');
            return sb.ToString();
        } 

        public static bool IsNullableType(Type theType)
        {
            return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
        }

        public static string GenericArgType(Type theType)
        {
            Type[] t = theType.GetGenericArguments();
            if (t.Length > 0)
            {
                return t[0].Name;
            }
            return "";
        }

        public static string ToTimeFormat(string num)
        {
            string result = num;
            if (!string.IsNullOrEmpty(num))
            {
                int temp = int.Parse(num);
                if (temp > 1230)
                {
                    temp = temp - 1200;
                }
                result = (temp > 930) ? temp.ToString().Substring(0, 2) + ":" + temp.ToString().Substring(2, 2) : temp.ToString().Substring(0, 1) + ":" + temp.ToString().Substring(1, 2);
            }
            return result;
        }

        public static object ToType(string val, Type ty)
        {
            string myTypeName = ty.Name.ToUpper();
            Type ovType = ty;
            if (IsNullableType(ty))
            {
                myTypeName = GenericArgType(ty).ToUpper();
            }
            if (IsNullableType(ty))
            {
                if (String.IsNullOrEmpty(val))
                {
                    return null;
                }
                NullableConverter nc = new NullableConverter(ty);
                ovType = nc.UnderlyingType;
            }
            switch (myTypeName)
            {
                case "BOOLEAN":
                    switch (val.ToUpper())
                    {

                        case "1":
                        case "-1":
                        case "T":
                        case "TRUE":
                        case "Y":
                        case "YES":
                            return true;
                        default:
                            return false;
                    }
                case "DATETIME":
                    if ((val??"").Equals(""))
                    {
                        return DateTime.MinValue;
                    }
                    break;
                case "BYTE":
                case "DECIMAL":
                case "INT32":
                case "INT16":
                case "SBYTE":
                case "SHORT":
                case "USHORT":
                case "FLOAT":
                case "DOUBLE":
                case "LONG":
                case "ULONG":
                    if (val.Equals(""))
                    {
                        return Convert.ChangeType("0", ovType);
                    }
                    break;
                case "GUID":
                    return new Guid(val);
            }

            return Convert.ChangeType(val, ovType);
        }

        public static string MenuSelectScript(string ms)
        {
            string result = "";
            if (!string.IsNullOrEmpty(ms))
            {
                result = "<script type=\"text/javascript\"> $().ready(function(){ $('#liMenu" + ms + "').addClass('Selected');";
                result += "$('#bleft" + ms + "').addClass(\"L\");";
                result += "$('#bright" + ms + "').addClass(\"R\");";
                result += "}); </script>";
            }
            return result;
        }

        public static string StateOptions()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<option value=\"\"></option>");
            sb.Append("<option value=\"AK\">AK  (Alaska)</option>");
            sb.Append("<option value=\"AL\">AL  (Alabama)</option>");
            sb.Append("<option value=\"AR\">AR  (Arkansas)</option>");
            sb.Append("<option value=\"AZ\">AZ  (Arizona)</option>");
            sb.Append("<option value=\"CA\" selected>CA  (California)</option>");
            sb.Append("<option value=\"CO\">CO  (Colorado)</option>");
            sb.Append("<option value=\"CT\">CT  (Connecticut)</option>");
            sb.Append("<option value=\"DC\">DC  (Washington DC)</option>");
            sb.Append("<option value=\"DE\">DE  (Delaware)</option>");
            sb.Append("<option value=\"FL\">FL  (Florida)</option>");
            sb.Append("<option value=\"GA\">GA  (Georgia)</option>");
            sb.Append("<option value=\"HI\">HI  (Hawaii)</option>");
            sb.Append("<option value=\"IA\">IA  (Iowa)</option>");
            sb.Append("<option value=\"ID\">ID  (Idaho)</option>");
            sb.Append("<option value=\"IL\">IL  (Illinois)</option>");
            sb.Append("<option value=\"IN\">IN  (Indiana)</option>");
            sb.Append("<option value=\"KS\">KS  (Kansas)</option>");
            sb.Append("<option value=\"KY\">KY  (Kentucky)</option>");
            sb.Append("<option value=\"LA\">LA  (Lousiana)</option>");
            sb.Append("<option value=\"MA\">MA  (Massachusetts)</option>");
            sb.Append("<option value=\"MD\">MD  (Maryland)</option>");
            sb.Append("<option value=\"ME\">ME  (Maine)</option>");
            sb.Append("<option value=\"MI\">MI  (Michigan)</option>");
            sb.Append("<option value=\"MN\">MN  (Minnesota)</option>");
            sb.Append("<option value=\"MO\">MO  (Missouri)</option>");
            sb.Append("<option value=\"MS\">MS  (Mississippi)</option>");
            sb.Append("<option value=\"MT\">MT  (Montana)</option>");
            sb.Append("<option value=\"NC\">NC  (North Carolina)</option>");
            sb.Append("<option value=\"ND\">ND  (North Dakota)</option>");
            sb.Append("<option value=\"NE\">NE  (Nebraska)</option>");
            sb.Append("<option value=\"NH\">NH  (New Hampshire)</option>");
            sb.Append("<option value=\"NJ\">NJ  (New Jersey)</option>");
            sb.Append("<option value=\"NM\">NM  (New Mexico)</option>");
            sb.Append("<option value=\"NV\">NV  (Nevada)</option>");
            sb.Append("<option value=\"NY\">NY  (New York)</option>");
            sb.Append("<option value=\"OH\">OH  (Ohio)</option>");
            sb.Append("<option value=\"OK\">OK  (Oklahoma)</option>");
            sb.Append("<option value=\"OR\">OR  (Oregon)</option>");
            sb.Append("<option value=\"PA\">PA  (Pennsylvania)</option>");
            sb.Append("<option value=\"PR\">PR  (Puerto Rico)</option>");
            sb.Append("<option value=\"RI\">RI  (Rhode Island)</option>");
            sb.Append("<option value=\"SC\">SC  (South Carolina)</option>");
            sb.Append("<option value=\"SD\">SD  (South Dakota)</option>");
            sb.Append("<option value=\"TN\">TN  (Tennessee)</option>");
            sb.Append("<option value=\"TX\">TX  (Texas)</option>");
            sb.Append("<option value=\"UT\">UT  (Utah)</option>");
            sb.Append("<option value=\"VA\">VA  (Virginia)</option>");
            sb.Append("<option value=\"VI\">VI  (Virgin Islands)</option>");
            sb.Append("<option value=\"VT\">VT  (Vermont)</option>");
            sb.Append("<option value=\"WA\">WA  (Washington)</option>");
            sb.Append("<option value=\"WI\">WI  (Wisconsin)</option>");
            sb.Append("<option value=\"WV\">WV  (West Virginia)</option>");
            sb.Append("<option value=\"WY\">WY  (Wyoming)</option>");
            return sb.ToString();
        }

        public static string RelationshipOptions()
        {
                return "<option value=\"\"></option>" +
                "<option value=\"Adopted Child\">Adopted Child</option>" +
                "<option value=\"Brother or Sister\">Brother or Sister</option>" +
                "<option value=\"Brother/Sister-in-law\">Brother/Sister-in-law</option>" +
                "<option value=\"Child\">Child</option>" +
                "<option value=\"Collateral Dependent\">Collateral Dependent</option>" +
                "<option value=\"Court Appointed Guardian\">Court Appointed Guardian</option>" +
                "<option value=\"Cousin\">Cousin</option>" +
                "<option value=\"Dependent of a Minor Dependent\">Dependent of a Minor Dependent</option>" +
                "<option value=\"Ex-spouse\">Ex-spouse</option>" +
                "<option value=\"Father\">Father</option>" +
                "<option value=\"Father or Mother\">Father or Mother</option>" +
                "<option value=\"Foster Child\">Foster Child</option>" +
                "<option value=\"Grandfather or Grandmother\">Grandfather or Grandmother</option>" +
                "<option value=\"Grandson or Granddaughter\">Grandson or Granddaughter</option>" +
                "<option value=\"Guardian\">Guardian</option>" +
                "<option value=\"Life Partner\">Life Partner</option>" +
                "<option value=\"Mother\">Mother</option>" +
                "<option value=\"Mother/Father-in-law\">Mother/Father-in-law</option>" +
                "<option value=\"Nephew or Niece\">Nephew or Niece</option>" +
                "<option value=\"Other\">Other</option>" +
                "<option value=\"Son/Daughter-in-law\">Son/Daughter-in-law</option>" +
                "<option value=\"Sponsored Dependent\">Sponsored Dependent</option>" +
                "<option value=\"Spouse\">Spouse</option>" +
                "<option value=\"Stepfather\">Stepfather</option>" +
                "<option value=\"Stepmother\">Stepmother</option>" +
                "<option value=\"Stepson or Stepdaughter\">Stepson or Stepdaughter</option>" +
                "<option value=\"Uncle or Aunt\">Uncle or Aunt</option>" +
                "<option value=\"Ward\">Ward</option>";
        }
    }
}
Back to Top