/*********************************************************************
*	All Rights reserved,Copyright K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JKKGetDataUtil
*	ソースファイル名：JKKGetDataUtil.java
*	作成者			：富士通
*	日付			：2011年05月11日
*＜機能概要＞
*	契約管理システムWeb共通部品
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/04/05	富士通		新規作成
	v41.00.00	2019/04/16	FJ) 黒田   【ANK-3640-00-00】元号変更対応
*
**********************************************************************/

package eo.web.webview.common;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import com.fujitsu.futurity.model.base.jcc.util.JCCCalendarException;
import com.fujitsu.futurity.model.base.jcc.util.JCCJapaneseCalendar;
import com.fujitsu.futurity.model.base.jcc.util.JCCWestCalendar;
import com.fujitsu.futurity.web.x31.X31BWebBusinessLogic;
import com.fujitsu.futurity.web.x31.X31CWebConst;
import com.fujitsu.futurity.web.x31.X31SDataBeanAccess;
import com.sun.enterprise.tools.common.util.StringUtils;

import eo.common.constant.JKKStrConst;

/**
 * 契約管理システムWebデータ取得部品<p>
 * <BR>
 * @author 富士通
 */
public class JKKGetDataUtil
{
	/**
	 * プルダウン項目の選択値に対応するコード値を返却します。
	 * <br />
	 * (未選択の時はnullを返却します。)
	 * @param bean フォームDataBean
	 * @param pullDownId プルダウン項目ID
	 * @param indexId インデックス項目ID
	 * @param codeListId コードリストID
	 * @return 変換後コード(未選択時はnull)
	 * @throws Exception Exception
	 */
	public static String getPullDownCode(X31SDataBeanAccess bean, String pullDownId, String indexId, String codeListId) {
		String code = null;
		X31SDataBeanAccess comboBean = bean.getDataBeanArray(pullDownId).getDataBean(0);
		// ANK-4468-01-00 MOD START
//		String indexStr = comboBean.sendMessageString(indexId, X31CWebConst.DATABEAN_GET_VALUE);
		String indexStr = null;
		if (comboBean != null) {
			indexStr = comboBean.sendMessageString(indexId, X31CWebConst.DATABEAN_GET_VALUE);
		}
		// ANK-4468-01-00 MOD END
		if ((indexStr != null)&&(!"".equals(indexStr))) {
			try
			{
				// ANK-4468-01-00 ADD START
				int itemCount = ((Integer)(comboBean.sendMessage(codeListId, X31CWebConst.DATABEAN_GET_COUNT))).intValue();
				if (itemCount == 0) {
					return code;
				}
				// ANK-4468-01-00 ADD END
				int index = Integer.parseInt(comboBean.sendMessageString(indexId, X31CWebConst.DATABEAN_GET_VALUE), 10);
				code = comboBean.sendMessageString(codeListId, X31CWebConst.DATABEAN_GET_VALUE, index);
			}
			catch (NumberFormatException e)
			{
			}
		}
		return code;
	}

	/**
	 * プルダウン項目、コード取得処理です。
	 * 
	 * @param bean フォームDataBean
	 * @param pullDownId プルダウン項目ID
	 * @param indexId インデックス項目ID
	 * @param codeListId コードリストID
	 * @return 変換後コード
	 * @throws Exception Exception
	 */
	public static String convertCode(X31SDataBeanAccess bean, String pullDownId, String indexId, String codeListId) {
		int index = 0;
		X31SDataBeanAccess comboBean = bean.getDataBeanArray(pullDownId).getDataBean(0);
		try {
			index = Integer.parseInt(comboBean.sendMessageString(indexId, X31CWebConst.DATABEAN_GET_VALUE), 10);
		} catch (NumberFormatException e) {
		}
		return comboBean.sendMessageString(codeListId, X31CWebConst.DATABEAN_GET_VALUE, index);
	}

	/**
	 * プルダウン項目・インデックス取得処理です。
	 * 
	 * @param bean フォームDataBean
	 * @param pullDownId プルダウン項目ID
	 * @param indexId インデックス項目ID
	 * @param codeListId コードリストID
	 * @return 変換後コード
	 * @throws Exception Exception
	 */
	public static String convertIndex(X31SDataBeanAccess bean, String code, String pullDownId, String indexId, String codeListId) {
		code = (code != null) ? code : "";
		X31SDataBeanAccess comboBean = bean.getDataBeanArray(pullDownId).getDataBean(0);
		int count = ((Integer)(comboBean.sendMessage(codeListId, X31CWebConst.DATABEAN_GET_COUNT))).intValue();
		String index = "";
		for (int i = 0; i < count; i++) {
			if (code.equals(comboBean.sendMessageString(codeListId, X31CWebConst.DATABEAN_GET_VALUE, i))) {
				index = String.valueOf(i);
				break;
			}
		}
		return index;
	}

