// XmlMemberValidation.cpp: implementation of the CXmlMemberValidation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlMemberValidation.h"
#include "XmlBaseElement.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXmlMemberValidation::CXmlMemberValidation():
	validationMode_(enValidationMode_None)
{
}

CXmlMemberValidation::~CXmlMemberValidation()
{
	Clear();
}



void CXmlMemberValidation::Clear()
{
	validationMode_ = enValidationMode_None;
}

bool CXmlMemberValidation::BuildFromXml(IXMLDOMNode* node)
{
	Clear();

	ExtractMemberNode(node, _T("ValidationMode"), (int&)validationMode_);	
	ExtractMemberNode(node, CXmlMemberValidation_StaticEnum::GetXmlNodeName(), staticEnum_);
	ExtractMemberNode(node, CXmlMemberValidation_Interval::GetXmlNodeName(), intervalInfo_);
		
	return true;
}

bool CXmlMemberValidation::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, IXMLDOMElement* parentElement)
{
	IXMLDOMElement* newElement;
	map<cmString, cmString>::iterator it;
	HRESULT hr;

	hr = xmlDoc->createElement(CComBSTR((const TCHAR*)GetXmlNodeName()), &newElement);

	AppendMemberNode(_T("ValidationModeStr"), CXmlBaseElement::ValidationMode2String(validationMode_), newElement, xmlDoc);
	AppendMemberNode(_T("ValidationMode"), (int)validationMode_, newElement, xmlDoc);
	
	switch (validationMode_) {
		case enValidationMode_None:
			break;

		case enValidationMode_Interval:
			intervalInfo_.AppendToDOMDocument(xmlDoc, newElement);
			break;

		case enValidationMode_StaticEnum:
			staticEnum_.AppendToDOMDocument(xmlDoc, newElement);
			break;

		case enValidationMode_DynamicEnum:
			break;
	}

	IXMLDOMNode* newOutNode;

	if (parentElement)
		hr = parentElement->appendChild(newElement, &newOutNode);
	else
		hr = xmlDoc->appendChild(newElement, &newOutNode);
	newOutNode->Release();	
   
	return true;
}

void CXmlMemberValidation::SetValidationMode(enValidationMode mode)
{
	validationMode_ = mode;
}

enValidationMode CXmlMemberValidation::GetValidationMode() const
{
	return validationMode_;
}

CXmlMemberValidation_StaticEnum* CXmlMemberValidation::GetStaticEnum()
{
	return &staticEnum_;
}

void CXmlMemberValidation::SetDefaults()
{
	validationMode_ = enValidationMode_None;
}

CXmlMemberValidation_Interval* CXmlMemberValidation::GetIntervalInfo()
{
	return &intervalInfo_;
}
