DBModel.cs 33.9 KB
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AIAHTML5.API.Properties;
using AIAHTML5.API.Constants;
using log4net;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.Collections;

namespace AIAHTML5.API.Models
{
    public class DBModel
    {
        static string dbConnectionString = System.Configuration.ConfigurationManager.AppSettings["AIADatabaseV5Context"];


        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        public DBModel() { }


        protected static DataSet GetDataFromStoredProcedure(string commandText)
        {

            logger.Debug(" Inside GetSQLData for command text = " + commandText);
            DataSet ds = null;
            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand(commandText, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            ds = new DataSet();
            da.Fill(ds);

            return ds;
        }

        internal ArrayList GetAllModules()
        {
            logger.Debug(" Inside GetAllModules");

            ArrayList arrUserModules = new ArrayList();

            Hashtable userModuleHash = new Hashtable();

            string sp = DBConstants.GET_ALL_MODULES;

            DataSet ds = DBModel.GetDataFromStoredProcedure(sp);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                foreach (DataRow drModule in dt.Rows)
                {
                    userModuleHash = new Hashtable();
                    userModuleHash.Add(AIAConstants.KEY_ID, drModule["Id"]);
                    userModuleHash.Add(AIAConstants.KEY_NAME, drModule["Name"]);
                    userModuleHash.Add(AIAConstants.KEY_SLUG, drModule["Slug"]);
                    arrUserModules.Add(userModuleHash);
                }
            }

            return arrUserModules;
        }

        internal static User GetUserDetailsByLoginId(string loginId)
        {
            logger.Debug(" Inside GetUserDetailsByLoginId for LoginId = " + loginId);

            User objUser = null;
            DBModel objModel = new DBModel();


            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            SqlParameter param;
            DataSet ds = new DataSet();

            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_USER_DELAILS_BY_LOGIN_ID;
            cmd.CommandType = CommandType.StoredProcedure;

            param = new SqlParameter("@sLoginId", loginId);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.String;
            cmd.Parameters.Add(param);

            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
            da.Fill(dt);


            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int tempVal;
                    DateTime date;
                    objUser = new User();

                    objUser.Id = Convert.ToInt32(dr["Id"]);
                    objUser.FirstName = dr["FirstName"].ToString();
                    objUser.LastName = dr["LastName"].ToString();
                    objUser.EmailId = dr["EmailId"].ToString();
                    objUser.LoginId = dr["LoginId"].ToString();
                    objUser.Password = dr["Password"].ToString();
                    objUser.SecurityQuestionId = Int32.TryParse(dr["SecurityQuestionId"].ToString(), out tempVal) ? tempVal : (int?)null;
                    objUser.SecurityAnswer = dr["SecurityAnswer"].ToString(); ;
                    objUser.CreatorId = Int32.TryParse(dr["CreatorId"].ToString(), out tempVal) ? tempVal : (int?)null;
                    objUser.CreationDate = Convert.ToDateTime(dr["CreationDate"]);
                    objUser.DeactivationDate = DateTime.TryParse(dr["DeactivationDate"].ToString(), out date) ? date : (DateTime?)null;
                    objUser.ModifierId = Int32.TryParse(dr["ModifierId"].ToString(), out tempVal) ? tempVal : (int?)null;
                    objUser.ModifiedDate = DateTime.TryParse(dr["ModifiedDate"].ToString(), out date) ? date : (DateTime?)null;
                    objUser.UserTypeId = Convert.ToInt32(dr["UserTypeId"]);
                    objUser.UserType = objModel.GetUserTypeStringById(Convert.ToInt32(dr["UserTypeId"]));
                    objUser.IsActive = Convert.ToBoolean(dr["IsActive"]);
                }
            }


