/***************************************************************************
                   Service Open Class (ServiceOpen.cpp)
                -----------------------------------------

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

CServiceOpen::CServiceOpen()
{

}

CServiceOpen::~CServiceOpen()
{

}

int CServiceOpen::run( CJobDataMap &jobData )
{
	int r = JOB_SUCCESS;
	_LOG( LDBG, "Job Service Open running...");

	CMyConfig* pConf = CMyConfig::GetInstance();

	if(!pConf)
	{
		LOG( LERR, "Config is NULL.");
		return JOB_ERR_CREATION_FAIL;
	}

	// rcdb
	CRCDBControl rcdb;

	if( rcdb.Init() != 0 )
	{
		LOG( LERR, "RCDB Initialization failed.");
		return JOB_ERR_CREATION_FAIL;
	}

	// 2014.07.15
	// fimngd 주기적(3분)으로 생성하기 때문에 서비스 시 개통 시 fhs로 신호 보낼 필요 없음

	// check relation value
	if( !jobData[KEY_AUTH_SERVER].empty() )
	{
		jobData[KEY_VALIDATE_SP_SESSION_ID_YN] = "Y";
		if(!jobData[KEY_REFERER_CHK_URL].empty())
		{
			jobData[KEY_REFERER_CHK_YN] = "Y";
		}
	}

	if(!jobData[KEY_VOLUME_BANDWIDTH_LIMIT].empty() || !jobData[KEY_SESSION_BANDWIDTH_LIMIT].empty())
	{
		jobData[KEY_BANDWIDTH_CTRL_YN] = "Y";
	}

	bool existuser = false, existproduct = false;
	bool existproductauth = false, existproductband = false, existproductsync = false;
	bool existvolume = false, existroot = false;

	while ( 1 )
	{
		// check 
		if( !rcdb.CheckUserInfo(jobData, existuser) )
		{
			r = JOB_ERR_CHK_USER;
			break;
		}

		if(existuser && !rcdb.CheckProductInfo(jobData, existproduct) )
		{
			r = JOB_ERR_CHK_PRODUCT;
			break;
		}

		if( existproduct && !rcdb.CheckProductExtAuth(jobData, existproductauth) )
		{
			r = JOB_ERR_CHK_PRODUCT_EXTAUTH;
			break;
		}

		if( existproduct && !rcdb.CheckProductExtBand(jobData, existproductband) )
		{
			r = JOB_ERR_CHK_PRODUCT_BAND;
			break;
		}

		if (existproduct && !rcdb.CheckProductSync(jobData, existproductsync))
		{
			r = JOB_ERR_CHK_PRODUCT_SYNC;
			break;
		}

		if( existproduct && !rcdb.CheckVolumeInfo(jobData, existvolume) )
		{
			r = JOB_ERR_CHK_VOLUME_INFO;
			break;
		}

		if( existproduct && !rcdb.CheckRootInfo(jobData, existroot) )
		{
			r = JOB_ERR_CHK_ROOT_INFO;
			break;
		}

		if( !rcdb.BeginTrans() )
		{
			r = JOB_ERR_RCDB_BEGIN_TRANS;
			break;
		}

		do
		{
			// set
			if( !rcdb.SetUserInfo(jobData, existuser) )
			{
				r = JOB_ERR_SET_USER;
				break;
			}

			if( !rcdb.SetProductInfo(jobData, existproduct) )
			{
				r = JOB_ERR_SET_PRODUCT;
				break;
			}

			if( !rcdb.SetProductExtAuth(jobData, existproductauth) )
			{
				r = JOB_ERR_SET_PRODUCT_EXTAUTH;
				break;
			}

			if( !rcdb.SetProductBand(jobData, existproductband) )
			{
				r = JOB_ERR_SET_PRODUCT_BAND;
				break;
			}

			if (!rcdb.SetProductSync(jobData, existproductsync))
			{
				r = JOB_ERR_SET_PRODUCT_SYNC;
				break;
			}

			if( !rcdb.SetRootInfo(jobData, existroot) )
			{
				r = JOB_ERR_SET_ROOT_INFO;
				break;
			}

			// 2014.01.08 pgpool 비정상 종료 현상 때문에 쿼리 실행 순서 변경
			// root 생성 후 볼륨 용량 정보 관련 테이블 생성되도록 변경함
			if (!rcdb.SetVolumeInfo(jobData, existvolume))
			{
				r = JOB_ERR_SET_VOLUME_INFO;
				break;
			}

		} while(false);

		if(r == JOB_SUCCESS)
			rcdb.SetSuccess(true);
		else 
			rcdb.SetSuccess(false);

		if( !rcdb.EndTrans() )
			r = JOB_ERR_RCDB_END_TRANS;
		break;
	}

	if(r != 0)
	{
		// pgpool 3.0.9 이전 버전 경우 정적 쿼리 수행 후 동일 세션을 사용 시
		// pgpool 비정상 종료 현상이 발생하기 때문에 사용한 Pool을 강제 종료 시킴
		rcdb.ForceClose();
		LOG( LERR, "Service Open error.[%d][%s@%s]", r,
					jobData[KEY_SP_SVC_ID].c_str(),	jobData[KEY_SP_USER_ID].c_str());
	}
	else 
	{
		_LOG( LINF, "Service Open [%s@%s].",
					jobData[KEY_SP_SVC_ID].c_str(),	jobData[KEY_SP_USER_ID].c_str());
	}

	rcdb.Final();

	_LOG( LDBG, "Job Service Open End.");
	return r;
}
