//------------------------------------------------------------------------------
// $Workfile: XMLModelAccess.cpp $
// $Header: /SbjDev/XmlMvc/XMLModelAccess.cpp 12 10/22/08 4:48p Steve $
//
// Copyright � 2006 Tartus, Inc.
// All rights reserved.
//
// The information contained herein is confidential, proprietary to
// Tartus, Inc., and considered a trade secret as defined
// in section 499C of the penal code of the State of California. Use
// of this information by anyone other than authorized employees of
// Tartus, Inc. is granted only under a written non-disclosure
// agreement, which expressly prescribes the scope and manner of such
// use.
//
// *** Authors ***
// Neal O'Hara
// John La Porta
// Zhimin Lin
// Andreas Christmann
// Steve Johnson
// Frankie Jefferson
// Tartus, Inc.
//
// $Revision: 12 $
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
namespace localNS
{
void RemoveChild(MSXML2::IXMLDOMElementPtr spChild,
MSXML2::IXMLDOMNodePtr spParent)
{
SbjCore::Mvc::DME::t_EventRemove eventRemoving(SbjCore::Mvc::DME::Removing, XmlMvc::XmlModelAccess::GetXMLNodeHandle(spChild));
(void)spParent->removeChild(spChild);
SbjCore::Mvc::DME::t_EventRemove eventRemoved(SbjCore::Mvc::DME::Removed, XmlMvc::XmlModelAccess::GetXMLNodeHandle(spChild));
}
void InsertChild(XmlMvc::XmlModelAccess::InsertData* pData)
{
XmlMvc::XmlModelAccess::t_EventInsert eventNodeInserting(SbjCore::Mvc::DME::Inserting, pData, true);
if (pData->spTheAfter != NULL)
{
pData->spTheParent->insertBefore(pData->spTheChild, _variant_t(pData->spTheAfter.GetInterfacePtr()));
}
else
{
pData->spTheParent->appendChild(pData->spTheChild);
}
XmlMvc::XmlModelAccess::t_EventInsert eventNodeInserted(SbjCore::Mvc::DME::Inserted, pData, true);
}
}
namespace XmlMvc
{
namespace XmlModelAccess
{
MSXML2::IXMLDOMNodePtr InsertChild(
MSXML2::IXMLDOMElementPtr spChild,
MSXML2::IXMLDOMElementPtr spParent,
MSXML2::IXMLDOMElementPtr spAfter /*= NULL*/,
bool bAddToUndoRedo /*= true*/)
{
class UndoRedoHandler : public SbjCore::UndoRedo::Handler
{
CString sActionName;
InsertData theEventData;
HANDLE hNode;
public:
UndoRedoHandler(
MSXML2::IXMLDOMElementPtr spChild,
MSXML2::IXMLDOMElementPtr spParent,
MSXML2::IXMLDOMElementPtr spAfter) :
sActionName(_T("Insert")),
theEventData(spChild, spParent, spAfter)
{
}
virtual bool OnHandleUndo()
{
HRESULT hr = S_OK;
bool bRslt = true;
try
{
localNS::RemoveChild(theEventData.spTheChild, theEventData.spTheParent);
}
catch (COleException* p) // the new Atl CString throws this
{
ASSERT(FALSE);
hr = p->m_sc;
p->Delete();
}
catch (_com_error& e) // native type throw this
{
ASSERT(FALSE);
hr = e.Error();
}
catch (SbjCore::EventMgr::AbortException* pAbort)
{
delete pAbort;
}
return bRslt;
}
virtual bool OnHandleRedo()
{
HRESULT hr = S_OK;
bool bRslt = true;
try
{
localNS::InsertChild(&theEventData);
SbjCore::EventMgr::Fire(SbjCore::Mvc::DME::Modified);
}
catch (COleException* p) // the new Atl CString throws this
{
ASSERT(FALSE);
hr = p->m_sc;
p->Delete();
}
catch (_com_error& e) // native type throw this
{
ASSERT(FALSE);
hr = e.Error();
}
catch (SbjCore::EventMgr::AbortException* pAbort)
{
delete pAbort;
}
return bRslt;
}
virtual LPCTSTR OnGetHandlerName() const
{
return sActionName;
}
};
/////////////////////////////////////////////////////////////////
HRESULT hr = S_OK;
MSXML2::IXMLDOMNodePtr spRslt;
try
{
InsertData theData(spChild, spParent, spAfter);
localNS::InsertChild(&theData);
SbjCore::EventMgr::Fire(SbjCore::Mvc::DME::Modified);
if (bAddToUndoRedo)
{
UndoRedoHandler* pUndoRedoHandler = new UndoRedoHandler(spChild, spParent, spAfter);
SbjCore::Mvc::CmdTargetController* pCtrlr = GetDocController();
pCtrlr->GetUndoRedoMgr()->Push(pUndoRedoHandler);
}
}
catch (COleException* p) // the new Atl CString throws this
{
ASSERT(FALSE);
hr = p->m_sc;
p->Delete();
}
catch (_com_error& e) // native type throw this
{
ASSERT(FALSE);
hr = e.Error();
}
catch (SbjCore::EventMgr::AbortException* pAbort)
{
delete pAbort;
}
return spRslt;
}
MSXML2::IXMLDOMNodePtr RemoveChild(
MSXML2::IXMLDOMElementPtr spChild,
MSXML2::IXMLDOMNodePtr spParent /*= NULL*/)
{
class UndoRedoHandler : public SbjCore::UndoRedo::Handler
{
CString sActionName;
InsertData theEventData;
public:
UndoRedoHandler(
MSXML2::IXMLDOMElementPtr spChild,
MSXML2::IXMLDOMElementPtr spParent,
MSXML2::IXMLDOMElementPtr spAfter) :
sActionName(_T("Remove")),
theEventData(spChild, spParent, spAfter) {
}
virtual bool OnHandleUndo()
{
bool bRslt = true;
localNS::InsertChild(&theEventData);
return bRslt;
}
virtual bool OnHandleRedo()
{
HRESULT hr = S_OK;
bool bRslt = true;
try
{
localNS::RemoveChild(theEventData.spTheChild, theEventData.spTheParent);
SbjCore::EventMgr::Fire(SbjCore::Mvc::DME::Modified);
}
catch (COleException* p) // the new Atl CString throws this
{
ASSERT(FALSE);
hr = p->m_sc;
p->Delete();
}
catch (_com_error& e) // native type throw this
{
ASSERT(FALSE);
hr = e.Error();
}
catch (SbjCore::EventMgr::AbortException* pAbort)
{
delete pAbort;
}
return bRslt;
}
virtual LPCTSTR OnGetHandlerName() const
{
return sActionName;
}
};
/////////////////////////////////////////////////////////////////
HRESULT hr = S_OK;
MSXML2::IXMLDOMNodePtr spRslt;
MSXML2::IXMLDOMNodePtr spAfter;
InsertData theData(spChild, spParent, spAfter);
if (NULL == spParent)
{
spParent = spChild->GetparentNode();
}
HANDLE hParentNode = GetXMLNodeHandle(spParent);
hParentNode;
try
{
spAfter = spChild->GetnextSibling();
localNS::RemoveChild(spChild, spParent);
SbjCore::EventMgr::Fire(SbjCore::Mvc::DME::Modified);
UndoRedoHandler* pUndoRedoHandler = new UndoRedoHandler(spChild, spParent, spAfter);
SbjCore::Mvc::CmdTargetController* pCtrlr = GetDocController();
pCtrlr->GetUndoRedoMgr()->Push(pUndoRedoHandler);
}
catch (COleException* p) // the new Atl CString throws this
{
ASSERT(FALSE);
hr = p->m_sc;
p->Delete();
}
catch (_com_error& e) // native type throw this
{
ASSERT(FALSE);
hr = e.Error();
}
catch (SbjCore::EventMgr::AbortException* pAbort)
{
delete pAbort;
}
return spRslt;
}
HANDLE GetXMLNodeHandle(MSXML2::IXMLDOMNodePtr sp)
{
MSXML2::IXMLDOMElementPtr spDocElement = SbjCore::Utils::Xml::GetDocElement(sp);
UINT hNext = NULL;
UINT hItem = NULL;
(void)SbjCore::Utils::Xml::GetAttribute(spDocElement, _T("nextSbjHandle"), hNext);
(void)SbjCore::Utils::Xml::GetAttribute(sp, _T("sbjHandle"), hItem);
if (NULL == hItem)
{
hItem = hNext;
MSXML2::IXMLDOMElementPtr spElement = sp;
(void)SbjCore::Utils::Xml::SetAttribute(sp, _T("sbjHandle"), hItem);
(void)SbjCore::Utils::Xml::SetAttribute(spDocElement, _T("nextSbjHandle"), ++hNext);
SbjCore::EventMgr::Fire(SbjCore::Mvc::DME::Modified);
}
return (HANDLE)hItem;
}
CString CookAttrName(LPCTSTR lpsz)
{
ASSERT(lpsz != NULL);
CString s;
int nIndex = 0;
while (lpsz[nIndex] != NULL)
{
CString sTemp(lpsz[nIndex]);
if (0 == nIndex)
{
s = sTemp.MakeUpper();
}
else if (_istupper(lpsz[nIndex]))
{
if (!_istupper(lpsz[nIndex - 1]))
{
s += " ";
}
s += sTemp;
}
else
{
s += sTemp;
}
nIndex++;
}
return s;
}
///////////////////////////////////////////////////////////////////////////////////
void AFX_EXT_API GetPointAttributes(MSXML2::IXMLDOMElementPtr sp, CPoint& pt)
{
GetAttribute(sp, _T("x"), pt.x);
GetAttribute(sp, _T("y"), pt.y);
}
void AFX_EXT_API SetPointAttributes(MSXML2::IXMLDOMElementPtr sp, const CPoint pt)
{
SetAttribute(sp, _T("x"), pt.x);
SetAttribute(sp, _T("y"), pt.y);
}
void AFX_EXT_API GetRectAttributes(MSXML2::IXMLDOMElementPtr sp, CRect& r)
{
GetAttribute(sp, _T("left"), r.left);
GetAttribute(sp, _T("top"), r.top);
GetAttribute(sp, _T("right"), r.right);
GetAttribute(sp, _T("bottom"), r.bottom);
}
void AFX_EXT_API SetRectAttributes(MSXML2::IXMLDOMElementPtr sp, const CRect r)
{
class UndoRedoHandler : public SbjCore::UndoRedo::Handler
{
CString sActionName;
ChangeData data;
HANDLE hNode;
CRect rBefore;
CRect rAfter;
public:
UndoRedoHandler(MSXML2::IXMLDOMElementPtr sp, CRect rB, CRect rA) :
sActionName(_T("Move")),
data(sp, _T("Rectangle")),
hNode(NULL),
rBefore(rB),
rAfter(rA)
{
if (rBefore.TopLeft() == rAfter.TopLeft())
{
if (rBefore.BottomRight() != rAfter.BottomRight())
{
sActionName = _T("Resize");
}
}
else if (rBefore.BottomRight() == rAfter.BottomRight())
{
sActionName = _T("Resize");
}
hNode = GetXMLNodeHandle(data.spTheElement);
}
virtual bool OnHandleUndo()
{
bool bRslt = true;
SetAttribute(data.spTheElement, _T("left"), rBefore.left);
SetAttribute(data.spTheElement, _T("top"), rBefore.top);
SetAttribute(data.spTheElement, _T("right"), rBefore.right);
SetAttribute(data.spTheElement, _T("bottom"), rBefore.bottom);
t_EventChange eventNodeChanged(SbjCore::Mvc::DME::Changed, &data, true);
return bRslt;
}
virtual bool OnHandleRedo()
{
bool bRslt = true;
SetAttribute(data.spTheElement, _T("left"), rAfter.left);
SetAttribute(data.spTheElement, _T("top"), rAfter.top);
SetAttribute(data.spTheElement, _T("right"), rAfter.right);
SetAttribute(data.spTheElement, _T("bottom"), rAfter.bottom);
t_EventChange eventNodeChanged(SbjCore::Mvc::DME::Changed, &data, true);
return bRslt;
}
virtual LPCTSTR OnGetHandlerName() const
{
return sActionName;
}
};
/////////////////////////////////////////////////////////////////
CRect rB;
GetRectAttributes(sp, rB);
UndoRedoHandler* pUndoRedoHandler = new UndoRedoHandler(sp, rB, r);
SbjCore::Mvc::CmdTargetController* pCtrlr = GetDocController();
pCtrlr->GetUndoRedoMgr()->Push(pUndoRedoHandler);
SetAttribute(sp, _T("left"), r.left);
SetAttribute(sp, _T("top"), r.top);
SetAttribute(sp, _T("right"), r.right);
SetAttribute(sp, _T("bottom"), r.bottom);
}
CString AssureNewVal(MSXML2::IXMLDOMElementPtr spNode, LPCTSTR lpszAttr, LPCTSTR lpszVal)
{
HRESULT hr = NULL;
MSXML2::IXMLDOMNodeListPtr spNodeList = NULL;
CString sVal(lpszVal);
CString sXPath;
sXPath.Format(_T("*[starts-with(@%s, '%s')]"), lpszAttr, lpszVal);
try
{
spNodeList = spNode->selectNodes((LPCTSTR)sXPath);
}
catch (_com_error& e) // native type throw this
{
hr = e.Error();
}
if (spNodeList != NULL)
{
int nCount = spNodeList->Getlength();
if (nCount > 0)
{
nCount++;
sVal.Format(_T("%s (%d)"), lpszVal, nCount);
}
}
return sVal;
}
}
}