/*******************************************************************************
*	 All Rights reserved,Copyright (c) K-Opticom 
********************************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JEKK2811C250KRCK
*	ソースファイル名：JEKK2811C250KRCK.java
*	作成者			：富士通
*	日付			：2013年11月15日
*＜機能概要＞
*	関連チェック部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v7.00.00	2013/11/15	富士通		新規作成
*
********************************************************************************/

package eo.ejb.common.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.fujitsu.futurity.common.JCMConstants;
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.base.CAANRuntimeException;
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.AgentDispatchContext;

import eo.ejb.cbm.entity.KK0591ETMsg;
import eo.ejb.cbm.entity.KK0791ETMsg;
import eo.ejb.cbm.entity.KK2811ETMsg;
import eo.ejb.cbs.cbsmsg.EKK2811C170CBSMsg;

/**
 * <p>
 * 機器オプションサービス契約料金プラン変更関連チェッククラスです。
 * ＜メソッド一覧＞
 * execKRCK
 * execEKK0341C360_KR2
 * execEKK0341C360_KR3 
 * </p>
 * @author 富士通
 */

public class JEKK2811C250KRCK
{

	/**
	 * コンストラクタ
	 */

	public JEKK2811C250KRCK()
	{
	}

	/**
	 * 機器オプションサービス契約料金プラン変更関連チェックを行います。
	 * 
	 * @param inCBSMsg エンティティ
	 * @param inContext Agentから渡されたAgentDispatchContext
	 */

	public void execKRCK(CAANMsg inCBSMsg, AgentDispatchContext inContext)
	{
		JSYejbLog.println(JSYejbLog.DEBUG, getClass(), "call:JEKK2811C250KRCK.execKRCK");

		// ステータス
		int rtn = 0;
		
		rtn = execEKK2811C250_1(inCBSMsg, inContext, "EA");
		if(rtn <= StatusCodes.WARNING)
		{
			rtn = execEKK2811C250_3(inCBSMsg, inContext, "EA");
		}
		if(rtn <= StatusCodes.WARNING)
		{
			rtn = execEKK2811C250_4(inCBSMsg, inContext, "EB");
		}
		if(rtn <= StatusCodes.WARNING)
		{
			rtn = execEKK2811C250_5(inCBSMsg, inContext, "EA");
		}
		if(rtn <= StatusCodes.WARNING)
		{
			rtn = execEKK2811C250_6(inCBSMsg, inContext, "EA");
		}
		
		JSYejbLog.println(JSYejbLog.DEBUG, getClass(), "end:JEKK2811C250KRCK.execKRCK");
	}
	
