/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：プロジェクト共通
*   モジュール名    ：JKKBatFTPUtil
*   ソースファイル名：JKKBatFTPUtil.java
*   作成者          ：富士通
*   日付            ：2011年03月24日
*＜機能概要＞
*   バッチ部品のアクセッサークラスです。
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v1.00.00    2011/03/24   FJ) 富士通  新規作成
*
**********************************************************************/
package eo.business.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.log.JBSbatLogPrintControl;

/**
 * ファイル送信を行う共通クラスです。<p>
 * <br>
 * @author 富士通
 */
public class JKKBatFTPUtil
{

	/**
	 * <dd>メソッド名	：SPRiファイル連携処理
	 * <dd>メソッド説明	：SPRiに対してファイル連携を行う。
	 * @param commonItem 業務共通電文
	 * @param ifId       FTP転送依頼するインターフェイスID
	 * @param esbSbt     ESB種別。1:ISI(定数JCCModelCommon.ISI) 2:III(定数JCCModelCommon.III)
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	public static void putSPRi(JBSbatCommonItem commonItem, String ifId, int esbSbt) throws Exception
	{
		// 処理結果詳細を格納するHashMap。
		HashMap<String, Object> resultMap = new HashMap<String, Object>();

		// ログ出力制御オブジェクトの取得
		JBSbatLogPrintControl logPrint = commonItem.getLogPrint();

		// ファイルのフルパスの取得
		String freeItem = commonItem.getFreeItem();

		// ファイル名が不正な場合
		if(freeItem == null || "".equals(freeItem.trim()))
		{
			// ファイル名が不正です。
// 2013/01/30 異常メッセージの修正対応 suzuki mod start
//			throw new JBSbatBusinessException(JKKBatConst.FILE_ERR_EKKB0040CE);
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0130CE, new String[] {"ファイル名が不正（freeItemが空）"});
// 2013/01/30 異常メッセージの修正対応 suzuki mod end
		}

		String[] fileNames = freeItem.split(";");
		if(fileNames.length <= 0)
		{
			// ファイル名が不正です。
// 2013/01/30 異常メッセージの修正対応 suzuki mod start
//			throw new JBSbatBusinessException(JKKBatConst.FILE_ERR_EKKB0040CE);
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0130CE, new String[] {"ファイル名が不正（freeItemが空）"});
// 2013/01/30 異常メッセージの修正対応 suzuki mod end
		}

		// ファイルパスの格納
		ArrayList<String> fileList = new ArrayList<String>();
		for (int i = 0; i < fileNames.length; i++)
		{
			fileList.add(fileNames[i]);
		}

		logPrint.printDebugLog("FTP送信処理開始");

		// FTPクライアントのPUT処理の呼出し
		if(JCCBatCommon.putFTP(commonItem, fileList, ifId, esbSbt, resultMap))
		{
			logPrint.printDebugLog("FTP送信処理終了");
			return;
		}

		// エラー区分の取得
		String errKbn = resultMap.get(JKKBatConst.FTP_ERR_KBN).toString();

		// 正常終了の場合
		if(JKKBatConst.FTP_ERR_KBN_NOMAL.equals(errKbn))
		{
			logPrint.printDebugLog("FTP送信処理終了");
			return;
		}
		// 接続エラーの場合
		else if(JKKBatConst.FTP_ERR_KBN_CONNECT_ERR.equals(errKbn))
		{
			HashMap<?, ?> connectErrMap = (HashMap<?, ?>)resultMap.get(JKKBatConst.FTP_CONNECT_ERR);
			if(!Boolean.valueOf(connectErrMap.get(JKKBatConst.FTP_CONNECT_ERR_FLG).toString()))
			{
				throw new JBSbatBusinessException(connectErrMap.get(JKKBatConst.FTP_CONNECT_ERR_MESSAGE).toString());
			}
		}
		// ファイルエラーの場合
		else if(JKKBatConst.FTP_ERR_KBN_FILE_ERR.equals(errKbn))
		{
			ArrayList<?> fileErrList = (ArrayList<?>)resultMap.get(JKKBatConst.FTP_FILE_ERR);
			for(int i = 0; i < fileErrList.size(); i++)
			{
				HashMap<?, ?> fileErrMap = (HashMap<?, ?>)fileErrList.get(i);
				if(!Boolean.valueOf(fileErrMap.get(JKKBatConst.FTP_FILE_ERR_FLG).toString()))
				{
					throw new JBSbatBusinessException(fileErrMap.get(JKKBatConst.FTP_FILE_ERR_MESSAGE).toString());
				}
			}
		}
	}

	/**
	 * <dd>メソッド名	：ファイル連携処理
	 * <dd>メソッド説明	：SPRi以外のファイルを連携する。
	 * @param commonItem 業務共通電文
	 * @param ifId       FTP転送依頼するインターフェイスID
	 * @param esbSbt     ESB種別。1:ISI(定数JCCModelCommon.ISI) 2:III(定数JCCModelCommon.III)
	 * @param fileNameList 連携するファイル名を設定（フルパス指定）
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	public static void putFile(JBSbatCommonItem commonItem, String ifId, int esbSbt, ArrayList<String> fileNameList) throws Exception
	{
		// 処理結果詳細を格納するHashMap。
		HashMap<String, Object> resultMap = new HashMap<String, Object>();

		// ログ出力制御オブジェクトの取得
		JBSbatLogPrintControl logPrint = commonItem.getLogPrint();
		
		// 送信ファイルのリストが取得できない場合は終了
		if(fileNameList.isEmpty())
		{
			return;
		}
		
		logPrint.printDebugLog("FTP送信処理開始");

		// FTPクライアントのPUT処理の呼出し
		if(JCCBatCommon.putFTP(commonItem, fileNameList, ifId, esbSbt, resultMap))
		{
			logPrint.printDebugLog("FTP送信処理終了");
			return;
		}

		// エラー区分の取得
		String errKbn = resultMap.get(JKKBatConst.FTP_ERR_KBN).toString();

		// 正常終了の場合
		if(JKKBatConst.FTP_ERR_KBN_NOMAL.equals(errKbn))
		{
			logPrint.printDebugLog("FTP送信処理終了");
			return;
		}
		// 接続エラーの場合
		else if(JKKBatConst.FTP_ERR_KBN_CONNECT_ERR.equals(errKbn))
		{
			HashMap<?, ?> connectErrMap = (HashMap<?, ?>)resultMap.get(JKKBatConst.FTP_CONNECT_ERR);
			if(!Boolean.valueOf(connectErrMap.get(JKKBatConst.FTP_CONNECT_ERR_FLG).toString()))
			{
				throw new JBSbatBusinessException(connectErrMap.get(JKKBatConst.FTP_CONNECT_ERR_MESSAGE).toString());
			}
		}
		// ファイルエラーの場合
		else if(JKKBatConst.FTP_ERR_KBN_FILE_ERR.equals(errKbn))
		{
			ArrayList<?> fileErrList = (ArrayList<?>)resultMap.get(JKKBatConst.FTP_FILE_ERR);
			for(int i = 0; i < fileErrList.size(); i++)
			{
				HashMap<?, ?> fileErrMap = (HashMap<?, ?>)fileErrList.get(i);
				if(!Boolean.valueOf(fileErrMap.get(JKKBatConst.FTP_FILE_ERR_FLG).toString()))
				{
					throw new JBSbatBusinessException(fileErrMap.get(JKKBatConst.FTP_FILE_ERR_MESSAGE).toString());
				}
			}
		}
	}

	/**
	 * <dd>メソッド名	：FTP送信結果文字列生成処理
	 * <dd>メソッド説明	：FTP部品により取得した処理結果マップをログ出力用に文字列生成する。
	 * @param resultMap FTP部品により取得した処理結果マップ
	 * @return 処理結果マップをログ出力用に生成した文字列
	 */
	public static final String getFTPResultMessageString(HashMap<String, Object> resultMap)
	{
		StringBuffer buf = new StringBuffer();
		if (resultMap == null)
		{
			return "";
		}
		Iterator<String> it = resultMap.keySet().iterator();
		while (it.hasNext())
		{
			String key = it.next();
			Object ob = resultMap.get(key);
			if (ob != null)
			{
				if (ob instanceof Integer)
				{
					Integer errKbn = (Integer)resultMap.get(key);
					buf.append(String.format("%s = %s;", key, errKbn.toString()));
				}
				else if (ob instanceof HashMap)
				{
					HashMap<?, ?> innerMap = (HashMap<?, ?>)resultMap.get(key);
					Iterator<?> it2 = innerMap.keySet().iterator();
					while (it2.hasNext())
					{
						String key2 = (String)it2.next();
						buf.append(String.format("%s = %s;", key2, innerMap.get(key2)));
					}
					
				}
				else if (ob instanceof ArrayList<?>)
				{
					ArrayList<?> arr = (ArrayList<?>)resultMap.get(key);
					for (int i = 0; i < arr.size(); i++)
					{
						HashMap<?, ?> innerMap = (HashMap<?, ?>)arr.get(i);
						Iterator<?> it3 = (innerMap.keySet()).iterator();
						while (it3.hasNext())
						{
							String key3 = (String)it3.next();
							buf.append(String.format("%s = %s;", key3, innerMap.get(key3)));
						}
					}
				}
			}
		}
		return buf.toString();
	}
}