            return objUser;
        }

        internal Hashtable GetLicenseDetailByUserId(int userId)
        {
            logger.Debug(" Inside GetUserLicenseDetailByUserId for UserId = " + userId);

            Hashtable hash = new Hashtable();

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter;
            SqlParameter param;
            DataSet ds = new DataSet();

            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_LICENSE_DETAILS_BY_USER_ID;
            cmd.CommandType = CommandType.StoredProcedure;

            param = new SqlParameter("@iUserId", userId);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.Int32;
            cmd.Parameters.Add(param);

            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    hash.Add(AIAConstants.LICENSE_KEY_ID, ds.Tables[0].Rows[0][0]);
                    hash.Add(AIAConstants.EDITION_KEY_ID, ds.Tables[0].Rows[0][1]);
                }
            }

            return hash;
        }

        internal ArrayList GetUserModulesByLicenseId(int licenseId)
        {
            logger.Debug(" Inside GetUserModulesByLicenseId for LicenseId = " + licenseId);

            ArrayList userModulelist = new ArrayList();

            Hashtable modulesHash;
            DataSet ds = new DataSet();

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter;
            SqlParameter param;

            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_USER_MODULES_BY_LICENSE_ID;
            cmd.CommandType = CommandType.StoredProcedure;

            param = new SqlParameter("@iLicenseId", licenseId);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.Int32;
            cmd.Parameters.Add(param);

            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);


            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        modulesHash = new Hashtable();
                        modulesHash.Add(AIAConstants.KEY_ID, dr["Id"]);
                        modulesHash.Add(AIAConstants.KEY_NAME, dr["Title"]);
                        modulesHash.Add(AIAConstants.KEY_SLUG, dr["Slug"]);
                        userModulelist.Add(modulesHash);
                    }
                }
            }


            return userModulelist;
        }

        protected string GetUserTypeStringById(int userTypeId)
        {
            string userType = string.Empty;

            switch (userTypeId)
            {
                case (int)UserType.SUPER_ADMIN:
                    userType = User.SUPER_ADMIN;
                    break;
                case (int)UserType.GENERAL_ADMIN:
                    userType = User.GENERAL_ADMIN;
                    break;
                case (int)UserType.DISTRICT_ADMIN:
                    userType = User.DISTRICT_ADMIN;
                    break;
                case (int)UserType.CLIENT_ADMIN:
                    userType = User.CLIENT_ADMIN;
                    break;
                case (int)UserType.SINGLE_USER:
                    userType = User.SINGLE_USER;
                    break;
                case (int)UserType.CONCURRENT_USER:
                    userType = User.CONCURRENT_USER;
                    break;
                case (int)UserType.RESELLER:
                    userType = User.RESELLER;
                    break;
                case (int)UserType.TEST_ACCOUNT:
                    userType = User.TEST_ACCOUNT;
                    break;
                case (int)UserType.SITE_USER:
                    userType = User.SITE_USER;
                    break;
            }
            return userType;
        }

        internal static User GetUserDetailsByEmailId(string emailId)
        {
            logger.Debug(" Inside GetUserDetailsByEmailId for emailId = " + emailId);

            User objUser = null;
            DBModel objModel = new DBModel();

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter;
            SqlParameter param;
            DataSet ds = new DataSet();

            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_USER_DETAILS_BY_EMAILID;
            cmd.CommandType = CommandType.StoredProcedure;

            param = new SqlParameter("@sEmailId", emailId);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.String;
            cmd.Parameters.Add(param);

            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);


            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        int tempVal;
                        DateTime date;
                        objUser = new User();

                        objUser.Id = Convert.ToInt32(dr["Id"]);
                        objUser.FirstName = dr["FirstName"].ToString();
                        objUser.LastName = dr["LastName"].ToString();
                        objUser.EmailId = dr["EmailId"].ToString();
                        objUser.LoginId = dr["LoginId"].ToString();
                        objUser.Password = dr["Password"].ToString();
                        objUser.SecurityQuestionId = Int32.TryParse(dr["SecurityQuestionId"].ToString(), out tempVal) ? tempVal : (int?)null;
                        objUser.SecurityAnswer = dr["SecurityAnswer"].ToString(); ;
                        objUser.CreatorId = Int32.TryParse(dr["CreatorId"].ToString(), out tempVal) ? tempVal : (int?)null;
                        objUser.CreationDate = Convert.ToDateTime(dr["CreationDate"]);
                        objUser.DeactivationDate = DateTime.TryParse(dr["DeactivationDate"].ToString(), out date) ? date : (DateTime?)null;
                        objUser.ModifierId = Int32.TryParse(dr["ModifierId"].ToString(), out tempVal) ? tempVal : (int?)null;
                        objUser.ModifiedDate = DateTime.TryParse(dr["ModifiedDate"].ToString(), out date) ? date : (DateTime?)null;
                        objUser.UserTypeId = Convert.ToInt32(dr["UserTypeId"]);
                        objUser.UserType = objModel.GetUserTypeStringById(Convert.ToInt32(dr["UserTypeId"]));
                        objUser.IsActive = Convert.ToBoolean(dr["IsActive"]);
                    }
                }
            }

            return objUser;
        }

        internal static int UpdateUserPassword(dynamic userInfo, string loginId, string emailId)
        {
            logger.Debug(" Inside UpdateUserPassword for LoginId: " + loginId + ", EmailId: " + emailId);

            int result = 0;

            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.UPDATE_USER_PASSWORD;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@sLoginId", loginId);
                cmd.Parameters.AddWithValue("@sEmailId", emailId);
                cmd.Parameters.AddWithValue("@sNewPassword", userInfo["newPassword"].ToString());
                result = cmd.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                conn.Close();
                logger.Fatal("Exception in UpdateUserPassword for LoginId: " + loginId + ", EmailId: " + emailId + ", Exception= " + ex.Message + ", STACKTRACE= " + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }
            return result;
        }

        internal LicenseSubscriptionDetails GetLicenseSubscriptionDetailsByLicenseId(int licenseId)
        {
            logger.Debug(" Inside GetLicenseSubscriptionDetailsByLicenseId for LicenseId = " + licenseId);

            LicenseSubscriptionDetails licenseSubscriptionDetails = null;

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter;
            SqlParameter param;
            DataSet ds = new DataSet();

            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_SUBSCRIPTION_DETAILS_BY_LICENSE_ID;
            cmd.CommandType = CommandType.StoredProcedure;

            param = new SqlParameter("@iLicenseId", licenseId);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.Int32;
            cmd.Parameters.Add(param);

            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);

            if (ds != null && ds.Tables.Count > 0)
            {
                licenseSubscriptionDetails = new LicenseSubscriptionDetails();
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        int tempVal;
                        DateTime date;
                        licenseSubscriptionDetails.Id = Convert.ToInt32(dr["Id"]);
                        licenseSubscriptionDetails.LicenseId = Convert.ToInt32(dr["LicenseId"]);
                        licenseSubscriptionDetails.SubscriptionPlanId = Int32.TryParse(dr["SubscriptionPlanId"].ToString(), out tempVal) ? tempVal : (int?)null;
                        licenseSubscriptionDetails.SubscriptionValidFrom = DateTime.TryParse(dr["SubscriptionValidFrom"].ToString(), out date) ? date : (DateTime?)null;
                        licenseSubscriptionDetails.SubscriptionValidThrough = DateTime.TryParse(dr["SubscriptionValidThrough"].ToString(), out date) ? date : (DateTime?)null;
                        licenseSubscriptionDetails.RenewalDate = DateTime.TryParse(dr["RenewalDate"].ToString(), out date) ? date : (DateTime?)null;
                        licenseSubscriptionDetails.PaymentMode = dr["PaymentMode"].ToString();
                        licenseSubscriptionDetails.TotalAmount = Convert.ToDouble(dr["TotalAmount"]);
                        licenseSubscriptionDetails.AmountPaid = Convert.ToDouble(dr["AmountPaid"]);
                        licenseSubscriptionDetails.AmountPending = Convert.ToDouble(dr["AmountPending"]);
                        licenseSubscriptionDetails.NoOfImages = Convert.ToInt32(dr["NoofImages"]);
                    }
                }
            }



            return licenseSubscriptionDetails;
        }

        internal License GetLicenseDetailsByLicenseId(int licenseId)
        {
            logger.Debug(" inside GetLicenseDetailsByLicenseId for LicenseId = " + licenseId);

            License license = null;

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter;
            SqlParameter param;
            DataSet ds = new DataSet();

            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_LICENSE_DETAILS_BY_ID;
            cmd.CommandType = CommandType.StoredProcedure;

            param = new SqlParameter("@Id", licenseId);
            param.Direction = ParameterDirection.Input;
            param.DbType = DbType.Int32;
            cmd.Parameters.Add(param);

            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);

            if (ds != null && ds.Tables.Count > 0)
            {

                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    license = new License();

                    foreach (DataRow dr in dt.Rows)
                    {
                        DateTime date;
                        license.Id = Convert.ToInt32(dr["Id"]);
                        license.AccountNumber = dr["AccountNumber"].ToString();
                        license.LicenseeFirstName = dr["LicenseeFirstName"].ToString();
                        license.LicenseeLastName = dr["LicenseeLastName"].ToString();
                        license.LicenseTypeId = Convert.ToInt32(dr["LicenseTypeId"]);
                        license.InstitutionName = dr["InstitutionName"].ToString();
                        license.Address1 = dr["Address1"].ToString();
                        license.Address2 = dr["Address2"].ToString();
                        license.CountryId = Convert.ToInt32(dr["CountryId"]);
                        license.StateId = Convert.ToInt32(dr["StateId"]);
                        license.City = dr["City"].ToString();
                        license.Zip = dr["Zip"].ToString();
                        license.Phone = dr["Phone"].ToString();
                        license.EmailId = dr["EmailId"].ToString();
                        license.TotalLogins = Convert.ToInt32(dr["TotalLogins"]);
                        license.AccountTypeId = Convert.ToInt32(dr["AccountTypeId"]);
                        license.IsActive = Convert.ToBoolean(dr["IsActive"]);
                        license.IsDistrictSiteLicense = Convert.ToBoolean(dr["IsDistrictSiteLicense"]);
                        license.CreationDate = Convert.ToDateTime(dr["CreationDate"]);
                        license.ModifiedDate = DateTime.TryParse(dr["ModifiedDate"].ToString(), out date) ? date : (DateTime?)null;
                        license.NoOfRenewals = Convert.ToInt32(dr["NoOfRenewals"]);
                        license.IsTermAccepted = Convert.ToBoolean(dr["IsTermsAccepted"]);
                        license.ProductId = dr["ProductId"].ToString();
                    }
                }
            }



            return license;
        }

        internal static int UpdateLicenseTermStatus(string accountNumber)
        {
            logger.Debug(" inside UpdateLicenseTermStatus for AccountNumber = " + accountNumber);

            int result = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.UPDATE_LICENSE_TERM_STATUS;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@sAccountNumber", accountNumber);
                result = cmd.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in UpdateLicenseTermStatus for AccountNumber =" + accountNumber + ", Exception= " + ex.Message + ", STACKTRACE= " + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }
            return result;
        }

        internal static ArrayList GetTermsAndConditions()
        {
            logger.Debug(" inside GetTermsAndConditions");

            ArrayList arrTermsAndConditions = new ArrayList();


            Hashtable contentHash = null;
            string str = string.Empty;
            string spName = DBConstants.GET_TERMS_AND_CONDITIONS;
            DataSet ds = DBModel.GetDataFromStoredProcedure(spName);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        contentHash = new Hashtable();
                        contentHash.Add(AIAConstants.KEY_TITLE, dr["Title"]);
                        contentHash.Add(AIAConstants.KEY_CONTENT, dr["Content"]);
                        arrTermsAndConditions.Add(contentHash);
                    }
                }
            }


            return arrTermsAndConditions;
        }

        internal int InsertLoginDetails(int userId)
        {
            logger.Debug(" inside InsertLoginDetails for UserId= " + userId);

            int result = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.INSERT_LOGIN_DETAIL;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@iUserId", userId);
                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in InsertLoginDetails for UserId= " + userId + ", Exception= " + ex.Message + ", STACKTRACE=" + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }

            return result;
        }

        internal int InsertIncorrectLoginAttempts(int userId)
        {
            logger.Debug(" inside InsertIncorrectLoginAttempts for UserId= " + userId);

            int result = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.INSERT_INCORRECT_LOGIN_ATTEMPTS;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@iUserId", userId);
                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in InsertIncorrectLoginAttempts for UserId= " + userId + ", Exception= " + ex.Message + ", STACKTRACE= " + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }
            return result;
        }

        internal int GetIncorrectLoginAttempts(int userId)
        {
            logger.Debug(" inside GetIncorrectLoginAttempts for UserId = " + userId);

            int count = 0;

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_INCORRECT_LOGIN_ATTEMPTS;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@iUserId", userId);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            count = Convert.ToInt32(dr[dc]);
                        }
                    }
                }
            }


            return count;
        }

        internal int UpdateIncorrectLoginAttempts(int userId)
        {
            logger.Debug(" inside UpdateIncorrectLoginAttempts for UserId= " + userId);

            int result = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.UPDATE_INCORRECT_LOGIN_ATTEMPTS;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@iUserId", userId);
                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in UpdateIncorrectLoginAttempts for UserId= " + userId + ", Exception= " + ex.Message + ", STACKTRACE=" + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }
            return result;
        }

        public int DeleteIncorrectLoginAttempts(int userId)
        {
            logger.Debug(" inside DeleteIncorrectLoginAttempts for UserId= " + userId);

            int result = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.DELETE_INCORRECT_LOGIN_ATTEMPTS;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@iUserId", userId);
                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in DeleteIncorrectLoginAttempts for UserId= " + userId + ", Exception= " + ex.Message + ", STACKTRACE= " + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }
            return result;
        }

        protected ArrayList GetLoginFailureCauses()
        {
            logger.Debug(" Inside GetLoginFailureCauses");

            ArrayList failureCauseList = new ArrayList();

            Hashtable fcHash = null;

            string sp = DBConstants.GET_ALL_LOGIN_FAILURE_CAUSES;

            DataSet ds = DBModel.GetDataFromStoredProcedure(sp);

            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow drFailureCause in dt.Rows)
                    {
                        fcHash = new Hashtable();
                        fcHash.Add(AIAConstants.KEY_ID, drFailureCause["Id"]);
                        fcHash.Add(AIAConstants.KEY_DESCRIPTION, drFailureCause["Description"]);
                        failureCauseList.Add(fcHash);
                    }
                }
            }

            return failureCauseList;
        }

        internal int InsertUserLoginLog(string accountNumber, Int16 failureId, string referalUrl, string edition, string httpReferer)
        {
            logger.Debug(" inside InsertUserLoginLog for AccountNumber= " + accountNumber);

            int result = 0;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(dbConnectionString);
                SqlCommand cmd = new SqlCommand();
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = DBConstants.INSERT_LOGIN_ERROR_LOG;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@nvAccountNumber", accountNumber);
                cmd.Parameters.AddWithValue("@dtLogDate", DateTime.Now);
                cmd.Parameters.AddWithValue("@tiFailureId", failureId);
                cmd.Parameters.AddWithValue("@nvReferalUrl", referalUrl);
                cmd.Parameters.AddWithValue("@nvEdition", edition);
                cmd.Parameters.AddWithValue("@nvHttpReferer", httpReferer);
                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in InsertUserLoginLog for AccountNumber= " + accountNumber + ", Exception= " + ex.Message + ", STACKTRACE= " + ex.StackTrace);
                throw;
            }
            finally
            {
                conn.Dispose();
            }
            return result;
        }

        internal BlockedUser GetUserBlockedStatusByUserId(int userId)
        {
            logger.Debug(" inside GetUserBlockedStatusByUserId for UserId= " + userId);

            BlockedUser blockedUser = null;
            DataTable dt = null;

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_BLOCKED_USER_BY_USER_ID;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@userId", userId);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            dt = new DataTable();
            da.Fill(dt);

            if (dt != null && dt.Rows.Count > 0)
            {
                blockedUser = new BlockedUser();
                foreach (DataRow dr in dt.Rows)
                {
                    blockedUser.Id = Convert.ToInt32(dr["Id"]);
                    blockedUser.FirstName = dr["FirstName"].ToString();
                    blockedUser.LastName = dr["LastName"].ToString();
                    blockedUser.EmailId = dr["EmailId"].ToString();
                    blockedUser.LoginId = dr["LoginId"].ToString();
                    blockedUser.Password = dr["Password"].ToString();
                    blockedUser.AccountNumber = dr["AccountNumber"].ToString();
                    blockedUser.LoginTime = Convert.ToDateTime(dr["LoginTime"]);
                }
            }


            return blockedUser;
        }

        protected ArrayList GetBlockedUsersByUserType(int userTypeId)
        {
            logger.Debug(" inside GetBlockedUsersByUserType for UserTypeId= " + userTypeId);

            ArrayList blockedUsersList = new ArrayList();
            BlockedUser blockedUser = null;
            DataTable dt = null;

            SqlConnection conn = new SqlConnection(dbConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = DBConstants.GET_BLOCKED_USERS_BY_USER_TYPE;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@iUserTypeId", userTypeId);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    blockedUser = new BlockedUser();

                    blockedUser.Id = Convert.ToInt32(dr["Id"]);
                    blockedUser.FirstName = dr["FirstName"].ToString();
                    blockedUser.LastName = dr["LastName"].ToString();
                    blockedUser.EmailId = dr["EmailId"].ToString();
                    blockedUser.LoginId = dr["LoginId"].ToString();
                    blockedUser.Password = dr["Password"].ToString();
                    blockedUser.AccountNumber = dr["AccountNumber"].ToString();
                    blockedUser.LoginTime = Convert.ToDateTime(dr["LoginTime"]);

                    blockedUsersList.Add(blockedUser);
                }
            }



            return blockedUsersList;
        }

        internal static int UnblockUser(int userId)
        {
            int result = 0;
            DBModel objModel = new DBModel();

            result = objModel.DeleteIncorrectLoginAttempts(userId);

            return result;
        }

        internal static bool ValidateUserAuthenticity(string username, string password, User user)
        {
            logger.Debug(" Inside ValidateUserAuthenticity for Username = " + username + ", Password: " + password);

            bool result = false;

            try
            {
                if ((string.Equals(username.ToUpper(), user.LoginId.ToUpper())) && (string.Equals(password, user.Password)))
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (SqlException ex)
            {
                logger.Fatal("Exception in ValidateUserAuthenticity for Username = " + username + ", Password: " + password + ", Exception= " + ex.Message + ", STACKTRACE= " + ex.StackTrace);
                throw;
            }

            return result;
        }
    }
}