/***************************************************************************
                   File Control Class ( FileControl.cpp )
                -----------------------------------------

	begin      : 2013/03/20
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/03/20 - 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 "FileControl.h"
#include "JobData.h"
#include "Configs.h"
#include "Logger.h"
#include "UtilFn.h"

CFileControl::CFileControl()
{

}

CFileControl::~CFileControl()
{

}

int CFileControl::beforecheck(CJobDataMap & jobData, bool & IgnoreCase)
{
	int r = JOB_SUCCESS;
	CRCDBControl rcdb;

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

	do 
	{
		// check info
		int t = 0;
		t = ExistInfo(rcdb, jobData);
		if (t != 1)
		{
			r = t;
			LOG(LERR, "Base information check error.");
			break;
		}

		if (!rcdb.CheckUriIgnoreCase(jobData, IgnoreCase))
		{
			r = JOB_ERR_FILE_CHK_IGNORECASE;
			break;
		}

		// find file info
		if (!rcdb.GetFileInfo(jobData, IgnoreCase))
		{
			r = JOB_ERR_CHK_FILE_INFO;
			break;
		}

	} while (false);
	
	rcdb.Final();

	return r;
}

int CFileControl::run( CJobDataMap &jobData )
{
	int r = JOB_SUCCESS;
	_LOG( LDBG, "File Control running...");

	CMyConfig* pConf = CMyConfig::GetInstance();

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

	while ( 1 ) 
	{
		// (FHS 통신 발생 시) DB 점유로 인한 pool 획득 실패 방지를 위해서 
		// 사전 체크 항목 분리함
		bool ignoreCase = false;
		r = beforecheck(jobData, ignoreCase);
		if (r != JOB_SUCCESS)
			break;
		
		if (jobData[KEY_HOST_NAME].empty())
			r = JOB_ERR_FILE_NOT_FOUND;

		if ( IsSubKey(jobData, JOB_FILE_CTL_EXIST) )
		{
			jobData[KEY_RES_FILE_SIZE] = jobData[KEY_GET_CONTENT_LENGTH];
		}
		else if (IsSubKey(jobData, JOB_FILE_CTL_MKDIR))
		{
			if (r == JOB_ERR_FILE_NOT_FOUND)
			{
				CRCDBControl rcdb;

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

				do 
				{
					if (!rcdb.CheckParentDir(jobData, ignoreCase))
					{
						r = JOB_ERR_CHK_PARENT;
						break;
					}

					if (jobData[KEY_URI_PARENT].empty())
					{
						jobData[KEY_URI_PARENT] = p_name(jobData[KEY_URI]);
						if (!rcdb.MakeDir(jobData, jobData[KEY_URI_PARENT], ignoreCase))
						{
							r = JOB_ERR_FILE_MKDIR_PARENT;
							break;
						}
					}

					if (rcdb.MakeDir(jobData, jobData[KEY_URI], ignoreCase))
						r = JOB_SUCCESS;
					else
						r = JOB_ERR_FILE_MKDIR;
				} while (false);				

				rcdb.Final();
			}
			else
			{
				r = JOB_ERR_FILE_EXIST;
			}
		}
		else if (FindInSubKey(jobData, JOB_FILE_CTL_GET_HASH))
		{
			// file exist
			if (r == JOB_SUCCESS)
			{
				CFhsControl fhs;
				if (!fhs.Create(pConf->GetRCID(), jobData[KEY_HOST_NAME].c_str(), pConf->GetFHSControlTCP(), false))
				{
					r = JOB_ERR_CREATION_FAIL;
					LOG(LERR, "FHS Control Initialization failed.");
					break;
				}

				if (IsSubKey(jobData, JOB_FILE_CTL_GET_HASH_10MB))
				{
					if (!fhs.GetFront10MMD5(jobData))
					{
						r = JOB_ERR_FILE_HASH;
						break;
					}
				} 
				else if (IsSubKey(jobData, JOB_FILE_CTL_TOTO_EXTRACT_WITHPLATFORM))
				{
					if (!fhs.GetTotoExtractWithplatform(jobData))
					{
						r = JOB_ERR_FILE_HASH;
						break;
					}
				}
				else 
				{
					r = JOB_ERR_UNKNOWN;
				}
			}
		}
		else 
		{
			r = JOB_ERR_UNKNOWN;			
		}
				
		break;
	}

	if( r != 0 )
	{
		if (r == JOB_ERR_UNKNOWN)
			LOG(LERR, "Subkey is unknown.");

		LOG( LERR, "File Control(%s|%d) error.[%s@%s]",
					jobData[KEY_JOBSUB_TYPE].c_str(), r,
					jobData[KEY_SP_SVC_ID].c_str(),	jobData[KEY_SP_USER_ID].c_str());
	}
	else 
	{
		_LOG( LINF, "File Control(%s) success.[%s@%s]",
					jobData[KEY_JOBSUB_TYPE].c_str(),
					jobData[KEY_SP_SVC_ID].c_str(),	jobData[KEY_SP_USER_ID].c_str());
	}

	_LOG( LDBG, "File Control end.");
	return r;
}

