/***************************************************************************
                   RCDB Control Class Header ( RCDBControl.h )
                -----------------------------------------

	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.
***************************************************************************/
#ifndef __RCDB_CONTROL_H__
#define __RCDB_CONTROL_H__

#include "JobData.h"

class DataBase;

class CRCDBControl
{
public:
	CRCDBControl();
	~CRCDBControl();

	int Init();
	int Final();

	bool BeginTrans();
	bool EndTrans();

	bool	GetVolumeInformation(CJobDataMap & jobData);
	bool	GetAnonyDir(CJobDataMap & jobData, string targeturi, vector<string> & rdata);
	bool	GetFileInfo(CJobDataMap & jobData, bool IgnoreCase = false);
			
	bool	CheckUserInfo(CJobDataMap & jobData, bool & exist);			// check t_sms_sp_user
	bool	CheckAnotherProduct(CJobDataMap & jobData, bool & exist);	// check t_sms_sp_svc_product
	bool	CheckProductInfo(CJobDataMap & jobData, bool & exist);		// check t_sms_sp_svc_product
	bool	CheckProductExtAuth(CJobDataMap & jobData, bool & exist);	// check t_sms_sp_svc_product_val
	bool	CheckProductExtBand(CJobDataMap & jobData, bool & exist);	// check t_sms_sp_svc_product_band	
	bool	CheckProductSync(CJobDataMap & jobData, bool & exist);		// check t_sms_sp_svc_product_sync	
	bool	CheckVolumeInfo(CJobDataMap & jobData, bool & exist);		// check t_dav_use_dir
	bool	CheckRootInfo(CJobDataMap & jobData, bool & exist);			// check meta table
	bool	CheckParentDir(CJobDataMap & jobData, bool & IgnoreCase);	// check parent dir
	bool	HasFile(CJobDataMap & jobData, bool & exist);				// file in meta table
	bool	CheckUriIgnoreCase(CJobDataMap & jobData, bool & IgnoreCase); // check uri ignore case
	bool	GetTotalCntFiles(CJobDataMap & jobData, uint64_t & total, uint64_t limit = 0); // total count file 
	bool	GetServiceInfoAll(CJobDataMap & jobData);					// get service information all
	bool	GetSyncLink(CJobDataMap & jobData);							// get sync link information of service
		
	
	bool	SetUserInfo(CJobDataMap & jobData, bool & exist);			// Set t_sms_sp_user	
	bool	SetProductInfo(CJobDataMap & jobData, bool & exist);		// Set t_sms_sp_svc_product	
	bool	SetProductExtAuth(CJobDataMap & jobData, bool & exist);		// Set t_sms_sp_svc_product_val
	bool	SetProductBand(CJobDataMap & jobData, bool & exist);		// Set t_sms_sp_svc_product_band	
	bool	SetProductSync(CJobDataMap & jobData, bool & exist);		// Set t_sms_sp_svc_product_sync
	bool	SetVolumeInfo(CJobDataMap & jobData, bool & exist);			// Set t_dav_use_dir
	bool	SetRootInfo(CJobDataMap & jobData, bool & exist);			// Set meta table root
	bool	SetAnonyDir(CJobDataMap & jobData, string targeturi, bool isdel = false);		// Set t_dav_anonymous
	bool	SetMetaIgnoreCase(CJobDataMap & jobData, bool IgnoreCase);	// Set meta table ignore case
	bool	MakeDir(CJobDataMap & jobData, string & spath, bool & IgnoreCase);			// Make Dir
	bool	DropMetaTable(CJobDataMap & jobData);				// DROP meta table
	
	inline void	SetSuccess(bool success)	{ m_success = success; }
	inline bool	IsSucces()					{ return m_success; }
	inline void SetUsed(short nMode )		{ m_used = nMode; }
	inline short GetUsed()					{ return m_used; }
	void	ForceClose();
protected:
	// t_sms_sp_user
	bool	InsertUerInfo(CJobDataMap & jobData);
	bool	UpdateUserInfo(CJobDataMap & jobData);
	bool	DeleteUserInfo(CJobDataMap & jobData);
	// t_sms_sp_svc_product
	bool	InsertProductInfo(CJobDataMap & jobData);
	bool	UpdateProductInfo(CJobDataMap & jobData);
	bool	DeleteProductInfo(CJobDataMap & jobData);
	// t_sms_sp_svc_product_val
	bool	InsertProductExtAuth(CJobDataMap & jobData);
	bool	UpdateProductExtAuth(CJobDataMap & jobData);
	bool	DeleteProductExtAuth(CJobDataMap & jobData);
	// t_sms_sp_svc_product_band
	bool	InsertProductBand(CJobDataMap & jobData);
	bool	UpdateProductBand(CJobDataMap & jobData);
	bool	DeleteProductBand(CJobDataMap & jobData);
	// t_sms_sp_svc_product_sync
	bool	InsertProductSync(CJobDataMap & jobData);
	bool	DeleteProductSync(CJobDataMap & jobData);
	// t_dav_t_dav_use_dir
	bool	InsertUseDir(CJobDataMap & jobData);
	bool	DeleteUseDir(CJobDataMap & jobData);
	// meta table
	bool	InsertRootInfo(CJobDataMap & jobData);
	bool	UpdateRootInfo(CJobDataMap & jobData);	
	bool	DeleteRootInfo(CJobDataMap & jobData);
	// t_dav_anonymous
	bool	DeleteAnonymous(CJobDataMap & jobData, string &targeturi);
	bool	InsertAnonymous(CJobDataMap & jobData, string &targeturi);

	bool	CheckJobDataUserInfo(CJobDataMap & jobData, bool & hasuserseq, bool & hasuserid);
	bool	CheckJobDataSvcProduct(CJobDataMap & jobData, bool & hastranid, bool & hassvcid);
	bool	SetDB2JobData(bool & has, CJobDataMap & jobData, string valkey, const char * val);
	bool	SetDB2JobData(CJobDataMap & jobData, string valkey, const char * val, bool force = false);

	bool	GetTargetFHS(string & targethost);

	string	InsertStr(CJobDataMap & jobData, const char* key, int index, const char **paramValues);
	string	UpdateStr(CJobDataMap & jobData, const char* key, int index, const char **paramValues);
	string	UpdateStr(CJobDataMap & jobData, const char* key);
	string	WhereStr(CJobDataMap & jobData, const char* key, int &index, const char **paramValues, bool IgnoreCase = false);
	bool	DoSQL(string  sql, int nParamCnt = 0, const char * const *paramValues = NULL);
	bool	DoSetSQL(string  sql,  int nParamCnt = 0, const char * const *paramValues = NULL);

	// 2014.06.07 dadamin
	// 메타 테이블명 반환
	string	GetMataTableName(const char* tranid);
private:
	DataBase*	m_pDB;
	short		m_used;
	bool		m_success;

	// 2014.06.07 dadamin
	// -1: unset, 0: t_dav_resource, 1: t_meat_[sp_svc_tran_id]
	int			m_tabletype;
	bool		m_usedisplay;
};

#endif // __RCDB_CONTROL_H__
