/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：eo顧客基幹システム
*   モジュール名    ：JCCAuthorityCtrlUtil
*   ソースファイル名：JCCAuthorityCtrlUtil.java
*   作成者          ：富士通
*   日付            ：2011年05月07日
*＜機能概要＞
* 権限制御の権限判定部品/権限判定オンラインのユーティリティーメソッド/
* 共通メソッドを提供します。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v18.00.00   2015/08/11   FJ) 阪口   【ANK-2631-00-00】ワンストップ案件（ＳＴＥＰ１）※koptWebBからkoptWebAへコピー
*
**********************************************************************/

package eo.web.webview.commonOneStop;

import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.fujitsu.futurity.web.x31.X31BWebBusinessLogic;
import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccessArray;

import eo.common.util.JCCFrameworkException;
import eo.web.webview.CommonInfoCF.CommonInfoCFConst;
import eo.web.webview.commonOneStop.JCCAuthorityCtrlCache;

public class JCCAuthorityCtrlUtil
{

	/** ブランク文字　*/
	public static final String STR_BLANK = "";

	/**
	 * 入力パラメータチェックをする処理です。
	 * <br>
	 * @param vl VIEWロジックインスタンス
	 * @exception JCCFrameworkExceptionがスローされます。
	 */
	public static void checkInputpParameter(X31BWebBusinessLogic vl) throws JCCFrameworkException
	{
	    if (null == vl)
	    {
			throw new JCCFrameworkException("VIEWロジックインスタンスが設定されていません。");
		}
	}

	/**
	 * 権限コードリスト形式チェック処理.<br>
	 * 権限制御定義ファイルに設定された権限コードリストの形式をチェックします。<br>
	 * 形式が正しければメッセージIDリスト/権限コードリストをリストに設定して返却します。<br>
	 * <br>
	 * @param authorityCdLst 権限コードリスト
	 * @exception 	JCCFrameworkExceptionがスローされます。
	 */
	public static void checkAuthorityCdList(String authorityCdLst) throws JCCFrameworkException
	{
		StringTokenizer stTokenList = null;			// メッセージID;権限コードリスト分割用
		String messageIdList = null;				// メッセージIDリスト
		String authorityCdList = null;				// 権限コードリスト
		try
		{
			authorityCdList = authorityCdLst;
			stTokenList = new StringTokenizer(authorityCdList, JCCAuthorityCtrlCache.KEY_DELIMITERS);
			// メッセージIDリスト;権限コードリスト形式でなければシステムエラー
			if (stTokenList.countTokens() != JCCAuthorityCtrlCache.VAL_LIST_COUNT)
			{
				throw new JCCFrameworkException("権限制御定義ファイルの内容が不正です。(" + authorityCdList + ")");
			}
			messageIdList  = stTokenList.nextToken();
			// メッセージIDリストチェック
			if (null == messageIdList || JCCAuthorityCtrlUtil.STR_BLANK.equals(messageIdList.trim()))
			{
				throw new JCCFrameworkException("権限制御定義ファイルの内容が不正です。(" + authorityCdList + ")");
			}
			// 権限コードリストチェック
			authorityCdList = stTokenList.nextToken();
			if (null == authorityCdList || JCCAuthorityCtrlUtil.STR_BLANK.equals(authorityCdList.trim()))
			{
				throw new JCCFrameworkException("権限制御定義ファイルの内容が不正です。(" + authorityCdList + ")");
			}
		}
		finally
		{
			stTokenList = null;
		}	
	}

	/**
	 * 権限リスト取得処理<BR>
	 * 共有フォームBeanより権限リストを取得します。
	 * <br>
	 * @param vl VIEWロジックインスタンス
	 * @param 権限リストマップ
	 * @exception 	JCCFrameworkExceptionがスローされます。
	 */
	public static void setAuthorityList(X31BWebBusinessLogic vl, HashMap<String, String> lstMap) throws JCCFrameworkException
	{
		
		X31SDataBeanAccess commonbean = null;
		X31SDataBeanAccessArray commonBeanArray = null;
		X31SDataBeanAccess commonSubbean = null;
		try
		{
			Integer count = 0;
			Integer subCount = 0;
			String key = null;
			// ------------------------------
			// 権限リスト取得
			// ------------------------------
			// 共有フォームBeanを取得する。
			commonbean = vl.getCommonInfoBean();
			// 共有フォームBeanからKGGRP_CD_LISTデータタイプBeanを取得する。
			commonBeanArray = commonbean.getDataBeanArray(CommonInfoCFConst.KGGRP_CD_LIST);
			// データ要素数を取得する。
			count = commonBeanArray.getCount();
			
			// 2012/10/10 null判定はループの外に出す start
			if (null == lstMap)
			{
				lstMap = new HashMap<String, String>();
			}
			// 2012/10/10 null判定はループの外に出す end
			
			// 2012/10/10 共有フォームBeanの権限コードはセッション領域にキャッシュする start
			FacesContext ctx = FacesContext.getCurrentInstance();
			HttpSession ses = (HttpSession)ctx.getExternalContext().getSession(false);
			HashMap<String, String> tmpMap = new HashMap<String, String>();
			tmpMap = (HashMap<String, String>)ses.getAttribute("COMMON_INFO_AUTHORITY_CD_CACHE");
			if(null == tmpMap || tmpMap.size() == 0)
			{			
				for (int ii = 0; ii < count; ii++)
				{
					commonSubbean = commonBeanArray.getDataBean(ii);
					subCount = (Integer)commonSubbean.sendMessage(CommonInfoCFConst.AUTHORITY_CD_01, X31CWebConst.DATABEAN_GET_COUNT);
					for (int jj = 0; jj < subCount; jj++)
					{
						key = commonSubbean.sendMessageString(CommonInfoCFConst.AUTHORITY_CD_01, X31CWebConst.DATABEAN_GET_VALUE, jj);
						// 2012/10/10 null判定はループの外に出す start
						//if (null == lstMap)
						//{
						//	lstMap = new HashMap<String, String>();
						//}
						// 2012/10/10 null判定はループの外に出す end
						if (!lstMap.containsKey(key))
						{
							lstMap.put(key, STR_BLANK);
						}
					}
				}
				ses.setAttribute("COMMON_INFO_AUTHORITY_CD_CACHE", lstMap);
			}
			else
			{
				Iterator ite = tmpMap.keySet().iterator();
				while(ite.hasNext())
				{
					key = (String)ite.next();
					lstMap.put(key, STR_BLANK);
				}
				return;
			}
			// 2012/10/10 共有フォームBeanの権限コードはセッション領域にキャッシュする end
		}
		finally
		{
			commonbean = null;
			commonBeanArray = null;
			commonSubbean = null;
		}	
	}
	
	// 2013/03/21 FST_mukuo）権限リストのクリア処理を追加 start
	/**
	 * 権限リストクリア処理<BR>
	 * <br>
	 * @param なし
	 * @return なし
	 */
	public static void clearAuthorityList()
	{
		FacesContext ctx = FacesContext.getCurrentInstance();
		HttpSession ses = (HttpSession)ctx.getExternalContext().getSession(false);
		
		HashMap sesMap = (HashMap)ses.getAttribute("COMMON_INFO_AUTHORITY_CD_CACHE");
		if(null != sesMap)
		{
			sesMap.clear();
		}
		ses.setAttribute("COMMON_INFO_AUTHORITY_CD_CACHE", null);
	}
	// 2013/03/21 FST_mukuo）権限リストのクリア処理を追加 end
}
