/*******************************************************************************
*	All Rights reserved,Copyright (c) K-Opticom
********************************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JSYejbEZM0261B020TPDA
*	ソースファイル名：JSYejbEZM0261B020TPDA.java
*	作成者			：EK909306
*	日付			：2011年06月29日
*＜機能概要＞
*	ダウンロードファイル管理ファイル名一覧照会テンプレートDBアクセス部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v3.00.00	2012/03/17	FJ)楠神		新規作成
*	v3.00.01	2012/06/13	FJ)宮本		TAI-2012-0000054
*
********************************************************************************/

package eo.ejb.cbs.sqlf;

import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.base.CAANSQLFacility;
import com.fujitsu.futurity.model.base.CAANRuntimeException;
import com.fujitsu.futurity.model.base.CAANJDBCUtil;
import com.fujitsu.futurity.model.ejb.common.JSYejbConnection;
import com.fujitsu.futurity.model.ejb.common.JSYejbLog;
import com.fujitsu.futurity.model.ejb.common.StatusCodes;
import com.fujitsu.futurity.model.ejb.common.fw.TemplateDBAccessHandler;
import com.fujitsu.futurity.model.ejb.common.fw.AgentDispatchContext;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import eo.common.constant.JPCModelConstant;
import eo.common.constant.JTUStrConst;
import eo.ejb.cbs.cbsmsg.EZM0261B020CBSMsg;
import eo.ejb.cbs.cbsmsg.EZM0261B020CBSMsg1List;
import eo.ejb.cbm.entity.ZM0261ETMsg;

/**
*
*  テンプレートDBアクセス部品　EZM0261B020_SQLF<br>
*  入力された項目からSQL文を動的に組み立て、<br>
*  検索結果をCBSMsgにセットします。<br>
*  @author FJ
*/
public class JSYejbEZM0261B020TPDA extends CAANSQLFacility implements TemplateDBAccessHandler
{
	
	/**
	 * コンストラクタ
	 */
	public JSYejbEZM0261B020TPDA()	
	{
	}
	
	/**
	*   EZM0261B020_SQLF<br>
	*   @param inMsg    入力値の格納されたCBSMsg <br>
	*   @param inContext
	*   @return void
	*/
	public void invoke(CAANMsg inMsg, AgentDispatchContext inContext)
	{
		
		// コネクション
		Connection con1 = null;
		
		// プリペアステートメント
		PreparedStatement pstmt = null;
		
		// リザルトセット
		ResultSet rsltQuery = null;
		
		/**
		 * 最大検索件数が未設定の場合
		 */
		if (inMsg.isNull(EZM0261B020CBSMsg.MAX_SEARCH_NUM))
		{
			throw new IllegalArgumentException(JTUStrConst.ICRN_ERR_MSG_IKT);
		}
		
		/******************** 
		 * SQL文の作成
		 ********************/

		try
		{
			
			//コネクション取得
			con1 = JSYejbConnection.getConnection(ZM0261ETMsg.getTableName());
			
			// SQL文_基本部1
			StringBuffer sql_Buff = new StringBuffer();
			StringBuffer sql_Select = new StringBuffer();
			StringBuffer sql_From = new StringBuffer();
			StringBuffer sql_Where = new StringBuffer();
			StringBuffer sql_Order = new StringBuffer();
			
			sql_Select.append(" SELECT ")
					.append(" 	DL.FILE_NO, ")
					.append(" 	DL.TRN_KANRI_NO, ")
					.append(" 	DL.EFILE_KANRI_NO, ")
					.append(" 	DL.FILE_NM, ")
					.append(" 	DL.FILE_SIZE, ")
					.append(" 	DL.DATA_CNT, ")
					.append(" 	DL.FILE_ADD_DTM, ")
					.append(" 	DL.FILE_DEL_YMD, ")
					.append(" 	DL.UPD_DTM ");
			sql_From.append(" FROM ")
					.append(" 	ZM_T_DL_FILE_KANRI DL ");
			// SQL文_個別部1
			if("1".equals(inMsg.getObject(EZM0261B020CBSMsg.FUNC_CODE).toString()))
			{
				sql_Where.append(" WHERE ")
						.append(" 	DL.FILE_NM = ? ")
						.append(" 	AND DL.TRN_KANRI_NO = ? ")
						.append(" 	AND DL.MK_FLG = '0' ");
				sql_Order.append(" ORDER BY ")
						.append(" 	DL.FILE_ADD_DTM ASC ");
			}
			
						//件数取得SQL
			StringBuffer sql_cnt = new StringBuffer();
			sql_cnt.append(" SELECT ")
				.append(" 	COUNT(*) AS CNT ")
				.append(sql_From)
				.append(sql_Where);

			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_cnt.toString());
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			if("1".equals(inMsg.getString(EZM0261B020CBSMsg.FUNC_CODE)))
			{
				// パラメータの設定(ＫＥＹ＿ファイル名を指定)
				CAANJDBCUtil.setParam(pstmt, 1, inMsg.getObject(EZM0261B020CBSMsg.KEY_FILE_NM));
				// パラメータの設定(ＫＥＹ＿処理管理番号を指定)
				CAANJDBCUtil.setParam(pstmt, 2, inMsg.getObject(EZM0261B020CBSMsg.KEY_TRN_KANRI_NO));
			}

			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_cnt);
			/**
			 * SQL文実行
			 */
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			// EZM0261B020CBSMsgの明細にセットする項目
			String[] msgKeyListCnt = {
					EZM0261B020CBSMsg.TOTAL_SEARCH_NUM
			};
			
