/*********************************************************************
* All Rights reserved, Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム共通（共通マスタ）
*   モジュール名    ：ZMW03601SFLogic
*   ソースファイル名：ZMW03601SFLogic.java
*   作成者          ：富士通
*   日付            ：2012年04月11日
*＜機能概要＞
*   ロール情報削除のビューロジックです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v3.00.00    2012/04/11   FJ）岩元    新規作成
*	v3.00.00	2012/04/11   FJ) 岩元	 【SGY-2012-000006】統合認証対応
*   v5.00.00    2013/08/14   FJ) 日野    【OT-2013-0000713】ディレイド文字コード対応
*
**********************************************************************/

package eo.web.webview.ZMW03601SF;

import java.util.ArrayList;
import java.util.HashMap;

import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;

import eo.common.constant.JPCModelConstant;
import eo.common.constant.JPCOnlineMessageConstant;
import eo.web.webview.JCCWebBusinessLogic;
import eo.web.webview.CommonInfoCF.CommonInfoCFConst;
import eo.web.webview.ZMW03401SF.ZMW03401SFConst;
import eo.web.webview.common.JZMScreenConst;
import eo.web.webview.common.JZMWebCommon;
import eo.web.webview.common.JZMWebConst;
import eo.web.webview.mapping.ZMSV0100_ZMSV0100OPDBMapper_ZMSV010002CC;
import eo.web.webview.mapping.ZMSV0119_ZMSV0119OPDBMapper;
import eo.web.webview.mapping.ZMSV0126_ZMSV0126OPDBMapper;

/**
 * ロール情報削除のビューロジックです。
 * @author 909281
 *
 */
@SuppressWarnings("serial")
public class ZMW03601SFLogic extends JCCWebBusinessLogic
{

	/** サーチエラーフラグ 1メッセージID EKB0300_KW  */
	private static final String SEARCH_ERR_MESSAGE_ZERO = JPCOnlineMessageConstant.EKB0320_KW;

	/** サーチエラーフラグ 2メッセージID EKB0320-KW  */
	private static final String SEARCH_ERR_MESSAGE_ROLE_UPD = JPCOnlineMessageConstant.EKB0320_KW;

	/** メッセージID EKB0350--I  */
	private static final String SEARCH_ERR_MESSAGE_NO_DISP_DTL =  JPCOnlineMessageConstant.EKBA620__I;
	
	/** サーチエラーフラグ 3メッセージID EKB0320_KW  */
	private static final String SEARCH_ERR_MESSAGE_NODATA =  JPCOnlineMessageConstant.EKB0320_KW;

	/** 最大検索数 */
	private static final String MAX_SHOW_CNT = "ZMW03601_MAX_SHOW_CNT";
	
	/** 最大表示数 */
	private static final String ROW_PER_PAGE = "ZMW03601_ROW_PER_PAGE";
	
	/** 確認メッセージ置換文字*/
	private static final String[] MSG_REP_INIT = {"ロール情報削除依頼"};
	
	/** 完了メッセージ置換文字*/
	private static final String[] MSG_REP_FIN = {"ロール情報削除依頼"};

	
	/**
	 * ロール削除初期表示処理を行います。
	 * <br>
	 * @return 処理結果 
	 * @throws Exception 予期せぬエラー
	 */
	public boolean init()throws Exception
	{
		
		if(JZMWebCommon.getScreenInfo(this) == null)
		{
			setBackScreenId();
			
			return true;
		}

		// ページリンク情報の消去
		JZMWebCommon.clearPageLinkInfo(this, this.getGamenId());
		
		// 共通フォームBeanの内容を取得
		X31SDataBeanAccess commonBean = getCommonInfoBean();
		
		// サービスフォームBeanの内容の取得
		X31SDataBeanAccess bean = getServiceFormBean();
		
		X31SDataBeanAccess[] paramBean = {bean};

		// ユースケースID格納用マップを生成。
		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		// サービス実行結果データ用Map
		HashMap<String, Object> outputMap = new HashMap<String, Object>();

		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, JZMWebConst.SV119);

		// マッピングクラスのインスタンスを生成
		ZMSV0119_ZMSV0119OPDBMapper mapper = new ZMSV0119_ZMSV0119OPDBMapper();
		
		mapper.setZMSV011901CC(paramBean, dataMap, JPCModelConstant.FUNC_CD_2);
		
		mapper.setZMSV011902SC(paramBean, dataMap, JPCModelConstant.FUNC_CD_1);

