/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：FUW05101SFLogic
*   ソースファイル名：FUW05101SFLogic.java
*   作成者          ：富士通
*   日付            ：2012年01月05日
*＜機能概要＞
*   eo-netフォンユーザー解約 ビューロジックです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2012/01/05   FJ）西川    新規作成
*
**********************************************************************/
package eo.web.webview.FUW05101SF;

import static com.fujitsu.futurity.web.x31.X31SWebLog.DEBUG_LOG;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fujitsu.futurity.web.x00.JCCBusinessException;
import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;

import eo.common.constant.JFUStrConst;
import eo.web.webview.JCCWebBusinessLogic;
import eo.web.webview.CommonInfoCF.CommonInfoCFConst;
import eo.web.webview.common.JFUScreenConst;
import eo.web.webview.common.JFUWebCommon;


/**
 * FUW051_eo-netフォンユーザー解約 ビューロジック
 * <br>
 * @author 富士通
 */
public class FUW05101SFLogic extends JCCWebBusinessLogic
{

	/** [置換文字列]プロパティファイル取得クエリ */
	private static final String QUERY_DISPTEXT_PATH = "DISPSETTING/SETTING[@screenId='FUW05101']/DISPTEXT[@groupid='err_msg_param']";

	/** [置換文字列]プロパティファイル取得属性名 */
	private static final String QUERY_ATTR_NAME = "id";

	/** 置換文字列取得キー:置換文字列_1 */
	private static final String CKAN_CHR_KEY_1 = "ckan_chr_1";

	/** 置換文字列取得キー:置換文字列_2 */
	private static final String CKAN_CHR_KEY_2 = "ckan_chr_2";

	/** 置換文字列取得キー:置換文字列_1 */
	private static final String CKAN_CHR_KEY_3 = "ckan_chr_3";

	/** エラーメッセージID:EKF1040-TW */
	private static final String ERR_MSG_ID_EKF1040 = "EKF1040-TW";

	/** エラーメッセージID:EKF1790-NW */
	private static final String ERR_MSG_ID_EKF1790 = "EKF1790-NW";

	/** 登録完了フラグ:"0"(未完了) */
	private static final String ADD_FIN_FLG_MI = "0";

	/** 登録完了フラグ:"1"(完了) */
	private static final String ADD_FIN_FLG_FIN = "1";

	/** 登録完了フラグリスト: */
	private static final List<String> ADD_FIN_FLG_LIST = Arrays.asList(ADD_FIN_FLG_MI, ADD_FIN_FLG_FIN);

	/** 外部コマンドリターンコード:正常 */
	private static final String EXIT_CODE_OK = "0";

	/** 外部コマンド実行結果取得インデックス:ステータス[0] */
	private static final int IDX_STATUS = 0;

	/** ステータス:"000"(指定されたVoIPユーザIDの解約に成功) */
	private static final String STATUS_000 = "000";

	/** 外部コマンドステータスＯＫリスト: */
	private static final List<String> STATUS_OK_LIST = Arrays.asList(STATUS_000);

	/** サービスID */
	private static final String SERVICE_ID = "FUSV0017_01";

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//【FUW05101】
////////////////////////////////////////////////////////////////////////////////

