/*********************************************************************
 * All Rights reserved,Copyright (c) K-Opticom
 **********************************************************************
 *＜プログラム内容＞
 *   システム名      ：eo顧客基幹システム
 *   モジュール名    ：FUW11711SFLogic
 *   ソースファイル名：FUW11711SFLogic.java
 *   作成者          ：富士通
 *   日付            ：2015年02月22日
 *＜機能概要＞
 *   ID登録証情報照会ログイン(PC)のビューロジッククラスです。
 *＜修正履歴＞
 *   バージョン  修正日       修正者      修正内容
 *	v1.00.00	2015/02/22	FJ)土井		新規作成
 *
**********************************************************************/
package eo.web.webview.FUW11711SF;

import static com.fujitsu.futurity.web.x31.X31SWebLog.DEBUG_LOG;

import java.util.HashMap;

import com.fujitsu.futurity.web.x00.JCCBusinessException;
import com.fujitsu.futurity.web.x00.JCCWebServiceException;
import com.fujitsu.futurity.web.x31.X31CHashMap;
import com.fujitsu.futurity.web.x31.X31CMessageResult;
import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;
import com.fujitsu.futurity.web.x31.X31CMessageResult.X31CMessageMoreInfo;

import eo.common.constant.JFUStrConst;
import eo.common.constant.JPCModelConstant;
import eo.web.webview.JCCWebBusinessLogic;
import eo.web.webview.CommonInfoCF.CommonInfoCFConst;
import eo.web.webview.FUW11721SF.FUW11721SFConst;
import eo.web.webview.common.JFUScreenConst;
import eo.web.webview.common.JFUWebCommon;

import eo.web.webview.mapping.FUSV0290_FUSV0290OPDBMapper;


/**
 * IPv6サービス解約申込のビューロジッククラスです。
 * <p>
 * <BR>
 * @author 富士通
 */
@SuppressWarnings("serial")
public class FUW11711SFLogic extends JCCWebBusinessLogic
{

	/** DataBean操作種別：項目値設定 */
	private static final String SET = X31CWebConst.DATABEAN_SET_VALUE;
	/** DataBean操作種別：項目値取得 */
	private static final String GET = X31CWebConst.DATABEAN_GET_VALUE;
	/** DataBean操作種別：項目値取得 */
	private static final String ADD = X31CWebConst.DATABEAN_ADD_VALUE;

	/** インターフェイス項目名:Webアクセス認証キー */
	private static final String POST_ID_KEY = "key";
	
	/** 認証結果フラグ:成功 */
	private static final String NINSHO_RESULT_FLG_SUCCESS = "0";

	/** サービスID： */
	private static final String USECASE_ID_FUSV0290 = "FUSV0290";


