// XmlClassCardinality.cpp: implementation of the CXmlClassCardinality class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlClassCardinality.h"

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

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

CXmlClassCardinality::CXmlClassCardinality()
{
	SetDefaults();
}

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

void CXmlClassCardinality::SetCardinality(enCardinality cardinality)
{
	cardinality_ = cardinality;
}

enCardinality CXmlClassCardinality::GetCardinality()
{
	return cardinality_;
}

void CXmlClassCardinality::SetArrayMinSize(int minSize)
{
	if (minSize > arrayMaxSize_)
		arrayMinSize_ = 0;
	else
		arrayMinSize_ = minSize;
}

int CXmlClassCardinality::GetArrayMinSize()
{
	return arrayMinSize_;
}

void CXmlClassCardinality::SetArrayMaxSize(int maxSize)
{
	if (maxSize < arrayMinSize_ && maxSize != -1)
		arrayMaxSize_ = arrayMinSize_ + 1;
	else
		arrayMaxSize_ = maxSize;
}

int CXmlClassCardinality::GetArrayMaxSize()
{
	return arrayMaxSize_;
}

void CXmlClassCardinality::SetMapMinSize(int minSize)
{
	if (minSize > mapMaxSize_)
		mapMinSize_ = 0;
	else
		mapMinSize_ = minSize;
}

int CXmlClassCardinality::GetMapMinSize()
{
	return mapMinSize_;
}

void CXmlClassCardinality::SetMapMaxSize(int maxSize)
{
	if (maxSize < mapMinSize_ && maxSize != -1)
		mapMaxSize_ = mapMinSize_ + 1;
	else
		mapMaxSize_ = maxSize;
}

int CXmlClassCardinality::GetMapMaxSize()
{
	return mapMaxSize_;
}

void CXmlClassCardinality::SetMapSortKey(const cmString& sortKey)
{
	mapSortKey_ = sortKey;
}

cmString CXmlClassCardinality::GetMapSortKey()
{
	return mapSortKey_;
}

bool CXmlClassCardinality::BuildFromXml(IXMLDOMNode* node)
{
	ExtractMemberNode(node, _T("Cardinality"), (int&)cardinality_);
	ExtractMemberNode(node, _T("ArrayMinSize"), arrayMinSize_);
	ExtractMemberNode(node, _T("ArrayMaxSize"), arrayMaxSize_);
	ExtractMemberNode(node, _T("MapMinSize"), mapMinSize_);
	ExtractMemberNode(node, _T("MapMaxSize"), mapMaxSize_);
	ExtractMemberNode(node, _T("MapSortKey"), mapSortKey_);
	ExtractMemberNode(node, _T("MapInterfaceGuid"), mapInterfaceGuid_);
	ExtractMemberNode(node, _T("ArrayInterfaceGuid"), arrayInterfaceGuid_);
	ExtractMemberNode(node, _T("MapClassGuid"), mapClassGuid_);
	ExtractMemberNode(node, _T("ArrayClassGuid"), arrayClassGuid_);
	ExtractMemberNode(node, _T("DisplayToolbar"), displayToolbar_);
	ExtractMemberNode(node, _T("UseContainerNode"), useContainerNode_);

	return true;
}