			/**
			 * 結果取得
			 */
			rsltQuery.next();
			mapMessage(rsltQuery, msgKeyListCnt, inMsg);

			// 資源の解放
			if(rsltQuery != null)
			{
				rsltQuery.close();
			}
			if(pstmt != null)
			{
				pstmt.close();
			}
			
			// 一括検索、部分検索の場合
			// 検索件数が0件の場合、エラーフラグに"1"を設定して処理終了
			if (0 == (Integer.parseInt(inMsg.getString(EZM0261B020CBSMsg.TOTAL_SEARCH_NUM))))
			{
				inMsg.set(EZM0261B020CBSMsg.SEARCH_ERR_FLG, JPCModelConstant.SEARCH_ERR_FLG_ZERO);
				return;
			}
			// 検索件数が最大検索件数を超えた場合、エラーフラグに"2"を設定して処理終了
			if (Integer.parseInt(inMsg.getString(EZM0261B020CBSMsg.MAX_SEARCH_NUM))
				< (Integer.parseInt(inMsg.getString(EZM0261B020CBSMsg.TOTAL_SEARCH_NUM))))
			{
				inMsg.set(EZM0261B020CBSMsg.SEARCH_ERR_FLG, JPCModelConstant.SEARCH_ERR_FLG_OVERMAX);
				return;
			}
			
			/***********************************************************************************************
			 * データ取得
			 */

			sql_Buff.append(sql_Select)
				.append(sql_From)
				.append(sql_Where)
				.append(sql_Order);
			
			//prepareStatementにSQL文をセット
			pstmt = con1.prepareStatement(sql_Buff.toString());
			
			//ログ出力(SQL文の出力)
			JSYejbLog.outlog(inContext, JSYejbLog.DBACCESS, this.getClass(), sql_Buff);
			
			// PreparedStatementの指定されたパラメータカラムに 指定された値を設定
			if("1".equals(inMsg.getString(EZM0261B020CBSMsg.FUNC_CODE)))
			{
				// パラメータの設定(ＫＥＹ＿ファイル名を指定)
				CAANJDBCUtil.setParam(pstmt, 1, inMsg.getObject(EZM0261B020CBSMsg.KEY_FILE_NM));
				// パラメータの設定(ＫＥＹ＿処理管理番号を指定)
				CAANJDBCUtil.setParam(pstmt, 2, inMsg.getObject(EZM0261B020CBSMsg.KEY_TRN_KANRI_NO));
			}
			
			// ResultSetの取得
			rsltQuery = pstmt.executeQuery();
			
			// EZM0261B020CBSMsgの明細にセットする項目
			String[] msgKeyList1 = {
					EZM0261B020CBSMsg1List.FILE_NO
					, EZM0261B020CBSMsg1List.TRN_KANRI_NO
					, EZM0261B020CBSMsg1List.EFILE_KANRI_NO
					, EZM0261B020CBSMsg1List.FILE_NM
					, EZM0261B020CBSMsg1List.FILE_SIZE
					, EZM0261B020CBSMsg1List.DATA_CNT
					, EZM0261B020CBSMsg1List.FILE_ADD_DTM
					, EZM0261B020CBSMsg1List.FILE_DEL_YMD
					, EZM0261B020CBSMsg1List.UPD_DTM
			};
			
			// 明細に処理結果(ResultSet)をマッピング
			CAANMsg[] outMsg1 = mapMessageList(rsltQuery, msgKeyList1, "eo.ejb.cbs.cbsmsg.EZM0261B020CBSMsg1List", 0);
			
			// 検索件数が0件の場合、エラーフラグに"1"を設定して処理終了
			if (0 == outMsg1.length)
			{
				inMsg.set(EZM0261B020CBSMsg.SEARCH_ERR_FLG, JPCModelConstant.SEARCH_ERR_FLG_ZERO);
				inMsg.set(EZM0261B020CBSMsg.TOTAL_SEARCH_NUM, "0");
				return;
			}

			// 検索件数が最大検索件数を超えた場合、エラーフラグに"2"を設定して処理終了
			if (Integer.parseInt(inMsg.getString(EZM0261B020CBSMsg.MAX_SEARCH_NUM))
				< (outMsg1.length))
			{
				inMsg.set(EZM0261B020CBSMsg.SEARCH_ERR_FLG, JPCModelConstant.SEARCH_ERR_FLG_OVERMAX);
				return;
			}
			
			//総検索件数を再設定
			inMsg.set(EZM0261B020CBSMsg.TOTAL_SEARCH_NUM, Integer.toString(outMsg1.length));
			
			// inMsg(CBSMsg)に明細をセット
			inMsg.set("EZM0261B020CBSMsg1List", outMsg1);
			

		} 
		catch(SQLException e) 
		{
			inMsg.set(EZM0261B020CBSMsg.STATUS, StatusCodes.FIND_DB_ERR);
			throw new CAANRuntimeException(e);
		} 
		finally 
		{
			// 資源の解放
			try
			{
				if(rsltQuery != null)
				{
					rsltQuery.close();
				}
				if(pstmt != null)
				{
					pstmt.close();
				}
				if(con1 != null)
				{
					closeConnection(con1);
				}
			}
			catch(SQLException e)
			{
				inMsg.set(EZM0261B020CBSMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
	}
}
