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


#pragma hdrstop

#include "CDSVmsFormF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
void TCDSVmsForm::Init()
{
}
//---------------------------------------------------------------------------
TCDSVmsFormManager *CDSVmsFormManager = NULL;
//---------------------------------------------------------------------------

/*
* Manager
*/
TCDSVmsFormManager::TCDSVmsFormManager()
{
}
//---------------------------------------------------------------------------
TCDSVmsFormManager::~TCDSVmsFormManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TCDSVmsFormManager::Init()
{
    FOR_STL(TCDSVmsForm*, pObj, FLists)
    {
        pObj->Init();
    }
}
//---------------------------------------------------------------------------
bool TCDSVmsFormManager::LoadFromDb(TADOConnection *ADbConn)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT VMS_FORM_ID, VALID_YN    \r\n"
           "  FROM TB_VMS_FORM              \r\n";

    FLists.Lock();
    try
    {
        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())
            {
                String VMS_FORM_ID = pADO->FieldByName("VMS_FORM_ID")->AsString.Trim();
				TCDSVmsForm *pObj = FLists.Find(VMS_FORM_ID);
                if (!pObj)
                {
                    pObj = new TCDSVmsForm();

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

                pObj->VALID_YN = pADO->FieldByName("VALID_YN")->AsString.Trim();
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TCDSVmsFormManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TCDSVmsFormManager::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;
}
//---------------------------------------------------------------------------
void TCDSVmsFormManager::AddVmsForm(String AVMS_FORM_ID, String AVALID_YN)
{
    TCDSVmsForm *pObj = FLists.Find(AVMS_FORM_ID);
    if (!pObj)
    {
        pObj = new TCDSVmsForm();

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

    pObj->VALID_YN = AVALID_YN;
}
//---------------------------------------------------------------------------
bool TCDSVmsFormManager::IsValid(String AVMS_FORM_ID)
{
    TCDSVmsForm *pObj = FLists.Find(AVMS_FORM_ID);
    if (!pObj)
    {
        return false;
    }
    return pObj->VALID_YN == "Y" ? true : false;
}
//---------------------------------------------------------------------------