		mapper.setZMSV011903CC(paramBean, dataMap, JPCModelConstant.FUNC_CD_1);
		
		// ページング情報の設定
		JZMWebCommon.upmapperPageLinkInfo(this, JZMWebConst.CC11903, dataMap);
		
		// サービスの呼び出し
		invokeService(paramMap, dataMap, outputMap);

		// ロールに権限グループコードが紐付いている場合エラーを返します。
		if(isErrKGGRP(outputMap, JZMWebConst.CC11901))
		{
			setBackScreenId();
			return true;
		}
		
		// 一覧の初期化
		bean.getDataBeanArray(ZMW03601SFConst.EZM0011B011CBSMSG1LIST).clearArray();
		
		mapper.getZMSV011902SC(paramBean, outputMap);
		
		mapper.getZMSV011903CC(paramBean, outputMap);

		// 検索エラーフラグの取得
		String roleSearchErrFlg = JZMWebCommon.getSearchErrFlg(JZMWebConst.SC11902, outputMap);
		String authSearchErrFlg = JZMWebCommon.getSearchErrFlg(JZMWebConst.CC11903, outputMap);

		// 検索結果0件の場合
		if(JPCModelConstant.SEARCH_ERR_FLG_ZERO.equals(roleSearchErrFlg) || JPCModelConstant.SEARCH_ERR_FLG_ZERO.equals(authSearchErrFlg))
		{
			
			// 検索結果が0件の場合、エラーメッセージを表示
			JZMWebCommon.setMessageInfo(this, SEARCH_ERR_MESSAGE_ZERO);

			// 戻り先画面IDを設定する。
			setBackScreenId();
			
			return true;
		}
		
		// 検索結果が最大超過の場合
		else if(JPCModelConstant.SEARCH_ERR_FLG_OVERMAX.equals(authSearchErrFlg))
		{
			String[] item = {JZMWebCommon.getSearchConst(MAX_SHOW_CNT)};

			// 検索結果が最大検索件数を超える場合、エラーメッセージを表示
			JZMWebCommon.setMessageInfo(this, SEARCH_ERR_MESSAGE_NO_DISP_DTL, item);
			JZMWebCommon.setSendMessageBoolean(bean, ZMW03401SFConst.DTL_DSP_FLG, false);
		}
		
		// ページング情報の設定
		JZMWebCommon.downmapperPageLinkInfo(this, JZMWebConst.CC11903, outputMap);
	
		// 検索のコマンドIDを設定する。
		JZMWebCommon.setSearchCommand(this, JZMWebConst.EVENT_ID_PAGING);
		
		// 背景色のスタイルクラスを設定
		JZMWebCommon.setRowStyleClass(bean, ZMW03601SFConst.EZM0011B011CBSMSG1LIST, ZMW03601SFConst.ROW_STYLE_CLASS_01);
		
