/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：SCW00301SFLogic
*   ソースファイル名：SCW00301SFLogic.java
*   作成者          ：富士通
*   日付            ：2011年08月01日
*＜機能概要＞
*   電話用VLAN-ID発行依頼登録を処理するロジッククラスです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
**********************************************************************/
package eo.web.webview.SCW00301SF;

import static com.fujitsu.futurity.web.x31.X31SWebLog.DEBUG_LOG;
import static eo.web.webview.common.JSCScreenConst.*;

import java.util.HashMap;

import static eo.common.constant.JPCOnlineMessageConstant.*;
import static eo.common.constant.JPCModelConstant.*;
import static eo.web.webview.CommonInfoCF.CommonInfoCFConst.*;
import static eo.web.webview.SCW00401SF.SCW00401SFConst.*;

import com.fujitsu.futurity.web.x31.X31CMessageResult;
import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;

import eo.common.constant.JPCOnlineMessageConstant;
import eo.common.constant.JSCStrConstant;
import eo.common.constant.JPCModelConstant;
import eo.common.util.JSCCommonUtil;
import eo.web.webview.JCCWebBusinessLogic;
import eo.web.webview.SCW00401SF.SCW00401SFConst;
import eo.web.webview.common.JCCWebCommon;
import eo.web.webview.mapping.SCSV0004_SCSV0004OPDBMapper;
import eo.web.webview.mapping.SCSV0005_SCSV0005OPDBMapper;

/**
 * VLAN-ID登録を処理するロジッククラスです。
 * @author 富士通
 */
public class SCW00301SFLogic extends JCCWebBusinessLogic
{
	/** メッセージ */
	private static final String MSG_REGISTER_VLAN_ID = "VLAN-ID発行依頼の登録";
	