	/**
	 * 初期表示処理
	 * <br>
	 * @return boolean true(固定)
	 * @throws Exception
	 */
	public boolean init() throws Exception
	{

		// 画面情報を取得し、サービスフォームBeanに設定
		JFUWebCommon.getScreenInfo(this);

		//----------------------------------------------
		// Bean取得
		//----------------------------------------------
		// 共有フォームBeanのアクセスクラスを取得
		X31SDataBeanAccess  commonInfoBean = super.getCommonInfoBean();

		// -------------------------------------------------
		// 利用可能サービスチェック
		// -------------------------------------------------
		JFUWebCommon.checkCommonRelation(this, SERVICE_ID);

		//----------------------------------------------
		// 共有フォームBean設定
		//----------------------------------------------
		// 遷移先画面ＩＤ
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_ID_FUW05101);
		// 遷移先画面名
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_NAME_FUW05101);

		return true;
	}

	/**
	 * 確認するボタン押下処理
	 * 
	 * @return boolean true(固定)
	 * @throws Exception
	 */
	public boolean cfm() throws Exception
	{

		//----------------------------------------------
		// Bean取得
		//----------------------------------------------
		// 共有フォームBeanのアクセスクラスを取得
		X31SDataBeanAccess  commonInfoBean = super.getCommonInfoBean();
		// サービスフォームBeanのデータBeanアクセスクラスを取得
		X31SDataBeanAccess bean = super.getServiceFormBean();

		//----------------------------------------------
		// 入力チェック
		//----------------------------------------------
		boolean chkInputRslt = chkInput(bean);

		DEBUG_LOG.debug("■入力チェック結果：" + chkInputRslt);

		// エラーが発生しなかった場合、共有フォームbeanに遷移先画面の設定を行う
		if (chkInputRslt)
		{
			//----------------------------------------------
			// 共有フォームBean設定
			//----------------------------------------------
			// 遷移先画面ＩＤ
			commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_ID_FUW05102);
			// 遷移先画面名
			commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE,
												JFUScreenConst.SCREEN_NAME_FUW05102);
		}

		return true;
	}

	/**
	 * 入力チェック
	 * <br>
	 * @param bean			サービスフォームBean
	 * @return true:チェックＯＫ／false:チェックＮＧ
	 * @throws Exception	すべての例外
	 */
	private boolean chkInput(X31SDataBeanAccess bean) throws Exception
	{
		boolean chkRslt = true;

		// ----------------------------------------------
		// ■画面定義情報の取得
		// ----------------------------------------------
		// ＜置換文字列マップ＞
		Map<String, String> dispDefMap = JFUWebCommon.getDispItemsAsMap(QUERY_DISPTEXT_PATH, QUERY_ATTR_NAME);

		//----------------------------------------------------------------------------------
		// VoIPユーザーID(voip_user_id)がユーザーID(user_id)と一致するレコードを抽出
		//----------------------------------------------------------------------------------
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(FUW05101SFConst.VOIP_USER_ID_01, JFUWebCommon.getDataBeanItemByPath(bean, FUW05101SFConst.USER_ID));
		X31SDataBeanAccess voipUserIf = JFUWebCommon.getDataBeanTypeByPathWithKey(bean, FUW05101SFConst.VOIP_USER_IF, condition);

		// ----------------------------------------------
		// ■ユーザID存在チェック
		// ----------------------------------------------
		if (null == voipUserIf)
		{
			//----------------------------------------------------------------------------------
			// ユーザIDが、サービスフォームBean.VoIPユーザー情報に含まれない場合、エラー
			//----------------------------------------------------------------------------------
			// メッセージID:EKF1040-TW, 置換文字列_1
			//----------------------------------------------------------------------------------
			JFUWebCommon.setMessageInfo(this, ERR_MSG_ID_EKF1040,
												new String[]{dispDefMap.get(CKAN_CHR_KEY_1)},
												bean.getItemId(FUW05101SFConst.USER_ID));

			chkRslt = false;	// エラー
		}
		// ----------------------------------------------
		// ■登録完了フラグチェック
		// ----------------------------------------------
		else if (!ADD_FIN_FLG_LIST.contains(JFUWebCommon.getDataBeanItemByPath(voipUserIf, FUW05101SFConst.ADD_FIN_FLG_01)))
		{
			//----------------------------------------------------------------------------------
			// 登録完了フラグが、"0"(未完了)もしくは"1"(完了)以外である場合、エラー
			//----------------------------------------------------------------------------------
			// メッセージID:EKF1790-NW, 置換文字列_2, 置換文字列_3
			//----------------------------------------------------------------------------------
			JFUWebCommon.setMessageInfo(this, ERR_MSG_ID_EKF1790,
												new String[]{dispDefMap.get(CKAN_CHR_KEY_2), dispDefMap.get(CKAN_CHR_KEY_3)},
												bean.getItemId(FUW05101SFConst.USER_ID));
			chkRslt = false;	// エラー
		}

		return chkRslt;
	}