	/**
	 * 初期処理
	 * 
	 * @return 処理結果
	 * @throws Exception
	 */
	public boolean init() throws Exception
	{
		DEBUG_LOG.info("----- FUW11711 Start init -----");
		// -------------------------------------------------
		// ■使用フォームBeanの取得
		// -------------------------------------------------
		// 共有フォームBeanのデータBeanアクセスクラスを取得。
		X31SDataBeanAccess commonInfoBean = super.getCommonInfoBean();
		// サービスフォームBeanのデータBeanアクセスクラスを取得。
		X31SDataBeanAccess serviceFormBean = super.getServiceFormBean();

		X31CHashMap posted_requests = null;

		// ■POSTされてきたパラメータを取得します
		posted_requests = this.getRequestParams();

		Object tmp_obj = null;
		String key = null;

		// Webアクセス認証キー取得
		tmp_obj = posted_requests.get(POST_ID_KEY);
		key = (tmp_obj == null) ? (JFUStrConst.EMPTY) : (tmp_obj.toString().trim());
		// Webアクセス認証キー設定
		serviceFormBean.sendMessageString(FUW11711SFConst.WEB_ACCSS_NSKEY, SET, key);

		// ●サービス呼び出し処理
		executeInitService(commonInfoBean, serviceFormBean);

		// -------------------------------------------------
		// ■遷移先画面の指定
		// -------------------------------------------------
		// 遷移先画面IDを設定
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUWebCommon.getScreenId(this));
		// 遷移先画面名を設定
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_NAME_FUW11711);

		DEBUG_LOG.info("----- FUW11711 End init -----");

		return true;
	}

	/**
	 * 次へ処理
	 * 
	 * @return 処理結果
	 * @throws Exception
	 */
	public boolean next() throws Exception
	{
		DEBUG_LOG.info("----- FUW11711_next Start init -----");
		// -------------------------------------------------
		// ■使用フォームBeanの取得
		// -------------------------------------------------
		// 共有フォームBeanのデータBeanアクセスクラスを取得。
		X31SDataBeanAccess commonInfoBean = super.getCommonInfoBean();
		// サービスフォームBeanのデータBeanアクセスクラスを取得。
		X31SDataBeanAccess serviceFormBean = super.getServiceFormBean();

		// ●サービス呼び出し処理
		executeDispService(commonInfoBean, serviceFormBean);

		// -------------------------------------------------
		// ■遷移先画面の指定
		// -------------------------------------------------
		if (NINSHO_RESULT_FLG_SUCCESS.equals(serviceFormBean.sendMessageString(FUW11711SFConst.NINSHO_RESULT_FLG, GET))){
			// 正常時は次の画面へ
			// 遷移先画面IDを設定
			commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_ID_FUW11712);
			// 遷移先画面名を設定
			commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_NAME_FUW11712);
		}else{
			// そうでない場合は、ログイン画面へ戻る
			// 遷移先画面IDを設定
			commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUWebCommon.getScreenId(this));
			// 遷移先画面名を設定
			commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_NAME_FUW11711);

		}

		DEBUG_LOG.info("----- FUW11711 End init -----");

		return true;
	}

	private void executeInitService(final X31SDataBeanAccess commonInfoBean, final X31SDataBeanAccess serviceFormBean) throws Exception
	{
		DEBUG_LOG.info("----- FUW11711 Start executeInitService -----");

		// ●サービス呼び出し処理
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, USECASE_ID_FUSV0290);

		FUSV0290_FUSV0290OPDBMapper mapper = new FUSV0290_FUSV0290OPDBMapper();

		HashMap<String, Object> dataMap = new HashMap<String, Object>();


		// 1.ネットオプションサービス契約取得CC(FUSV029001CC)
		dataMap = mapper.setFUSV029001CC_INIT_PC(serviceFormBean, dataMap);

		// 検索結果格納用マップを生成。
		HashMap<String, Object> outputMap = new HashMap<String, Object>();

		try
		{
			// サービスの呼び出しを行う。
			invokeService(paramMap, dataMap, outputMap);

		}catch (JCCWebServiceException se){
			// 関連チェックを行う
			handleInitException(se);
		}catch (Exception ex){
			throw new Exception(ex);
		}

		// ●サービス処理結果の取得
		// 1.ネットオプションサービス契約取得CC(FUSV029001CC)
		mapper.getFUSV029001CC_INIT_PC(serviceFormBean, outputMap);

		DEBUG_LOG.info("----- FUW11711 End executeInitService -----");
	}

	private void executeDispService(final X31SDataBeanAccess commonInfoBean, final X31SDataBeanAccess serviceFormBean) throws Exception
	{
		DEBUG_LOG.info("----- FUW11711 Start executeDispService -----");

		// ●サービス呼び出し処理
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, USECASE_ID_FUSV0290);

		FUSV0290_FUSV0290OPDBMapper mapper = new FUSV0290_FUSV0290OPDBMapper();

		HashMap<String, Object> dataMap = new HashMap<String, Object>();


		// 1.ネットオプションサービス契約取得CC(FUSV029001CC)
		dataMap = mapper.setFUSV029001CC_DISP_PC(serviceFormBean, dataMap);

		// 検索結果格納用マップを生成。
		HashMap<String, Object> outputMap = new HashMap<String, Object>();

		try
		{
			// サービスの呼び出しを行う。
			invokeService(paramMap, dataMap, outputMap);

		}catch (JCCWebServiceException se){
			// 関連チェックを行う
			handleInitException(se);
		}catch (Exception ex){
			throw new Exception(ex);
		}

		// ●サービス処理結果の取得
		// 1.ネットオプションサービス契約取得CC(FUSV029001CC)
		mapper.getFUSV029001CC_DISP_PC(serviceFormBean, outputMap);

		DEBUG_LOG.info("----- FUW11711 End executeDispService -----");
	}

	private void handleInitException( JCCWebServiceException se) throws Exception{
		// 例外情報を取得
		X31CMessageResult msgResult = se.getMessageList();
		X31CMessageMoreInfo[] moreInfo = msgResult.getMessageMoreInfoList();
		X31CMessageMoreInfo info = moreInfo[0];// 関連チェック系は必ずエラーが1つのため0番目から取得する。
		String templateid = info.getTemplateId();
		String itemid = info.getItemId();
		String status = info.getStatus();
		String errFlg = info.getItemCheckErr();
		if (String.valueOf(JPCModelConstant.RELATION_ERR).equals(status))
		{
			if ("EZM0321A010".equals(templateid) && JFUStrConst.RELATION_CHECK_ERR_EA.equals(errFlg))
			{
				if ("key_work_param_id".equals(itemid))
				{
					throw new JCCBusinessException(JFUStrConst.ERROR_CODE_0003);
				}
			}
			if ("ECK0011A010".equals(templateid) && JFUStrConst.RELATION_CHECK_ERR_EA.equals(errFlg))
			{
				if ("key_sysid".equals(itemid))
				{
					throw new JCCBusinessException(JFUStrConst.ERROR_CODE_0003);
				}
			}
		}
		throw new JCCBusinessException(JFUStrConst.ERROR_CODE_0002);
	}

}
