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

#pragma hdrstop

#include "CDSIncidentF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsIncident::TItsIncident()
{
    FMarker = NULL;

    Clear();
}
//---------------------------------------------------------------------------
void TItsIncident::Clear()
{
}
//---------------------------------------------------------------------------
TItsIncident::~TItsIncident()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsIncidentManager *ItsIncidentManager = NULL;
//---------------------------------------------------------------------------

/*
* Incident Manager
*/
TItsIncidentManager::TItsIncidentManager()
{
}
//---------------------------------------------------------------------------
TItsIncidentManager::~TItsIncidentManager()
{
}
//---------------------------------------------------------------------------
bool TItsIncidentManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*, DECODE(B.INCD_OCRR_ID, NULL, 'N', 'Y') AS DISP_INCD \r\n"
           "  FROM TB_INCD_OCRR A, VW_INCD_OCRR B                           \r\n"
           " WHERE A.INCD_OCRR_ID = B.INCD_OCRR_ID(+)                       \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())
            {
				TItsIncident *pObj = new TItsIncident();

                pObj->INCD_OCRR_ID          = pADO->FieldByName("INCD_OCRR_ID")->AsString;
                pObj->RGST_DT               = pADO->FieldByName("RGST_DT")->AsString;
                pObj->INCD_ORGN_CD          = pADO->FieldByName("INCD_ORGN_CD")->AsString;
                pObj->OCRR_LCTN_NM          = pADO->FieldByName("OCRR_LCTN_NM")->AsString;
                pObj->X_CRDN                = pADO->FieldByName("X_CRDN")->AsString;
                pObj->Y_CRDN                = pADO->FieldByName("Y_CRDN")->AsString;
                pObj->INCD_EXPL             = pADO->FieldByName("INCD_EXPL")->AsString;
                pObj->CMTR_GRAD_CD          = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                pObj->IFMR_NM               = pADO->FieldByName("IFMR_NM")->AsString;
                pObj->INCD_CNFM_MTHD_CD     = pADO->FieldByName("INCD_CNFM_MTHD_CD")->AsString;
                pObj->INCD_CNFM_DT          = pADO->FieldByName("INCD_CNFM_DT")->AsString;
                pObj->INCD_STRT_DT          = pADO->FieldByName("INCD_STRT_DT")->AsString;
                pObj->INCD_END_PRAR_DT      = pADO->FieldByName("INCD_END_PRAR_DT")->AsString;
                pObj->INCD_TYPE_CLSF_CD     = pADO->FieldByName("INCD_TYPE_CLSF_CD")->AsString;
                pObj->INCD_TYPE_CD          = pADO->FieldByName("INCD_TYPE_CD")->AsString;
                pObj->INCD_TITL             = pADO->FieldByName("INCD_TITL")->AsString;
                pObj->INCD_SERN_CD          = pADO->FieldByName("INCD_SERN_CD")->AsString;
                pObj->DELY_LNGT             = pADO->FieldByName("DELY_LNGT")->AsString;
                pObj->INCD_LNGT             = pADO->FieldByName("INCD_LNGT")->AsString;
                pObj->INCD_OCRR_LANE        = pADO->FieldByName("INCD_OCRR_LANE")->AsString;
                if (pObj->INCD_OCRR_LANE.IsEmpty())
                {
                    pObj->INCD_OCRR_LANE = "000000";
                }
                else
                if (pObj->INCD_OCRR_LANE.Length() != 6)
                {
                    pObj->INCD_OCRR_LANE = "000000";
                }
                pObj->INCD_CLSR_LANE        = pADO->FieldByName("INCD_CLSR_LANE")->AsString;
                pObj->TRVL_LMNT_TYPE_CD     = pADO->FieldByName("TRVL_LMNT_TYPE_CD")->AsString;
                pObj->TRUB_VHCL_YN          = pADO->FieldByName("TRUB_VHCL_YN")->AsString;
                pObj->FALT_YN               = pADO->FieldByName("FALT_YN")->AsString;
                pObj->FRWT_YN               = pADO->FieldByName("FRWT_YN")->AsString;
                pObj->UTIS_CNCT_YN          = pADO->FieldByName("UTIS_CNCT_YN")->AsString;
                pObj->SMLT_VHCL_UNUM        = pADO->FieldByName("SMLT_VHCL_UNUM")->AsString;
                pObj->MDLT_VHCL_UNUM        = pADO->FieldByName("MDLT_VHCL_UNUM")->AsString;
                pObj->INJR_PNUM             = pADO->FieldByName("INJR_PNUM")->AsString;
                pObj->FTLT_PNUM             = pADO->FieldByName("FTLT_PNUM")->AsString;
                pObj->WTHR_CD               = pADO->FieldByName("WTHR_CD")->AsString;
                pObj->RESP_LVL_CD           = pADO->FieldByName("RESP_LVL_CD")->AsString;
                pObj->VMS_DSPL_YN           = pADO->FieldByName("VMS_DSPL_YN")->AsString;
                pObj->INCD_PRGR_STEP_CD     = pADO->FieldByName("INCD_PRGR_STEP_CD")->AsString;
                pObj->INCD_END_CNFM_MTHD_CD = pADO->FieldByName("INCD_END_CNFM_MTHD_CD")->AsString;
                pObj->INCD_END_DT           = pADO->FieldByName("INCD_END_DT")->AsString;
                pObj->MBLZ_EQPM_CONT        = pADO->FieldByName("MBLZ_EQPM_CONT")->AsString;
                pObj->MBLZ_PNUM             = pADO->FieldByName("MBLZ_PNUM")->AsString;
                pObj->RECR_CONT             = pADO->FieldByName("RECR_CONT")->AsString;
                pObj->RECR_PERD             = pADO->FieldByName("RECR_PERD")->AsString;
                pObj->LINK_ID               = pADO->FieldByName("LINK_ID")->AsString;

                pObj->DISP_INCD              = pADO->FieldByName("DISP_INCD")->AsString;

				FLists.Push(pObj->INCD_OCRR_ID, pObj);
                pObj->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsIncidentManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            return false;
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsIncidentManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            return false;
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return bResult;
}
//---------------------------------------------------------------------------