	/**
	 * プルダウン項目、コード取得処理です。
	 * 
	 * @param bean フォームDataBean
	 * @param pullDownId プルダウン項目ID
	 * @param indexId インデックス項目ID
	 * @param codeListId コードリストID
	 * @return 変換後コード
	 * @throws Exception Exception
	 */
	public static String getColumnValue(HashMap  outputMap, String mapKey, String listKey, String itemKey) {

		if (outputMap == null) {
			return null;
		}
		
		if(!outputMap.containsKey(mapKey)){
			return null;
		}
		HashMap parentMap = (HashMap)outputMap.get(mapKey);

		if(!parentMap.containsKey(listKey)){
			return null;
		}

		ArrayList childList = (ArrayList)parentMap.get(listKey);

		if (childList == null || childList.size() == 0) {
			return null;
		}

		HashMap childMap = (HashMap)childList.get(0);

		if(!childMap.containsKey(itemKey)){
			return null;
		}
		
		return (String) childMap.get(itemKey);
		
	}
	
	/**
	 * プルダウン項目・インデックス取得処理です。
	 * プルダウン項目から、指定コードが設定された場合のインデックスを取得します。
	 * 
	 * @param bean フォームDataBean
	 * @param selectedCode 選択コード
	 * @param pullDownId プルダウン項目ID
	 * @param codeListId コードリストID
	 * @return 選択インデックス
	 */
	public static String getPullDownIndex(X31SDataBeanAccess bean, String selectedCode, String pullDownId, String codeListId)
	{

		// フォームDataBeanがnullの場合
		if (bean == null)
		{
			return null;
		}

		// 選択コードがnullの場合
		if (isNull(selectedCode))
		{
			return null;
		}

		// プルダウン項目IDがnullの場合
		if (isNull(pullDownId))
		{
			return null;
		}

		// コードリストIDがnullの場合
		if (isNull(codeListId))
		{
			return null;
		}

		// プルダウンデータBeanの取得
		X31SDataBeanAccess comboBean = bean.getDataBeanArray(pullDownId).getDataBean(0);
		
		// プルダウンデータBeanがnullの場合
		if (comboBean == null)
		{
			return null;
		}

		// プルダウンの項目数を取得
		int itemCount = ((Integer)(comboBean.sendMessage(codeListId, X31CWebConst.DATABEAN_GET_COUNT))).intValue();

		int selectedIndex = 0;
		// プルダウン項目より、選択されたインデックスを取得
		for (int i = 0; i < itemCount; i++)
		{
			
			// 選択されたインデックスの場合、ループを終了し、その際のループカウンタをインデックスとする
			if (selectedCode.equals(comboBean.sendMessageString(codeListId, X31CWebConst.DATABEAN_GET_VALUE, i)))
			{
				selectedIndex = i;
				break;
			}
		}
		return String.valueOf(selectedIndex);
	}

	/**
	 * プルダウン項目・名称取得処理です。
	 * プルダウン項目から、選択された名称を取得します。
	 * 
	 * @param bean フォームDataBean
	 * @param pullDownId プルダウン項目ID
	 * @param indexId インデックス項目ID
	 * @param codeListId コードリストID
	 * @param nameListId 名称リストID
	 * @param dataBeanIndex データBean要素番号
	 * @return 選択名称
	 */
	public static String getPullDownName(X31SDataBeanAccess bean, String pullDownId, String indexId, String codeListId, String nameListId, int dataBeanIndex)
	{

		// フォームDataBeanがnullの場合
		if (bean == null)
		{
			return null;
		}

		// プルダウン項目IDがnullの場合
		if (isNull(pullDownId))
		{
			return null;
		}

		// インデックス項目IDがnullの場合
		if (isNull(indexId))
		{
			return null;
		}

		// コードリストIDがnullの場合
		if (isNull(codeListId))
		{
			return null;
		}

		// 名称リストIDがnullの場合
		if (isNull(nameListId))
		{
			return null;
		}

		// プルダウンデータBeanの取得
		X31SDataBeanAccess comboBean = bean.getDataBeanArray(pullDownId).getDataBean(dataBeanIndex);
		
		// プルダウンデータBeanがnullの場合
		if (comboBean == null)
		{
			return null;
		}

		// 選択されたインデックスを取得する。
		if (isNull(comboBean.sendMessageString(indexId, X31CWebConst.DATABEAN_GET_VALUE)))
		{
			return null;
		}
		int selectedIndex = Integer.parseInt(comboBean.sendMessageString(indexId, X31CWebConst.DATABEAN_GET_VALUE), 10);

		return comboBean.sendMessageString(nameListId, X31CWebConst.DATABEAN_GET_VALUE, selectedIndex);

	}