	/**
	 * 初期化処理
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	public boolean init() throws Exception
	{
		// サービスフォームBeanを操作するデータBeanアクセスクラスを取得します。
		X31SDataBeanAccess serviceFormBean = super.getServiceFormBean();
		
		// サービス契約番号保持領域をクリア
		serviceFormBean.sendMessageString(SCW00401SFConst.KEY_SVC_KEI_NO_HOJI, X31CWebConst.DATABEAN_SET_VALUE, "");
		
		//初期画面へ遷移
		setNextScreen(SCREEN_ID_SCW00301, SCREEN_NAME_SCW00301);
		return true;
	}
	
	/**
	 * 検索ボタン押下処理
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	public boolean search() throws Exception
	{
		HashMap paramMap = new HashMap();
		HashMap inputMap = new HashMap();
		HashMap outputMap = new HashMap();

		X31SDataBeanAccess bean = super.getServiceFormBean();
		X31SDataBeanAccess[] paramBean = {bean};

		//入力値チェック
		if(!chkItemValue1(paramBean[0]))
		{
			return false;
		}

		//データビーンクリア
		clearDatabean();
		
		// 入力されたサービス契約番号を保持
		String scvKeiNo = bean.sendMessageString(SCW00401SFConst.KEY_SVC_KEI_NO, X31CWebConst.DATABEAN_GET_VALUE);
		bean.sendMessageString(SCW00401SFConst.KEY_SVC_KEI_NO_HOJI, X31CWebConst.DATABEAN_SET_VALUE, scvKeiNo);

		//サービス呼び出し(検索処理)
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, "SCSV0004");
		SCSV0004_SCSV0004OPDBMapper mapper = new SCSV0004_SCSV0004OPDBMapper();
		mapper.setSCSV000401SC(paramBean, inputMap, JPCModelConstant.FUNC_CD_1);
		X31CMessageResult msgResult = invokeService(paramMap, inputMap, outputMap);
		if (msgResult != null)
		{
			JCCWebCommon.setMessageInfo(this, msgResult);
			return true;
		}
		mapper.getSCSV000401SC(paramBean, outputMap);
		
		// 検索結果が0件の場合の処理
		String searchErrFlg = JCCWebCommon.getSearchErrFlg("SCSV000401SC", outputMap);
		if (JPCModelConstant.SEARCH_ERR_FLG_ZERO.equals(searchErrFlg))
		{
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB0330__I);
			return false;
		}

		//自画面へ遷移
		setNextScreen(SCREEN_ID_SCW00301, SCREEN_NAME_SCW00301);
		return true;
	}
	
	/**
	 * 登録確認ボタン押下処理
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	public boolean confirmCreate() throws Exception
	{
		HashMap paramMap = new HashMap();
		HashMap inputMap = new HashMap();
		HashMap outputMap = new HashMap();

		X31SDataBeanAccess bean = super.getServiceFormBean();
		X31SDataBeanAccess[] paramBean = {bean};

		//リスト項目データビーンの取得
		X31SDataBeanAccess subbean = paramBean[0].getDataBeanArray(ESC0021A010CBSMSG1LIST).getDataBean(0);
		
		//データビーンのチェック
		if(!chkItemValue2(subbean))
		{
			return false;
		}
		
		//サービス呼び出し(登録前チェック処理)
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, "SCSV0005");
		SCSV0005_SCSV0005OPDBMapper mapper = new SCSV0005_SCSV0005OPDBMapper();
		mapper.setSCSV000501SC(paramBean, inputMap, FUNC_CD_6);
		X31CMessageResult msgResult = invokeService(paramMap, inputMap, outputMap);
		if(msgResult != null)
		{
			JCCWebCommon.setMessageInfo(this, msgResult);
			return false;
		}

		//次画面に表示するメッセージの設定
		JCCWebCommon.setMessageInfo(this, EKB0370__I, new String [] { MSG_REGISTER_VLAN_ID});
		
		//次画面へ遷移
		setNextScreen(SCREEN_ID_SCW00302, SCREEN_NAME_SCW00302);
		return true;
	}
	
	/**
	 * 確定ボタン押下処理
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	@SuppressWarnings("unchecked")
	public boolean fix() throws Exception
	{
		HashMap paramMap = new HashMap();
		HashMap inputMap = new HashMap();
		HashMap outputMap = new HashMap();

		X31SDataBeanAccess bean = super.getServiceFormBean();
		X31SDataBeanAccess[] paramBean = {bean};

		//サービスの呼び出し(登録処理)
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, "SCSV0005");
		SCSV0005_SCSV0005OPDBMapper mapper = new SCSV0005_SCSV0005OPDBMapper();
		mapper.setSCSV000501SC(paramBean, inputMap, FUNC_CD_5);
		X31CMessageResult msgResult = invokeService(paramMap, inputMap, outputMap);
		if(msgResult != null)
		{
			JCCWebCommon.setMessageInfo(this, msgResult);
			return false;
		}

		//次画面に表示するメッセージの設定
		JCCWebCommon.setMessageInfo(this, EKB0380__I, new String [] { MSG_REGISTER_VLAN_ID});

		//次画面へ遷移
		setNextScreen(SCREEN_ID_SCW00303, SCREEN_NAME_SCW00303);
		return true;
	}
	
	/**
	 * 修正ボタン押下処理
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	public boolean modify() throws Exception
	{
		//前画面へ遷移
		setNextScreen(SCREEN_ID_SCW00301, SCREEN_NAME_SCW00301);
		return true;
	}
	
	/**
	 * 完了ボタン押下処理
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	public boolean complete() throws Exception
	{
		X31SDataBeanAccess bean = super.getServiceFormBean();
		bean.sendMessageString(KEY_SVC_KEI_NO,      X31CWebConst.DATABEAN_SET_VALUE, "");
		bean.sendMessageString(KEY_SVC_KEI_NO_HOJI, X31CWebConst.DATABEAN_SET_VALUE, "");

		//データビーンのクリア
		clearDatabean();
		
		//初期画面へ遷移
		setNextScreen(SCREEN_ID_SCW00301, SCREEN_NAME_SCW00301);
		return true;
	}

	/**
	 * データビーンをクリアします。
	 * @throws Exception 例外発生時
	 */
	private void clearDatabean() throws Exception
	{
		X31SDataBeanAccess bean = super.getServiceFormBean();
		bean.getDataBeanArray(ESC0021A010CBSMSG1LIST).clearArray();
		bean.getDataBeanArray(ESC0021A010CBSMSG1LIST).addDataBean();
		
	}

