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

#pragma hdrstop

#include "CDSVmsDsplSchF.h"
#include "AppGlobalF.h"

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

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

//---------------------------------------------------------------------------
TCDSVmsDsplSchManager *CDSVmsDsplSchManager = NULL;
//---------------------------------------------------------------------------

/*
* Manager
*/
TCDSVmsDsplSchManager::TCDSVmsDsplSchManager()
{
}
//---------------------------------------------------------------------------
TCDSVmsDsplSchManager::~TCDSVmsDsplSchManager()
{
    Clear();
    FPhases.RemoveAll();
}
//---------------------------------------------------------------------------
void TCDSVmsDsplSchManager::Clear()
{
    FMaxPhase = 0;
    FOR_STL(TCDSVmsDsplSch*, pObj, FPhases)
    {
        pObj->FWeeks.RemoveAll();
    }
}
//---------------------------------------------------------------------------
bool TCDSVmsDsplSchManager::LoadFromDb(TADOConnection *ADbConn, String AVMS_CTLR_NMBR, String AVMS_SCH_TYPE)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.PHASE, B.WEEK_CD, B.SCH_HH         \r\n"
           "  FROM TB_VMS_DSPL_SCH      A,              \r\n"
           "       TB_VMS_DSPL_SCH_WEEK B               \r\n"
           " WHERE A.VMS_SCH_TYPE  = :p01               \r\n"
           "   AND A.VMS_CTLR_NMBR = :p02               \r\n"
           "   AND A.VMS_SCH_TYPE  = B.VMS_SCH_TYPE(+)  \r\n"
           "   AND A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR(+) \r\n"
           "   AND A.PHASE         = B.PHASE(+)         \r\n"
           " ORDER BY A.PHASE, B.WEEK_CD                \r\n";

    Clear();

    FPhases.Lock();
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            pADO->Parameters->ParamByName("p01")->Value = AVMS_SCH_TYPE;
            pADO->Parameters->ParamByName("p02")->Value = AVMS_CTLR_NMBR;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                int PHASE = pADO->FieldByName("PHASE")->AsInteger;
                TCDSVmsDsplSch *pObj = FPhases.Find(PHASE);
                if (!pObj)
                {
                    pObj = new TCDSVmsDsplSch();
                    pObj->PHASE = PHASE;
                    FPhases.Push(PHASE, pObj);
                }
                if (PHASE > FMaxPhase)
                {
                    FMaxPhase = PHASE;
                }

                String WEEK_CD = pADO->FieldByName("WEEK_CD")->AsString;
                if (WEEK_CD == "") continue;

				TCDSVmsDsplSchWeek *pWeek = pObj->FWeeks.Find(WEEK_CD);
                if (!pWeek)
                {
                    pWeek = new TCDSVmsDsplSchWeek();
                    pWeek->WEEK_CD = WEEK_CD;
                    pObj->FWeeks.Push(pWeek->WEEK_CD, pWeek);
                }
                pWeek->SCH_HH = pADO->FieldByName("SCH_HH")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TCDSVmsDsplSchManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TCDSVmsDsplSchManager::LoadFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        FOR_STL(TCDSVmsDsplSch*, pObj, FPhases)
        {
            for (int nWeek = 1; nWeek <= 7; nWeek++)
            {
				TCDSVmsDsplSchWeek *pWeek = pObj->FWeeks.Find(String(nWeek));
                if (!pWeek)
                {
                    pWeek = new TCDSVmsDsplSchWeek();
                    pWeek->WEEK_CD = String(nWeek);
                    pObj->FWeeks.Push(pWeek->WEEK_CD, pWeek);
                }
            }
        }

        FPhases.UnLock();
    }

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