/***************************************************************************
                   Convert Service Open Class  ( ConvertServiceOpen.cpp )
                -----------------------------------------

	begin      : 2013/02/28
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/02/28 - 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 "ConvertServiceOpen.h"
#include "Logger.h"

CConvertServiceOpen::CConvertServiceOpen()
{

}

CConvertServiceOpen::~CConvertServiceOpen()
{

}

int CConvertServiceOpen::ToJobData(int nServiceType, void* pFrom, void *pTo)
{
	int r = 0;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pTo);
	
	switch(nServiceType)
	{
	case CServiceInfo::XML:
		{
			pMap->insert(CJobDataPair(KEY_JOB_TYPE, JOB_SERVICE_OPEN));			
			try
			{
				const xmlpp::Node* node = const_cast<xmlpp::Node*>((xmlpp::Node*)pFrom);

				string svcid = GetElementValue(node, XML_STR_SVC_ID);

				// value_list
				pMap->insert(CJobDataPair(KEY_SP_USER_ID, GetElementValue(node, XML_STR_USER_ID)));
				pMap->insert(CJobDataPair(KEY_SP_USER_SEQ, GetElementValue(node, XML_STR_USER_SEQ)));
				pMap->insert(CJobDataPair(KEY_SP_USER_PASSWORD, GetElementValue(node, XML_STR_USER_PASSWORD)));
			
				pMap->insert(CJobDataPair(KEY_SP_SVC_ID, svcid));
				pMap->insert(CJobDataPair(KEY_SP_SVC_TRAN_ID, GetElementValue(node, XML_STR_SVC_SEQ)));
				pMap->insert(CJobDataPair(KEY_MAX_STR_SIZE, GetElementValue(node, XML_STR_SVC_STGSIZE)));
				pMap->insert(CJobDataPair(KEY_SP_SVC_BEGIN_DATE, GetElementValue(node, XML_STR_SVC_START_DATE)));
				pMap->insert(CJobDataPair(KEY_SP_SVC_END_DATE, GetElementValue(node, XML_STR_SVC_END_DATE)));
				pMap->insert(CJobDataPair(KEY_INITIAL_VECTOR, GetElementValue(node, XML_STR_SVC_AUTHSTRING)));
				pMap->insert(CJobDataPair(KEY_PRIVATE_KEY, GetElementValue(node, XML_STR_SVC_PRIVATEKEY)));

				pMap->insert(CJobDataPair(KEY_REPLICATION_COUNT, GetElementValue(node, XML_STR_SVC_REPLICA)));
				pMap->insert(CJobDataPair(KEY_URI_IGNORE_CASE, GetElementValue(node, XML_STR_SVC_URIIGNORECASE)));
				pMap->insert(CJobDataPair(KEY_DEL_FILE_KEEP_TIME, GetElementValue(node, XML_STR_SVC_DELFILEKEEPTIME)));
				pMap->insert(CJobDataPair(KEY_CAL_USED_DEPTH, GetElementValue(node, XML_STR_SVC_USEDSIZECALCDEPTH)));

				string svcDomain = GetElementValue(node, XML_STR_SVC_DOMAIN);
				if(!svcDomain.empty())				
					pMap->insert(CJobDataPair(KEY_HOST_NAME, svcDomain));

				// 2014.11.03 rc_syncd 설정을 위한 설정
				pMap->insert(CJobDataPair(KEY_LINK_MASTER_YN, GetElementValue(node, XML_STR_LINK_MASTER_YN)));
				pMap->insert(CJobDataPair(KEY_LINK_SP_SVC_TRAN_ID, GetElementValue(node, XML_STR_LINK_SERVICE_SEQ)));
				pMap->insert(CJobDataPair(KEY_LINK_RCTS, GetElementValue(node, XML_STR_LINK_RCTS)));

				// 2014.08.07
				// 추가 속성에 대한 값을 받을 수 있게 처리함
				pMap->insert(CJobDataPair(KEY_BANDWIDTH_CTRL_YN, GetElementValue(node, XML_STR_TRAFFICLIMIT_YN)));
				pMap->insert(CJobDataPair(KEY_VOLUME_BANDWIDTH_LIMIT, GetElementValue(node, XML_STR_TRAFFICLIMIT)));

				pMap->insert(CJobDataPair(KEY_VALIDATE_SP_SESSION_ID_YN, GetElementValue(node, XML_STR_CUSTOMAUTH_YN)));
				pMap->insert(CJobDataPair(KEY_REQ_AUTH_MODE, GetElementValue(node, XML_STR_CUSTOMAUTH_MODE)));
				pMap->insert(CJobDataPair(KEY_AUTH_SERVER, GetElementValue(node, XML_STR_AUTHSERVER)));
				pMap->insert(CJobDataPair(KEY_AUTH_PATH, GetElementValue(node, XML_STR_AUTHPATH)));
				pMap->insert(CJobDataPair(KEY_AUTH_PORT, GetElementValue(node, XML_STR_AUTHPORT)));
				pMap->insert(CJobDataPair(KEY_AUTH_TIMEOUT, GetElementValue(node, XML_STR_AUTHTIMEOUT)));
				pMap->insert(CJobDataPair(KEY_BILL_SERVER, GetElementValue(node, XML_STR_BILLSERVER)));
				pMap->insert(CJobDataPair(KEY_BILL_PATH, GetElementValue(node, XML_STR_BILLPATH)));
				pMap->insert(CJobDataPair(KEY_BILL_PORT, GetElementValue(node, XML_STR_BILLPORT)));
				pMap->insert(CJobDataPair(KEY_BILL_TIMEOUT, GetElementValue(node, XML_STR_BILLTIMEOUT)));
						
				r = pMap->size();
			}
			catch(const std::exception& ex)
			{
				LOG(LERR,"Exception caught: %s" , ex.what() );
			}
		}
		break;
	case CServiceInfo::SDK:
		{
			SDKReqBodySpInfo *pBody = (SDKReqBodySpInfo*)pFrom;
			pMap->insert(CJobDataPair(KEY_JOB_TYPE, JOB_SERVICE_OPEN));
			
			pMap->insert(CJobDataPair(KEY_SP_USER_SEQ, pBody->szSpUserSeq));
			pMap->insert(CJobDataPair(KEY_SP_USER_ID, pBody->szSpUserId));
			pMap->insert(CJobDataPair(KEY_SP_USER_PASSWORD, pBody->szSpUserPassword));
			pMap->insert(CJobDataPair(KEY_SP_SVC_TRAN_ID, pBody->szSpSvcTranId));
			pMap->insert(CJobDataPair(KEY_SP_SVC_ID, pBody->szSpSvcId));
			pMap->insert(CJobDataPair(KEY_SP_SVC_BEGIN_DATE, pBody->szSpSvcBeginDate));
			pMap->insert(CJobDataPair(KEY_SP_SVC_END_DATE, pBody->szSpSvcEndDate));
			pMap->insert(CJobDataPair(KEY_INITIAL_VECTOR, pBody->szInitialVector));
			pMap->insert(CJobDataPair(KEY_PRIVATE_KEY, pBody->szPrivateKey));
			pMap->insert(CJobDataPair(KEY_MAX_BANDWIDTH, pBody->szMaxBandwidth));
			pMap->insert(CJobDataPair(KEY_MAX_STR_SIZE, pBody->szVolQuota));
			pBody->szSyncYN;
			r = pMap->size();
		}
		break;
	case CServiceInfo::ETC:
	default:
		r = JOB_ERR_CONVERT;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}		
	
	return r;
}

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

	switch(nServiceType)
	{
	case CServiceInfo::XML:
		{
			string strtype  = GetJobMapValue(pFrom, KEY_RES_XML_JOB_TYPE);
			string strlist  = GetJobMapValue(pFrom, KEY_RES_XML_JOB_TYPE_List);

			string svcid = GetJobMapValue(pFrom, KEY_SP_SVC_ID);
			string userid = GetJobMapValue(pFrom, KEY_SP_USER_ID);

			string volumeid =  svcid + "@" + userid;
			string rValue = ((nEnd == JOB_SUCCESS) ? JOX_RESULT_OK : JOX_RESULT_FAIL);
			ostringstream rcode;
			rcode << nEnd;

			try
			{
				xmlpp::Document document;
				// root 
				xmlpp::Element* nodeRoot = document.create_root_node(XML_STR_ROOT, "", "");
				// Response
				xmlpp::Element* nodeResponses = nodeRoot->add_child(XML_STR_JOB);

				nodeResponses->set_attribute(XML_STR_JOB_TYPE, strtype);
				if(!strlist.empty())
					nodeResponses->set_attribute(XML_STR_JOB_TYPE_LIST, strlist);
				SetElementValue(nodeResponses, XML_STR_VOLUME_ID, volumeid);		
				SetElementValue(nodeResponses, XML_STR_RESULT, rValue);
				SetElementValue(nodeResponses, XML_STR_RESULT_CODE, rcode.str());

				string szData = document.write_to_string();

				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, "CConvertServiceOpen ToClientData(XML) : %s, %d", (char *)r, outlen);
				}
				else 
				{
					outlen = -1;
					LOG(LERR, "Memory allocation error.[%d]", errno);
				}
			}
			catch(const std::exception& ex)
			{
				outlen = -1;
				LOG(LERR,"Exception caught: %s" , ex.what() );
			}
		}
		break;
	case CServiceInfo::SDK:
		{
			outlen = 0;
		}
		break;
	case CServiceInfo::ETC:
	default:
		outlen = -1;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}

	return r;
}



