/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKFtpPut
*	ソースファイル名	：JBSbatKKFtpPut.java
*	作成者				：富士通　
*	作成日				：2014年02月25日
*＜機能概要＞
*　FTP送信部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v8.00.00	2014/02/25   富士通		新規作成
*********************************************************************/
package eo.business.service;

import eo.business.common.JKKBatConst;
import eo.business.common.JBSbatBusinessService;
import eo.business.common.JKKBatCommon;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.application.JBSbatBusinessException;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKFtpPut extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** FTP転送対象ファイルリスト(ローカルのフルパス)（ファイル名 or ディレクトリ名） */
	private ArrayList<String> fileNameList = null;
	
	/** 退避.インターフェースID */
	private String interfaceId = null;

	/** 退避.ESB種別 1:ISI(定数JCCModelCommon.ISI) 2:III(定数JCCModelCommon.III) */
	private int esbSbt = 0;
	
	/** ファイルリスト指定開始位置 */
	private static final int COUNT = 2;
	
	/** 退避.ファイルリスト */
	private File targetDir = null;
	
	/** 退避.ディレクトリの中にあるファイルリスト */
	private String directoryFileList = null;
	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		super.logPrint.printDebugLog("初期処理開始");
		super.logPrint.printDebugLog("[opeDate = " + opeDate + "]");
		super.logPrint.printDebugLog("[freeItem = " + freeItem + "]");

		// フリー項目取得
		String[] mfreeItem = super.freeItem.split(";");

		// FTP転送依頼するインターフェイスID
		this.interfaceId = mfreeItem[0];
		super.logPrint.printDebugLog("[インターフェイスID = " + interfaceId + "]");

		// FTP転送先のESB種別
		this.esbSbt = Integer.parseInt(mfreeItem[1]);
		super.logPrint.printDebugLog("[ESB種別 = " + esbSbt + "]");

		// FTP転送対象ファイルリスト(ローカルのフルパス)
		fileNameList = new ArrayList<String>();

		int k = COUNT;
		
		while(k < mfreeItem.length)
		{
			super.logPrint.printDebugLog("[指定ファイル" + (k - 1) + " = " + mfreeItem[k] + "]");

			// targetDirのインスタンスを作成する
			targetDir = new File(mfreeItem[k]);
			
			// targetDirが「ディレクトリ」のとき
			if (targetDir.isDirectory())
			{
				super.logPrint.printDebugLog("[指定ファイル" + (k - 1) + " = ディレクトリ]");
				fileNameGet(targetDir);
			}
			
			// targetDirが「ファイル」のとき
			else if (targetDir.isFile())
			{
				super.logPrint.printDebugLog("[指定ファイル" + (k - 1) + " = ファイル]");

				// fileNameListにファイル名を格納する
				fileNameList.add(mfreeItem[k]);
			}
			
			else
			{
				super.logPrint.printDebugLog("[指定ファイルが存在しない]");

				// 業務例外を発生「ファイルが存在しません。ファイルパス(%1%)」
				throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0020CE, new String[] {mfreeItem[k]});
			}
			k++;
		}
		super.logPrint.printDebugLog("初期処理終了");
		
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}
	
	/**
	 * ディレクトリの中にあるファイルを取得する
	 * @param File targetDirectory 送信対象のファイルが格納されているディレクトリのフルパス
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void fileNameGet(File targetDirectory) throws Exception
	{
		// fileListに指定したディレクトリの中にあるファイルを格納する
		File[] fileList = targetDirectory.listFiles();
		
		// fileListに格納されているファイルの個数回繰り返す
		for (int i = 0 ; i < fileList.length; i++)
		{
			// ディレクトリの中にあるファイルリスト
			directoryFileList = fileList[i].getAbsolutePath();
			
			// fileNameのインスタンスを作成する
			File fileName = new File(directoryFileList);
			
			// 指定したディレクトリの中のファイルが「ファイル」のとき
			if(fileName.isFile())
			{
				// fileNameListにファイル名を格納する
				fileNameList.add(directoryFileList);
			}
		}
	}
	
	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		super.logPrint.printDebugLog("[主処理開始]");

		// 結果を格納するMAPを生成
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		
		// FTP部品アクセッサを呼び出す
		JKKBatCommon.putFTP(commonItem, fileNameList, interfaceId, esbSbt, resultMap);
		
		// エラー区分を取得
		String errKbn = resultMap.get(JKKBatConst.FTP_ERR_KBN).toString();
		super.logPrint.printDebugLog("[エラー区分 = " + errKbn + "]");
				
		// 接続エラーの場合
		if (JKKBatConst.FTP_ERR_KBN_CONNECT_ERR.equals(errKbn))
		{
			super.logPrint.printDebugLog("[接続エラー フラグ判定前]");

			// 接続エラー情報を取得
			HashMap<?, ?> connectErr = (HashMap<?, ?>)resultMap.get(JKKBatConst.FTP_CONNECT_ERR);
			// エラーフラグを取得
			boolean errFlg = Boolean.valueOf(connectErr.get(JKKBatConst.FTP_CONNECT_ERR_FLG).toString()).booleanValue();
			// 接続エラーの判定処理
			if (!errFlg)
			{
				// 接続エラーの場合
				super.logPrint.printDebugLog("[接続エラー フラグ判定後]");

				// エラーメッセージを取得
				String msg = connectErr.get(JKKBatConst.FTP_CONNECT_ERR_MESSAGE).toString();
				// 業務例外を発生
				throw new JBSbatBusinessException(JPCBatchMessageConstant.EACB0530CE, new String[] {msg});
			}
		}
		// ファイルエラーの場合
		else if (JKKBatConst.FTP_ERR_KBN_FILE_ERR.equals(errKbn))
		{
			super.logPrint.printDebugLog("[ファイルエラー フラグ判定前]");
			
			// ファイルエラーリストの回数分繰り返す
			ArrayList<?> fileErrList = (ArrayList<?>)resultMap.get(JKKBatConst.FTP_FILE_ERR);
			for(int i = 0; i < fileErrList.size(); i++)
			{
				// ファイルエラー情報を取得
				HashMap<?, ?> fileErr = (HashMap<?, ?>)fileErrList.get(i);
				// エラーフラグを取得
				boolean errFlg = Boolean.valueOf(fileErr.get(JKKBatConst.FTP_FILE_ERR_FLG).toString()).booleanValue();
				// ファイルエラーの判定処理
				if (!errFlg)
				{
					super.logPrint.printDebugLog("[ファイルエラー フラグ判定後]");
					
					// エラーメッセージを取得
					String msg = fileErr.get(JKKBatConst.FTP_FILE_ERR_MESSAGE).toString();
					// 業務例外を発生
					throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0990CE, new String[] {msg});
				}
			}
		}
		super.logPrint.printDebugLog("[主処理終了]");

		// nullを返却
		return null;
		
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		
		super.logPrint.printDebugLog("[終了処理開始]");
		
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
		
		super.logPrint.printDebugLog("[終了処理終了]");
		
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
}
