/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom					 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatDKTknTenso
*	ソースファイル名	：JBSbatDKTknTenso.java
*	作成者				：富士通　
*	作成日				：2011年05月23日
*＜機能概要＞
*　出荷指図情報転送部品です。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2011/05/23   富士通		新規作成
*	v54.00.00   2021/08/20   GDC)j.hortilano        【ANK-4069-00-00】EOL対応(バッチ・DBサーバリプレース対応)
*********************************************************************/
package eo.business.service;

import java.io.File;
import java.util.ArrayList;
import eo.business.common.JBSbatBusinessService;
import eo.common.constant.JDKStrConst;
import eo.framework.item.JBSbatCommonItem;
import eo.framework.item.JBSbatOutputItem;
import eo.framework.util.JBSbatAplConst;
import static eo.common.constant.JDKStrConst.*;

/**
* (クラスの機能概要) <p>
*<BR>
* @author 富士通
*/
public class JBSbatDKTknTenso extends JBSbatBusinessService
{
	/**▼▼▼▼▼▼ツールから生成した宣言です 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成した宣言です 終了▲▲▲▲▲▲*/

	/**
	 * 初期処理
	 * @param JBSbatCommonItem commonItem　バッチ共通パラメータ電文
	 * @throws Exception
	 */
	public void initial(JBSbatCommonItem commonItem) throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの初期処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した初期化のソースです 開始▼▼▼▼▼▼*/
		// 共通パラメータを設定します
		super.setCommonInfo(commonItem);
		/**▲▲▲▲▲▲ツールから生成した初期化のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの初期処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 主処理
	 * @return JBSbatOutputItem　出力情報
	 * @throws Exception
	 */
	public JBSbatOutputItem execute() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの主処理を記述してください。▼▼▼▼▼▼*/
		// インターフェイスID
		String ifid = commonItem.getFreeItem();
		// 転送元ディレクトリに存在するファイル名を取得
		String filePath = rnktPath(JBSbatAplConst.getAplConstValue(JDKStrConst.ENV_GAIBU_SEND_DIR) + PATH_DLMT_UNIX + ifid, "");
		// 中間ディレクトリに存在を取得
		String filePath2 = rnktPath(JBSbatAplConst.getAplConstValue(JDKStrConst.ENV_MID_DIR), "");
		File direc = new File(filePath);
		ArrayList<String> outFileList = new ArrayList<String>();
		String[] fileArray = direc.list();
		for (int i = 0; i < fileArray.length; i++)
		{
			// エラーメッセージ
			StringBuffer errMsg = new StringBuffer();
			// ファイル名称
			String fileNm = fileArray[i];
			// フラグファイル
			File flgfile = new File(filePath2 + PATH_DLMT_UNIX + fileNm.replace(".csv", ".flg"));

			// フラグファイルが中間ファイルフォルダに存在しない場合
			if (!flgfile.isFile()) 
			{
				super.logPrint.printDebugLog("中間ファイルフォルダにフラグファイルが存在しません。(" + filePath + fileArray[i] + ")");
				continue;
			}

			// 転送ファイルを搭載
			outFileList.add(filePath + fileArray[i]);

		}

		// ファイル転送(1件づつ転送)
		for (int i = 0; i < outFileList.size(); i++)
		{
// ANK-4069-00-00 DEL START
		//	HashMap<String, Object> resultMap = new HashMap<String, Object>();
		//	boolean resultBln = true;
		//
		//	ArrayList<String> outFileList2 = new ArrayList<String>();
		//	outFileList2.add(outFileList.get(i));
		//
		//	resultBln = JCCBatCommon.putFTP(super.commonItem, outFileList2, ifid, 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("[エラー区分:エラーコード]=(" + errKbn + ":" + errCd + ")");
		//
		//			super.logPrint.printDebugLog("ファイル転送失敗");
		//			throw new JBSbatBusinessException(JPCBatchMessageConstant.EDKB0010CE, 
		//												new String[] {errMsg.toString()});
		//		}
		//		else
		//		{
		//			// ファイルエラーの場合
		//			ArrayList<HashMap> fileErr = (ArrayList<HashMap>)resultMap.get("FILE_ERR");
		//			for (int j = 0; j < fileErr.size(); j++)
		//			{
		//				shosai = (HashMap)fileErr.get(i);
		//				fileNm = shosai.get("FILE_NAME").toString();
		//				errFlg = shosai.get("ERR_FLG").toString();
		//				errCd = shosai.get("ERR_CD").toString();
		//
		//				// エラーメッセージ組み立て
		//				errMsg.append("[ファイル名:エラーフラグ:エラーコード]=(" 
		//								+ fileNm + ":" + errFlg + ":" + errCd + ")");
		//			}
		//
		//			super.logPrint.printDebugLog("ファイル転送失敗");
		//			throw new JBSbatBusinessException(JPCBatchMessageConstant.EDKB0010CE, 
		//												new String[] {errMsg.toString()});
		//		}
		//	}
// ANK-4069-00-00 DEL END
		
			// フラグファイル削除
			File flgfile = new File(outFileList.get(i).replace(filePath, filePath2).replace(".csv", ".flg"));
			flgfile.delete();
		}
		return null;

		/**▲▲▲▲▲▲業務サービスの主処理を記述してください。▲▲▲▲▲▲*/
	}

	/**
	 * 業務サービス終了処理
	 * @throws Exception
	 */
	public void terminal() throws Exception
	{
	/**▼▼▼▼▼▼業務サービスの終了処理を記述してください。▼▼▼▼▼▼*/
		/**▼▼▼▼▼▼ツールから生成した終了処理のソースです 開始▼▼▼▼▼▼*/
		/**▲▲▲▲▲▲ツールから生成した終了処理のソースです 終了▲▲▲▲▲▲*/
	/**▲▲▲▲▲▲業務サービスの終了処理を記述してください。▲▲▲▲▲▲*/
	}

	/**▼▼▼▼▼▼ツールから生成したメソッドです 開始▼▼▼▼▼▼*/
	/**▲▲▲▲▲▲ツールから生成したメソッドです 終了▲▲▲▲▲▲*/

	/* ++++++++++ v1.00.00 追加開始 ++++++++++ */

	/**
	 * パス文字列の連結<br>
	 * 連結するパス文字列にパス区切り文字の設定を制御する。
	 * @param path1 パス文字列
	 * @param path2 パス文字列
	 * @return 連結したパス文字列
	 */
	private String rnktPath(String path1, String path2)
	{
		String pathDlmt = PATH_DLMT_UNIX;
		if (path1.indexOf(PATH_DLMT_WIN) > -1 || path2.indexOf(PATH_DLMT_WIN) > -1)
		{
			pathDlmt = PATH_DLMT_WIN;
		}
		if (!path1.endsWith(pathDlmt))
		{
			return path1 + pathDlmt + path2;
		}
		return path1 + path2;
	}
	/* ++++++++++ v1.00.00 追加終了 ++++++++++ */
}
