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

#pragma hdrstop

#include "CDSWCamCtlrF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)


//---------------------------------------------------------------------------
TWCamCtlr::TWCamCtlr()
{
}
//---------------------------------------------------------------------------

TWCamCtlr::~TWCamCtlr()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TWCamCtlrManager *WCamCtlrManager = NULL;
//---------------------------------------------------------------------------

TWCamCtlrManager::TWCamCtlrManager()
{
    FImgStream = NULL;
}
//---------------------------------------------------------------------------
TWCamCtlrManager::~TWCamCtlrManager()
{
    FLists.RemoveAll();
    if (FImgStream) delete FImgStream;
    FImgStream = NULL;
}
//---------------------------------------------------------------------------
bool TWCamCtlrManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*,                                                             \r\n"
           "       scp.dec_b64('DAMO','SCP','ARIA_256.SCPS',A.CMRA_PSWD) AS PWD,    \r\n"
           "       A.WCAM_TYPE_CD,                                                  \r\n"
           "       (SELECT CMMN_CD_KOR_NM                                           \r\n"
           "          FROM TB_CMMN_CD                                               \r\n"
           "         WHERE CMMN_CLSF_CD = 'WCAM'                                    \r\n"
           "           AND CMMN_CD      = A.WCAM_TYPE_CD) AS WCAM_TYPE_NM           \r\n"
           "  FROM TB_WCAM_CTLR A                                                   \r\n";
           " WHERE A.DEL_YN = 'N'                                                   \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())
            {
				TWCamCtlr *pObj = new TWCamCtlr();

                pObj->WCAM_CTLR_NMBR        = pADO->FieldByName("WCAM_CTLR_NMBR")->AsString;
                pObj->WCAM_CTLR_ID          = pADO->FieldByName("WCAM_CTLR_ID")->AsString.Trim();
                pObj->WCAM_NM               = pADO->FieldByName("WCAM_NM")->AsString.Trim();
                pObj->WCAM_CTLR_IP          = pADO->FieldByName("WCAM_CTLR_IP")->AsString.Trim();
                pObj->WCAM_CTLR_PORT        = pADO->FieldByName("WCAM_CTLR_PORT")->AsInteger;
                pObj->WCAM_CTLR_LOCAL_NO    = pADO->FieldByName("WCAM_CTLR_LOCAL_NO")->AsInteger;
                pObj->WCAM_TYPE_CD          = pADO->FieldByName("WCAM_TYPE_CD")->AsString.Trim();
                pObj->ISTL_LCTN_NM          = pADO->FieldByName("ISTL_LCTN_NM")->AsString.Trim();
                pObj->X_CRDN                = pADO->FieldByName("X_CRDN")->AsFloat;
                pObj->Y_CRDN                = pADO->FieldByName("Y_CRDN")->AsFloat;
                pObj->DEL_YN                = pADO->FieldByName("DEL_YN")->AsString;
                pObj->STRM_SVR_IP           = pADO->FieldByName("STRM_SVR_IP")->AsString.Trim();
                pObj->STRM_SVR_PORT         = pADO->FieldByName("STRM_SVR_PORT")->AsString.Trim();
                pObj->STRM_SESN_NM          = pADO->FieldByName("STRM_SESN_NM")->AsString.Trim();
                pObj->STRM_RTSP_ADDR        = pADO->FieldByName("STRM_RTSP_ADDR")->AsString.Trim();
                pObj->STRM_RTMP_ADDR        = pADO->FieldByName("STRM_RTMP_ADDR")->AsString.Trim();
                pObj->STRM_HTTP_ADDR        = pADO->FieldByName("STRM_HTTP_ADDR")->AsString.Trim();
                pObj->STRM_STOR_ADDR        = pADO->FieldByName("STRM_STOR_ADDR")->AsString.Trim();
                pObj->CMRA_PORT             = pADO->FieldByName("CMRA_PORT")->AsInteger;
                pObj->CMRA_ID               = pADO->FieldByName("CMRA_ID")->AsString.Trim();
                //pObj->CMRA_PSWD             = pADO->FieldByName("CMRA_PSWD")->AsString.Trim();
                pObj->CMRA_PSWD             = pADO->FieldByName("PWD")->AsString.Trim();
                pObj->CMRA_CHNL             = pADO->FieldByName("CMRA_CHNL")->AsInteger;
                pObj->LINK_ID               = pADO->FieldByName("LINK_ID")->AsString.Trim();
                pObj->ISTL_LCTN_ADDR        = pADO->FieldByName("ISTL_LCTN_ADDR")->AsString.Trim();
                pObj->FULL_STRM_ADDR        = pADO->FieldByName("FULL_STRM_SESN_NM")->AsString.Trim();
                pObj->WCAM_TYPE_NM          = pADO->FieldByName("WCAM_TYPE_NM")->AsString.Trim();

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

    return LoadPreSet(ADbConn);
}
//---------------------------------------------------------------------------