	/**
	 * EKK2811C250_制約ＮＯ1チェック
	 *
	 * @param inETMsg チェック対象のETMsg(KK2811ETMsg)
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param errFlag ERRフラグ
	 * @return int チェック結果
	 */
	private int execEKK2811C250_1(CAANMsg inETMsg, AgentDispatchContext inContext, String errFlag){

		// ステータス
		int rtn = 0;

		Connection con = JSYejbConnection.getConnection(KK2811ETMsg.getTableName());

		PreparedStatement pstmtQuery = null;
		ResultSet rsltQuery = null;
		StringBuffer sqlQuery = new StringBuffer();

		try{

			// チェック条件定数
			// ＳＱＬ引数定義
			String joken1_2 = null;
			if(!inETMsg.isNull(KK2811ETMsg.KKOP_SVC_KEI_NO)){
				joken1_2 = new String(inETMsg.getObject(KK2811ETMsg.KKOP_SVC_KEI_NO).toString());
			}

			String joken2_2 = "0";
			// 条件文定義
			String joken_1 = KK2811ETMsg.getDBColumnName(KK2811ETMsg.KKOP_SVC_KEI_NO);
			if(joken1_2 == null){
				joken_1 += " IS NULL ";
			}else{
				joken_1 += " = ? ";
			}

			String joken_2 = KK2811ETMsg.getDBColumnName(KK2811ETMsg.MK_FLG);
			if(joken2_2 == null){
				joken_2 += " IS NULL ";
			}else{
				joken_2 += " = ? ";
			}

			// チェック条件
			if (!inETMsg.isNull(KK2811ETMsg.KKOP_SVC_KEI_NO)) { 

				// ＳＱＬ文の組み立て
				sqlQuery.append("SELECT COUNT(*) CNT FROM ").append(KK2811ETMsg.getTableName()).append(" WHERE ")
					.append("(" + joken_1 + " AND " + joken_2 + " ) ");

				// ＳＱＬ文の解析
				pstmtQuery = con.prepareStatement(sqlQuery.toString());

				// パラメータの設定
				int para_idx = 1;
				if(joken1_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken1_2);
				}
				if(joken2_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken2_2);
				}

				// ＳＱＬ文の実行
				rsltQuery = pstmtQuery.executeQuery();

				if(rsltQuery.next()){
				// 判定条件
					if(!(rsltQuery.getLong("CNT")>0)){
						inETMsg.set(KK2811ETMsg.KKOP_SVC_KEI_NO_ERR, errFlag);
						if("W".equals(errFlag.substring(0,1))){
							rtn = StatusCodes.WARNING;
						} else {
							rtn = StatusCodes.RELATION_ERR;
						}
					}
				}

			}

		} catch(SQLException e) {
			inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
			throw new CAANRuntimeException(e);
		} finally {
			// このメソッドで確保した資源の解放
			try {
				if(rsltQuery != null) {
					rsltQuery.close();
				}
				if(pstmtQuery != null) {
					pstmtQuery.close();
				}
				if(con != null) {
					CAANConnectionMgr.getInstance().close(con);
				}
			} catch(SQLException e) {
				inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
		
		// ステータスのセット
		if (rtn > inETMsg.getInt(KK2811ETMsg.STATUS)) {
			inETMsg.set(KK2811ETMsg.STATUS, rtn);
		}

		return rtn;

	}
	
	/**
	 * EKK2811C250_制約ＮＯ3チェック
	 *
	 * @param inETMsg チェック対象のETMsg(KK2811ETMsg)
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param errFlag ERRフラグ
	 * @return int チェック結果
	 */
	private int execEKK2811C250_3(CAANMsg inETMsg, AgentDispatchContext inContext, String errFlag){

		// ステータス
		int rtn = 0;

		Connection con = JSYejbConnection.getConnection(KK0791ETMsg.getTableName());

		PreparedStatement pstmtQuery = null;
		ResultSet rsltQuery = null;
		StringBuffer sqlQuery = new StringBuffer();

		try{

			// チェック条件定数
			// ＳＱＬ引数定義
			String joken1_2 = null;
			if(!inETMsg.isNull(KK2811ETMsg.PCRS_CD)){
				joken1_2 = new String(inETMsg.getObject(KK2811ETMsg.PCRS_CD).toString());
			}

			String joken2_2 = "0";
			// 条件文定義
			String joken_1 = KK0791ETMsg.getDBColumnName(KK0791ETMsg.PCRS_CD);
			if(joken1_2 == null){
				joken_1 += " IS NULL ";
			}else{
				joken_1 += " = ? ";
			}

			String joken_2 = KK0791ETMsg.getDBColumnName(KK0791ETMsg.MK_FLG);
			if(joken2_2 == null){
				joken_2 += " IS NULL ";
			}else{
				joken_2 += " = ? ";
			}

			// チェック条件
			if (!inETMsg.isNull(KK2811ETMsg.PCRS_CD)) { 

				// ＳＱＬ文の組み立て
				sqlQuery.append("SELECT COUNT(*) CNT FROM ").append(KK0791ETMsg.getTableName()).append(" WHERE ")
					.append("(" + joken_1 + " AND " + joken_2 + " ) ");

				// ＳＱＬ文の解析
				pstmtQuery = con.prepareStatement(sqlQuery.toString());

				// パラメータの設定
				int para_idx = 1;
				if(joken1_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken1_2);
				}
				if(joken2_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken2_2);
				}

				// ＳＱＬ文の実行
				rsltQuery = pstmtQuery.executeQuery();

				if(rsltQuery.next()){
				// 判定条件
					if(!(rsltQuery.getLong("CNT")>0)){
						inETMsg.set(KK2811ETMsg.PCRS_CD_ERR, errFlag);
						if("W".equals(errFlag.substring(0,1))){
							rtn = StatusCodes.WARNING;
						} else {
							rtn = StatusCodes.RELATION_ERR;
						}
					}
				}

			}

		} catch(SQLException e) {
			inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
			throw new CAANRuntimeException(e);
		} finally {
			// このメソッドで確保した資源の解放
			try {
				if(rsltQuery != null) {
					rsltQuery.close();
				}
				if(pstmtQuery != null) {
					pstmtQuery.close();
				}
				if(con != null) {
					CAANConnectionMgr.getInstance().close(con);
				}
			} catch(SQLException e) {
				inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
		
		// ステータスのセット
		if (rtn > inETMsg.getInt(KK2811ETMsg.STATUS)) {
			inETMsg.set(KK2811ETMsg.STATUS, rtn);
		}

		return rtn;

	}
	
	/**
	 * EKK2811C250_制約ＮＯ3チェック
	 *
	 * @param inETMsg チェック対象のETMsg(KK2811ETMsg)
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param errFlag ERRフラグ
	 * @return int チェック結果
	 */
	private int execEKK2811C250_4(CAANMsg inETMsg, AgentDispatchContext inContext, String errFlag){

		// ステータス
		int rtn = 0;

		Connection con = JSYejbConnection.getConnection(KK0791ETMsg.getTableName());

		PreparedStatement pstmtQuery = null;
		ResultSet rsltQuery = null;
		StringBuffer sqlQuery = new StringBuffer();

		try{

			// チェック条件定数
			// ＳＱＬ引数定義
			String joken1_2 = null;
			if(!inETMsg.isNull(KK2811ETMsg.PCRS_CD)){
				joken1_2 = new String(inETMsg.getObject(KK2811ETMsg.PCRS_CD).toString());
			}

			String joken2_2 = "0";
			String joken3_2 = "9";
			// 条件文定義
			String joken_1 = KK0791ETMsg.getDBColumnName(KK0791ETMsg.PCRS_CD);
			if(joken1_2 == null){
				joken_1 += " IS NULL ";
			}else{
				joken_1 += " = ? ";
			}

			String joken_2 = KK0791ETMsg.getDBColumnName(KK0791ETMsg.MK_FLG);
			if(joken2_2 == null){
				joken_2 += " IS NULL ";
			}else{
				joken_2 += " = ? ";
			}

			String joken_3 = KK0791ETMsg.getDBColumnName(KK0791ETMsg.PCRS_SBT_CD);
			if(joken3_2 == null){
				joken_3 += " IS NULL ";
			}else{
				joken_3 += " = ? ";
			}

			// チェック条件
			if (!inETMsg.isNull(KK2811ETMsg.PCRS_CD)) { 

				// ＳＱＬ文の組み立て
				sqlQuery.append("SELECT COUNT(*) CNT FROM ").append(KK0791ETMsg.getTableName()).append(" WHERE ")
					.append("(" + "(" + joken_1 + " AND " + joken_2 + " ) " + " AND " + joken_3 + " ) ");

				// ＳＱＬ文の解析
				pstmtQuery = con.prepareStatement(sqlQuery.toString());

				// パラメータの設定
				int para_idx = 1;
				if(joken1_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken1_2);
				}
				if(joken2_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken2_2);
				}
				if(joken3_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken3_2);
				}

				// ＳＱＬ文の実行
				rsltQuery = pstmtQuery.executeQuery();

				if(rsltQuery.next()){
				// 判定条件
					if(!(rsltQuery.getLong("CNT")>0)){
						inETMsg.set(KK2811ETMsg.PCRS_CD_ERR, errFlag);
						if("W".equals(errFlag.substring(0,1))){
							rtn = StatusCodes.WARNING;
						} else {
							rtn = StatusCodes.RELATION_ERR;
						}
					}
				}

			}

		} catch(SQLException e) {
			inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
			throw new CAANRuntimeException(e);
		} finally {
			// このメソッドで確保した資源の解放
			try {
				if(rsltQuery != null) {
					rsltQuery.close();
				}
				if(pstmtQuery != null) {
					pstmtQuery.close();
				}
				if(con != null) {
					CAANConnectionMgr.getInstance().close(con);
				}
			} catch(SQLException e) {
				inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
		
		// ステータスのセット
		if (rtn > inETMsg.getInt(KK2811ETMsg.STATUS)) {
			inETMsg.set(KK2811ETMsg.STATUS, rtn);
		}

		return rtn;

	}

	/**
	 * EKK2811C250_制約ＮＯ5チェック
	 *
	 * @param inETMsg チェック対象のETMsg(KK2811ETMsg)
	 * @param inContext Agentから渡されたAgentDispatchContext
	 * @param errFlag ERRフラグ
	 * @return int チェック結果
	 */
	private int execEKK2811C250_5(CAANMsg inETMsg, AgentDispatchContext inContext, String errFlag){

		// ステータス
		int rtn = 0;

		Connection con = JSYejbConnection.getConnection(KK0591ETMsg.getTableName());

		PreparedStatement pstmtQuery = null;
		ResultSet rsltQuery = null;
		StringBuffer sqlQuery = new StringBuffer();

		try{

			// チェック条件定数
			// ＳＱＬ引数定義
			String joken1_2 = null;
			if(!inETMsg.isNull(KK2811ETMsg.PCRS_CD)){
				joken1_2 = new String(inETMsg.getObject(KK2811ETMsg.PCRS_CD).toString());
			}

			String joken2_2 = null;
			if(!inETMsg.isNull(KK2811ETMsg.PPLAN_CD)){
				joken2_2 = new String(inETMsg.getObject(KK2811ETMsg.PPLAN_CD).toString());
			}

			String joken3_2 = "0";
			// 条件文定義
			String joken_1 = KK0591ETMsg.getDBColumnName(KK0591ETMsg.PCRS_CD);
			if(joken1_2 == null){
				joken_1 += " IS NULL ";
			}else{
				joken_1 += " = ? ";
			}

			String joken_2 = KK0591ETMsg.getDBColumnName(KK0591ETMsg.PPLAN_CD);
			if(joken2_2 == null){
				joken_2 += " IS NULL ";
			}else{
				joken_2 += " = ? ";
			}

			String joken_3 = KK0591ETMsg.getDBColumnName(KK0591ETMsg.MK_FLG);
			if(joken3_2 == null){
				joken_3 += " IS NULL ";
			}else{
				joken_3 += " = ? ";
			}

			// チェック条件
			if (!inETMsg.isNull(KK2811ETMsg.PCRS_CD) && !inETMsg.isNull(KK2811ETMsg.PPLAN_CD)) { 

				// ＳＱＬ文の組み立て
				sqlQuery.append("SELECT COUNT(*) CNT FROM ").append(KK0591ETMsg.getTableName()).append(" WHERE ")
					.append("(" + "(" + joken_1 + " AND " + joken_2 + " ) " + " AND " + joken_3 + " ) ");

				// ＳＱＬ文の解析
				pstmtQuery = con.prepareStatement(sqlQuery.toString());

				// パラメータの設定
				int para_idx = 1;
				if(joken1_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken1_2);
				}
				if(joken2_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken2_2);
				}
				if(joken3_2 != null){
					CAANJDBCUtil.setParam(pstmtQuery, para_idx++ , joken3_2);
				}

				// ＳＱＬ文の実行
				rsltQuery = pstmtQuery.executeQuery();

				if(rsltQuery.next()){
				// 判定条件
					if(!(rsltQuery.getLong("CNT")>0)){
						inETMsg.set(KK2811ETMsg.PCRS_CD_ERR, errFlag);
						if("W".equals(errFlag.substring(0,1))){
							rtn = StatusCodes.WARNING;
						} else {
							rtn = StatusCodes.RELATION_ERR;
						}
					}
				}

			}

		} catch(SQLException e) {
			inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
			throw new CAANRuntimeException(e);
		} finally {
			// このメソッドで確保した資源の解放
			try {
				if(rsltQuery != null) {
					rsltQuery.close();
				}
				if(pstmtQuery != null) {
					pstmtQuery.close();
				}
				if(con != null) {
					CAANConnectionMgr.getInstance().close(con);
				}
			} catch(SQLException e) {
				inETMsg.set(KK2811ETMsg.STATUS, StatusCodes.FIND_DB_ERR);
				throw new CAANRuntimeException(e);
			}
		}
		
		// ステータスのセット
		if (rtn > inETMsg.getInt(KK2811ETMsg.STATUS)) {
			inETMsg.set(KK2811ETMsg.STATUS, rtn);
		}

		return rtn;

	}

	/**
	 * EKK2811C250_制約No6（関連）チェック
	 *
	 * @param  inCBSMsg  チェック対象のCBSMsg(KK2811CBSMsg)
	 * @param  inContext Agentから渡されたAgentDispatchContext
	 * @param  errFlag   ERRフラグ
	 * @return result    チェック結果
	 */
	private int execEKK2811C250_6(CAANMsg inCBSMsg, AgentDispatchContext inContext, String errFlag)
	{
		// ステータス
		int result = 0;

		JKKejbKK0021KRCK kk0021krck = new JKKejbKK0021KRCK();

		// チェック条件
		if (!inCBSMsg.isNull(EKK2811C170CBSMsg.MSKM_DTL_NO))
		{
			// 判定条件
			if(!kk0021krck.isExistsNonGene(inCBSMsg.getString(EKK2811C170CBSMsg.MSKM_DTL_NO)))
			{
				inCBSMsg.set(EKK2811C170CBSMsg.MSKM_DTL_NO_ERR, errFlag);
				if("W".equals(errFlag.substring(0, 1)))
				{
					result = StatusCodes.WARNING;
				}
				else
				{
					result = StatusCodes.RELATION_ERR;
				}
			}
		}
		// ステータスのセット
		if (result > inCBSMsg.getInt(JCMConstants.STATUS_INT_KEY))
		{
			inCBSMsg.set(JCMConstants.STATUS_INT_KEY, result);
		}
		return result;
	}
	
}

