/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom					 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKBmpPreTchFileSend
*	ソースファイル名	：JBSbatKKBmpPreTchFileSend.java
*	作成者				：富士通　
*	作成日				：2011年05月26日
*＜機能概要＞
*　番ポなし番号事前通知ファイル送信部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/05/26   富士通		新規作成
*	v4.00.00	2012/08/15	 FJ)井熊	送信ファイルチェックにエラー処理追記
*********************************************************************/
package eo.business.service;

import java.util.ArrayList;
import java.util.HashMap;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JCCBatCommon;
import eo.business.common.JKKBatFTPUtil;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.file.JBSbatInputFileUtil;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKBmpPreTchFileSend extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	/** FTP送信対象ファイル名 */
	private ArrayList<String> ftpSendFileNames = null;
	/** FTP送信対象ファイル文字コード */
	private static final String SJIS  = "Shift-JIS";
	/** FTP送信対象インターフェースID*/
	private static final String TARGET_IF_ID = "KKIFE117";

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		// フリーアイテムで渡されたファイル名を取得します
		ftpSendFileNames = new ArrayList<String>();
		this.ftpSendFileNames.add(commonItem.getFreeItem());
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		// 送信可能であればFTP送信を行う。
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		if (isFileSendOk())
		{
			super.logPrint.printDebugLog("FTP送信処理開始");
			if (!JCCBatCommon.putFTP(super.commonItem, 
									this.ftpSendFileNames,
									TARGET_IF_ID,
									JCCBatCommon.ISI,
									resultMap))
			{
				super.logPrint.printBusinessErrorLog(JPCBatchMessageConstant.EKKB0030CE);
				super.logPrint.printDebugLog(JKKBatFTPUtil.getFTPResultMessageString(resultMap));
				super.logPrint.printDebugLog(String.format("処理日付:%s", commonItem.getOpeDate()));
			}
			super.logPrint.printDebugLog("FTP送信処理終了");
		}
		else
		{
			// エラーフラグ設定
			super.commonItem.setErrFlg(true);
			// システムエラーとする
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0090TE,
					new String[]{"送信ファイル件数０"});
		}
		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/

	/**
	 * ファイルが送信可能かどうかチェックを行います。
	 * @return 送信可能の場合 true, 送信不要の場合(データなし) false
	 * @throws Exception
	 */
	private boolean isFileSendOk() throws Exception
	{
		int inputCount = 0;
		// 入力ファイル名を取得する
		String inFileName = this.ftpSendFileNames.get(0);
		JBSbatInputFileUtil inFileObj = null;
		try
		{
			if(inFileName != null && !"".equals(inFileName))
			{
				// 入力ファイルオブジェクトを生成する
				inFileObj = new JBSbatInputFileUtil(inFileName);
				inFileObj.setEncode(SJIS);
				//Readerオブジェクトを生成する。
				inFileObj.createReader();
				//ファイル読み込み可否を判定する。
				if (inFileObj.ready())
				{
					inputCount++;
				}
				inFileObj.close();
			}
		}
		catch (Exception e)
		{
			throw new Exception("ファイルの読み込みに失敗しました。");
		}
		if (inputCount <= 0) 
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