bool TWCamCtlrManager::LoadStatusFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *                                                        \r\n"
           "  FROM TB_WCAM_CTLR_STTS A,                                     \r\n"
           "       TB_WCAM_CTLR      B                                      \r\n"
           " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR                      \r\n"
           "   AND A.UPDT_DT >= TO_CHAR(SYSDATE-2/1440, 'YYYYMMDDHH24MISS') \r\n";

    FLists.Lock();
    try
    {
        FOR_STL(TWCamCtlr*, pTmp, FLists)
        {
            pTmp->STATE.Comm = wcam_error;
        }
        TWCamCtlr *pObj = NULL;
        try
        {
            int nORD;

            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())
            {
                String WCAM_CTLR_NMBR = pADO->FieldByName("WCAM_CTLR_NMBR")->AsString;
                pObj = FLists.Find(WCAM_CTLR_NMBR);
                if (!pObj) continue;

                String UPDT_DT      = pADO->FieldByName("UPDT_DT")->AsString;
                String CMNC_STTS_CD = pADO->FieldByName("CMNC_STTS_CD")->AsString;
                if (CMNC_STTS_CD != "CMS0")
                {
                    //Åë½ÅÀå¾Ö
                    continue;
                }
                pObj->STATE.Comm = (CMNC_STTS_CD == "CMS0") ? wcam_normal : wcam_error;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::LoadStatusFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::LoadStatusFromDb", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        FLists.UnLock();

        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::DeleteWCam(TWCamCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_WCAM_CTLR          \r\n"
           "   SET DEL_YN = 'Y'          \r\n"
           " WHERE WCAM_CTLR_NMBR = :p01 \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->Parameters->ParamByName("p01")->Value = AObj->WCAM_CTLR_NMBR;
			pADO->ExecSQL();

        sQry = "DELETE TB_FCLT_INFR      \r\n"
               " WHERE FCLT_ID = :p01    \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->WCAM_CTLR_ID;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::DeleteWCam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::DeleteWCam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::InsertWCam(TWCamCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeWCam(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TWCamCtlrManager::UpdateWCam(TWCamCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    return MergeWCam(AObj, ADbConn);
}
//---------------------------------------------------------------------------

bool TWCamCtlrManager::MergeWCam(TWCamCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_WCAM_CTLR L                                   \r\n"
           "USING (SELECT :p01 AS WCAM_CTLR_NMBR,                       \r\n"
           "              :p02 AS WCAM_CTLR_ID,                         \r\n"
           "              :p03 AS WCAM_NM,                              \r\n"
           "              :p04 AS WCAM_CTLR_IP,                         \r\n"
           "              :p05 AS WCAM_CTLR_PORT,                       \r\n"
           "              :p06 AS WCAM_CTLR_LOCAL_NO,                   \r\n"
           "              :p07 AS WCAM_TYPE_CD,                         \r\n"
           "              :p08 AS ISTL_LCTN_NM,                         \r\n"
           "              :p09 AS X_CRDN,                               \r\n"
           "              :p10 AS Y_CRDN,                               \r\n"
           "              :p11 AS DEL_YN,                               \r\n"
           "              :p12 AS STRM_SVR_IP,                          \r\n"
           "              :p13 AS STRM_SVR_PORT,                        \r\n"
           "              :p14 AS STRM_SESN_NM,                         \r\n"
           "              :p15 AS STRM_RTSP_ADDR,                       \r\n"
           "              :p16 AS STRM_RTMP_ADDR,                       \r\n"
           "              :p17 AS STRM_HTTP_ADDR,                       \r\n"
           "              :p18 AS STRM_STOR_ADDR,                       \r\n"
           "              :p19 AS CMRA_PORT,                            \r\n"
           "              :p20 AS CMRA_ID,                              \r\n"
           "              :p21 AS CMRA_PSWD,                            \r\n"
           "              :p22 AS CMRA_CHNL,                            \r\n"
           "              :p23 AS LINK_ID,                              \r\n"
           "              :p24 AS ISTL_LCTN_ADDR                        \r\n"
           "         FROM DUAL) M                                       \r\n"
           "        ON (L.WCAM_CTLR_NMBR = M.WCAM_CTLR_NMBR)            \r\n"
           "WHEN MATCHED THEN                                           \r\n"
           "   UPDATE SET L.WCAM_CTLR_ID        = M.WCAM_CTLR_ID,       \r\n"
           "              L.WCAM_NM             = M.WCAM_NM,            \r\n"
           "              L.WCAM_CTLR_IP        = M.WCAM_CTLR_IP,       \r\n"
           "              L.WCAM_CTLR_PORT      = M.WCAM_CTLR_PORT,     \r\n"
           "              L.WCAM_CTLR_LOCAL_NO  = M.WCAM_CTLR_LOCAL_NO, \r\n"
           "              L.WCAM_TYPE_CD        = M.WCAM_TYPE_CD,       \r\n"
           "              L.ISTL_LCTN_NM        = M.ISTL_LCTN_NM,       \r\n"
           "              L.X_CRDN              = M.X_CRDN,             \r\n"
           "              L.Y_CRDN              = M.Y_CRDN,             \r\n"
           "              L.DEL_YN              = M.DEL_YN,             \r\n"
           "              L.STRM_SVR_IP         = M.STRM_SVR_IP,        \r\n"
           "              L.STRM_SVR_PORT       = M.STRM_SVR_PORT,      \r\n"
           "              L.STRM_SESN_NM        = M.STRM_SESN_NM,       \r\n"
           "              L.STRM_RTSP_ADDR      = M.STRM_RTSP_ADDR,     \r\n"
           "              L.STRM_RTMP_ADDR      = M.STRM_RTMP_ADDR,     \r\n"
           "              L.STRM_HTTP_ADDR      = M.STRM_HTTP_ADDR,     \r\n"
           "              L.STRM_STOR_ADDR      = M.STRM_STOR_ADDR,     \r\n"
           "              L.CMRA_PORT           = M.CMRA_PORT,          \r\n"
           "              L.CMRA_ID             = M.CMRA_ID,            \r\n"
           "              L.CMRA_PSWD           = M.CMRA_PSWD,          \r\n"
           "              L.CMRA_CHNL           = M.CMRA_CHNL,          \r\n"
           "              L.LINK_ID             = M.LINK_ID,            \r\n"
           "              L.ISTL_LCTN_ADDR      = M.ISTL_LCTN_ADDR      \r\n"
           "WHEN NOT MATCHED THEN                                       \r\n"
           "   INSERT (WCAM_CTLR_NMBR,                                  \r\n"
           "           WCAM_CTLR_ID,                                    \r\n"
           "           WCAM_NM,                                         \r\n"
           "           WCAM_CTLR_IP,                                    \r\n"
           "           WCAM_CTLR_PORT,                                  \r\n"
           "           WCAM_CTLR_LOCAL_NO,                              \r\n"
           "           WCAM_TYPE_CD,                                    \r\n"
           "           ISTL_LCTN_NM,                                    \r\n"
           "           X_CRDN,                                          \r\n"
           "           Y_CRDN,                                          \r\n"
           "           DEL_YN,                                          \r\n"
           "           STRM_SVR_IP,                                     \r\n"
           "           STRM_SVR_PORT,                                   \r\n"
           "           STRM_SESN_NM,                                    \r\n"
           "           STRM_RTSP_ADDR,                                  \r\n"
           "           STRM_RTMP_ADDR,                                  \r\n"
           "           STRM_HTTP_ADDR,                                  \r\n"
           "           STRM_STOR_ADDR,                                  \r\n"
           "           CMRA_PORT,                                       \r\n"
           "           CMRA_ID,                                         \r\n"
           "           CMRA_PSWD,                                       \r\n"
           "           CMRA_CHNL,                                       \r\n"
           "           LINK_ID,                                         \r\n"
           "           ISTL_LCTN_ADDR )                                 \r\n"
           "   VALUES (M.WCAM_CTLR_NMBR,                                \r\n"
           "           M.WCAM_CTLR_ID,                                  \r\n"
           "           M.WCAM_NM,                                       \r\n"
           "           M.WCAM_CTLR_IP,                                  \r\n"
           "           M.WCAM_CTLR_PORT,                                \r\n"
           "           M.WCAM_CTLR_LOCAL_NO,                            \r\n"
           "           M.WCAM_TYPE_CD,                                  \r\n"
           "           M.ISTL_LCTN_NM,                                  \r\n"
           "           M.X_CRDN,                                        \r\n"
           "           M.Y_CRDN,                                        \r\n"
           "           M.DEL_YN,                                        \r\n"
           "           M.STRM_SVR_IP,                                   \r\n"
           "           M.STRM_SVR_PORT,                                 \r\n"
           "           M.STRM_SESN_NM,                                  \r\n"
           "           M.STRM_RTSP_ADDR,                                \r\n"
           "           M.STRM_RTMP_ADDR,                                \r\n"
           "           M.STRM_HTTP_ADDR,                                \r\n"
           "           M.STRM_STOR_ADDR,                                \r\n"
           "           M.CMRA_PORT,                                     \r\n"
           "           M.CMRA_ID,                                       \r\n"
           "           M.CMRA_PSWD,                                     \r\n"
           "           M.CMRA_CHNL,                                     \r\n"
           "           M.LINK_ID,                                       \r\n"
           "           M.ISTL_LCTN_ADDR )                               \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->Parameters->ParamByName("p01")->Value = AObj->WCAM_CTLR_NMBR;
            pADO->Parameters->ParamByName("p02")->Value = AObj->WCAM_CTLR_ID;
            pADO->Parameters->ParamByName("p03")->Value = AObj->WCAM_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->WCAM_CTLR_IP;
            pADO->Parameters->ParamByName("p05")->Value = AObj->WCAM_CTLR_PORT;
            pADO->Parameters->ParamByName("p06")->Value = AObj->WCAM_CTLR_LOCAL_NO;
            pADO->Parameters->ParamByName("p07")->Value = AObj->WCAM_TYPE_CD;
            pADO->Parameters->ParamByName("p08")->Value = AObj->ISTL_LCTN_NM;
            pADO->Parameters->ParamByName("p09")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p10")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p11")->Value = AObj->DEL_YN;
            pADO->Parameters->ParamByName("p12")->Value = AObj->STRM_SVR_IP;
            pADO->Parameters->ParamByName("p13")->Value = AObj->STRM_SVR_PORT;
            pADO->Parameters->ParamByName("p14")->Value = AObj->STRM_SESN_NM;
            pADO->Parameters->ParamByName("p15")->Value = AObj->STRM_RTSP_ADDR;
            pADO->Parameters->ParamByName("p16")->Value = AObj->STRM_RTMP_ADDR;
            pADO->Parameters->ParamByName("p17")->Value = AObj->STRM_HTTP_ADDR;
            pADO->Parameters->ParamByName("p18")->Value = AObj->STRM_STOR_ADDR;
            pADO->Parameters->ParamByName("p19")->Value = AObj->CMRA_PORT;
            pADO->Parameters->ParamByName("p20")->Value = AObj->CMRA_ID;
            pADO->Parameters->ParamByName("p21")->Value = AObj->CMRA_PSWD;
            pADO->Parameters->ParamByName("p22")->Value = AObj->CMRA_CHNL;
            pADO->Parameters->ParamByName("p23")->Value = AObj->LINK_ID;
            pADO->Parameters->ParamByName("p24")->Value = AObj->ISTL_LCTN_ADDR;

			pADO->ExecSQL();

            return MergeFcltInfo(AObj, ADbConn);
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::MergeWCam", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::MergeWCam", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::MergeFcltInfo(TWCamCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_FCLT_INFR L                                         \r\n"
           "USING (SELECT :p01 AS FCLT_ID,                                    \r\n"
           "              :p02 AS FCLT_TYPE,                                  \r\n"
           "              :p03 AS FCLT_LCTN                                   \r\n"
           "         FROM DUAL) M                                             \r\n"
           "        ON (L.FCLT_ID = M.FCLT_ID AND L.FCLT_TYPE = M.FCLT_TYPE)  \r\n"
           "WHEN MATCHED                                                      \r\n"
           "THEN                                                              \r\n"
           "   UPDATE SET L.FCLT_LCTN = M.FCLT_LCTN,                          \r\n"
           "              L.DEL_YN    = 'N'                                   \r\n"
           "WHEN NOT MATCHED                                                  \r\n"
           "THEN                                                              \r\n"
           "   INSERT (  FCLT_ID,   FCLT_TYPE,   FCLT_LCTN)                   \r\n"
           "   VALUES (M.FCLT_ID, M.FCLT_TYPE, M.FCLT_LCTN)                   \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->Parameters->ParamByName("p01")->Value = AObj->WCAM_CTLR_ID;
			pADO->Parameters->ParamByName("p02")->Value = "WCAM";
			pADO->Parameters->ParamByName("p03")->Value = AObj->WCAM_NM;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::MergeFcltInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::MergeFcltInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::GetNextWCamId(int &ANmbr, int &AId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT NVL(MAX(WCAM_CTLR_NMBR), 10000) + 1 AS NEWID FROM TB_WCAM_CTLR \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();
            ANmbr = pADO->FieldByName("NEWID")->AsInteger;
            AId = ANmbr / 10000;
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::GetNextWCamId", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::GetNextWCamId", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TWCamCtlrManager::LoadPreSet(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    FLists.Lock();
    try
    {
        FOR_STL(TWCamCtlr*, pObj, FLists)
        {
            pObj->FLists.RemoveAll();
        }
    }
    __finally
    {
        FLists.UnLock();
    }

    sQry = "SELECT B.*                                 \r\n"
           "  FROM TB_WCAM_CTLR A,                     \r\n"
           "       TB_WCAM_PSET B                      \r\n"
           " WHERE A.WCAM_CTLR_NMBR = B.WCAM_CTLR_NMBR \r\n"
           " ORDER BY B.WCAM_CTLR_NMBR, B.PSET_NMBR    \r\n";
    try
    {
        String WCAM_CTLR_NMBR;

        FLists.Lock();
        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())
            {
                WCAM_CTLR_NMBR = pADO->FieldByName("WCAM_CTLR_NMBR")->AsString;
				TWCamCtlr *pWCam = FLists.Find(String(WCAM_CTLR_NMBR));
                if (!pWCam) continue;

                TWCamPset *pObj = new TWCamPset();
                if (pObj)
                {
                    pObj->WCAM_CTLR_NMBR = WCAM_CTLR_NMBR;
                    pObj->PSET_NMBR      = pADO->FieldByName("PSET_NMBR")->AsInteger;
                    pObj->PSET_NM        = pADO->FieldByName("PSET_NM")->AsString;
                    pObj->PAN            = pADO->FieldByName("PAN")->AsInteger;
                    pObj->TILT           = pADO->FieldByName("TILT")->AsInteger;
                    pObj->ZOOM           = pADO->FieldByName("ZOOM")->AsInteger;
                    pObj->FOCS           = pADO->FieldByName("FOCS")->AsInteger;
                    pObj->ANGLE          = pADO->FieldByName("ANGLE")->AsInteger;
                    pObj->DFLT_YN        = pADO->FieldByName("DFLT_YN")->AsString;

                    pObj->DEL_YN         = "N";

                    pWCam->FLists.Push(pObj->PSET_NMBR, pObj);
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TWCamCtlrManager::LoadPreSet", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TWCamCtlrManager::LoadPreSet", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

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

bool TWCamCtlrManager::SavePreSet(TWCamCtlr *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "DELETE FROM TB_WCAM_PSET WHERE WCAM_CTLR_NMBR = :p01";
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
			pADO->Parameters->ParamByName("p01")->Value = AObj->WCAM_CTLR_NMBR;
			pADO->ExecSQL();

    sQry = "INSERT INTO TB_WCAM_PSET (  \r\n"
           "            WCAM_CTLR_NMBR, \r\n"
           "            PSET_NMBR,      \r\n"
           "            PSET_NM,        \r\n"
           "            PAN,            \r\n"
           "            TILT,           \r\n"
           "            ZOOM,           \r\n"
           "            FOCS,           \r\n"
           "            ANGLE,          \r\n"
           "            DFLT_YN         \r\n"
           "   ) VALUES (               \r\n"
           "           :p01,            \r\n"
           "           :p02,            \r\n"
           "           :p03,            \r\n"
           "           :p04,            \r\n"
           "           :p05,            \r\n"
           "           :p06,            \r\n"
           "           :p07,            \r\n"
           "           :p08,            \r\n"
           "           :p09             \r\n"
           "      )                     \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            FOR_STL(TWCamPset *, pObj, AObj->FLists)
            {
                if (pObj->DEL_YN != "N") continue;

                pADO->Parameters->ParamByName("p01")->Value = pObj->WCAM_CTLR_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = pObj->PSET_NMBR;
                pADO->Parameters->ParamByName("p03")->Value = pObj->PSET_NM;
                pADO->Parameters->ParamByName("p04")->Value = pObj->PAN;
                pADO->Parameters->ParamByName("p05")->Value = pObj->TILT;
                pADO->Parameters->ParamByName("p06")->Value = pObj->ZOOM;
                pADO->Parameters->ParamByName("p07")->Value = pObj->FOCS;
                pADO->Parameters->ParamByName("p08")->Value = pObj->ANGLE;
                pADO->Parameters->ParamByName("p09")->Value = pObj->DFLT_YN;

    			pADO->ExecSQL();
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::SavePreSet", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::SavePreSet", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::DeletePreSet(TWCamCtlr *AObj, int APsetNmbr, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    TWCamPset* pObj = AObj->FLists.Find(APsetNmbr);
    if (!pObj) return false;

    sQry = "DELETE FROM TB_WCAM_PSET WHERE WCAM_CTLR_NMBR = :p01 AND PSET_NMBR = :p02";

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

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Parameters->ParamByName("p01")->Value = AObj->WCAM_CTLR_NMBR;
			pADO->Parameters->ParamByName("p02")->Value = APsetNmbr;

			pADO->ExecSQL();

            pObj->DEL_YN = "Y";

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::DeletePreSet", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::DeletePreSet", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::MergePreSet(TWCamCtlr *AObj, TWCamPset* APset, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "MERGE INTO TB_WCAM_PSET L                           \r\n"
           "USING (SELECT :p01 AS WCAM_CTLR_NMBR,               \r\n"
           "              :p02 AS PSET_NMBR,                    \r\n"
           "              :p03 AS PSET_NM,                      \r\n"
           "              :p04 AS PAN,                          \r\n"
           "              :p05 AS TILT,                         \r\n"
           "              :p06 AS ZOOM,                         \r\n"
           "              :p07 AS FOCS,                         \r\n"
           "              :p08 AS ANGLE,                        \r\n"
           "              :p09 AS DFLT_YN                       \r\n"
           "         FROM DUAL) M                               \r\n"
           "        ON (L.WCAM_CTLR_NMBR = M.WCAM_CTLR_NMBR)    \r\n"
           "WHEN MATCHED THEN                                   \r\n"
           "   UPDATE SET L.PSET_NMBR   = M.PSET_NMBR,          \r\n"
           "              L.PSET_NM     = M.PSET_NM,            \r\n"
           "              L.PAN         = M.PAN,                \r\n"
           "              L.TILT        = M.TILT,               \r\n"
           "              L.ZOOM        = M.ZOOM,               \r\n"
           "              L.FOCS        = M.FOCS,               \r\n"
           "              L.ANGLE       = M.ANGLE,              \r\n"
           "              L.DFLT_YN     = M.DFLT_YN             \r\n"
           "WHEN NOT MATCHED THEN                               \r\n"
           "   INSERT (WCAM_CTLR_NMBR,                          \r\n"
           "           PSET_NMBR,                               \r\n"
           "           PSET_NM,                                 \r\n"
           "           PAN,                                     \r\n"
           "           TILT,                                    \r\n"
           "           ZOOM,                                    \r\n"
           "           FOCS,                                    \r\n"
           "           ANGLE,                                   \r\n"
           "           DFLT_YN )                                \r\n"
           "   VALUES (M.WCAM_CTLR_NMBR,                        \r\n"
           "           M.PSET_NMBR,                             \r\n"
           "           M.PSET_NM,                               \r\n"
           "           M.PAN,                                   \r\n"
           "           M.TILT,                                  \r\n"
           "           M.ZOOM,                                  \r\n"
           "           M.FOCS,                                  \r\n"
           "           M.ANGLE,                                 \r\n"
           "           M.DFLT_YN )                              \r\n";

			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;
            FOR_STL(TWCamPset *, pObj, AObj->FLists)
            {
                if (pObj->DEL_YN != "N") continue;

                pADO->Parameters->ParamByName("p01")->Value = APset->WCAM_CTLR_NMBR;
                pADO->Parameters->ParamByName("p02")->Value = APset->PSET_NMBR;
                pADO->Parameters->ParamByName("p03")->Value = APset->PSET_NM;
                pADO->Parameters->ParamByName("p04")->Value = APset->PAN;
                pADO->Parameters->ParamByName("p05")->Value = APset->TILT;
                pADO->Parameters->ParamByName("p06")->Value = APset->ZOOM;
                pADO->Parameters->ParamByName("p07")->Value = APset->FOCS;
                pADO->Parameters->ParamByName("p08")->Value = APset->ANGLE;
                pADO->Parameters->ParamByName("p09")->Value = APset->DFLT_YN;

    			pADO->ExecSQL();

                TWCamPset* pObj = AObj->FLists.Find(APset->PSET_NMBR);
                if (pObj)
                {
                    pObj->WCAM_CTLR_NMBR = APset->WCAM_CTLR_NMBR;
                    pObj->PSET_NMBR      = APset->PSET_NMBR;
                    pObj->PSET_NM        = APset->PSET_NM;
                    pObj->PAN            = APset->PAN;
                    pObj->TILT           = APset->TILT;
                    pObj->ZOOM           = APset->ZOOM;
                    pObj->FOCS           = APset->FOCS;
                    pObj->ANGLE          = APset->ANGLE;
                    pObj->DFLT_YN        = APset->DFLT_YN;
                }
                else
                {
                    AObj->FLists.Push(APset->PSET_NMBR, APset);
                }
            }
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::SavePreSet", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::SavePreSet", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

bool TWCamCtlrManager::InsWCamControlHs(String ACtlDt, String ACctvNmbr, String ACtlType, String ACtlVal, String ACtlRes, String AUserId, TADOConnection *ADbConn/*=NULL*/)
{
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_WCAM_CTRL_HS(               \r\n"
           "                          CTRL_DT,         \r\n"
           "                          WCAM_CTLR_NMBR,  \r\n"
           "                          CTRL_TYPE,       \r\n"
           "                          CTRL_VAL,        \r\n"
           "                          CTRL_RESULT,     \r\n"
           "                          USER_ID          \r\n"
           "                         )                 \r\n"
           "                   VALUES(                 \r\n"
           "                          :p01,            \r\n"
           "                          :p02,            \r\n"
           "                          :p03,            \r\n"
           "                          :p04,            \r\n"
           "                          :p05,            \r\n"
           "                          :p06             \r\n"
           "                         )                 \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->Parameters->ParamByName("p01")->Value = ACtlDt;
            pADO->Parameters->ParamByName("p02")->Value = ACctvNmbr;
            pADO->Parameters->ParamByName("p03")->Value = ACtlType;
            pADO->Parameters->ParamByName("p04")->Value = ACtlVal;
            pADO->Parameters->ParamByName("p05")->Value = ACtlRes;
            pADO->Parameters->ParamByName("p06")->Value = AUserId;
            pADO->ExecSQL();
            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORLOG("TWCamCtlrManager::InsWCamControlHs", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORLOG("TWCamCtlrManager::InsWCamControlHs", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

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

