/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKBundleInfoFileTenso
*	ソースファイル名	：JBSbatKKBundleInfoFileTenso.java
*	作成者				：富士通　
*	作成日				：2011年10月19日
*＜機能概要＞
*　バンドル情報ファイル転送部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/10/19	FJ)神吉		新規作成
*	v4.00.00	2012/12/13	FJ)団		【IT1-2012-0002418】対応
*	v4.01.00	2013/01/21	FJ)団		【ST1-2013-0000196】対応
*********************************************************************/
package eo.business.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JCCBatCommon;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatKKBundleInfoFileTenso extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/
	
	/**
	 * 外部インターフェイスID（バンドル情報ファイル送信情報）
	 */
	private static final String IF_ID = "KKIFE049";
	
	/**
	 *  送信ファイル名 
	 */
	private static final String TEMP_FILE_NAME = "temp500162000104502438000000";
	
	/** 
	 * テキストファイル名 
	 */
	private static final String TEXT_FILE_NAME = "etv4500162000104502438000000";
	
	/**
	 * 初期処理
	 * @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 free_item = super.commonItem.getFreeItem();
		String[] item_list = free_item.split(";");
		
		//パラメータ数チェック
		if(3 != item_list.length)
		{
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0130CE, new String[]{"FREE"});
		}
		// 送信ファイルパスを取得。
		String snd_dir = item_list[0];
		
		// 送信対象のファイル名（リネーム前）を取得。
		String file_nm = item_list[1];
		
		// ファイル名に使用しているSYSDATEを取得。
		String sysdate = item_list[2];
		
		// 拡張子なしファイル名（リネーム前）
		String wk_file_nm = snd_dir + file_nm;
		
		// 圧縮ファイル名（リネーム前）
		//String zipFileNm = wk_file_nm + ".tar.gz";
		String zipFileNm = wk_file_nm + ".zip";
		
		// 送信ファイル名（リネーム後）
		String tmp_file_nm = TEMP_FILE_NAME + "_" + sysdate;
		
		// 送信ファイル（リネーム後）
		String snd_file_nm = snd_dir + tmp_file_nm;
		
		// リネーム処理
		File outFile1 = new File(zipFileNm);
		File outFile2 = new File(snd_file_nm);
		outFile1.renameTo(outFile2);
		
		// テキストファイル作成
		String textFileName = executeOutFile();
		
		super.logPrint.printDebugLog("FTP送信ファイル----->" + snd_file_nm);
		// データファイルの送信
		this.executeESB(snd_file_nm);
		
		super.logPrint.printDebugLog("FTP送信ファイル----->" + textFileName);
		// テキストファイルの送信
		this.executeESB(textFileName);
		
		super.logPrint.printDebugLog("execute_END");
		
		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/**
	 * テキストファイル出力処理
	 * @return String
	 * @throws Exception 
	 */
	private String executeOutFile() throws Exception
	{
		String free_item = super.commonItem.getFreeItem();
		String[] item_list = free_item.split(";");
		// ファイル生成
		BufferedWriter out_writer = null;
		String newFileNm = null;
		String date = item_list[2];
		try{
			
			newFileNm = item_list[0] + TEXT_FILE_NAME + "_" + date;
			
			// ファイル生成
			out_writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFileNm)));
			
			StringBuilder sb = new StringBuilder();
			
			sb.append(TEXT_FILE_NAME);	// テキストファイル名
			sb.append(TEMP_FILE_NAME);	// 添付ファイル名
			sb.append("_");
			sb.append(date);			// ファイル名に使用したシステム日時
			sb.append("001");
			
			out_writer.write(sb.toString());
			
			// ファイル出力
			out_writer.flush();
			
		}

		catch(IOException e)
		{
			throw new JBSbatBusinessException("EKKB0250CE", new String[]{newFileNm, e.getMessage()});
		}
		finally
		{
			if (null != out_writer)
			{
				out_writer.close();
			}
		}
		
		return newFileNm;
		
	}
	
	/**
	 * ESB連携部品を使用してファイル送信を行います。<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.ファイルパスが空の場合、エラーとします。<br>
	 * 
	 * 2.連携部品の引数を作成します。<br>
	 * 
	 * 3.ESB連携部品を実行します。<br>
	 * 
	 * 4.処理結果が正常以外の場合、エラーを発生させます。<br>
	 * 
	 * </pre>
	 * <p>
	 * @param value　転送対象ファイルパス。
	 * @param if_id　インターフェースID。
	 * @throws Exception 業務サービス内で発生した例外全般。
	 */
	private void executeESB(String value) throws Exception
	{
		ArrayList<String> file_list = new ArrayList<String>(1);
		HashMap<String, Object> out_map = new HashMap<String, Object>(3);

		file_list.add(value);

		// 転送処理を実行します
		boolean result = JCCBatCommon.putFTP(super.commonItem, file_list, IF_ID, JCCBatCommon.ISI, out_map);

		// エラー判定をします
		if (!result)
		{
			String kbn = out_map.get("ERR_KBN").toString();
			String err_msg = "";

			// ファイルエラーの場合
			if ("2".equals(kbn))
			{
				err_msg = (String)((HashMap)((ArrayList)out_map.get("FILE_ERR")).get(0)).get("ERR_MESSAGE");
			}
			else
			{
				err_msg = (String)((HashMap)out_map.get("CONNECT_ERR")).get("ERR_MESSAGE");
			}

			throw new JBSbatBusinessException(JPCBatchMessageConstant.EKKB0200CE, new String[]{value, err_msg});
		}
	}
}