	/**
	 * 引数の文字列をそのまま返却します。
	 * <br />
	 * (マッピングツールで利用します。)
	 * @param param 任意の文字列
	 * @return 文字列
	 */
	public static String getConstantValue(String param){
		return param;
	}
	
	/**
	 * 1レコード目項目取得処理
	 * 1レコード目の項目を取得します。
	 * 
	 * @param mapParam 対象マップ
	 * @param mapKey マップキー
	 * @param listKey リストキー
	 * @param itemKey 項目キー
	 * @return 取得文字列
	 */
	@SuppressWarnings("unchecked")
	public static String getColumnValue(HashMap mapParam, String listKey, String itemKey)
	{

		// リストが存在しない場合
		if(!mapParam.containsKey(listKey))
		{
			return null;
		}

		// リストの取得
		ArrayList childList = (ArrayList)mapParam.get(listKey);

		// リストが存在しない場合
		if (childList == null || childList.size() == 0)
		{
			return null;
		}

		// １レコード目の取得
		HashMap childMap = (HashMap)childList.get(0);

		// 項目が存在しない場合
		if(!childMap.containsKey(itemKey))
		{
			return null;
		}
		
		return (String)childMap.get(itemKey);
		
	}

	/**
	 * 漢字年号付和暦文字列取得処理です。
	 * 設定されている標準文字列に、漢字年号と区切り文字をつけた文字列を返します。
	 * 
	 * @param stdstr 西暦年月日の標準文字列（"YYYYMMDD"）
	 * @return 和暦年月日
	 */
	public static String getJapaneseCalendar(String stdstr) throws JCCCalendarException
	{
		// 西暦クラスのインスタンス生成
		JCCWestCalendar jWestCal = new JCCWestCalendar(stdstr);

		// 和暦年月日の標準文字列を取得
		String jWestStdDate = jWestCal.toJapaneseCalendar();

		// 和暦クラスのインスタンス生成
		JCCJapaneseCalendar jJapaneseCal = new JCCJapaneseCalendar(jWestStdDate);

		// 漢字年号付和暦文字列取得
		String jWestDate = jJapaneseCal.toCcString("", JKKStrConst.DATE_JAPANESE_YEAR, JKKStrConst.DATE_JAPANESE_MONTH, JKKStrConst.DATE_JAPANESE_DAY);

		return jWestDate;
	}
	
	/**
	 * 漢字年号付和暦文字変換処理です。
	 * 設定された漢字年号と年月日の値を西暦年月日の標準文字列にして返します。
	 * 
	 * @param wareki 和暦(元号コード)
	 * @param year 年
	 * @param month 月
	 * @param day 日
	 * @return 西暦年月日（例：20111231）
	 */
	public static String getAdCalendar(String wareki, String year, String month, String day) throws JCCCalendarException
	{

		// 引数を和暦クラスのインスタンスに合った形で変数設定
		char symbol = '0';
		int iYear = Integer.parseInt(year);
		int iMonth = Integer.parseInt(month);
		int iDay = Integer.parseInt(day);
		
		// 変数：和暦の値を和暦クラスのシンボルに合った形で設定(1:昭和、2:平成、3:大正、4:明治)
		if("1".equals(wareki)) {
			symbol = 'S';
		} else if ("2".equals(wareki)) {
			symbol = 'H';
		} else if ("3".equals(wareki)) {
			symbol = 'T';
		} else if ("4".equals(wareki)) {
			symbol = 'M';
// ANK-3640-00-00 ADD START
		} else if ("5".equals(wareki)) {
			symbol = 'R';
// ANK-3640-00-00 ADD END
		}
		
		// 和暦クラスのインスタンス生成		
		JCCJapaneseCalendar jJapaneseCal = new JCCJapaneseCalendar(symbol, iYear, iMonth, iDay);

		// 西暦変換処理
		String jJapaneseDate = jJapaneseCal.toWestCalendar();

		return jJapaneseDate;
	}
	