	/**
	 * 画面遷移先を設定します。
	 * @author 富士通
	 * @param nextScreenId 遷移先画面ＩＤ
	 * @param nextScreenName 遷移先画面名
	 */
	private void setNextScreen(String nextScreenId, String nextScreenName)
	{
		X31SDataBeanAccess  commoninfoBean = super.getCommonInfoBean();
	
		commoninfoBean.sendMessageString(NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, nextScreenId);
		commoninfoBean.sendMessageString(NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, nextScreenName);

		DEBUG_LOG.debug(String.format("遷移先 CD：%s NM：%s", nextScreenId, nextScreenName));
	}

	/**
	 * チェック処理１
	 * @param bean サービスフォームビーン
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	private boolean chkItemValue1(X31SDataBeanAccess bean) throws Exception
	{
		String sValue = null;
		//サービス契約番号
		sValue = bean.sendMessageString(KEY_SVC_KEI_NO, X31CWebConst.DATABEAN_GET_VALUE);
		if (!JSCCommonUtil.isValidParameter(sValue))
		{
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB0010_TW, new String[]{"サービス契約番号"});
			return false;
		}
		return true;
	}

	/**
	 * チェック処理２
	 * @param bean サービスフォームビーン
	 * @return 成否
	 * @throws Exception 例外発生時
	 */
	private boolean chkItemValue2(X31SDataBeanAccess bean) throws Exception
	{
		// eo光電話契約
		String eoflg = bean.sendMessageString(EO_TEL_FLG_01, X31CWebConst.DATABEAN_GET_VALUE);
		if (!("1".equals(eoflg)))
		{
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB0780_KW, new String[]{"登録処理", "eo光電話以外の", "発行依頼処理"});
			return false;
		}

		// SODステータス
		String sodsts = bean.sendMessageString(TEL_VLAN_ORDER_STAT_01, X31CWebConst.DATABEAN_GET_VALUE);
		if (JSCStrConstant.SOD_STAT_SND_WAIT.equals(sodsts))
		{
			// SODステータス（送信待ち）
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB1040_JW, new String[]{"SODステータス", "送信待ち", "VLAN-ID発行依頼"});
			return false;
		}
		else if (JSCStrConstant.SOD_STAT_ACK_WAIT.equals(sodsts))
		{
			// SODステータス（応答待ち）
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB1040_JW, new String[]{"SODステータス", "応答待ち", "VLAN-ID発行依頼"});
			return false;
		}
		else if (JSCStrConstant.SOD_STAT_ACK_ABNORMAL.equals(sodsts))
		{
			// SODステータス（応答異常）
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB1040_JW, new String[]{"SODステータス", "応答異常", "VLAN-ID発行依頼"});
			return false;
		}
		else if (JSCStrConstant.SOD_STAT_REAPPLY.equals(sodsts))
		{
			// SODステータス（再送依頼）
			JCCWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB1040_JW, new String[]{"SODステータス", "再送依頼", "VLAN-ID発行依頼"});
			return false;
		}

		// SOD業務連携ステータス
		String sodworksts = bean.sendMessageString(SOD_WORK_RNKI_STAT_01, X31CWebConst.DATABEAN_GET_VALUE);
		if (JSCStrConstant.SOD_RNKI_STAT_WAIT_RES.equals(sodworksts))
		{
			JCCWebCommon.setMessageInfo(
					this, JPCOnlineMessageConstant.EKB1040_JW, new String[]{"SOD業務連携ステータス", "業務連携待ちSOD応答未", "VLAN-ID発行依頼"});
			return false;
		}
		if (JSCStrConstant.SOD_RNKI_STAT_WAIT_FIN.equals(sodworksts))
		{
			JCCWebCommon.setMessageInfo(
					this, JPCOnlineMessageConstant.EKB1040_JW, new String[]{"SOD業務連携ステータス", "業務連携待ちSOD応答済", "VLAN-ID発行依頼"});
			return false;
		}
		
		// 依頼時工事案件番号取得エラー
		String reqJiKjakNo = bean.sendMessageString(REQ_JI_KJAK_NO_01, X31CWebConst.DATABEAN_GET_VALUE);
		if ("".equals(reqJiKjakNo) || reqJiKjakNo == null)
		{
			JCCWebCommon.setMessageInfo(
					this, JPCOnlineMessageConstant.EKB0930_NW, new String[]{"依頼時工事案件番号が取得出来ない", "VLAN-ID発行依頼"});
			return false;
		}

		return true;
	}
}
