/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatACDlydTrnFileCst
*	ソースファイル名	：JBSbatACDlydTrnFileCst.java
*	作成者				：富士通　
*	作成日				：2011年09月14日
*＜機能概要＞
*　ディレイド処理ファイル抽出部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/09/14   富士通		新規作成
*********************************************************************/
package eo.business.service;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import eo.business.common.JACbatDebugLogUtil;
import eo.business.common.JBSbatBusinessService;
import eo.business.common.JCCbatShoriIraiUtil;
import eo.business.util.file.JBSbatACIFM099;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatServiceInterfaceMap;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.application.JBSbatBusinessException;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatACDlydTrnFileCst extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/** システムエラーフラグ（true：あり、false：なし） */
	private boolean errFlg = false;

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][initial]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][onlineOpeDate=" + super.onlineOpeDate + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][initial][freeItem=" + super.freeItem + "]");
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][initial]");
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][execute]");
		
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][JobID=" + this.jobid + "]");
		
		// ディレイド処理依頼情報リストを取得
		ArrayList<HashMap<String, Object>> infoMapList = JCCbatShoriIraiUtil.getShoriIraiInfo(commonItem, this.jobid);

		// ディレイド処理依頼情報リストが取得出来た場合
		if (infoMapList != null)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][ディレイド処理依頼情報"+ (infoMapList.size()) +"件あり]");

			// 出力ファイルを作成する
			// 最後の1件を取得
			HashMap<String, Object> resultMap = (HashMap<String, Object>) infoMapList.get(infoMapList.size()-1);
			
			// 各情報を取得
			String shoriIraiNo   = (String)resultMap.get("DLYD_TRN_REQ_NO");		// ディレイド処理依頼番号

			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][ディレイド処理依頼番号="+ shoriIraiNo +"]");
			
			try
			{
				// 2. ディレイド処理対象ファイル作成
				this.outPutFileMake(resultMap);
			}
			catch(IOException e)
			{
				assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][IOException発生]");
				this.errFlg = true;
			}
			
			// 出力ファイル作成
			JBSbatServiceInterfaceMap outMap = new JBSbatServiceInterfaceMap();
			outMap.setString(JBSbatACIFM099.DLYD_TRN_REQ_NO,	shoriIraiNo);	// ディレイド処理依頼番号
			//outMap.setString(JBSbatACIFM099.DLYD_TRN_RSLT_CD,	"");			// 処理結果コード ※Javaアプリでは出力しない。
			outMap.setOutFlg(true);
			
			// 出力共通電文クラス作成
			JBSbatOutputItem outputInItem = new JBSbatOutputItem();
			outputInItem.addOutMapList(outMap);

			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");

			return outputInItem;
		}
		// (2) 検索結果が0件の場合、処理依頼なしとし正常終了する。
		else
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][execute][ディレイド処理依頼情報なし]");
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][execute]");
			
			return null;
		}
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][terminal]");

		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][terminal][エラーフラグ=" + this.errFlg +"]");

		// エラーフラグ
		if(this.errFlg)
		{
			// システムエラーをスローする
			// ファイル作成に失敗しました。
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EACB0550AH);
		}

		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][terminal]");

	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲

	/* ファイル出力処理
	 * @param HashMap<String, Object> ディレイド処理依頼情報
	 * @throws JBSbatBusinessException 
	 * @throws IOException
	 */
	private void outPutFileMake(HashMap<String, Object> resultMap) throws JBSbatBusinessException, IOException
	{
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[S][outPutFileMake]");

		BufferedOutputStream bStream = null;

		// ファイル出力実施
		try
		{
			// 出力ファイル名
			String fileName = this.freeItem;

			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][outPutFileMake][出力ファイル名：" + this.freeItem + "]");

			byte[] eBinaryFile = (byte[])resultMap.get("INPUT_EFILE");	// 入力電子ファイル
			if (eBinaryFile != null)
			{
				FileOutputStream fStream = new FileOutputStream(fileName);
				bStream = new BufferedOutputStream(fStream);
				bStream.write(eBinaryFile);
			}
		}
		catch (IOException e)
		{
			assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[L][outPutFileMake][IOException発生]");

			this.errFlg = true;
		}
		finally
		{
			if (bStream != null)
			{
				bStream.close();
				
			}
		}
		assert JACbatDebugLogUtil.printDebugLog(super.logPrint, "[E][outPutFileMake]");
	}
}
