/*********************************************************************
*  All Rights reserved,Copyright (c) Fujitsu ,2011					 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKIdPrintReqFileSend
*	ソースファイル名	：JBSbatKKIdPrintReqFileSend.java
*	作成者				：富士通　
*	作成日				：2011年05月06日
*＜機能概要＞
*　ID通知書印刷依頼ファイル送信処理部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00		2011/05/06   富士通		新規作成
*	v4.00		2013/03/04	 FJ)鷲塚    【ST2-2013-0001237】ファイルコピー処理削除
*	v4.01		2013/03/10	 FJ)鷲塚	【ST1-2013-0000472】エラー発生時のバックアップ処理削除
*********************************************************************/
package eo.business.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JCCBatCommon;
import eo.business.common.JKKBatCommon;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKIdPrintReqFileSend extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	
	/** インターフェースＩＤ(ID通知書印刷依頼(代行会社向け)ファイル送信) */
	private static final String KKIFE039 = "KKIFE039";
	/** インターフェースＩＤ(ＩＤ速報書情報送信) */
	private static final String DKIFE010 = "DKIFE010";
	
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		super.logPrint.printDebugLog("execute start");
		
		// ファイル名を取得
		String[] fileNames = freeItem.split(";");

		// 送信用フォルダパス
		String sendPath = null;
		// 送信ファイル
		File sendFile = null;
		
		if (fileNames != null)
		{
			// ファイル送信処理
			try 
			{
				for (String fileName : fileNames) 
				{
					// 送信対象のファイル
					File targetFile = new File(fileName);

					super.logPrint.printDebugLog("処理対象ファイル(" + fileName + ")");

					// 送信対象のファイルが存在しない、または0バイトの場合
					if (!targetFile.exists() || !(targetFile.length() > 0)) 
					{
						super.logPrint.printDebugLog("ファイルが存在しない、または0バイトのファイルのため、送信対象外です。");
						// 送信せず次の処理へ移る
						continue;
					}

					// 送信用のファイル生成
					sendPath = targetFile.getPath().replaceFirst(
							targetFile.getName(), "");
					// ST2-2013-0001237 2013/03/04 DEL START
//					sendFile = new File(sendPath + "eo_ID_" + JCCBatCommon.getSysDateTime());
//					
//					// 送信対象のファイルから、送信用のファイルへコピー
//					copyFile(targetFile, sendFile);
					// ST2-2013-0001237 2013/03/04 DEL END
					// ST2-2013-0001237 2013/03/04 ADD START
					sendFile = targetFile;
					// ST2-2013-0001237 2013/03/04 ADD END

					// ファイル名設定リスト
					ArrayList<String> fileNameList = new ArrayList<String>();

					// 物流用のファイルの場合
					if (targetFile.getName().indexOf("KKIFE039001") >= 0) 
					{
						// 送信ファイルを設定
						fileNameList.add(sendFile.getPath());

						// 送信処理を行う。
						// FTP転送処理を実施
						JKKBatCommon.putFile(commonItem, DKIFE010, JCCBatCommon.ISI, fileNameList);

					}
					// 守口用のファイルの場合
					else if (targetFile.getName().indexOf("KKIFE039002") >= 0) 
					{
						// 送信ファイルを設定
						fileNameList.add(sendFile.getPath());

						// 送信処理を行う。
						// FTP転送処理を実施
						JKKBatCommon.putFile(commonItem, KKIFE039, JCCBatCommon.ISI, fileNameList);
					}
					// ファイル名が秒までのため、１秒スリープして一意にする
					Thread.sleep(1000);
				}

			} 
			catch (Exception e)
			{
				// ST1-2013-0000472 2013/03/10 DEL START
//				// システムエラー時はファイルをバックアップフォルダに移動する。
//				if (sendFile != null) 
//				{
//					sendFile.renameTo(new File(sendPath + "/bk/" + sendFile.getName()));
//				}
//				// ST1-2013-0000472 2013/03/10 DEL END
				throw new Exception("ファイルの送信に失敗しました。");
			}
		}
		super.logPrint.printDebugLog("execute end");
		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	// ST2-2013-0001237 20130304 DEL START
//	/**
//	 * inファイルからoutファイルへファイルコピーを行います.
//	 * @param in 入力ファイル
//	 * @param out 出力
//	 * @throws Exception
//	 */
//	private void copyFile(File in, File out) throws Exception
//	{
//		// ファイルコピー
//		FileChannel sorceChannel = new FileInputStream(in).getChannel();
//		FileChannel destinationChannel = new FileOutputStream(out).getChannel();
//		sorceChannel.transferTo(0, sorceChannel.size(), destinationChannel);
//		sorceChannel.close();
//		destinationChannel.close();
//	}
	// ST2-2013-0001237 20130304 DEL END
	
	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
}
