#include "StdAfx.h"
|
#include "MBThreadSyncDb.h"
|
#include "MBVaultDbMgr.h"
|
#include "MBVaultExtendEvents.h"
|
#include "json.h"
|
#include "Util.h"
|
#include "StrFileUtils.h"
|
|
CMBThreadSyncDb::CMBThreadSyncDb(void)
|
{
|
m_bStop = false;
|
m_pNDWebCMgr = NULL;
|
m_hWnd = NULL;
|
m_pVaultDbMgr = NULL;
|
}
|
|
|
|
|
CMBThreadSyncDb::~CMBThreadSyncDb(void)
|
{
|
|
}
|
|
|
// ¿ªÊ¼Ïß³Ì
|
void CMBThreadSyncDb::StartThread()
|
{
|
if(IsRunning())
|
return;
|
|
BeginThread();
|
}
|
|
// ½áÊøÏß³Ì
|
void CMBThreadSyncDb::OverThread()
|
{
|
m_bStop = true;
|
|
|
if(!IsRunning())
|
return;
|
|
EndThread();
|
}
|
|
// ÉèÖòÎÊý
|
void CMBThreadSyncDb::SetParam( HWND hWnd,CMBVaultDbMgr *pVaultDbMgr,CMBNetDiskWebCMgr *pNDWebCMgr )
|
{
|
m_pNDWebCMgr = pNDWebCMgr;
|
m_hWnd = hWnd;
|
m_pVaultDbMgr = pVaultDbMgr;
|
}
|
|
// ´íÎóÐÅÏ¢
|
void CMBThreadSyncDb::SendEventErr(CString strErrInfo)
|
{
|
EventErr *pEvt = new EventErr(NULL);
|
pEvt->m_strErrInfo = strErrInfo;
|
SNotifyCenter::getSingleton().FireEventAsync(pEvt);
|
pEvt->Release();
|
}
|
|
// ·¢ËÍ ÏÂÔØ
|
void CMBThreadSyncDb::SendEventDld( CString strXml,CString strVaultID )
|
{
|
if( strXml.IsEmpty() )
|
return ;
|
|
CString strPath;
|
|
strPath.Format(_T("%s\\valut\\%s\\File"),m_pVaultDbMgr->GetExePath(),strVaultID);
|
|
EventDld *pEvt = new EventDld(NULL);
|
pEvt->m_strXml = strXml;
|
pEvt->m_strPath = strPath;
|
SNotifyCenter::getSingleton().FireEventAsync(pEvt);
|
pEvt->Release();
|
}
|
|
// ·¢ËÍ Í¬²½Êý¾Ý ÏûÏ¢
|
void CMBThreadSyncDb::SendEventSyncDbMsgFinish( CString strMsg )
|
{
|
EventSyncDbMsgFinish *pEvt = new EventSyncDbMsgFinish(NULL);
|
pEvt->m_strMsg = strMsg;
|
SNotifyCenter::getSingleton().FireEventAsync(pEvt);
|
pEvt->Release();
|
}
|
|
UINT CMBThreadSyncDb::Run()
|
{
|
CString strVaultList,strUserSessionID,strRootList,strErrInfo;
|
|
///// ¶ÔOI_VAULTµÄ¸üÐÂ
|
CMBVaultMgr *pVaultMgr = new CMBVaultMgr();
|
|
if( !GetVaultFromSvr( pVaultMgr,strErrInfo ) )
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
if( !SetVaultToDb(pVaultMgr,strErrInfo) )
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
// ¶ÔOI_VAULT_CATALOGµÄ¸üÐÂ
|
if(!GetVaultCatalogFromSvr( pVaultMgr,strErrInfo ))
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
if(!SetVaultCatalogToDb(pVaultMgr,strErrInfo))
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
|
////////////´´½¨ TNXXX
|
if( !GetClsAttrsFromSvr(pVaultMgr,strErrInfo ) )
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
|
if( !CreateTNXXX( pVaultMgr,strErrInfo ) )
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
/////////ÉèÖÃTNXXX Êý¾Ý
|
if( !SetTNXXXToDb( pVaultMgr,strErrInfo ))
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
////////OI_VAULT_CATALOG_HLINK/////////////////
|
// ÏÈɾ³ý
|
if( !m_pVaultDbMgr->DelVaultCatalogHlink( strErrInfo ) )
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
if( !SetCatalogHlinkToDb( pVaultMgr,strErrInfo ))
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
if( !SetGridStyleJson( pVaultMgr,strErrInfo ) )
|
{
|
SendEventErr(strErrInfo);
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
return 0;
|
}
|
|
delete pVaultMgr;
|
pVaultMgr = NULL;
|
|
// ¸üÐÂÍê³É
|
SendEventSyncDbMsgFinish(_T("ÒÑÍê³ÉÊý¾Ýͬ²½£¡"));
|
|
return 0;
|
}
|
|
|
// ´Ó·þÎñÖлñÈ¡VaultÊý¾Ý
|
bool CMBThreadSyncDb::GetVaultFromSvr( CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
CString strVaultList;
|
|
m_pNDWebCMgr->VaultGetList( strVaultList,strErrInfo,false );
|
|
|
if(!pVaultMgr->ParseXml( strVaultList,strErrInfo ))
|
{
|
return false;
|
}
|
return true;
|
}
|
|
bool CMBThreadSyncDb::SetVaultToDb(CMBVaultMgr *pVaultMgr,CString &strErrInfo)
|
{
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CString strVaultInfo;
|
CMBVaultVector vector;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
m_pNDWebCMgr->VaultGetInfo(pVault->m_strID,strVaultInfo,strErrInfo );
|
|
if(!pVault->ParseXml(strVaultInfo,strErrInfo))
|
return false;
|
|
if(!m_pVaultDbMgr->SetVaultToDb(pVault,strErrInfo))
|
return false;
|
}
|
|
return true;
|
}
|
|
bool CMBThreadSyncDb::GetVaultCatalogFromSvr( CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
CString strParentID,strCatalogList;
|
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
if(!m_pNDWebCMgr->VaultCatalogGetList( pVault->m_strID,strParentID,strCatalogList, strErrInfo))
|
return false;
|
|
|
if( !pVault->GetVaultCatalogMgr()->ParseXML( strCatalogList,pVault->m_strID,_T(""),strErrInfo ) )
|
return false;
|
|
/// ±éÀú£¬µÝ¹é
|
CMBVaultCatalogVector vectorCatalog;
|
CMBVaultCatalogVector::iterator it;
|
CMBVaultCatalog *pVaultCatalog = NULL;
|
|
pVault->GetVaultCatalogMgr()->GetVaultCatalogVector( vectorCatalog );
|
|
for( it = vectorCatalog.begin();it != vectorCatalog.end();it++ )
|
{
|
pVaultCatalog = *it;
|
if( pVaultCatalog->m_nSubCount > 0 )
|
{
|
GetVaultCatalogFromSvr( pVaultCatalog->GetVaultCatalogMgr(),pVault->m_strID,pVaultCatalog->m_strID,strErrInfo );
|
}
|
}
|
}
|
|
// VaultCatalogGetList( CString strVaultID, CString strParentID, CString &strCatalogList,CString &strErrInfo );
|
return true;
|
}
|
|
bool CMBThreadSyncDb::GetVaultCatalogFromSvr( CMBVaultCatalogMgr *pVaultCatalogMgr,CString &strVaultID,CString &strParentID,CString &strErrInfo )
|
{
|
CString strCatalogList;
|
CMBVaultCatalogVector vectorCatalog;
|
CMBVaultCatalogVector::iterator it;
|
CMBVaultCatalog *pVaultCatalog = NULL;
|
|
if(!m_pNDWebCMgr->VaultCatalogGetList( strVaultID,strParentID,strCatalogList, strErrInfo))
|
return false;
|
|
if( !pVaultCatalogMgr->ParseXML( strCatalogList,strVaultID,strParentID,strErrInfo ) )
|
return false;
|
|
pVaultCatalogMgr->GetVaultCatalogVector( vectorCatalog );
|
|
for( it = vectorCatalog.begin();it != vectorCatalog.end();it++ )
|
{
|
pVaultCatalog = *it;
|
if( pVaultCatalog->m_nSubCount > 0 )
|
{
|
GetVaultCatalogFromSvr( pVaultCatalog->GetVaultCatalogMgr(),strVaultID,pVaultCatalog->m_strID,strErrInfo );
|
}
|
}
|
|
return true;
|
}
|
|
|
bool CMBThreadSyncDb::SetVaultCatalogToDb(CMBVaultMgr *pVaultMgr,CString &strErrInfo)
|
{
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
if(!SetVaultCatalogToDb(pVault->GetVaultCatalogMgr(),strErrInfo))
|
return false;
|
}
|
return true;
|
}
|
|
bool CMBThreadSyncDb::SetVaultCatalogToDb(CMBVaultCatalogMgr *pVaultCatalogMgr,CString &strErrInfo)
|
{
|
/// ±éÀú£¬µÝ¹é
|
CMBVaultCatalogVector vectorCatalog;
|
CMBVaultCatalogVector::iterator it;
|
CMBVaultCatalog *pVaultCatalog = NULL;
|
|
pVaultCatalogMgr->GetVaultCatalogVector( vectorCatalog );
|
|
for( it = vectorCatalog.begin();it != vectorCatalog.end();it++ )
|
{
|
pVaultCatalog = *it;
|
if(!m_pVaultDbMgr->SetVaultCatalogToDb(pVaultCatalog,strErrInfo))
|
return false;
|
if(!SetVaultCatalogToDb(pVaultCatalog->GetVaultCatalogMgr(),strErrInfo))
|
return false;
|
}
|
return true;
|
}
|
|
// ´´½¨±íTN_XXX
|
bool CMBThreadSyncDb::CreateTNXXX( CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
if( NULL == pVaultMgr )
|
return false;
|
|
CString strClsID,strTableSql,strTableName;
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
strClsID = pVault->m_strClsID;
|
strTableName = _T("TN_") + strClsID;
|
|
int nCount = pVault->GetClsAttrsMgr()->GetClsAttrsCount();
|
if( nCount == 0 )
|
continue;
|
|
strTableSql = pVault->GetClsAttrsMgr()->GetTableSql(strTableName);
|
|
if(!m_pVaultDbMgr->CreateTableInDb( strTableName,strTableSql,strErrInfo ))
|
return false;
|
|
}
|
|
return true;
|
}
|
|
bool CMBThreadSyncDb::GetClsAttrsFromSvr(CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
CString strAttrList;
|
CString strClsID;
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
strClsID = pVault->m_strClsID;
|
// µÃµ½Êý¾ÝÀàÊôÐÔ
|
if(!m_pNDWebCMgr->GetClassAttrs( strClsID, strAttrList, strErrInfo ))
|
return false;
|
|
if(!pVault->GetClsAttrsMgr()->ParseXml(strAttrList,strErrInfo))
|
return false;
|
}
|
return true;
|
}
|
|
|
bool CMBThreadSyncDb::SetTNXXXToDb( CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
CString strCatalogID,strQueryCondition,strResult,strObjAttrs,strOrderBy;
|
CString strClsID,strTableName,strVaultID;
|
CString strSessionID;
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
int nCurPage = 0,nTotalPages = 0;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
strClsID = pVault->m_strClsID;
|
strTableName = _T("TN_") + strClsID;
|
strVaultID = pVault->m_strID;
|
|
// 100¸öдһ´Î
|
if(!m_pNDWebCMgr->VaultQueryCatalogObj( pVault->m_strID, strCatalogID, strQueryCondition,strResult,strErrInfo, strObjAttrs, strOrderBy, 100,true ))
|
return false;
|
|
CMBTNXXXObjMgr *pObjMgr = new CMBTNXXXObjMgr();
|
|
if(!pObjMgr->ParseXml( strResult,strErrInfo))
|
{
|
delete pObjMgr;
|
pObjMgr = NULL;
|
return false;
|
}
|
|
if( !SetTNXXXToDb(pObjMgr,strTableName,strErrInfo ) )
|
{
|
delete pObjMgr;
|
pObjMgr = NULL;
|
return false;
|
}
|
|
nCurPage = pObjMgr->m_nCurPage;
|
nTotalPages = pObjMgr->m_nTotalPages;
|
strSessionID = pObjMgr->m_strSessionID;
|
// ·¢Ë͹ýÈ¥ÏÂÔØ
|
CString strDldXml = pObjMgr->GenDldXml();
|
SendEventDld(strDldXml,strVaultID);
|
///////////////////////////////////////////
|
if( NULL != pObjMgr )
|
{
|
delete pObjMgr;
|
pObjMgr = NULL;
|
}
|
|
if( !SetTNXXXToDb( strVaultID,strTableName,strSessionID,nCurPage, nTotalPages,strErrInfo ) )
|
{
|
return false;
|
}
|
}
|
return true;
|
}
|
|
bool CMBThreadSyncDb::SetTNXXXToDb( CString strVaultID,CString strTableName,CString strSessionID,int nCurPage,int nTotalPages,CString &strErrInfo )
|
{
|
CString strResult;
|
// Ñ»·²éѯҳ
|
for( int i = nCurPage+1;i <= nTotalPages;i++)
|
{
|
// 100¸öдһ´Î
|
if(!m_pNDWebCMgr->VaultQueryCatalogObj( strSessionID,i, strResult,strErrInfo ))
|
return false;
|
|
CMBTNXXXObjMgr *pTmpObjMgr = new CMBTNXXXObjMgr();
|
|
if(!pTmpObjMgr->ParseXml( strResult,strErrInfo))
|
{
|
delete pTmpObjMgr;
|
pTmpObjMgr = NULL;
|
return false;
|
}
|
|
if( !SetTNXXXToDb(pTmpObjMgr,strTableName,strErrInfo ) )
|
{
|
delete pTmpObjMgr;
|
pTmpObjMgr = NULL;
|
return false;
|
}
|
|
// ·¢Ë͹ýÈ¥ÏÂÔØ
|
CString strDldXml = pTmpObjMgr->GenDldXml();
|
SendEventDld(strDldXml,strVaultID);
|
///////////////////////////////////////////
|
|
if( NULL != pTmpObjMgr )
|
{
|
delete pTmpObjMgr;
|
pTmpObjMgr = NULL;
|
}
|
}
|
return true;
|
}
|
|
bool CMBThreadSyncDb::SetTNXXXToDb( CMBTNXXXObjMgr *pObjMgr,CString strTableName,CString &strErrInfo )
|
{
|
CString strInsertSql;
|
CMBTNXXXObjMap mapObj;
|
CMBTNXXXObjMap::iterator it;
|
CMBTNXXXObj *pObj = NULL;
|
|
CString strExsitSql,strSql;
|
|
pObjMgr->GetObjMap(mapObj);
|
|
for( it = mapObj.begin();it != mapObj.end();it++ )
|
{
|
pObj = it->second;
|
|
strExsitSql = pObj->IsExsitSqlToSTb( strTableName );
|
|
bool bExsit = m_pVaultDbMgr->IsExsitDataInDb(strExsitSql,strErrInfo);
|
|
if( bExsit )
|
{
|
strSql = pObj->GetUpdateSql( strTableName );
|
}
|
else
|
{
|
strSql = pObj->GetInsertSql( strTableName );
|
}
|
|
if(!m_pVaultDbMgr->RunSQLInDb( strSql,strErrInfo ))
|
return false;
|
}
|
|
return true;
|
}
|
|
|
bool CMBThreadSyncDb::SetCatalogHlinkToDb(CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
CString strResult;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
if(!SetCatalogHlinkToDb(pVault->m_strID,pVault->GetVaultCatalogMgr(),strErrInfo ) )
|
{
|
return false;
|
}
|
}
|
|
return true;
|
}
|
|
bool CMBThreadSyncDb::SetCatalogHlinkToDb(CString strVaultID,CMBVaultCatalogMgr *pVaultCatalogMgr,CString &strErrInfo )
|
{
|
CString strResult;
|
CMBVaultCatalogVector vectorCatalog;
|
CMBVaultCatalogVector::iterator it;
|
CMBVaultCatalog *pVaultCatalog = NULL;
|
|
pVaultCatalogMgr->GetVaultCatalogVector( vectorCatalog );
|
|
for( it = vectorCatalog.begin();it != vectorCatalog.end();it++ )
|
{
|
pVaultCatalog = *it;
|
if(!m_pNDWebCMgr->VaultCatalogGetObjList( strVaultID,pVaultCatalog->m_strID, strResult,strErrInfo, false, 0, 0 ))
|
return false;
|
|
// ½âÎö£¬
|
CMBVaultCatalogHlinkMgr *pVaultCatalogHlinkMgr = new CMBVaultCatalogHlinkMgr();
|
|
if(!pVaultCatalogHlinkMgr->ParseXml( strResult,strVaultID, pVaultCatalog->m_strID,strErrInfo ))
|
{
|
return false;
|
}
|
// дdb
|
if( !SetCatalogHlinkToDb(pVaultCatalogHlinkMgr,strErrInfo ) )
|
{
|
delete pVaultCatalogHlinkMgr;
|
pVaultCatalogHlinkMgr = NULL;
|
|
return false;
|
}
|
|
if( NULL != pVaultCatalogHlinkMgr){
|
delete pVaultCatalogHlinkMgr;
|
pVaultCatalogHlinkMgr = NULL;
|
}
|
|
if( !SetCatalogHlinkToDb(strVaultID,pVaultCatalog->GetVaultCatalogMgr(),strErrInfo ) )
|
{
|
return false;
|
}
|
|
}
|
return true;
|
}
|
|
bool CMBThreadSyncDb::SetCatalogHlinkToDb(CMBVaultCatalogHlinkMgr *pVaultCatalogHlinkMgr,CString &strErrInfo )
|
{
|
CMBVaultCatalogHlinkVector::iterator it;
|
CMBVaultCatalogHlink *pVaultCatalogHlink = NULL;
|
CMBVaultCatalogHlinkVector vectorCatalogHlink;
|
|
pVaultCatalogHlinkMgr->GetVaultCatalogHlinkVector( vectorCatalogHlink);
|
|
for( it = vectorCatalogHlink.begin();it != vectorCatalogHlink.end();it++ )
|
{
|
pVaultCatalogHlink = *it;
|
if( !m_pVaultDbMgr->InsertVaultCatalogHlink( pVaultCatalogHlink,strErrInfo ) )
|
return false;
|
}
|
|
return true;
|
}
|
|
|
bool CMBThreadSyncDb::SetGridStyleJson( CMBVaultMgr *pVaultMgr,CString &strErrInfo )
|
{
|
CMBVaultVector::iterator it;
|
CMBVault *pVault = NULL;
|
CMBVaultVector vector;
|
CString strExtParam,strClsID,strGridStyleDef,strStyleName,strJsonPath;
|
|
pVaultMgr->GetVaultVector( vector );
|
|
for( it = vector.begin();it != vector.end();it++ )
|
{
|
pVault = *it;
|
|
if( !pVault->m_bCanSyncToClient )
|
continue;
|
|
strClsID = pVault->m_strClsID;
|
strExtParam = pVault->m_strExtParamter;
|
|
if(strExtParam.IsEmpty()){
|
if(!m_pNDWebCMgr->ClassGridStyleGetDefaultInfo( strClsID, strGridStyleDef,strErrInfo ))
|
return false;
|
}else{
|
if(!GetGridStyleName( strExtParam,strStyleName,strErrInfo))
|
return false;
|
if(!m_pNDWebCMgr->ClassGridStyleGetInfo( strClsID, strStyleName, strGridStyleDef,strErrInfo ))
|
return false;
|
}
|
// strGridStyleDef
|
strJsonPath.Format(_T("%s\\valut\\%s\\GridStyle.json"),m_pVaultDbMgr->GetExePath(),pVault->m_strID);
|
|
if(!CStrFileUtils::WriteFile(strJsonPath,strGridStyleDef,strErrInfo ))
|
return false;
|
|
}
|
|
return true;
|
}
|
|
|
// »ñÈ¡GridstyleÃû³Æ
|
bool CMBThreadSyncDb::GetGridStyleName( CString strJson,CString &strStyleName,CString &strErrInfo )
|
{
|
Json::Reader reader;
|
Json::Value json_object;
|
string json,gridStyle;
|
|
json = CT2A(strJson);
|
|
if (!reader.parse(json, json_object)){
|
strErrInfo = strJson + _T(" ·Çjson¸ñʽ£¡");
|
return false;
|
}
|
|
gridStyle = json_object["GridStyle"].asString() ;
|
strStyleName = gridStyle.c_str();
|
|
return true;
|
}
|