/***************************************************************************
                   Convert Request Service Link Class  ( ConvertReqServiceLink.cpp )
                -----------------------------------------

	begin      : 2013/03/18
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/03/18 - 1st dadamin
	email      : dev1@solbox.com
	version    : 3.2.0

	CopyRight(C) 2005 Solbox Inc. All Rights reserved.
	Redistribution and use in source and binary forms, with or with out
	modification, are not permitted in outside of Solbox Inc.
***************************************************************************/
#include "rc_mngd.h"
#include "Convert.h"
#include "ConvertReqServiceLink.h"
#include "Logger.h"
#include "UtilFn.h"

CConvertReqSvcLink::CConvertReqSvcLink(string subkey /* = "" */)
	: m_subkey(subkey)
{

}

CConvertReqSvcLink::~CConvertReqSvcLink()
{

}

int CConvertReqSvcLink::ToJobData(int nServiceType, void* pFrom, void *pTo)
{
	int r = 0;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pTo);

	switch(nServiceType)
	{
	case CServiceInfo::XML:
		{
			//if (m_subkey.compare(JOB_SERVICE_MOVE_REQUEST) == 0 || 
			//	m_subkey.compare(JOB_SERVICE_MOVE_COMPLETE) == 0)
			//{
				try
				{
					const xmlpp::Node* node = const_cast<xmlpp::Node*>((xmlpp::Node*)pFrom);
										
					pMap->insert(CJobDataPair(KEY_VOLUME_ID, GetElementValue(node, XML_STR_VOLUME_ID)));
					pMap->insert(CJobDataPair(KEY_VOLUME_SEQ, GetElementValue(node, XML_STR_VOLUME_SEQ)));
					pMap->insert(CJobDataPair(KEY_RESP_RESULT, GetElementValue(node, XML_STR_RESULT)));
					pMap->insert(CJobDataPair(KEY_RESP_RESULT_CODE, GetElementValue(node, XML_STR_RESULT_CODE)));

					r = pMap->size();
				}
				catch (const std::exception& ex)
				{
					LOG(LERR, "Exception caught: %s", ex.what());
				}
			//}
			//else
			//{
			//	r = JOB_ERR_CONVERT;
			//	LOG(LERR, "Service does not support the conversion.");
			//}
		}		
		break;
	case CServiceInfo::SDK:
	case CServiceInfo::ETC:
	default:
		r = JOB_ERR_CONVERT;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}		

	return r;
}

string CConvertReqSvcLink::MakeServiceLinkXML(void* pData)
{
	string r;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pData);

	try
	{

		xmlpp::Document document;
		// root 
		xmlpp::Element* nodeRoot = document.create_root_node(XML_STR_ROOT, "", "");

		xmlpp::Element* nodeRequest = nodeRoot->add_child(XML_STR_JOB);

		nodeRequest->set_attribute(XML_STR_JOB_TYPE, XML_STR_TYPE_CHANGE);
		nodeRequest->set_attribute(XML_STR_JOB_TYPE_LIST, XML_STR_SVC_SYNC_LINK);

		SetElementValue(nodeRequest, XML_STR_VOLUME_SEQ, GetJobMapValue(pData, KEY_VOLUME_SEQ));
		SetElementValue(nodeRequest, XML_STR_SELF_RCTS, GetJobMapValue(pData, KEY_SELF_RCTS));
		SetElementValue(nodeRequest, XML_STR_LINK_MASTER_YN, GetJobMapValue(pData, KEY_LINK_MASTER_YN));
		SetElementValue(nodeRequest, XML_STR_LINK_USER_SEQ, GetJobMapValue(pData, KEY_LINK_USER_SEQ));
		SetElementValue(nodeRequest, XML_STR_LINK_SERVICE_SEQ, GetJobMapValue(pData, KEY_LINK_SP_SVC_TRAN_ID));
		SetElementValue(nodeRequest, XML_STR_LINK_RCTS, GetJobMapValue(pData, KEY_LINK_RCTS));
		SetElementValue(nodeRequest, XML_STR_LINK_CODE, GetJobMapValue(pData, KEY_REQ_LINK_CODE));
		SetElementValue(nodeRequest, XML_STR_LINK_ONE_WAY, GetJobMapValue(pData, KEY_REQ_LINK_ONE_WAY));
		
		r = document.write_to_string();
	}
	catch (const std::exception& ex)
	{
		LOG(LERR, "Exception caught: %s", ex.what());
	}


	return r;
}

void* CConvertReqSvcLink::ToClientData(int nServiceType, int nEnd, void* pFrom, int & outlen)
{
	void *r = NULL;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pFrom);

	switch(nServiceType)
	{	
	case CServiceInfo::XML:
		{
			string szData;


			szData = MakeServiceLinkXML(pFrom);

			if (szData.size())
			{
				BYTE *t = NULL;
				outlen = szData.size();
				t = new BYTE[outlen + 1];

				if (t)
				{
					memcpy(t, szData.c_str(), outlen);
					t[outlen] = '\0';
					r = (void*)t;
					LOG(LDBG, "CConvertReqSvcMove ToClientData(XML) : %s, %d", (char *)r, outlen);
				}
				else
				{
					outlen = -1;
					LOG(LERR, "Memory allocation error.[%d]", errno);
				}
			}
		}
		break;
	case CServiceInfo::SDK:
	case CServiceInfo::ETC:
	default:
		outlen = -1;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}

	return r;
}
