/*******************************************************************************
*	All Rights reserved,Copyright (c) K-Opticom
********************************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*   モジュール名    ：JECC0011C010TPMA
*   ソースファイル名：JECC0011C010TPMA.java
*   作成者          ：富士通
*   日付            ：2011年05月19日
*＜機能概要＞
*   主処理(共通)<BR>
*   メール受信ステータス更新独自処理部品クラス.
*＜修正履歴＞
*   バージョン  修正日      修正者      修正内容
*   v1.00.00    2011/05/19  富士通      新規作成
*   v32.00.00   2017/05/26  FJ)河邊    【OM-2017-0000488】オープンカーソルエラー
*
**********************************************************************/

package eo.ejb.cbs.mainproc;

import java.rmi.server.UID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.fujitsu.futurity.model.base.CAANConnectionMgr;
import com.fujitsu.futurity.model.base.CAANJDBCUtil;
import com.fujitsu.futurity.model.base.CAANMsg;
import com.fujitsu.futurity.model.ejb.common.JSYejbConnection;
import com.fujitsu.futurity.model.ejb.common.StatusCodes;
import com.fujitsu.futurity.model.ejb.common.fw.AgentDispatchContext;
import com.fujitsu.futurity.model.ejb.common.fw.TemplateMainHandler;

import eo.common.util.JCCFrameworkException;
import eo.ejb.cbm.entity.CC0011ETMsg;
import eo.ejb.cbm.entity.CC0081ETMsg;
import eo.ejb.cbs.cbsmsg.ECC0011C010CBSMsg;
import eo.ejb.cbs.cbsmsg.ECC0081D010CBSMsg;
import eo.ejb.common.JCCModelCommon;
import eo.ejb.common.JPCejbSysParam;

/**
 * <p>
 * メール受信ステータス更新独自処理部品クラスです。
 * </p>
 * @author 富士通
 */
public class JECC0011C010TPMA implements TemplateMainHandler {

	/**
	 * コンストラクタ.
	 */
	public JECC0011C010TPMA()
	{
		super();
	}

	static final String KAIDOKU = "002";
	static final String HENSHINZUMI = "003";
	static final String HIHYOUJI = "004";
	
	/**
	 * メール受信ステータス管理更新.
	 * メール受信ステータスが「既読」の場合、メール開封処理年月日時分秒にシステム日付を代入。
	 * メール受信ステータスが「返信済」の場合、メール返信処理年月日時分秒にシステム日付を代入。
	 * メール受信ステータスが「非表示」の場合、メール非表示処理年月日時分秒にシステム日付を代入。
	 * @param inMsg 
	 * @param inContext 
	 */
	public static void invoke(CAANMsg inCBSMsg, AgentDispatchContext inContext) throws Exception 
	{
		//----------------------------------------------------
		// ユーザー情報の確認
		//----------------------------------------------------
		// Connection の取得
		Connection con = JSYejbConnection.getConnection(CC0011ETMsg.getTableName());
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try
		{
			// SQL文の組立て
			StringBuffer sql_Buff = new StringBuffer();
			sql_Buff.append(" SELECT ")
					.append("   CC0011.MAIL_KAIFU_TRN_DTM AS KAIHU, ")
					.append("   CC0011.MAIL_HNSIN_TRN_DTM AS HISIN, ")
					.append("   CC0011.MAIL_HIDSP_TRN_DTM AS HIDSP ")
					.append(" FROM  CC_T_MAIL_RCV CC0011 ")
					.append(" WHERE CC0011.MAIL_RCV_NO = ? ");
			
			pstmt = con.prepareStatement(sql_Buff.toString());
			
			// inCBSMsgからメール受信番号を取得して設定
			CAANJDBCUtil.setParam(pstmt, 1, inCBSMsg.getString(ECC0011C010CBSMsg.MAIL_RCV_NO));
			
			// SQL実行
			rs = pstmt.executeQuery();
			
			// データが取得できた場合
			if (rs.next())
			{
				// inCBSMsgにメール開封処理年月日時分秒を設定
				inCBSMsg.set(ECC0011C010CBSMsg.MAIL_KAIFU_TRN_DTM, rs.getString("KAIHU"));
				// inCBSMsgにメール返信処理年月日時分秒を設定
				inCBSMsg.set(ECC0011C010CBSMsg.MAIL_HNSIN_TRN_DTM, rs.getString("HISIN"));
				// inCBSMsgにメール非表示処理年月日時分秒を設定
				inCBSMsg.set(ECC0011C010CBSMsg.MAIL_HIDSP_TRN_DTM, rs.getString("HIDSP"));
			}
		}
		catch (SQLException e)
		{
				throw new JCCFrameworkException(e);
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
				if (pstmt != null)
				{
					pstmt.close();
				}
// OM-2017-0000488 2017/05/26 ADD START
				if (con != null)
				{
					CAANConnectionMgr.getInstance().close(con);
				}
// OM-2017-0000488 2017/05/26 ADD END
			}
			catch (SQLException se)
			{
				throw new JCCFrameworkException(se);
			}
		}
		
		//----------------------------------------------------
		// システム時刻の設定
		//----------------------------------------------------
		
		// (1) 対象日付にシステム日付設定
		// @ メール受信ステータスが「002:既読」の場合
		if (KAIDOKU.equals(inCBSMsg.getString(ECC0011C010CBSMsg.MAIL_RCV_STAT)))
		{
// ■■メール受信対応に伴う修正 2011.05.26 Chg Start
			inCBSMsg.set(ECC0011C010CBSMsg.MAIL_KAIFU_TRN_DTM, JCCModelCommon.getSysDateTimeStamp());
		}
		// A メール受信ステータスが「003:返信済」の場合
		else if (HENSHINZUMI.equals(inCBSMsg.getString(ECC0011C010CBSMsg.MAIL_RCV_STAT)))
		{
			inCBSMsg.set(ECC0011C010CBSMsg.MAIL_HNSIN_TRN_DTM, JCCModelCommon.getSysDateTimeStamp());
		}
		// B メール受信ステータスが「004:非表示」の場合
		else if (HIHYOUJI.equals(inCBSMsg.getString(ECC0011C010CBSMsg.MAIL_RCV_STAT)))
		{
			inCBSMsg.set(ECC0011C010CBSMsg.MAIL_HIDSP_TRN_DTM, JCCModelCommon.getSysDateTimeStamp());
// ■■メール受信対応に伴う修正 2011.05.26 Chg End
		}
		
	}
}