		// メッセージの設定の設定
		JZMWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB0370__I , MSG_REP_INIT);
		JZMWebCommon.setMessageAreaBackColor(this, JZMWebConst.ONE);
		
		// 遷移先画面を設定する。
		JZMWebCommon.setNextScreenInfo(commonBean, JZMScreenConst.SCREEN_ID_ZMW03601, JZMScreenConst.SCREEN_NAME_ZMW03601);
		
		return true;
	}
	
	
	/**
	 * ページング処理を行います。
	 * <br>
	 * @return 処理結果 
	 * @throws Exception 予期せぬエラー
	 */
	public boolean paging()throws Exception
	{

		// 共通フォームBeanの内容を取得
		X31SDataBeanAccess commoninfoBean = getCommonInfoBean();
		
		if(!pagingBase())
		{
			// 権限選択画面に遷移する。
			JZMWebCommon.setNextScreenInfo(commoninfoBean, JZMScreenConst.SCREEN_ID_ZMW03301, JZMScreenConst.SCREEN_NAME_ZMW03301);
			return true;
		}
		
		// メッセージの設定の設定
		JZMWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB0370__I , MSG_REP_INIT);
		JZMWebCommon.setMessageAreaBackColor(this, JZMWebConst.ONE);

		JZMWebCommon.setNextScreenInfo(commoninfoBean, JZMScreenConst.SCREEN_ID_ZMW03601, JZMScreenConst.SCREEN_NAME_ZMW03601);
		
		return true;
	}
	
	
	/**
	 * ページング処理を行います。
	 * @return 処理結果 正常終了の場合 trueを返す。
	 */
	private boolean pagingBase()
	{
		
		// サービスフォームBeanのデータBeanアクセスクラスを取得。
		X31SDataBeanAccess bean = super.getServiceFormBean();
				
		// 検索処理の呼び出し
		String searchErrFlg = callAuthoritySearchSrv(bean);
		
		if(searchErrFlg != null && !JZMWebConst.BLANK.equals(searchErrFlg))
		{
			return false;
		}
		
		return true;
	}
	
	/**
	 * 登録確認ボタン処理を行います。
	 * <br>
	 * @return 処理結果 
	 * @throws Exception 予期せぬエラー
	 */
	@SuppressWarnings("unchecked")
	public boolean fix()throws Exception
	{

		// 共有フォームBean
		X31SDataBeanAccess commonBean = getCommonInfoBean();

		// サービスフォームBean
		X31SDataBeanAccess bean = getServiceFormBean();
		
		// サービスフォームBeanのデータBeanアクセスクラスを取得。
		X31SDataBeanAccess[] paramBean = {bean};

		// ユースケースID格納用マップ
		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		// マッピング用Map
		HashMap<String, Object> dataMap = new HashMap<String, Object>();

		// サービス実行結果データ用Map
		HashMap<String, Object> outputMap = new HashMap<String, Object>();
		
		// 格納データの取得
		String roleCd = JZMWebCommon.getSendMessageString(bean, ZMW03601SFConst.ROLE_CD);
		String roleNm = JZMWebCommon.getSendMessageString(bean, ZMW03601SFConst.ROLE_NM);
		String tstaymd = JZMWebCommon.getSendMessageString(bean, ZMW03601SFConst.TSTAYMD);
		String tendymd = JZMWebCommon.getSendMessageString(bean, ZMW03601SFConst.TENDYMD);
		String roleUpdDtm = JZMWebCommon.getSendMessageString(bean, ZMW03601SFConst.ROLE_UPD_DTM);
		
		// バイナリ用Map
		HashMap<String, Object> attachMap = null;
		
		StringBuffer sb = new StringBuffer();
		JZMWebCommon.setAttachMapLine(sb, JZMWebConst.ROLE_DLYD_DEL_ALL, roleCd, roleNm, tstaymd, tendymd, roleUpdDtm, null, null);
		
		attachMap = new HashMap<String, Object>();
		HashMap<String, Object> fileMap = new HashMap<String, Object>();
		ArrayList fileList = new ArrayList();
		fileMap.put(X31CWebConst.TELEGRAM_INFO_ATTACH_FILE,  sb.toString().getBytes(JZMWebConst.ENCODE_MS932));
		fileList.add(fileMap);
		attachMap.put(X31CWebConst.TELEGRAM_INFO_ATTACH_FILE_LIST, fileList);
		
		// ユースケースIDの設定
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, JZMWebConst.SV126);
		
		// マッピングクラスのインスタンスを生成
		ZMSV0126_ZMSV0126OPDBMapper mapper = new ZMSV0126_ZMSV0126OPDBMapper();
		mapper.setZMSV012601CC(paramBean, dataMap, JPCModelConstant.FUNC_CD_2);
		mapper.setZMSV012602SC(paramBean, dataMap, JPCModelConstant.FUNC_CD_1);
		mapper.setZMSV012603SC(paramBean, dataMap, JPCModelConstant.FUNC_CD_1);
		
		// SC12602へのマッピング
		HashMap<String, Object> inDataMap = (HashMap<String, Object>)dataMap.get(JZMWebConst.SC12602);
		inDataMap.put(JZMWebConst.ECC0111D010_DLYD_TRN_JSSI_UNYO_DTM, JZMWebCommon.getOpeDateTimeStamp(this, null));
		inDataMap.put(JZMWebConst.ECC0111D010_FILE_DEL_YMD, null);

		// SC12603へのマッピング
		inDataMap = (HashMap<String, Object>)dataMap.get(JZMWebConst.SC12603);
		inDataMap.put(JZMWebConst.EZM0251D010_TRN_REQ_DTM, JZMWebCommon.getOpeDateTime(this, null).substring(0, 12));
		inDataMap.put(JZMWebConst.EZM0251D010_FILE_DEL_YMD, null);
		inDataMap.put(JZMWebConst.EZM0251D010_TRN_IRAISHA_OPEACNT, JZMWebCommon.getSendMessageString(commonBean, CommonInfoCFConst.USER_ID));
		
		// サービスの呼び出し
		invokeService(paramMap, dataMap, outputMap, attachMap);

		// ロールに権限グループコードが紐付いている場合エラーを返します。
		if(isErrKGGRP(outputMap, JZMWebConst.CC12601))
		{
			JZMWebCommon.setNextScreenInfo(commonBean, JZMScreenConst.SCREEN_ID_ZMW03601, JZMScreenConst.SCREEN_NAME_ZMW03601);
			return true;
		}
		
		// メッセージの設定の設定
		JZMWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB4390__I , MSG_REP_FIN);
		
		// 画面遷移
		JZMWebCommon.setNextScreenInfo(commonBean, JZMScreenConst.SCREEN_ID_ZMW03602, JZMScreenConst.SCREEN_NAME_ZMW03602);
		
		return true;
	}

	
	/**
	 * 戻るボタン処理を行います。
	 * <br>
	 * @return 処理結果 
	 * @throws Exception 予期せぬエラー
	 */
	public boolean back()throws Exception
	{

		// 共有フォームBean
		X31SDataBeanAccess commonBean = getCommonInfoBean();

		// 画面遷移
		JZMWebCommon.setNextScreenInfo(commonBean, JZMWebCommon.getScreenId(this), JZMWebConst.BLANK);
		
		return true;
	}

	/**
	 * 完了ボタン処理を行います。
	 * <br>
	 * @return 処理結果 
	 * @throws Exception 予期せぬエラー
	 */
	public boolean fin()throws Exception
	{
		
		// HashMapを設定する
		JZMWebCommon.setScreenInfo(this, this.getGamenId(), new HashMap<String, Object>());
		
		// 共有フォームBean
		X31SDataBeanAccess commonBean = getCommonInfoBean();

		// 画面遷移
		JZMWebCommon.setNextScreenInfo(commonBean, JZMWebCommon.getScreenId(this), JZMWebConst.BLANK);
		
		return true;
	}
	
	/**
	 * 権限検索サービスを呼び出します。
	 * <br />
	 * @param bean サービスフォームBean
	 * @param outputMap 検索結果格納用マップ
	 * @return 検索エラーフラグ
	 */
	@SuppressWarnings("unchecked")
	private String callAuthoritySearchSrv(X31SDataBeanAccess bean)
	{
		
		X31SDataBeanAccess[] paramBean = {bean};
		
		// ユースケースID格納用マップを生成。
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put(X31CWebConst.TELEGRAM_INFO_USECASE_ID, JZMWebConst.SV100);

		// サービス実行結果データ用Map
		HashMap<String, Object> outputMap = new HashMap<String, Object>();

		// 検索エラーフラグの取得
		String searchErrFlg = null;
		
		String maxShowCnt = JZMWebCommon.getSearchConst(MAX_SHOW_CNT);
		
		String rowPerPage = JZMWebCommon.getSearchConst(ROW_PER_PAGE);
		
		// 一覧の初期化
		bean.getDataBeanArray(ZMW03601SFConst.EZM0011B011CBSMSG1LIST).clearArray();
		
		// マッピングクラスのインスタンスを生成
		ZMSV0100_ZMSV0100OPDBMapper_ZMSV010002CC mapper = new ZMSV0100_ZMSV0100OPDBMapper_ZMSV010002CC();
		
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		
		mapper.setBeanListNm(ZMW03601SFConst.EZM0011B011CBSMSG1LIST);
		mapper.setKeyClmNmRoleCd(ZMW03601SFConst.ROLE_CD);
		mapper.setListClmNmAuthorityCd(ZMW03601SFConst.AUTHORITY_CD_01);
		mapper.setListClmNmAuthorityNm(ZMW03601SFConst.AUTHORITY_NM_01);
		mapper.setListClmNmAuthoritySbtCdNm(ZMW03601SFConst.AUTHORITY_SBT_CD_NM_01);
		mapper.setListClmNmAuthorityTendymd(ZMW03601SFConst.AUTHORITY_TENDYMD_01);
		mapper.setListClmNmAuthorityTstaymd(ZMW03601SFConst.AUTHORITY_TSTAYMD_01);
		
		mapper.setZMSV010002CC(paramBean, dataMap, JPCModelConstant.FUNC_CD_1, maxShowCnt, rowPerPage);
		
		HashMap<String, Object> inDataMap = (HashMap<String, Object>)dataMap.get(JZMWebConst.CC10002);
		
		inDataMap.put(JZMWebConst.EZM0031_KEY_SEARCH_DIV, JZMWebConst.ONE);
		
		// ページング情報の設定
		JZMWebCommon.upmapperPageLinkInfo(this, JZMWebConst.CC10002, dataMap);
		
		// サービスの呼び出し
		invokeService(paramMap, dataMap, outputMap);
		
		// マッピングクラスの呼び出し
		mapper.getZMSV010002CC(paramBean, outputMap);

		// ページング情報の設定
		JZMWebCommon.downmapperPageLinkInfo(this, JZMWebConst.CC10002, outputMap);
		
		// 検索エラーフラグの取得
		searchErrFlg = JZMWebCommon.getSearchErrFlg(JZMWebConst.CC10002, outputMap);

		// 検索結果0件の場合
		if(JPCModelConstant.SEARCH_ERR_FLG_ZERO.equals(searchErrFlg))
		{
			
			// 検索結果が0件の場合、エラーメッセージを表示
			JZMWebCommon.setMessageInfo(this, SEARCH_ERR_MESSAGE_ZERO);
		}
		
		// 検索結果が最大超過の場合
		else if(JPCModelConstant.SEARCH_ERR_FLG_OVERMAX.equals(searchErrFlg))
		{
			if(JZMWebCommon.getSendMessageBoolean(bean, ZMW03601SFConst.DTL_DSP_FLG))
			{
				String[] item = {maxShowCnt};
				
				// 検索結果が最大検索件数を超える場合、エラーメッセージを表示
				JZMWebCommon.setMessageInfo(this, SEARCH_ERR_MESSAGE_ROLE_UPD, item);
			}
			else
			{
				searchErrFlg = null;
			}
		}

		// 表示ページなしの場合
		else if(JPCModelConstant.SEARCH_ERR_FLG_NODATA.equals(searchErrFlg))
		{
			
			// 表示ページに該当するデータが存在しない場合、エラーメッセージを表示
			JZMWebCommon.setMessageInfo(this, SEARCH_ERR_MESSAGE_NODATA);
		}
		
		// 行の背景色を設定
		JZMWebCommon.setRowStyleClass(bean, ZMW03601SFConst.EZM0011B011CBSMSG1LIST, ZMW03601SFConst.ROW_STYLE_CLASS_01);
		
		return searchErrFlg;
	}

	/**
	 * ロール削除CCがエラーを返しているかを判定します。
	 * @param outputMap サービス出力マップ
	 * @param ccName CC名称
	 * @return 判定結果（エラーの場合true)
	 */
	@SuppressWarnings("unchecked")
	private boolean isErrKGGRP(HashMap<String, Object> outputMap, String ccName)
	{
		if(outputMap.containsKey(ccName))
		{
			HashMap<String, Object> ccMap = (HashMap<String, Object>)outputMap.get(ccName);
			
			if(ccMap.containsKey(JZMWebConst.EZM0041B013CBSMSG1LIST))
			{
				ArrayList<?> msgList = (ArrayList<?>)ccMap.get(JZMWebConst.EZM0041B013CBSMSG1LIST);
				if(msgList.size() != 0)
				{
					for(int i = 0; i < msgList.size(); i++)
					{
						HashMap tmpMap = (HashMap)msgList.get(i);
						if(tmpMap.containsKey(JZMWebConst.EZM0041B013_KGGRP_CD))
						{
							String[] items = new String[]{JZMWebConst.MSG_ROLE_CD, JZMWebConst.MSG_KGGRP, (String)tmpMap.get(JZMWebConst.EZM0041B013_KGGRP_CD)};
							
							// ロールコードが権限グループマスタで使用されている場合エラー
							JZMWebCommon.setMessageInfo(this, JPCOnlineMessageConstant.EKB9960_KW, items);
						}
					}
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * セッションから遷移先画面を取得し共通ビーンに設定します。
	 */
	private void setBackScreenId()
	{
		
		// 共通フォームBeanの内容を取得
		X31SDataBeanAccess commonBean = getCommonInfoBean();
		
		String screenId = JZMWebCommon.getScreenId(this);

		// 遷移先画面ID、名称の設定
		commonBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_ID,
				X31CWebConst.DATABEAN_SET_VALUE, screenId);
		commonBean.sendMessageString(CommonInfoCFConst.NEXT_SCREEN_NAME,
				X31CWebConst.DATABEAN_SET_VALUE, JZMWebConst.BLANK);

		setTransferPageId(screenId + JZMWebConst.PJP);
	}
}
