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

#pragma hdrstop

#include "CDSCodeF.h"
#include "ITS_OPLibF.h"

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

#pragma package(smart_init)

TItsSubCode::TItsSubCode()
{
    TItsSubCode("", "");
}
//---------------------------------------------------------------------------
TItsSubCode::TItsSubCode(String ACd, String AClsfCd)
{
    CMMN_CD      = ACd;
    CMMN_CLSF_CD = AClsfCd;
    Clear();
    FModified = false;
    FCompleted= false;
}
//---------------------------------------------------------------------------
void TItsSubCode::Clear()
{
}
//---------------------------------------------------------------------------
TItsSubCode::~TItsSubCode()
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
TItsCode::TItsCode()
{
    TItsCode("");
}
//---------------------------------------------------------------------------
TItsCode::TItsCode(String AClsfCd)
{
    CMMN_CLSF_CD = AClsfCd;
    Clear();
    FModified = false;
    FCompleted= false;
}
//---------------------------------------------------------------------------
void TItsCode::Clear()
{
}
//---------------------------------------------------------------------------
TItsCode::~TItsCode()
{
    FSubLists.RemoveAll();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsCodeManager *ItsCodeManager = NULL;
//---------------------------------------------------------------------------

/*
* Link Manager
*/
TItsCodeManager::TItsCodeManager()
{
}
//---------------------------------------------------------------------------
TItsCodeManager::~TItsCodeManager()
{
}
//---------------------------------------------------------------------------
bool TItsCodeManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    FLists.RemoveAll();

    return (LoadCode(ADbConn) && LoadSubCode(ADbConn));
}
//---------------------------------------------------------------------------
bool TItsCodeManager::LoadCode(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT CMMN_CLSF_CD      ,          \r\n"
           "       MSG_TYPE_CD       ,          \r\n"
           "       CMMN_CLSF_KOR_NM  ,          \r\n"
           "       CMMN_CLSF_ENGL_NM ,          \r\n"
           "       USE_SYST_CD       ,          \r\n"
           "       RMRK              ,          \r\n"
           "       USE_YN                       \r\n"
           "  FROM TB_CMMN_CLSF_CD              \r\n"
           " WHERE USE_YN = 'Y'                 \r\n";
//           " ORDER BY MSG_TYPE_CD, CMMN_CLSF_CD \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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
				TItsCode *pCode = new TItsCode();

                pCode->CMMN_CLSF_CD      = pADO->FieldByName("CMMN_CLSF_CD")->AsString;        //'공통 분류 코드';
                pCode->MSG_TYPE_CD       = pADO->FieldByName("MSG_TYPE_CD")->AsString;         //''메시지 유형 코드';
                pCode->CMMN_CLSF_KOR_NM  = pADO->FieldByName("CMMN_CLSF_KOR_NM")->AsString;    //''공통 분류 한글 명';
                pCode->CMMN_CLSF_ENGL_NM = pADO->FieldByName("CMMN_CLSF_ENGL_NM")->AsString;   //''공통 분류 영문 명';
                pCode->USE_SYST_CD       = pADO->FieldByName("USE_SYST_CD")->AsString;         //''사용 시스템 코드';
                pCode->RMRK              = pADO->FieldByName("RMRK")->AsString;                //''비고';
                pCode->USE_YN            = pADO->FieldByName("USE_YN")->AsString;              //''사용 여부';

				FLists.Push(pCode->CMMN_CLSF_CD, pCode);
                pCode->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsCodeManager::LoadCode", 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("TItsCodeManager::LoadCode", 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 TItsCodeManager::LoadSubCode(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT CMMN_CD         ,        \r\n"
           "       CMMN_CLSF_CD    ,        \r\n"
           "       CMMN_CD_KOR_NM  ,        \r\n"
           "       CMMN_CD_ENGL_NM ,        \r\n"
           "       CMMN_CD_ABBR    ,        \r\n"
           "       RMRK            ,        \r\n"
           "       ATRB1           ,        \r\n"
           "       ATRB2           ,        \r\n"
           "       USE_YN                   \r\n"
           "  FROM TB_CMMN_CD               \r\n"
           " WHERE USE_YN = 'Y'             \r\n";
//           " ORDER BY CMMN_CLSF_CD, CMMN_CD \r\n";

    FLists.Lock();
    try
    {
        TItsCode *pCode = NULL;

        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();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sClsfCd = pADO->FieldByName("CMMN_CLSF_CD")->AsString;
                pCode = ItsCodeManager->FLists.Find(sClsfCd);
                if (!pCode) continue;

				TItsSubCode *pSubCode = new TItsSubCode();

                pSubCode->CMMN_CD          = pADO->FieldByName("CMMN_CD")->AsString;
                pSubCode->CMMN_CLSF_CD     = pADO->FieldByName("CMMN_CLSF_CD")->AsString;
                pSubCode->CMMN_CD_KOR_NM   = pADO->FieldByName("CMMN_CD_KOR_NM")->AsString;
                pSubCode->CMMN_CD_ENGL_NM  = pADO->FieldByName("CMMN_CD_ENGL_NM")->AsString;
                pSubCode->CMMN_CD_ABBR     = pADO->FieldByName("CMMN_CD_ABBR")->AsString;
                pSubCode->RMRK             = pADO->FieldByName("RMRK")->AsString;
                pSubCode->ATRB1            = pADO->FieldByName("ATRB1")->AsString;
                pSubCode->ATRB2            = pADO->FieldByName("ATRB2")->AsString;
                pSubCode->USE_YN           = pADO->FieldByName("USE_YN")->AsString;

                pCode->FSubLists.Push(pSubCode->CMMN_CD, pSubCode);
                pSubCode->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsCodeManager::LoadSubCode", 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("TItsCodeManager::LoadSubCode", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------

