/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JDKSendFileBaseService
*	ソースファイル名	：JDKSendFileBaseService.java
*	作成者				：富士通　
*	作成日				：2011年09月02日
*＜機能概要＞
*　ファイル転送ベースサービス部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/09/02   富士通		新規作成
*********************************************************************/
package eo.business.common;

import static eo.common.constant.JDKStrConst.PATH_DLMT_UNIX;
import static eo.common.constant.JDKStrConst.PATH_DLMT_WIN;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import eo.business.common.JBSbatBusinessService;
import eo.business.common.JCCBatCommon;
import eo.common.constant.JDKStrConst;
import eo.common.constant.JPCBatchMessageConstant;
import eo.framework.application.JBSbatBusinessException;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.util.JBSbatAplConst;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public abstract class JDKSendFileBaseService extends JBSbatBusinessService
{
	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
	}
	

	/**
	 * インターフェースID取得
	 * @return インターフェースID
	 */
	protected abstract String getInterfaceId();
	
	/**
	 * ファイル名の長さ取得
	 * @return ファイル名の長さ
	 */
	protected abstract int getFileNameLength();
	
	/**
	 * ファイルタイプ取得
	 * @return ファイル名の開始文字
	 */
	protected abstract String getFileType();
	
	
	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
		// 転送元ディレクトリに存在するファイル名を取得
		String filePath = rnktPath(JBSbatAplConst.getAplConstValue(JDKStrConst.ENV_GAIBU_SEND_DIR) + "/" + getInterfaceId(), "");
		File direc = new File(filePath);
		ArrayList<String> outFileList = new ArrayList<String>();
		String[] fileArray = direc.list();
		for (int i = 0; i < fileArray.length; i++)
		{
			String fileNm = fileArray[i];

			// 対象ファイルをピックアップ
			// ファイル名の長さが０より小さい場合は、ファイル名の長さのチェックはしない
			if ((getFileNameLength() == fileNm.length() || getFileNameLength() <= 0) && fileNm.startsWith(getFileType()))
			{
				outFileList.add(filePath + fileArray[i]);
			}
		}

		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		boolean resultBln = true;
		// ファイル転送
		resultBln = JCCBatCommon.putFTP(super.commonItem, outFileList, getInterfaceId(),  resultMap);

		// エラー発生時
		if (!resultBln)
		{
			String fileNm = "";		// ファイル名
			String errFlg = "";		// エラーフラグ
			String errKbn = "";		// エラー区分
			String errCd = "";		// エラーコード
			StringBuffer errMsg = new StringBuffer();	// エラーメッセージ
			HashMap<String, Object> shosai = new HashMap<String, Object>();		// エラー詳細

			errKbn = resultMap.get("ERR_KBN").toString();

			// 接続エラーの場合
			if ("1".equals(errKbn))
			{
				shosai = (HashMap)resultMap.get("CONNECT_ERR");
				errCd = shosai.get("ERR_CD").toString();

				// エラーメッセージ組み立て
				errMsg.append("[エラー区分:エラーコード]=(")
					.append(errKbn).append(":").append( errCd).append(")");
			}
			else
			{
				// ファイルエラーの場合
				ArrayList<HashMap> fileErr = (ArrayList<HashMap>)resultMap.get("FILE_ERR");
				for (int i = 0; i < fileErr.size(); i++)
				{
					shosai = fileErr.get(i);
					fileNm = shosai.get("FILE_NAME").toString();
					errFlg = shosai.get("ERR_FLG").toString();
					errCd = shosai.get("ERR_CD").toString();

					// エラーメッセージ組み立て
					errMsg.append("[ファイル名:エラーフラグ:エラーコード]=(")
						.append(fileNm).append(":").append(errFlg).append(":").append(errCd).append(")");
				}

			}
			super.logPrint.printDebugLog("ファイル転送失敗");
			throw new JBSbatBusinessException(JPCBatchMessageConstant.EDKB0010CE, 
												new String[] {errMsg.toString()});
		}
		return null;
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	}

	/**
	 * パス文字列の連結
	 * 連結するパス文字列にパス区切り文字の設定を制御する。
	 * @param path1 パス文字列
	 * @param path2 パス文字列
	 * @return 連結したパス文字列
	 */
	private String rnktPath(String path1, String path2)
	{
		String pathDlmt = PATH_DLMT_UNIX;
		StringBuffer buff = new StringBuffer();
		
		if (path1.indexOf(PATH_DLMT_WIN) > -1 || path2.indexOf(PATH_DLMT_WIN) > -1)
		{
			pathDlmt = PATH_DLMT_WIN;
		}
		if (!path1.endsWith(pathDlmt))
		{
			return buff.append(path1).append(pathDlmt).append(path2).toString();
		}
		return buff.append(path1).append(path2).toString();
	}

}