bool CXmlClassCardinality::AppendToDOMDocument(  IXMLDOMDocument2* xmlDoc, 
												 IXMLDOMElement* parentElement)
{
	IXMLDOMElement* newElement;
	bool ret;
	HRESULT hr;

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

	ret = CXmlBaseElement::AppendToDOMDocument(xmlDoc, newElement);
	if (!ret)
		return false;

	AppendMemberNode(_T("Cardinality"), cardinality_, newElement, xmlDoc);
	AppendMemberNode(_T("CardinalityStr"), Cardinality2String(cardinality_), newElement, xmlDoc);
	
	switch(cardinality_) {
		case enCardinality_SingleInstance:
			break;

		case enCardinality_Array:
			AppendMemberNode(_T("ArrayMinSize"), arrayMinSize_, newElement, xmlDoc);
			AppendMemberNode(_T("ArrayMaxSize"), arrayMaxSize_, newElement, xmlDoc);
			AppendMemberNode(_T("ArrayInterfaceGuid"), arrayInterfaceGuid_, newElement, xmlDoc);
			AppendMemberNode(_T("ArrayClassGuid"), arrayClassGuid_, newElement, xmlDoc);
			AppendMemberNode(_T("DisplayToolbar"), displayToolbar_, newElement, xmlDoc);
			AppendMemberNode(_T("UseContainerNode"), useContainerNode_, newElement, xmlDoc);
			break;

		case enCardinality_Map:
			AppendMemberNode(_T("MapMinSize"), mapMinSize_, newElement, xmlDoc);
			AppendMemberNode(_T("MapMaxSize"), mapMaxSize_, newElement, xmlDoc);
			AppendMemberNode(_T("MapSortKey"), mapSortKey_, newElement, xmlDoc);
			AppendMemberNode(_T("MapInterfaceGuid"), mapInterfaceGuid_, newElement, xmlDoc);
			AppendMemberNode(_T("MapClassGuid"), mapClassGuid_, newElement, xmlDoc);
			AppendMemberNode(_T("DisplayToolbar"), displayToolbar_, newElement, xmlDoc);
			AppendMemberNode(_T("UseContainerNode"), useContainerNode_, newElement, xmlDoc);
	}
	
	// append node to parent element
	//
	IXMLDOMNode* newOutNode;
	if (parentElement)
		hr = parentElement->appendChild(newElement, &newOutNode);
	else
		hr = xmlDoc->appendChild(newElement, &newOutNode);

	newOutNode->Release();
	return ret;
}

void CXmlClassCardinality::Clear()
{}

void CXmlClassCardinality::SetDefaults()
{
	cardinality_ = enCardinality_SingleInstance;

	arrayMinSize_ = 0;
	arrayMaxSize_ = 100;
	arrayInterfaceGuid_ = _T("");
	arrayClassGuid_ = _T("");

	mapMinSize_ = 0;
	mapMaxSize_ = 100;
	mapSortKey_ = _T("");
	mapInterfaceGuid_ = _T("");
	mapClassGuid_ = _T("");

	displayToolbar_ = false;
	useContainerNode_ = true;
}


void CXmlClassCardinality::SetMapInterfaceGuid(const cmString& guid)
{
	mapInterfaceGuid_ = guid;
}

cmString CXmlClassCardinality::GetMapInterfaceGuid() const
{
	return mapInterfaceGuid_;
}

void CXmlClassCardinality::SetMapClassGuid(const cmString& guid)
{
	mapClassGuid_ = guid;
}

cmString CXmlClassCardinality::GetMapClassGuid() const
{
	return mapClassGuid_;
}

void CXmlClassCardinality::SetArrayInterfaceGuid(const cmString& guid)
{
	arrayInterfaceGuid_ = guid;
}

cmString CXmlClassCardinality::GetArrayInterfaceGuid() const
{
	return arrayInterfaceGuid_;
}


void CXmlClassCardinality::SetArrayClassGuid(const cmString& guid)
{
	arrayClassGuid_ = guid;
}

cmString CXmlClassCardinality::GetArrayClassGuid() const
{
	return arrayClassGuid_;
}

void CXmlClassCardinality::SetDisplayToolbar(bool displayToolbar)
{
	displayToolbar_ = displayToolbar;
}

bool CXmlClassCardinality::GetDisplayToolbar() const
{
	return displayToolbar_;
}

void CXmlClassCardinality::SetUseContainerNode(bool use)
{
	useContainerNode_ = use;
}

bool CXmlClassCardinality::GetUseContainerNode() const
{
	return useContainerNode_;
}