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

#pragma hdrstop

#include "CDSVmsColorF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TCDSVmsColorManager *CDSVmsColorManager = NULL;
//---------------------------------------------------------------------------

/*
* Manager
*/
TCDSVmsColorManager::TCDSVmsColorManager()
{
}
//---------------------------------------------------------------------------
TCDSVmsColorManager::~TCDSVmsColorManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
int  TCDSVmsColorManager::GetNextColorId()
{
    FMaxID = 0;
    FOR_STL(TCDSVmsColor*, pObj, FLists)
    {
        if (pObj->VMS_FONT_COLR_CD > FMaxID)
        {
            FMaxID = pObj->VMS_FONT_COLR_CD;
        }
    }
    return FMaxID+1;
}
//---------------------------------------------------------------------------
bool TCDSVmsColorManager::LoadFromDb(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*                  \r\n"
           "  FROM TB_VMS_FONT_COLR A   \r\n";

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

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                int    VMS_FONT_COLR_CD  = pADO->FieldByName("VMS_FONT_COLR_CD")->AsInteger;
                String VMS_FONT_COLR_NM  = pADO->FieldByName("VMS_FONT_COLR_NM")->AsString.Trim();
                int    VMS_FONT_COLR_VAL = pADO->FieldByName("VMS_FONT_COLR_VAL")->AsInteger;
                String USE_YN            = pADO->FieldByName("USE_YN")->AsString.Trim();

                if (VMS_FONT_COLR_CD > FMaxID)
                {
                    FMaxID = VMS_FONT_COLR_CD;
                }
				TCDSVmsColor *pObj = FLists.Find(VMS_FONT_COLR_CD);
                if (!pObj)
                {
                    pObj = new TCDSVmsColor();

                    pObj->VMS_FONT_COLR_CD = VMS_FONT_COLR_CD;
                    FLists.Push(pObj->VMS_FONT_COLR_CD, pObj);
                }

                pObj->VMS_FONT_COLR_NM  = VMS_FONT_COLR_NM;
                pObj->VMS_FONT_COLR_VAL = VMS_FONT_COLR_VAL;
                pObj->USE_YN            = USE_YN;

                pObj->Completed = true;
                pObj->EditMode  = DB_INIT;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TCDSVmsColorManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TCDSVmsColorManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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