/*******************************************************************************
 *	All Rights reserved,Copyright (c) K-Opticom
 ********************************************************************************
 *＜プログラム内容＞
 *	システム名		：eo顧客基幹システム
 *	モジュール名	：JZMejbZM0021SecProc
 *	ソースファイル名：JZMejbZM0021SecProc.java
 *	作成者			：富士通
 *	日付			：2012年02月21日
 *＜機能概要＞
 *	ロール_権限に対する副次処理を行う部品です。
 *＜修正履歴＞
 *	バージョン	修正日		修正者		修正内容
 *	v3.00		2012/02/21	FJ) 遠藤	新規作成
 *	v3.00		2012/02/21	FJ) 遠藤	【SGY-2012-000006】統合認証対応
 *
 ********************************************************************************/

package eo.ejb.common.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.fujitsu.futurity.model.base.CAANRemoveException;
import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.base.CAANRuntimeException;

import com.fujitsu.futurity.model.ejb.common.JSYejbConnection;
import com.fujitsu.futurity.model.ejb.common.fw.AgentDispatchContext;

import eo.ejb.cbm.entity.ZM0021ETMsg;
import eo.ejb.common.JCCModelCommon;

/**
 * <p>
 * ロール_権限副次処理部品です。
 * </p>
 * @author 富士通
 */
public class JZMejbZM0021SecProc extends JPCejbDBABase
{
	/**
	 * <p>
	 * 新しいJZMejbZM0021SecProcを作成します。
	 * </p>
	 */
	public JZMejbZM0021SecProc()
	{
		super(ZM0021ETMsg.class.getName());
	}

	/**
	 * <p>
	 * 世代を管理するカラム名を取得します。
	 * </p>
	 * @return 世代を管理するカラム名
	 */
	protected String getGenerationColumn()
	{
		return null;
	}
	
	/**
	 * <p>
	 * 無効状態を管理するカラム名を取得します。
	 * </p>
	 * @return 無効状態を管理するカラム名
	 */
	protected String getInvalidColumn()
	{
		return ZM0021ETMsg.MK_FLG;
	}

	/**
	 * <p>
	 * 予約管理エンティティか判定します。
	 * </p>
	 * @return 予約管理エンティティの場合はtrue
	 */
	protected boolean isReserveMgr()
	{
		return false;
	}

	/**
	 * <p>
	 * 予約適用基準日となるカラム名を取得します。
	 * </p>
	 * @return 予約適用基準日となるカラム名
	 */
	protected String getCurrentColumn()
	{
		return null;
	}

	/**
	 * <p>
	 * 予約の状態を管理するエンティティか判定します。
	 * </p>
	 * @return 予約の状態を管理するエンティティの場合はtrue
	 */
	protected boolean isReserveStateMgr()
	{
		return false;
	}

	/**
	 * <p>
	 * 予約の状態を管理するカラム名を取得します。
	 * </p>
	 * @return 予約の状態を管理するカラム名
	 */
	protected String getReserveStateColumn()
	{
		return null;
	}

	/**
	 * <p>
	 * ロール_権限データの論理削除を行います。
	 * </p>
	 * @param inMsg 処理対象のメッセージキャリア
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param roleCd	      ロールコード
	 * @return 処理が正常に完了した場合trueを返却
	 */
	public boolean deleteRoleInfo(CAANMsg inMsg, AgentDispatchContext inContext, String roleCd)
	{
		String tmpRoleCd = roleCd;
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try
		{
			con = JSYejbConnection.getConnection(ZM0021ETMsg.getTableName());
			
			StringBuffer sqlBuf = new StringBuffer();
			sqlBuf.append(" UPDATE ZM_M_ROLE_AUTHORITY ");
			sqlBuf.append(" SET ");
			sqlBuf.append(JCCModelCommon.getCommonColumnDelete(inMsg));
			sqlBuf.append(" where ROLE_CD = '");
			sqlBuf.append(tmpRoleCd);
			sqlBuf.append("'");
			
			pstmt = con.prepareStatement(sqlBuf.toString());
			pstmt.executeUpdate();

		}
		catch (SQLException e)
		{
			throw new CAANRuntimeException(e);
		}
		finally
		{
			
			// 資源の解放
			try
			{
				if (pstmt != null)
				{
					pstmt.close();
				}
				if (con != null)
				{
					closeConnection(con);
				}
			}
			catch(SQLException e)
			{
				throw new CAANRuntimeException(e);
			}
		}
		
		return true;
	}
	
	/**
	 * <p>
	 * 指定された権限コードをもつロール_権限データの論理削除を行います。
	 * </p>
	 * @param inMsg 処理対象のメッセージキャリア
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param authorityCd	      権限コード
	 * @return 処理が正常に完了した場合trueを返却
	 */
	public boolean deleteRoleAuthorityInfo(CAANMsg inMsg, AgentDispatchContext inContext, String authorityCd)
	{
		String tmpAuthorityCd = authorityCd;
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try
		{
			con = JSYejbConnection.getConnection(ZM0021ETMsg.getTableName());
			
			StringBuffer sqlBuf = new StringBuffer();
			sqlBuf.append(" UPDATE ZM_M_ROLE_AUTHORITY ");
			sqlBuf.append(" SET ");
			sqlBuf.append(JCCModelCommon.getCommonColumnDelete(inMsg));
			sqlBuf.append(" where AUTHORITY_CD = '");
			sqlBuf.append(tmpAuthorityCd);
			sqlBuf.append("'");
			
			pstmt = con.prepareStatement(sqlBuf.toString());
			pstmt.executeUpdate();

		}
		catch (SQLException e)
		{
			throw new CAANRuntimeException(e);
		}
		finally
		{
			
			// 資源の解放
			try
			{
				if (pstmt != null)
				{
					pstmt.close();
				}
				if (con != null)
				{
					closeConnection(con);
				}
			}
			catch(SQLException e)
			{
				throw new CAANRuntimeException(e);
			}
		}
		
		return true;
	}

	/**
	 * <p>
	 * ロール_権限データの物理削除を行います。
	 * </p>
	 * @param inMsg 処理対象のメッセージキャリア
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param roleCd			ロールコード
	 * @param authorityCd		権限コード
	 * @return 処理が正常に完了した場合trueを返却
	 */
	public boolean physDeleteRoleInfo(CAANMsg inMsg, AgentDispatchContext inContext, String roleCd, String authorityCd)
	{
		String tmpRoleCd = roleCd;
		String tmpAuthorityCd = authorityCd;
		
		// 検索条件の設定
		CAANMsg msg = new CAANMsg(ZM0021ETMsg.class.getName());
		msg.set(ZM0021ETMsg.ROLE_CD, tmpRoleCd);
		msg.set(ZM0021ETMsg.AUTHORITY_CD, tmpAuthorityCd);
		msg.set(ZM0021ETMsg.MK_FLG, "1");

		// 検索処理の実行
		String whereStr = " where ROLE_CD = ? and AUTHORITY_CD = ? and MK_FLG = '1'";
		String[] whereList = {ZM0021ETMsg.ROLE_CD, ZM0021ETMsg.AUTHORITY_CD};
		String[][] selList = getSelectColumnList();

		// 検索処理の実行
		CAANMsg[] ret = super.findByCondition(whereStr, whereList, msg, 0, selList[1]);
		
		// 検索結果の判定
		if (ret.length > 0)
		{
			try
			{
				super.remove(msg);
			}
			catch (CAANRemoveException e1)
			{
				throw new RuntimeException(e1);
			}
		}
		
		return true;
	}
}