bool TItsIncidentManager::LoadFromAutoIncident(TADOConnection *ADbConn/*=NULL*/)
{
return true;

    bool bResult = true;
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT A.LINK_ID, A.DETC_DT, A.CLCT_SPED_AVRG, A.ESTM_SPED_AVRG,  \r\n"
           "       A.REAL_YN, B.X_CRDN, B.Y_CRDN, C.ROAD_NAME                 \r\n"
           "  FROM TB_INCD_ESTM_SECT A, TB_LINK_VRTX B, TB_LINK C             \r\n"
           " WHERE A.LINK_ID = B.LINK_ID                                      \r\n"
           "   AND A.LINK_ID = C.LINK_ID                                      \r\n"
           "   AND B.ORD = '1'                                                \r\n";

#else
    sQry = "SELECT A.LINK_ID, A.DETC_DT, A.CLCT_SPED_AVRG, A.ESTM_SPED_AVRG,        \r\n"
           "       A.REAL_YN, B.X_CRDN, B.Y_CRDN, C.ROAD_NAME,                      \r\n"
           "       B.NODE_NAME AS STNAME, D.NODE_NAME AS EDNAME                     \r\n"
           "  FROM TB_INCD_ESTM_SECT A, TB_NODE B, TB_LINK C, TB_NODE D             \r\n"
           " WHERE A.LINK_ID = C.LINK_ID                                            \r\n"
           "   AND C.F_NODE_ID = B.NODE_ID                                          \r\n"
           "   AND C.T_NODE_ID = D.NODE_ID                                          \r\n"
           "   AND A.DETC_DT >= TO_CHAR(SYSDATE - (2*60)/60/24, 'YYYYMMDDHH24MISS') \r\n"
           "   AND A.REAL_YN = 'N'                                                  \r\n";
#endif

    FAutoLists.Lock();
    try
    {
        FAutoIncident = 0;
        FAutoLists.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())
            {
				TItsAutoIncident *pObj = new TItsAutoIncident();

                pObj->LINK_ID        = pADO->FieldByName("LINK_ID")->AsString;
                pObj->DETC_DT        = pADO->FieldByName("DETC_DT")->AsString;
                pObj->CLCT_SPED_AVRG = pADO->FieldByName("CLCT_SPED_AVRG")->AsString;
                pObj->ESTM_SPED_AVRG = pADO->FieldByName("ESTM_SPED_AVRG")->AsString;
                pObj->REAL_YN        = pADO->FieldByName("REAL_YN")->AsString;
                pObj->X_CRDN         = pADO->FieldByName("X_CRDN")->AsString;
                pObj->Y_CRDN         = pADO->FieldByName("Y_CRDN")->AsString;
                pObj->ROAD_NAME      = pADO->FieldByName("ROAD_NAME")->AsString;
                pObj->STNAME         = pADO->FieldByName("STNAME")->AsString;
                pObj->EDNAME         = pADO->FieldByName("EDNAME")->AsString;

				FAutoLists.Push(String(FAutoIncident++), pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsIncidentManager::LoadFromAutoIncident", String(E.ClassName()), E.Message, sQry);
            return false;
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORLOG("TItsIncidentManager::LoadFromAutoIncident", String(exception.ClassName()), exception.Message, sQry);
            return false;
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FAutoLists.UnLock();
    }

    return bResult;
}
//---------------------------------------------------------------------------

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

    sQry = "SELECT * FROM TB_INCD_OCRR_VMS \r\n";

    FVmsLists.Lock();
    try
    {
        FVmsLists.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())
            {
				TItsVmsIncident *pObj = new TItsVmsIncident();

                pObj->INCD_OCRR_ID          = pADO->FieldByName("INCD_OCRR_ID")->AsString;
                pObj->STRT_LCTN_NM          = pADO->FieldByName("STRT_LCTN_NM")->AsString;
                pObj->END_LCTN_NM           = pADO->FieldByName("END_LCTN_NM")->AsString;
                pObj->OCRR_LCTN_NM          = pADO->FieldByName("OCRR_LCTN_NM")->AsString;
                pObj->CMTR_GRAD_CD          = pADO->FieldByName("CMTR_GRAD_CD")->AsString;
                pObj->INCD_CLSR_LANE        = pADO->FieldByName("INCD_CLSR_LANE")->AsString;
                pObj->VMS_DSPL_YN           = pADO->FieldByName("VMS_DSPL_YN")->AsString;
                pObj->INCD_PRGR_STEP_CD     = pADO->FieldByName("INCD_PRGR_STEP_CD")->AsString;
                pObj->INCD_STRT_DT          = pADO->FieldByName("INCD_STRT_DT")->AsString;
                pObj->INCD_END_PRAR_DT      = pADO->FieldByName("INCD_END_PRAR_DT")->AsString;
                pObj->LINK_ID               = pADO->FieldByName("LINK_ID")->AsString;
                pObj->VMS_INCD_TYPE_CD      = pADO->FieldByName("VMS_INCD_TYPE_CD")->AsString;
                pObj->VMS_INCD_DETL_TYPE_CD = pADO->FieldByName("VMS_INCD_DETL_TYPE_CD")->AsString;
                pObj->VMS_INCD_RSTR_TYPE_CD = pADO->FieldByName("VMS_INCD_RSTR_TYPE_CD")->AsString;

				FVmsLists.Push(pObj->INCD_OCRR_ID, pObj);
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::LoadFromVmsIncident", 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("TItsIncidentManager::LoadFromVmsIncident", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FVmsLists.UnLock();
    }

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

bool TItsIncidentManager::UpdateIncidentInfo(TItsIncident *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_INCD_OCRR                  \r\n"
           "   SET INCD_ORGN_CD          = :p02, \r\n"
           "       OCRR_LCTN_NM          = :p03, \r\n"
           "       X_CRDN                = :p04, \r\n"
           "       Y_CRDN                = :p05, \r\n"
           "       INCD_EXPL             = :p06, \r\n"
           "       CMTR_GRAD_CD          = :p07, \r\n"
           "       IFMR_NM               = :p08, \r\n"
           "       INCD_CNFM_MTHD_CD     = :p09, \r\n"
           "       INCD_CNFM_DT          = :p10, \r\n"
           "       INCD_STRT_DT          = :p11, \r\n"
           "       INCD_END_PRAR_DT      = :p12, \r\n"
           "       INCD_TYPE_CLSF_CD     = :p13, \r\n"
           "       INCD_TYPE_CD          = :p14, \r\n"
           "       INCD_TITL             = :p15, \r\n"
           "       INCD_SERN_CD          = :p16, \r\n"
           "       DELY_LNGT             = :p17, \r\n"
           "       INCD_LNGT             = :p18, \r\n"
           "       INCD_OCRR_LANE        = :p19, \r\n"
           "       INCD_CLSR_LANE        = :p20, \r\n"
           "       TRVL_LMNT_TYPE_CD     = :p21, \r\n"
           "       TRUB_VHCL_YN          = :p22, \r\n"
           "       FALT_YN               = :p23, \r\n"
           "       FRWT_YN               = :p24, \r\n"
           "       UTIS_CNCT_YN          = :p25, \r\n"
           "       SMLT_VHCL_UNUM        = :p26, \r\n"
           "       MDLT_VHCL_UNUM        = :p27, \r\n"
           "       INJR_PNUM             = :p28, \r\n"
           "       FTLT_PNUM             = :p29, \r\n"
           "       WTHR_CD               = :p30, \r\n"
           "       RESP_LVL_CD           = :p31, \r\n"
           "       VMS_DSPL_YN           = :p32, \r\n"
           "       INCD_PRGR_STEP_CD     = :p33, \r\n"
           "       INCD_END_CNFM_MTHD_CD = :p34, \r\n"
           "       INCD_END_DT           = :p35, \r\n"
           "       MBLZ_EQPM_CONT        = :p36, \r\n"
           "       MBLZ_PNUM             = :p37, \r\n"
           "       RECR_CONT             = :p38, \r\n"
           "       RECR_PERD             = :p39, \r\n"
           "       LINK_ID               = :p40  \r\n"
           " WHERE INCD_OCRR_ID = :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->INCD_OCRR_ID;
            pADO->Parameters->ParamByName("p02")->Value = AObj->INCD_ORGN_CD;
            pADO->Parameters->ParamByName("p03")->Value = AObj->OCRR_LCTN_NM;
            pADO->Parameters->ParamByName("p04")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p05")->Value = AObj->Y_CRDN;
            pADO->Parameters->ParamByName("p06")->Value = AObj->INCD_EXPL;
            pADO->Parameters->ParamByName("p07")->Value = AObj->CMTR_GRAD_CD;
            pADO->Parameters->ParamByName("p08")->Value = AObj->IFMR_NM;
            pADO->Parameters->ParamByName("p09")->Value = AObj->INCD_CNFM_MTHD_CD;
            pADO->Parameters->ParamByName("p10")->Value = AObj->INCD_CNFM_DT;
            pADO->Parameters->ParamByName("p11")->Value = AObj->INCD_STRT_DT;
            pADO->Parameters->ParamByName("p12")->Value = AObj->INCD_END_PRAR_DT;
            pADO->Parameters->ParamByName("p13")->Value = AObj->INCD_TYPE_CLSF_CD;
            pADO->Parameters->ParamByName("p14")->Value = AObj->INCD_TYPE_CD;
            pADO->Parameters->ParamByName("p15")->Value = AObj->INCD_TITL;
            pADO->Parameters->ParamByName("p16")->Value = AObj->INCD_SERN_CD;
            pADO->Parameters->ParamByName("p17")->Value = AObj->DELY_LNGT;
            pADO->Parameters->ParamByName("p18")->Value = AObj->INCD_LNGT;
            pADO->Parameters->ParamByName("p19")->Value = AObj->INCD_OCRR_LANE;
            pADO->Parameters->ParamByName("p20")->Value = AObj->INCD_CLSR_LANE;
            pADO->Parameters->ParamByName("p21")->Value = AObj->TRVL_LMNT_TYPE_CD;
            pADO->Parameters->ParamByName("p22")->Value = AObj->TRUB_VHCL_YN;
            pADO->Parameters->ParamByName("p23")->Value = AObj->FALT_YN;
            pADO->Parameters->ParamByName("p24")->Value = AObj->FRWT_YN;
            pADO->Parameters->ParamByName("p25")->Value = AObj->UTIS_CNCT_YN;
            pADO->Parameters->ParamByName("p26")->Value = AObj->SMLT_VHCL_UNUM;
            pADO->Parameters->ParamByName("p27")->Value = AObj->MDLT_VHCL_UNUM;
            pADO->Parameters->ParamByName("p28")->Value = AObj->INJR_PNUM;
            pADO->Parameters->ParamByName("p29")->Value = AObj->FTLT_PNUM;
            pADO->Parameters->ParamByName("p30")->Value = AObj->WTHR_CD;
            pADO->Parameters->ParamByName("p31")->Value = AObj->RESP_LVL_CD;
            pADO->Parameters->ParamByName("p32")->Value = AObj->VMS_DSPL_YN;
            pADO->Parameters->ParamByName("p33")->Value = AObj->INCD_PRGR_STEP_CD;
            pADO->Parameters->ParamByName("p34")->Value = AObj->INCD_END_CNFM_MTHD_CD;
            pADO->Parameters->ParamByName("p35")->Value = AObj->INCD_END_DT;
            pADO->Parameters->ParamByName("p36")->Value = AObj->MBLZ_EQPM_CONT;
            pADO->Parameters->ParamByName("p37")->Value = AObj->MBLZ_PNUM;
            pADO->Parameters->ParamByName("p38")->Value = AObj->RECR_CONT;
            pADO->Parameters->ParamByName("p39")->Value = AObj->RECR_PERD;
            pADO->Parameters->ParamByName("p40")->Value = AObj->LINK_ID;

			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::UpdateIncidentInfo", 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("TItsIncidentManager::UpdateIncidentInfo", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        UpdateIncidentLoc(AObj, ADbConn);
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsIncidentManager::DeleteIncidentLoc(String AIncidId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;


    sQry = "DELETE ROTA.INCIDENT_LOC  \r\n"
           " WHERE INCIDENTID = :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 = AIncidId;
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::DeleteIncidentLoc", 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("TItsIncidentManager::DeleteIncidentLoc", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------


bool TItsIncidentManager::UpdateIncidentLoc(TItsIncident *AObj, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    //공단에서 넘어온 돌발은 공단연계가 표출안되게
    if (AObj->INCD_ORGN_CD == "IST5")
    {
        return true;
    }

    //1. 기존 정보가 어떤것인지 확인하기 귀찮으니까 일단 삭제해 버리자.
    DeleteIncidentLoc(AObj->INCD_OCRR_ID, ADbConn);

    //2. 공단으로 정보를 연계하는 경우에만 정보를 입력하도록 한다.
    if (AObj->UTIS_CNCT_YN != "Y") return true;

    sQry = "INSERT INTO ROTA.INCIDENT_LOC ( \r\n"
           "            INCIDENTID,         \r\n"
           "            LOGDATE,            \r\n"
           "            REPORTDATE,         \r\n"
           "            LINKID,             \r\n"
           "            USERID,             \r\n"
           "            REPORTERID,         \r\n"
           "            STARTDATE,          \r\n"
           "            ENDDATE,            \r\n"
           "            TRAFFICGRADE,       \r\n"
           "            TROUBLEGRADE,       \r\n"
           "            INCIDENTTITLE,      \r\n"
           "            INCIDENTINFO,       \r\n"
           "            INCIDENTCODE,       \r\n"
           "            INCIDENTSUBCODE,    \r\n"
           "            LOCATION,           \r\n"
           "            DATARESTYPE,        \r\n"
           "            COORDX,             \r\n"
           "            COORDY )            \r\n"
           "    VALUES (:p01,               \r\n"
           "            TO_DATE(:p02, 'YYYYMMDDHH24MISS'), \r\n"
           "            TO_DATE(:p03, 'YYYYMMDDHH24MISS'), \r\n"
           "            :p04,               \r\n"
           "            :p05,               \r\n"
           "            :p06,               \r\n"
           "            TO_DATE(:p07, 'YYYYMMDDHH24MISS'), \r\n"
           "            TO_DATE(:p08, 'YYYYMMDDHH24MISS'), \r\n"
           "            :p09,               \r\n"
           "            :p10,               \r\n"
           "            :p11,               \r\n"
           "            :p12,               \r\n"
           "            :p13,               \r\n"
           "            :p14,               \r\n"
           "            :p15,               \r\n"
           "            :p16,               \r\n"
           "            :p17,               \r\n"
           "            :p18)               \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->INCD_OCRR_ID;
            pADO->Parameters->ParamByName("p02")->Value = AObj->RGST_DT;
            pADO->Parameters->ParamByName("p03")->Value = AObj->INCD_CNFM_DT;
            pADO->Parameters->ParamByName("p04")->Value = AObj->LINK_ID;
            pADO->Parameters->ParamByName("p05")->Value = AObj->INCD_OCRR_ID.SubString(1, 3);
            pADO->Parameters->ParamByName("p06")->Value = AObj->INCD_OCRR_ID.SubString(1, 3);
            pADO->Parameters->ParamByName("p07")->Value = AObj->INCD_STRT_DT;
            pADO->Parameters->ParamByName("p08")->Value = AObj->INCD_END_PRAR_DT;
            pADO->Parameters->ParamByName("p09")->Value = AObj->CMTR_GRAD_CD.SubString(1, 2);
            pADO->Parameters->ParamByName("p10")->Value = AObj->INCD_SERN_CD;
            pADO->Parameters->ParamByName("p11")->Value = AObj->INCD_TITL.Trim();
            pADO->Parameters->ParamByName("p12")->Value = AObj->INCD_EXPL.Trim().SubString(1, 250);
            pADO->Parameters->ParamByName("p13")->Value = AObj->INCD_TYPE_CLSF_CD;
            pADO->Parameters->ParamByName("p14")->Value = AObj->INCD_TYPE_CD;
            pADO->Parameters->ParamByName("p15")->Value = AObj->OCRR_LCTN_NM.Trim();
            pADO->Parameters->ParamByName("p16")->Value = "";
            pADO->Parameters->ParamByName("p17")->Value = AObj->X_CRDN;
            pADO->Parameters->ParamByName("p18")->Value = AObj->Y_CRDN;

			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::UpdateIncidentLoc", 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("TItsIncidentManager::UpdateIncidentLoc", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsIncidentManager::MergeVmsIncidentInfo(String AINCD_OCRR_ID,           // '돌발상황 발생 ID
                                               String ASTRT_LCTN_NM,           // '시작 위치 명
                                               String AEND_LCTN_NM,            // '종료 위치 명
                                               String AOCRR_LCTN_NM,           // '발생 위치 명
                                               String ACMTR_GRAD_CD,           // '소통 등급 코드
                                               String AINCD_CLSR_LANE,         // '돌발상황 차단 차로
                                               String AVMS_DSPL_YN,            // 'VMS 표출 여부
                                               String AINCD_PRGR_STEP_CD,      // '돌발상황 진행 단계 코드
                                               String AINCD_STRT_DT,           // '돌발상황 시작 일시
                                               String AINCD_END_PRAR_DT,       // '돌발상황 종료 예정 일시
                                               String ALINK_ID,                // '링크ID
                                               String AVMS_INCD_TYPE_CD,       // 'VMS 돌발유형코드
                                               String AVMS_INCD_DETL_TYPE_CD,  // 'VMS 돌발세부유형코드
                                               String AVMS_INCD_RSTR_TYPE_CD,  // 'VMS 돌발통제코드
                                               TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "MERGE INTO TB_INCD_OCRR_VMS L                                  \r\n"
           "  USING (SELECT :p01 INCD_OCRR_ID,                             \r\n"
           "                :p02 STRT_LCTN_NM,                             \r\n"
           "                :p03 END_LCTN_NM,                              \r\n"
           "                :p04 OCRR_LCTN_NM,                             \r\n"
           "                :p05 CMTR_GRAD_CD,                             \r\n"
           "                :p06 INCD_CLSR_LANE,                           \r\n"
           "                :p07 VMS_DSPL_YN,                              \r\n"
           "                :p08 INCD_PRGR_STEP_CD,                        \r\n"
           "                :p09 INCD_STRT_DT,                             \r\n"
           "                :p10 INCD_END_PRAR_DT,                         \r\n"
           "                :p11 LINK_ID,                                  \r\n"
           "                :p12 VMS_INCD_TYPE_CD,                         \r\n"
           "                :p13 VMS_INCD_DETL_TYPE_CD,                    \r\n"
           "                :p14 VMS_INCD_RSTR_TYPE_CD                     \r\n"
           "           FROM DUAL ) M                                       \r\n"
           "ON (L.INCD_OCRR_ID = M.INCD_OCRR_ID)                           \r\n"
           "WHEN MATCHED THEN                                              \r\n"
           " UPDATE SET L.STRT_LCTN_NM          = M.STRT_LCTN_NM,          \r\n"
           "            L.END_LCTN_NM           = M.END_LCTN_NM,           \r\n"
           "            L.OCRR_LCTN_NM          = M.OCRR_LCTN_NM,          \r\n"
           "            L.CMTR_GRAD_CD          = M.CMTR_GRAD_CD,          \r\n"
           "            L.INCD_CLSR_LANE        = M.INCD_CLSR_LANE,        \r\n"
           "            L.VMS_DSPL_YN           = M.VMS_DSPL_YN,           \r\n"
           "            L.INCD_PRGR_STEP_CD     = M.INCD_PRGR_STEP_CD,     \r\n"
           "            L.INCD_STRT_DT          = M.INCD_STRT_DT,          \r\n"
           "            L.INCD_END_PRAR_DT      = M.INCD_END_PRAR_DT,      \r\n"
           "            L.LINK_ID               = M.LINK_ID,               \r\n"
           "            L.VMS_INCD_TYPE_CD      = M.VMS_INCD_TYPE_CD,      \r\n"
           "            L.VMS_INCD_DETL_TYPE_CD = M.VMS_INCD_DETL_TYPE_CD, \r\n"
           "            L.VMS_INCD_RSTR_TYPE_CD = M.VMS_INCD_RSTR_TYPE_CD  \r\n"
           "WHEN NOT MATCHED THEN                                          \r\n"
           " INSERT (L.INCD_OCRR_ID, L.STRT_LCTN_NM, L.END_LCTN_NM,        \r\n"
           "         L.OCRR_LCTN_NM, L.CMTR_GRAD_CD, L.INCD_CLSR_LANE,     \r\n"
           "         L.VMS_DSPL_YN,  L.INCD_PRGR_STEP_CD, L.INCD_STRT_DT,  \r\n"
           "         L.INCD_END_PRAR_DT, L.LINK_ID, L.VMS_INCD_TYPE_CD,    \r\n"
           "         L.VMS_INCD_DETL_TYPE_CD, L.VMS_INCD_RSTR_TYPE_CD)     \r\n"
           " VALUES (M.INCD_OCRR_ID, M.STRT_LCTN_NM, M.END_LCTN_NM,        \r\n"
           "         M.OCRR_LCTN_NM, M.CMTR_GRAD_CD, M.INCD_CLSR_LANE,     \r\n"
           "         M.VMS_DSPL_YN,  M.INCD_PRGR_STEP_CD, M.INCD_STRT_DT,  \r\n"
           "         M.INCD_END_PRAR_DT, M.LINK_ID, M.VMS_INCD_TYPE_CD,    \r\n"
           "         M.VMS_INCD_DETL_TYPE_CD, M.VMS_INCD_RSTR_TYPE_CD)     \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 = AINCD_OCRR_ID;
            pADO->Parameters->ParamByName("p02")->Value = ASTRT_LCTN_NM;
            pADO->Parameters->ParamByName("p03")->Value = AEND_LCTN_NM;
            pADO->Parameters->ParamByName("p04")->Value = AOCRR_LCTN_NM;
            pADO->Parameters->ParamByName("p05")->Value = ACMTR_GRAD_CD;
            pADO->Parameters->ParamByName("p06")->Value = AINCD_CLSR_LANE;
            pADO->Parameters->ParamByName("p07")->Value = AVMS_DSPL_YN;
            pADO->Parameters->ParamByName("p08")->Value = AINCD_PRGR_STEP_CD;
            pADO->Parameters->ParamByName("p09")->Value = AINCD_STRT_DT;
            pADO->Parameters->ParamByName("p10")->Value = AINCD_END_PRAR_DT;
            pADO->Parameters->ParamByName("p11")->Value = ALINK_ID;
            pADO->Parameters->ParamByName("p12")->Value = AVMS_INCD_TYPE_CD;
            pADO->Parameters->ParamByName("p13")->Value = AVMS_INCD_DETL_TYPE_CD;
            pADO->Parameters->ParamByName("p14")->Value = AVMS_INCD_RSTR_TYPE_CD;

			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::MergeVmsIncidentInfo", 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("TItsIncidentManager::MergeVmsIncidentInfo", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsIncidentManager::UpdateVmsIncidentInfo(String AINCD_OCRR_ID, String AINCD_PRGR_STEP_CD, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "UPDATE TB_INCD_OCRR_VMS          \r\n"
           "   SET INCD_PRGR_STEP_CD = :p02  \r\n"
           " WHERE INCD_OCRR_ID      = :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 = AINCD_OCRR_ID;
            pADO->Parameters->ParamByName("p02")->Value = AINCD_PRGR_STEP_CD;

			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::UpdateVmsIncidentInfo", 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("TItsIncidentManager::UpdateVmsIncidentInfo", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------

bool TItsIncidentManager::GetNextIncidentId(String ACenterId, String &AIncidentId, TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "SELECT SQ_INCD_ID.NEXTVAL AS NEWID FROM DUAL \r\n";
#else
    sQry = "SELECT LPAD(NVL(MAX(SUBSTR(INCD_OCRR_ID, -9)), '000000000') + 1, 9, '0') AS NEWID \r\n"
           "  FROM TB_INCD_OCRR                                                               \r\n"
           " WHERE SUBSTR(INCD_OCRR_ID, 1, 3) = :p01                                          \r\n";
#endif
    AIncidentId = "";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

#if 0
            pADO->Prepared = true;
			pADO->Open();
            AIncidentId = pADO->FieldByName("NEWID")->AsString;
#else
            pADO->Parameters->ParamByName("p01")->Value = ACenterId;
            pADO->Prepared = true;
			pADO->Open();
            AIncidentId = ACenterId + pADO->FieldByName("NEWID")->AsString;
#endif
            return true;
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsIncidentManager::GetNextIncidentId", 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("TItsIncidentManager::GetNextIncidentId", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return false;
}
//---------------------------------------------------------------------------


