/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名		：eo顧客基幹システム
*	モジュール名	：JKUBatFtpUtil
*	ソースファイル名：JKUBatFtpUtil.java
*	作成者			：富士通
*	日付			：2011年07月13日
*＜機能概要＞
*	FTP処理を行う共通機能を提供する。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/07/13	富士通		新規作成
*
**********************************************************************/
package eo.business.common;

import java.util.ArrayList;
import java.util.HashMap;

import eo.common.constant.JKUStrConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.common.util.JPCCommonUtil;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;

/**
 * 文字列編集を行う共通処理を行うクラスです。<p>
 * <br>
 * @author 富士通
 */
public class JKUBatFtpUtil extends JPCCommonUtil 
{

	/**
	 * FTPクライアントのPUT処理です。<br>
	 * FTPクライアントのPUTを行います。<br>
	 * 
	 * @param commonItem 業務共通電文
	 * @throws Exception
	 */
	public static void putFTP(JBSbatCommonItem commonItem) throws Exception
	{
		// インターフェイスＩＤ
		String ifFileId = "";

		// 転送元ファイル配置先
		String[] freeValue = commonItem.getFreeItem().split("@");

		// FTP転送対象のファイルリスト(ローカルのフルパス)設定
		ArrayList<String> fileList = new ArrayList<String>();
		for (int i = 0; i < freeValue.length; i++)
		{
			// １件目は必ずインターフェイスＩＤ e.g."KUIFE001"
			if (i == 0)
			{
				ifFileId = freeValue[i];
			}
			else
			{
				fileList.add(freeValue[i]);
			}
		}
		
		// FTP転送処理
		putFTP(commonItem, fileList, ifFileId, JKUBatCommon.ISI);

	}

	/**
	 * FTPクライアントのPUT本処理です。<br>
	 * JCCBatCommonのputFTP処理を呼出ます。<br>
	 * 
	 * @param arg0 業務共通電文
	 * @param arg1 FTP転送対象のファイルリスト(ローカルのフルパス)
	 * @param arg2 FTP転送依頼するインターフェイスID
	 * @param arg3 ESB種別。1:ISI(定数JKUBatCommon.ISI) 2:III(定数JKUBatCommon.III)
	 * @throws Exception
	 */
	private static void putFTP(JBSbatCommonItem arg0, ArrayList<String> arg1,
			String arg2, int arg3) throws Exception
	{
		// 処理結果詳細を格納したHashMap
		HashMap<String, Object> resultDtl = new HashMap<String, Object>();
		
		if(!JCCBatCommon.putFTP(arg0, arg1, arg2, arg3, resultDtl))
		{
			ArrayList<HashMap<String, Object>> errDtlAry = new ArrayList<HashMap<String, Object>>();
			HashMap<String, Object> errDtlMap = new HashMap<String, Object>();
			if(Integer.parseInt(resultDtl.get("ERR_KBN").toString()) == JKUStrConst.FTP_SEND_CONNECT_ERR)
			{
				errDtlMap = (HashMap<String, Object>)resultDtl.get("CONNECT_ERR");
			}
			else if(Integer.parseInt(resultDtl.get("ERR_KBN").toString()) == JKUStrConst.FTP_SEND_FILE_ERR)
			{
				errDtlAry = (ArrayList)resultDtl.get("FILE_ERR");
				errDtlMap = errDtlAry.get(0);
			}
			
			// FTP転送失敗した場合
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EKUB0230CH, new String[]{
												errDtlMap.get("ERR_CD").toString(), 
												errDtlMap.get("ERR_MESSAGE").toString()});
		}
	}
}
