LoginHelper.cs 9.06 KB

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using ADAM.AIAHTML5.API.Models;
using System.Data;
using ADAM.AIAHTML5.API.Constants;
using log4net;
using ADAM.AIA50.BusinessLayer.Utility;

namespace ADAM.AIAHTML5.API.Helper
{
    public class LoginHelper
    {

        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
        internal AccountTransactInfo buildAdminTransactLicenseInfos(AccountTransactInfo accountTransactInfo)
        {
            accountTransactInfo.licenseEditionId = EditionHelper.GetInstance().GetHighestPriortyEdition().ToString();
            accountTransactInfo.licenseTermCondition = Convert.ToInt16(1).ToString();
            accountTransactInfo.licenseModesty = "true";
            accountTransactInfo.licenseTypeId = "5";//TEST
            accountTransactInfo.licenseId = "-1";
            return accountTransactInfo;
        }
        
        internal AccountTransactInfo buildAccountTransactLicenseInfos(int userId, AccountTransactInfo accountTransactInfo ,User userInfo)
        {
            Hashtable objLicEdition = null;

            objLicEdition = this.GetUserLicenceEditionDetail(userId,userInfo);

            accountTransactInfo.accountNumber = objLicEdition[LoginConst.ACCOUNT_NUMBER].ToString();
            accountTransactInfo.licenseTypeId = objLicEdition[LoginConst.LICENSE_TYPE_ID].ToString();
            accountTransactInfo.licenseEditionIsActive = Convert.ToBoolean(objLicEdition[LoginConst.LICENSE_ACTIVE]).ToString();
            accountTransactInfo.licenseId = Convert.ToInt32(objLicEdition[LoginConst.LICENSE_ID]).ToString();
            accountTransactInfo.licenseEditionId = Convert.ToByte(objLicEdition[LoginConst.EDITION_ID]).ToString();
            accountTransactInfo.licenseTotalLogin = Convert.ToUInt16(objLicEdition[LoginConst.TOTAL_LOGIN]).ToString();
            accountTransactInfo.licenseModesty = Convert.ToBoolean(objLicEdition[LoginConst.MODESTY_SETTING]).ToString();

            CheckLicenseStatus(userInfo);

            accountTransactInfo.licenseIsExpired = userInfo.IsSubscriptionExpired;
            accountTransactInfo.licenseExpirationDate = userInfo.SubscriptionExpirationDate;
            bool r = (bool)objLicEdition[LoginConst.TERM_CONDITION];
            accountTransactInfo.licenseTermCondition = Convert.ToInt16(r).ToString();
            return accountTransactInfo;
        }

        private static void CheckLicenseStatus(User userInfo)
        {
            //05.1 For normal user need to get the license details, get the license id for authenticated user 
            //int licenseId, editionId;
            //AIAHTML5.API.Models.Users.getLicenseIdForThisUser(userInfo.Id, out licenseId, out editionId);

            //userInfo.LicenseId = licenseId;
            //userInfo.EditionId = editionId;

            //05.2 get license details
            userInfo.LicenseInfo = AIAHTML5.API.Models.Users.getLicenseDetails(userInfo.LicenseId);

            if (userInfo.LicenseInfo != null)
            {
                //05.3 get licenseSubscription details
                userInfo.LicenseSubscriptions = AIAHTML5.API.Models.Users.getLicenseSubscriptionDetails(userInfo.LicenseId);

                //05.4 check the License expiration irespective of either user is active or not because on AIA
                //we shows the License expiration message for inactive users too
                string expirationDate = null;
                bool isLicenseExpired = false;

                if (userInfo.LicenseSubscriptions != null)
                {
                    isLicenseExpired = AIAHTML5.API.Models.Users.checkIfLicenseExpired(userInfo.LicenseSubscriptions, out expirationDate);
                }

                if (isLicenseExpired)
                {
                    userInfo.IsSubscriptionExpired = isLicenseExpired;
                    userInfo.SubscriptionExpirationDate = expirationDate;
                }
            }

            else
            {
                logger.Debug("userInfo.LicenseInfo  is null for userInfo.LicenseId= " + userInfo.LicenseId);
            }
        }

        internal AccountTransactInfo buildAccountTransactUserInfos(AccountTransactInfo accountTrasactInfo, String providedPassword, String expectedPassword, User userInfo)
        {
            accountTrasactInfo.userId = userInfo.Id.ToString();
            accountTrasactInfo.providedPassword = providedPassword;
            accountTrasactInfo.expectedPassword = expectedPassword;
            accountTrasactInfo.userTypeId = userInfo.UserTypeId.ToString();
            accountTrasactInfo.userCreatorId = userInfo.CreatorId.ToString();
            try
            {

                if(userInfo.DeactivationDate!= null)
                {
                   accountTrasactInfo.userDeactivationDate =Convert.ToString(userInfo.DeactivationDate);
                }
            }
            catch(Exception ex) 
            {
                logger.Error("exception at DeactivationDate on buildAccountTransactUserInfos method for user id: "+ accountTrasactInfo.userId); 
            }

            accountTrasactInfo.userIsActive = userInfo.IsActive.ToString();
            accountTrasactInfo.userLoginId = userInfo.LoginId.ToString();
            return accountTrasactInfo;
        }

