/*
 *  Author: bwilliams
 *  Created: April 6, 2012
 *
 *  Copyright (C) 2012-2016 VMware, Inc.  All rights reserved. -- VMware Confidential
 *
 *  This code was generated by the script "build/dev/codeGen/genCppXml". Please
 *  speak to Brian W. before modifying it by hand.
 *
 */

#include "stdafx.h"

#include "Doc/DocUtils/EnumConvertersXml.h"
#include "Doc/DocXml/SchemaTypesXml/ClassCardinalityXml.h"
#include "Doc/DocXml/SchemaTypesXml/JoinTypeXml.h"
#include "Doc/SchemaTypesDoc/SchemaTypesDocTypes.h"

#include "Doc/SchemaTypesDoc/CClassCardinalityDoc.h"
#include "Doc/SchemaTypesDoc/CJoinTypeDoc.h"
#include "Doc/SchemaTypesDoc/CLogicalRelationshipDoc.h"
#include "Xml/XmlUtils/CXmlElement.h"
#include "Doc/DocXml/SchemaTypesXml/LogicalRelationshipXml.h"

using namespace Caf;

void LogicalRelationshipXml::add(
	const SmartPtrCLogicalRelationshipDoc logicalRelationshipDoc,
	const SmartPtrCXmlElement thisXml) {
	CAF_CM_STATIC_FUNC_VALIDATE("LogicalRelationshipXml", "add");

	CAF_CM_ENTER {
		CAF_CM_VALIDATE_SMARTPTR(logicalRelationshipDoc);
		CAF_CM_VALIDATE_SMARTPTR(thisXml);

		const std::string namespaceValVal = logicalRelationshipDoc->getNamespaceVal();
		CAF_CM_VALIDATE_STRING(namespaceValVal);
		thisXml->addAttribute("namespace", namespaceValVal);

		const std::string nameVal = logicalRelationshipDoc->getName();
		CAF_CM_VALIDATE_STRING(nameVal);
		thisXml->addAttribute("name", nameVal);

		const std::string versionVal = logicalRelationshipDoc->getVersion();
		CAF_CM_VALIDATE_STRING(versionVal);
		thisXml->addAttribute("version", versionVal);

		const std::string arityVal =
			EnumConvertersXml::convertArityTypeToString(logicalRelationshipDoc->getArity());
		CAF_CM_VALIDATE_STRING(arityVal);
		thisXml->addAttribute("arity", arityVal);

		const SmartPtrCClassCardinalityDoc dataClassLeftVal =
			logicalRelationshipDoc->getDataClassLeft();
		CAF_CM_VALIDATE_SMARTPTR(dataClassLeftVal);

		const SmartPtrCXmlElement dataClassLeftXml =
			thisXml->createAndAddElement("dataClassLeft");
		ClassCardinalityXml::add(dataClassLeftVal, dataClassLeftXml);

		const SmartPtrCClassCardinalityDoc dataClassRightVal =
			logicalRelationshipDoc->getDataClassRight();
		CAF_CM_VALIDATE_SMARTPTR(dataClassRightVal);

		const SmartPtrCXmlElement dataClassRightXml =
			thisXml->createAndAddElement("dataClassRight");
		ClassCardinalityXml::add(dataClassRightVal, dataClassRightXml);

		const std::deque<SmartPtrCJoinTypeDoc> joinVal =
			logicalRelationshipDoc->getJoinCollection();
		CAF_CM_VALIDATE_STL(joinVal);

		if (! joinVal.empty()) {
			for (TConstIterator<std::deque<SmartPtrCJoinTypeDoc> > joinIter(joinVal);
				joinIter; joinIter++) {
				const SmartPtrCXmlElement joinXml =
					thisXml->createAndAddElement("join");
				JoinTypeXml::add(*joinIter, joinXml);
			}
		}

		const std::string descriptionVal = logicalRelationshipDoc->getDescription();
		if (! descriptionVal.empty()) {
			thisXml->addAttribute("description", descriptionVal);
		}
	}
	CAF_CM_EXIT;
}

SmartPtrCLogicalRelationshipDoc LogicalRelationshipXml::parse(
	const SmartPtrCXmlElement thisXml) {
	CAF_CM_STATIC_FUNC_VALIDATE("LogicalRelationshipXml", "parse");

	SmartPtrCLogicalRelationshipDoc logicalRelationshipDoc;

	CAF_CM_ENTER {
		CAF_CM_VALIDATE_SMARTPTR(thisXml);

		const std::string namespaceValStrVal =
			thisXml->findRequiredAttribute("namespace");
		const std::string namespaceValVal = namespaceValStrVal;

		const std::string nameStrVal =
			thisXml->findRequiredAttribute("name");
		const std::string nameVal = nameStrVal;

		const std::string versionStrVal =
			thisXml->findRequiredAttribute("version");
		const std::string versionVal = versionStrVal;

		const std::string arityStrVal =
			thisXml->findRequiredAttribute("arity");
		ARITY_TYPE arityVal = ARITY_NONE;
		if (! arityStrVal.empty()) {
			arityVal = EnumConvertersXml::convertStringToArityType(arityStrVal);
		}

		const SmartPtrCXmlElement dataClassLeftXml =
			thisXml->findRequiredChild("dataClassLeft");

		SmartPtrCClassCardinalityDoc dataClassLeftVal;
		if (! dataClassLeftXml.IsNull()) {
			dataClassLeftVal = ClassCardinalityXml::parse(dataClassLeftXml);
		}

		const SmartPtrCXmlElement dataClassRightXml =
			thisXml->findRequiredChild("dataClassRight");

		SmartPtrCClassCardinalityDoc dataClassRightVal;
		if (! dataClassRightXml.IsNull()) {
			dataClassRightVal = ClassCardinalityXml::parse(dataClassRightXml);
		}

		const CXmlElement::SmartPtrCElementCollection joinChildrenXml =
			thisXml->findRequiredChildren("join");

		std::deque<SmartPtrCJoinTypeDoc> joinVal;
		if (! joinChildrenXml.IsNull() && ! joinChildrenXml->empty()) {
			for (TConstIterator<CXmlElement::CElementCollection> joinXmlIter(*joinChildrenXml);
				joinXmlIter; joinXmlIter++) {
				const SmartPtrCXmlElement joinXml = joinXmlIter->second;
				const SmartPtrCJoinTypeDoc joinDoc =
					JoinTypeXml::parse(joinXml);
				joinVal.push_back(joinDoc);
			}
		}

		const std::string descriptionStrVal =
			thisXml->findOptionalAttribute("description");
		const std::string descriptionVal = descriptionStrVal;

		logicalRelationshipDoc.CreateInstance();
		logicalRelationshipDoc->initialize(
			namespaceValVal,
			nameVal,
			versionVal,
			arityVal,
			dataClassLeftVal,
			dataClassRightVal,
			joinVal,
			descriptionVal);
	}
	CAF_CM_EXIT;

	return logicalRelationshipDoc;
}

