//---------------------------------------------------------------------------

#pragma hdrstop

#include "CDSWebUserF.h"
#include "ITS_OPLibF.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

TWebUser::TWebUser()
{
	FCompleted = false;
    FEditMode  = DB_INIT;
	FGridIndex = -1;
	MEM_DEL    = "N";
    //Clear();
}
//---------------------------------------------------------------------------
void TWebUser::Clear()
{
}
//---------------------------------------------------------------------------
TWebUser::~TWebUser()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TWebUserManager *WebUserManager = NULL;
//---------------------------------------------------------------------------

/*
* User Manager
*/
TWebUserManager::TWebUserManager()
{
}
//---------------------------------------------------------------------------
TWebUserManager::~TWebUserManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
bool TWebUserManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*, RGST_DT AS REG_DATE  \r\n"
           "  FROM TB_WEB_USER_INFR A        \r\n";

    FLists.Lock();
    try
    {
        FLists.RemoveAll();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TWebUser *pObj = new TWebUser();

                pObj->USER_ID           = pADO->FieldByName("USER_ID")->AsString;
                pObj->EMAIL             = pADO->FieldByName("EMAIL")->AsString.Trim();
                pObj->NAME              = pADO->FieldByName("NAME")->AsString.Trim();
                pObj->PWD               = pADO->FieldByName("PWD")->AsString.Trim();
                pObj->CONTACTNUM        = pADO->FieldByName("CONTACTNUM")->AsString.Trim();
                pObj->USERAUTH          = pADO->FieldByName("USERAUTH")->AsString.Trim();
                pObj->QUESTION          = pADO->FieldByName("QUESTION")->AsString.Trim();
                pObj->ANSWER            = pADO->FieldByName("ANSWER")->AsString.Trim();
                pObj->RGST_DT           = pADO->FieldByName("RGST_DT")->AsString.Trim();
                pObj->LOGIN_FAIL_COUNT  = pADO->FieldByName("LOGIN_FAIL_COUNT")->AsString.Trim();
                pObj->IS_ACCOUNT_LOCK   = pADO->FieldByName("IS_ACCOUNT_LOCK")->AsString.Trim();
                pObj->IP_ADDRESS        = pADO->FieldByName("IP_ADDRESS")->AsString.Trim();
                pObj->DEL_YN            = pADO->FieldByName("DEL_YN")->AsString.Trim();

                if (pObj->IS_ACCOUNT_LOCK == "N")
                {
                    pObj->IS_ACCOUNT_LOCK_NM = "[N] Un Lock";
                }
                else
                {
                    pObj->IS_ACCOUNT_LOCK_NM = "[Y] Locked";
                }

				FLists.Push(pObj->USER_ID, pObj);

                pObj->Completed = true;
                pObj->MEM_DEL   = "N";
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TWebUserManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TWebUserManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
bool TWebUserManager::GetNextUserId(int &AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(USER_ID), 0) + 1 AS NEWID FROM TB_WEB_USER_INFR \r\n";

    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            AId = pADO->FieldByName("NEWID")->AsInteger;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWebUserManager::GetNextUserId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWebUserManager::GetNextUserId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

