/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatZMEcAdSend
*	ソースファイル名	：JBSbatZMEcAdSend.java
*	作成者				：富士通　
*	作成日				：2013年10月20日
*＜機能概要＞
*　EC向け住所マスタデータファイル送信部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2013/10/20   富士通		新規作成
*	v6.00.00	2013/10/21   FJ)日野	【ANK-1587-00-00】ECプラットフォーム
*********************************************************************/
package eo.business.service;

import java.io.File;
import java.util.ArrayList;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JBSbatZMFileReaderUtil;
import eo.business.common.JZMBatCommon;
import eo.business.common.JZMBatConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;

/**
* 住所マスタデータファイル（EC向け）送信処理クラスです <p>
*<BR>
* @author 富士通
*/
public class JBSbatZMEcAdSend extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/* ++++++++++ v1.0.0 追加開始 ++++++++++ */
	
	/** 中間ファイル名 */
	private static final String ZMIFM025001 = "ZMIFM025001.csv";
	
	/** 連携ファイル名先頭部 */
	private static final String ZMIFE055_CSV = "ZMIFE055_";
	
	/** 取得日付 */
	private String opeDateStr = null;
	
	/* ++++++++++ v1.0.0 追加終了 ++++++++++ */
	
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		
		/* ++++++++++ v1.0.0 追加開始 ++++++++++ */
		
		// 他システムへ送信するファイルの配置先を取得
		String inPath = JZMBatCommon.getGaibuSendDir(JZMBatConst.ZMIFE055_DIR);
		
		// ファイル名を取得
		String inFileNm = JZMBatCommon.getFileNameDatePattern(inPath, ZMIFE055_CSV, JZMBatConst.YYYYMMDDHHMMSS, JZMBatConst.CSV);
		
		File f_dataFile = new File(inPath + inFileNm);
		
		if(inFileNm == null || JZMBatConst.BLANK.equals(inFileNm) || !f_dataFile.exists())
		{
			// CSVファイルが存在しない場合はエラー
			String midFileStr = JZMBatCommon.getMidDir() + ZMIFM025001;
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EZMB0140CW, new String[]{midFileStr});
		}
		
		// 拡張子以外のファイル名を取得
		opeDateStr = inFileNm.substring(0, inFileNm.indexOf(JZMBatConst.CSV));
		
		// データカウント
		int cnt = getFileRowCnt(inPath + inFileNm);
		
		StringBuffer sb = new StringBuffer();
		String inFile = sb.append(inPath).append(opeDateStr).toString();
		
		// データファイルパス
		String dataFilePath = inPath + inFileNm;
		
		// カウントファイルパス
		String cntFilePath = inFile + JZMBatConst.CNT;
		
		// フラグファイルパス
		String flgFilePath = inFile + JZMBatConst.FLG;
		
		JZMBatCommon.printDoubleQuoteCntFile(cntFilePath, cnt, 1);
		JZMBatCommon.printDoubleQuoteFlgFile(flgFilePath, 1);
		
		super.logPrint.printDebugLog("他システム送信ディレクトリパス:" + inPath);
		super.logPrint.printDebugLog("他システム送信データファイルフルパス:" + dataFilePath);
		super.logPrint.printDebugLog("他システム送信件数ファイルフルパス:" + cntFilePath);
		super.logPrint.printDebugLog("他システム送信フラグファイルフルパス:" + flgFilePath);
		
		// ファイルパス格納用リスト
		ArrayList<String> filePathList = new ArrayList<String>();
		
		filePathList.add(dataFilePath);
		filePathList.add(cntFilePath);
		filePathList.add(flgFilePath);
		
		// 他システムに対するFTP転送
		JZMBatCommon.putFTPGaibu(commonItem, filePathList, JZMBatConst.ZMIFE055);
		
		/* ++++++++++ v1.0.0 追加終了 ++++++++++ */
		
		return null;
	/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/
	
	/* ++++++++++ v1.0.0 追加開始 ++++++++++ */

	/**
	 * ファイル内の行数を取得
	 * @param filePath filePath
	 * @return 件数
	 * @throws Exception
	 */
	private int getFileRowCnt(String filePath) throws Exception
	{
		int cnt = 0;
		JBSbatZMFileReaderUtil br = new JBSbatZMFileReaderUtil(filePath);

		while(br.readLine() != null)
		{
			cnt++;
		}
		br.close();

		return cnt;
	}
	
	/* ++++++++++ v1.0.0 追加終了 ++++++++++ */
}