	/**
	 * オブジェクトに値が設定されているか判定します。
	 * <br>
	 * @param arg0 判定するオブジェクト
	 * @return オブジェクトに値が設定されていない場合はtrue
	 */
	public static boolean isNull(Object arg0) 
	{
		if (arg0 == null) 
		{
			return true;
		}

		return 0 == arg0.toString().length();
	}
	
	
	
	
	/**
	 * 日付文字列からカレンダーを取得します。
	 * @param yyyymmdd 日付文字列(yyyyMMdd形式)
	 * @return カレンダーオブジェクト
	 */
	public static Calendar getCalendarFromString(String yyyymmdd)
	{
		int intY = -1;	// あり得ない年として-1を設定
		int intM = -1;	// あり得ない月として-1を設定
		int intD = -1;	// あり得ない日として-1を設定
		
		if (yyyymmdd == null || yyyymmdd.length() != 8) {
			Calendar errCal = Calendar.getInstance();
			errCal.set(intY, intM, intD);
			return errCal;
		}
		
		
		String stringY = yyyymmdd.substring(0, 4);
		String stringM = yyyymmdd.substring(4, 6);
		String stringD = yyyymmdd.substring(6, 8);

		if (stringY != null && !"".equals(stringY)) {
			intY = Integer.parseInt(stringY);
		}
		if (stringM != null && !"".equals(stringM)) {
			intM = Integer.parseInt(stringM);
		}
		if (stringD != null && !"".equals(stringD)) {
			intD = Integer.parseInt(stringD);
		}
		
		Calendar cal = Calendar.getInstance();
		cal.set(intY, intM - 1, intD);
		
		return cal;
	}

	
	/**
	 * 日付の整合性チェックを行います。
	 * @param yyyymmdd チェック対象となる日付(yyyyMMdd形式)
	 * @return 存在する日付の場合はtrue、それ以外はfalseを返します。
	 */
	public static boolean isValidDate(String yyyymmdd)
	{
		try {
			Calendar cal = getCalendarFromString(yyyymmdd);
			cal.setLenient(false);
			cal.getTime();
		} catch (IllegalArgumentException e) {
			return false;
		}

		return true;
	}
	
	
	/**
	 * 過去日付範囲チェックを行います。
	 * @param yyyymmdd チェック対象となる日付(yyyyMMdd形式)
	 * @param range 基準範囲(日数)
	 * @param viewLogic ビューロジックオブジェクト
	 * @return 運用日から基準範囲を超えて過去日付の場合はfalse、それ以外はtrueを返します。
	 */
	public static boolean isValidDateRange(String yyyymmdd, String range, X31BWebBusinessLogic viewLogic)
	{
		// サービスフォームBeanのデータBeanアクセスクラスを取得。
		int checkD = Integer.parseInt(range);
		
		Calendar lmtCal = getCalendarFromString(JCCWebCommon.getOpeDate(viewLogic, null));
		lmtCal.add(Calendar.DATE, checkD * -1);
		Date lmtDate = lmtCal.getTime();
		
		// チェック対象となる日付
		Date targetDate = getCalendarFromString(yyyymmdd).getTime();
		if (targetDate.before(lmtDate)) {
			return false;
		}
		return true;
	}
	
	/**
	 * 未来日付範囲チェックを行います。
	 * @param yyyymmdd チェック対象となる日付(yyyyMMdd形式)
	 * @param range 基準範囲(日数)
	 * @param viewLogic ビューロジックオブジェクト
	 * @return 運用日から基準範囲を超えて過去日付の場合はfalse、それ以外はtrueを返します。
	 */
	public static boolean isValidDateFutureRange(String yyyymmdd, String range, X31BWebBusinessLogic viewLogic)
	{
		// サービスフォームBeanのデータBeanアクセスクラスを取得。
		int checkD = Integer.parseInt(range);
		
		Calendar lmtCal = getCalendarFromString(JCCWebCommon.getOpeDate(viewLogic, null));
		lmtCal.add(Calendar.DATE, checkD);
		Date lmtDate = lmtCal.getTime();
		
		// チェック対象となる日付
		Date targetDate = getCalendarFromString(yyyymmdd).getTime();
		if (targetDate.after(lmtDate)) {
			return false;
		}
		return true;
	}

}