////////////////////////////////////////////////////////////////////////////////
//【FUW05102】
////////////////////////////////////////////////////////////////////////////////

	/**
	 * 戻るボタン押下処理
	 * 
	 * @return boolean true(固定)
	 * @throws Exception
	 */
	public boolean back() throws Exception
	{

		//----------------------------------------------
		// Bean取得
		//----------------------------------------------
		// 共有フォームBeanのアクセスクラスを取得
		X31SDataBeanAccess  commonInfoBean = super.getCommonInfoBean();

		//----------------------------------------------
		// 共有フォームBean設定
		//----------------------------------------------
		// 遷移先画面ＩＤ
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_ID_FUW05101);
		// 遷移先画面名
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_NAME_FUW05101);

		return true;
	}

	/**
	 * 解約するボタン押下処理
	 * 
	 * @return boolean true(固定)
	 * @throws Exception
	 */
	public boolean dsl() throws Exception
	{

		//----------------------------------------------
		// Bean取得
		//----------------------------------------------
		// 共有フォームBeanのアクセスクラスを取得
		X31SDataBeanAccess  commonInfoBean = super.getCommonInfoBean();
		// サービスフォームBeanのデータBeanアクセスクラスを取得
		X31SDataBeanAccess bean = super.getServiceFormBean();

		// -------------------------------------------------
		// 利用可能サービスチェック
		// -------------------------------------------------
		JFUWebCommon.checkCommonRelation(this, SERVICE_ID);

		//----------------------------------------------
		// 外部コマンド実行・結果取得
		//----------------------------------------------
		List<String> execSyncRsltList = removeVoipUserVc(commonInfoBean, bean);

		//----------------------------------------------
		// 取得データチェック
		//----------------------------------------------
		chkGetData(execSyncRsltList, bean);

		//----------------------------------------------
		// 共有フォームBean設定
		//----------------------------------------------
		// 遷移先画面ＩＤ
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_ID_FUW05103);
		// 遷移先画面名
		commonInfoBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME, X31CWebConst.DATABEAN_SET_VALUE, JFUScreenConst.SCREEN_NAME_FUW05103);

		return true;
	}

	/**
	 * [eo-netフォンコースVoIPユーザー解約コマンド]実行
	 * <br>
	 * @param commonInfoBean	共有フォームBean
	 * @param bean				サービスフォームBean
	 * @return 外部コマンド実行結果リスト
	 * @throws Exception	すべての例外
	 */
	private List<String> removeVoipUserVc(X31SDataBeanAccess commonInfoBean, X31SDataBeanAccess bean) throws Exception
	{
		// サービス契約番号[共有フォームBean.サービス契約情報(0).サービス契約番号]
		String svcKeiNo = JFUWebCommon.getDataBeanItemByPath(commonInfoBean, JFUWebCommon.SVC_KEI_INFO + JFUWebCommon.SEP_0
																+ CommonInfoCFConst.SVC_KEI_NO_23);
		// サービスフォームBean.ユーザーID
		String userId =  JFUWebCommon.getDataBeanItemByPath(bean, FUW05101SFConst.USER_ID);

		//----------------------------------------------
		// 外部コマンド実行処理
		//----------------------------------------------
		return execSyncVoip(svcKeiNo, userId);
	}

	/**
	 * 外部コマンド実行処理
	 * <br>
	 * @param svcKeiNo		サービス契約番号[eoユーザーID]
	 * @param userId		ユーザID[VoIPユーザーID]
	 * @return 外部コマンド実行結果リスト
	 * @throws Exception	すべての例外
	 */
	private List<String> execSyncVoip(String svcKeiNo, String userId) throws Exception
	{

		List<String> resultList = null;

		// 外部コマンド実行標準出力格納
		StringBuffer outPut = new StringBuffer();

		// 外部コマンド情報
		String[] cmd = {JFUWebCommon.getApplicationConst(JFUStrConst.REMOTE_CMD_NM),
						JFUWebCommon.getApplicationConst(JFUStrConst.REMOTE_CMD_OP1),
						JFUWebCommon.getApplicationConst(JFUStrConst.FUIFE017_PARAM1),
						svcKeiNo,
						userId,
						JFUWebCommon.getApplicationConst(JFUStrConst.REMOTE_CMD_OP2),
						JFUWebCommon.getApplicationConst(JFUStrConst.VOIP_ENV)};

		// (デバッグログ)
		DEBUG_LOG.debug("★ コマンドライン引数配列 ★：＝" + Arrays.toString(cmd));

		// 外部コマンド実行処理
		String exitCode = JFUWebCommon.execSync(cmd, outPut, Integer.parseInt(JFUWebCommon.getApplicationConst(JFUStrConst.REMOTE_EXEC_TIME_OUT)));

		// (デバッグログ)
		DEBUG_LOG.debug("★ リターンコード ★：＝" + exitCode);

		// リターンコードチェック
		if (!EXIT_CODE_OK.equals(exitCode))
		{
			DEBUG_LOG.debug("FUW051_execSyncVoip【システムエラー】 リターンコードが0(正常終了)以外");
			// "0"(正常終了)以外の場合、システムエラー
			throw new JCCBusinessException(JFUStrConst.ERROR_CODE_0002);
		}

		if (outPut.length() > 0)
		{
			// デバッグログ
			DEBUG_LOG.debug("★ 外部コマンド実行結果 ★：＝" + outPut.toString());
			// 出力結果をListに詰め直す
			resultList = Arrays.asList(outPut.toString().split(JFUWebCommon.LINE_SEPARATOR));
		}
		return resultList;
	}

	/**
	 * 取得データチェック<br>
	 * <br>
	 * @param execSyncRsltList	外部コマンド実行結果リスト
	 * @param bean				サービスフォームBean
	 * @throws Exception		すべての例外
	 */
	private void chkGetData(List<String> execSyncRsltList, X31SDataBeanAccess bean) throws Exception
	{
		//--------------------------------------------------------------------------------------------
		// 【取得データチェック】
		//--------------------------------------------------------------------------------------------
		if (JFUWebCommon.isNull(execSyncRsltList) || (0 >= execSyncRsltList.size()))
		{
			DEBUG_LOG.debug("FUW051_chkGetData【システムエラー】 外部コマンド実行結果リストが未設定");
			// 結果リストが未設定の場合、システムエラー
			throw new JCCBusinessException(JFUStrConst.ERROR_CODE_0002);
		}

		// ステータスを取得 ← 外部コマンド実行結果配列[0]
		String status = execSyncRsltList.get(IDX_STATUS);

		DEBUG_LOG.debug("★ ステータス ★：＝" + status);

		//----------------------------------------------
		// ＜ステータス異常時＞
		//----------------------------------------------
		// ステータスが"000"(指定されたVoIPユーザIDの解約に成功)以外の場合
		if (!STATUS_OK_LIST.contains(status))
		{
			DEBUG_LOG.debug("FUW051_chkGetData【システムエラー】 ステータスが 000 以外");
			// システムエラー
			throw new JCCBusinessException(JFUStrConst.ERROR_CODE_0002);
		}
	}
}