        internal AccountTransactInfo buildAccountTransactConcurrentUserInfos(AccountTransactInfo accountTrasactInfo, String providedPassword, String expectedPassword, ADAM.AIAHTML5.API.Models.User userInfo)
        {
            buildAccountTransactUserInfos(accountTrasactInfo, providedPassword, expectedPassword, userInfo);

            //AMI IMP tHING TO BE NOTICE
            accountTrasactInfo.usersLoggedIn = (string)SessionManager.GetInstance().CountSession(int.Parse(accountTrasactInfo.licenseId), byte.Parse(accountTrasactInfo.licenseEditionId)).ToString();

            return accountTrasactInfo;
        }

        // get user licence and edition detail
        private Hashtable GetUserLicenceEditionDetail(Int32 UserId,User userInfo)
        {            // TO DO: Check follwing functionlity can break in small function  
            Hashtable objHashUserLicDetail = new Hashtable();
            // Get detail from User 2 Licence Edition table
             DataTable dtUser2Lic = DBModel.getAIALicenseEditionDetails(UserId);

            if (dtUser2Lic.Rows.Count == 0)
            {
                //intErrorID = ErrorHelper.E_USER_NOT_MAP_TO_LICENCE_EDITION;
                //errors.Add(ErrorHelper.E_USER_NOT_MAP_TO_LICENCE_EDITION);

                //AMI TO DO
                userInfo.LoginFailureCauseId = ErrorHelper.E_USER_NOT_MAP_TO_LICENCE_EDITION;
                return objHashUserLicDetail;
            }

            //AdminDAL.AdminDal.AIAUserToLicenseEditionRow rowLicEd = (AdminDAL.AdminDal.AIAUserToLicenseEditionRow)dtUser2Lic.Rows[0];
            Int32 intLicenceEditionId = Convert.ToInt32(dtUser2Lic.Rows[0]["LicenseEditionId"]);

            //// Get detail form License 2 Editions table 
            DataTable dtLic2Edition = DBModel.getLicenseEditionById(intLicenceEditionId);

            if (dtLic2Edition.Rows.Count == 0)
            {
                //intErrorID = ErrorHelper.E_NO_ROW_FOUND_LICENCE_TO_EDITION_TABLE;

                userInfo.LoginFailureCauseId = ErrorHelper.E_NO_ROW_FOUND_LICENCE_TO_EDITION_TABLE;
                return objHashUserLicDetail;
            }


            //AdminDAL.AdminDal.LicenseToEditionRow rowLic2Edition = (AdminDAL.AdminDal.LicenseToEditionRow)dtLic2Edition.Rows[0];
            
            Int32 licenseId = Convert.ToInt32(dtLic2Edition.Rows[0]["LicenseId"]);

            //// get detail from licence tables 
            //LicenseTableAdapter adpLicence = new LicenseTableAdapter();
            //AdminDAL.AdminDal.LicenseDataTable dtLicence = new AdminDAL.AdminDal.LicenseDataTable();
            
            // usp_GetLicenseByLicenseId
            DataTable dtLicence = DBModel.getLicenseByLicenseId(licenseId);
            
            if (dtLicence.Rows.Count == 0)
            {
                userInfo.LoginFailureCauseId = ErrorHelper.E_NO_ROW_FOUND_LICENCE_TABLE;
                return objHashUserLicDetail;
            }

            //AdminDAL.AdminDal.LicenseRow rowLicence = (AdminDAL.AdminDal.LicenseRow)dtLicence.Rows[0];
            objHashUserLicDetail.Add(LoginConst.LICENSE_ID, dtLicence.Rows[0]["Id"]);
            objHashUserLicDetail.Add(LoginConst.LICENSE_ACTIVE, dtLicence.Rows[0]["IsActive"]);
            objHashUserLicDetail.Add(LoginConst.LICENSE_TYPE_ID, dtLicence.Rows[0]["LicenseTypeId"]);
            objHashUserLicDetail.Add(LoginConst.ACCOUNT_NUMBER, dtLicence.Rows[0]["AccountNumber"]);
            objHashUserLicDetail.Add(LoginConst.TOTAL_LOGIN, dtLicence.Rows[0]["TotalLogins"]);
            objHashUserLicDetail.Add(LoginConst.EDITION_ID, dtLicence.Rows[0]["EditionId"]);
            objHashUserLicDetail.Add(LoginConst.TERM_CONDITION, dtLicence.Rows[0]["IsTermsAccepted"]);
            objHashUserLicDetail.Add(LoginConst.MODESTY_SETTING, dtLicence.Rows[0]["IsModesty"]);
            return objHashUserLicDetail;
        }

    }
}