/*********************************************************************
* All Rights reserved,Copyright (c) K-Opticom
**********************************************************************
*＜プログラム内容＞
*   システム名      ：プロジェクト共通
*   モジュール名    ：JKUBatFileUtil
*   ソースファイル名：JKUBatFileUtil.java
*   作成者          ：富士通
*   日付            ：2014年06月26日
*＜機能概要＞
*   ファイル出力・編集部品
*＜修正履歴＞
*   バージョン  修正日       修正者      修正内容
*   v10.00.00   2014/06/26   FJ)高橋     【ANK-2138-00-00】新規作成
*
**********************************************************************/
package eo.business.common;

import java.io.File;
import eo.common.util.JCCFileUtil;
import eo.common.util.JCCFrameworkException;
import eo.framework.application.JCCbatFrameworkException;
import eo.framework.db.JBSbatSQLAccess;
import eo.framework.item.JBSbatCommonDBInterface;
import eo.framework.item.JBSbatCommonItem;


/**
 * 工事受付システム用ファイル出力・編集を提供する。<p>
 * ジョブで実装します。
 * <BR>
 * @author 富士通
 */
public class JKUBatFileUtil extends JCCBatCommon
{
	/**
	 * 圧縮レベルのプロパティファイルキー
	 */
	private static final String PROP_KEY_COMP_LEVEL = "COMP_LEVEL";
	
	/** SQL定義キー(KK_INSERT_002)*/
	private static final String DL_FILE_KANRI_INSERT_002 = "KK_INSERT_002";
	
	/**
	 * ファイル登録処理<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 * 1.初期処理<br>
	 *
	 * 2.ファイル圧縮処理<br>
	 * 
	 * 3.電子ファイル管理処理<br>
	 * 
	 * 4.ダウンロードファイル管理登録処理<br>
	 * 
	 * 5.ファイル削除処理<br>
	 * 
	 * </pre>
	 * <p>
	 * @param commonItem    バッチ共通パラメータ電文
	 * @param trnKanriNo    処理管理番号
	 * @param dataCnt    データ件数
	 * @param fileCd    ファイルコード
	 * @param fileDir    ファイルディレクトリ
	 * @param fileName    ファイル名
	 * @param fileDelYmd    ファイル削除年月日
	 * @param dbDlFileKanri    テーブルアクセスクラス
	 * @param isCompress    圧縮実行フラグ
	 * @param delFileFlg    ファイル削除フラグ
	 * @throws JCCbatFrameworkException, Exception      業務サービス内で発生した例外全般。
	 */
	public void createFile(JBSbatCommonItem commonItem, String trnKanriNo, Long dataCnt, 
									String fileCd, String fileDir, String fileName, String fileDelYmd, 
									JBSbatSQLAccess dbDlFileKanri, boolean isCompress, boolean delFileFlg) throws JCCbatFrameworkException, Exception
	{
		if (!JCCBatCommon.isNotNull(fileDir) || !JCCBatCommon.isNotNull(fileName))
		{
			return;
		}
		// ファイルパス設定
		String filePath = fileDir + "/" + fileName;
		
		// 登録ファイルパス設定
		String insertFilePath = fileDir + "/" + fileName;
		
		if(isCompress)
		{
			// 圧縮実行フラグがTrueの場合、圧縮ファイルを作成する。
			// 圧縮レベルの取得
			String compLevel = getCompLevel();
			
			// ファイルの圧縮、圧縮ファイルのパスの取得
			insertFilePath = JCCBatCommon.compressFile(filePath, fileDir, compLevel);
		}
		
		File insertFile = new File(insertFilePath);
		
		// ファイル存在確認
		if (insertFile.exists())
		{
			// 電子ファイル管理登録処理
			String[] resultKey = null;		// 電子ファイル管理テーブルに登録された場合に返却されるPKEY
			
			resultKey = JCCBatCommon.createDenshiFile(commonItem, fileCd, insertFile.getPath(), fileDelYmd);
			
			// ダウンロードファイル管理登録処理
			JBSbatCommonDBInterface paramList = new JBSbatCommonDBInterface();
			
			// 処理管理番号
			paramList.setValue(trnKanriNo);
			// ファイル名
			paramList.setValue(insertFile.getName());
			// データ件数
			paramList.setValue(dataCnt);
			// 電子ファイル管理番号
			paramList.setValue(resultKey[0]);
			// 世代登録年月日時分秒
			paramList.setValue(resultKey[1]);
			
			// ダウンロードファイル管理登録
			createDlFileKanri(paramList, dbDlFileKanri);
			
			// ファイル削除処理
			if(isCompress)
			{
				// 圧縮実行フラグがTrueの場合、圧縮ファイル削除
				removeDir(insertFile.getPath());
			}
		}
		
		if (delFileFlg)
		{
			// ファイル存在確認
			File file = new File(filePath);
			if (file.exists())
			{
				// ファイル削除
				file.delete();
			}
		}
	}
	
	/**
	 * 引数のファイルが残っている場合、そのファイルが入っているディレクトリごと削除する。
	 * @param filePath 削除対象のファイルパス
	 * @throws JCCFrameworkException
	 */
	private static void removeDir(String filePath) throws JCCFrameworkException
	{
		// 作業ディレクトリが残っていた場合、削除
		if(filePath != null)
		{
			File f_dir = new File(new File(filePath).getParent());
			if(f_dir.exists())
			{
				JCCFileUtil.removeDir(new File(filePath).getParent());
			}

		}
	}
	
	/**
	 * アプリケーションプロパティファイルから圧縮レベルを取得する。
	 * <br>
	 * @return 圧縮レベル
	 * @throws JCCbatFrameworkException バッチフレームワーク例外
	 */
	private static String getCompLevel() throws JCCbatFrameworkException 
	{
		// 圧縮レベル
		String compLevel = JCCBatCommon.getApplicationConst(PROP_KEY_COMP_LEVEL);
		if (!JCCBatCommon.isNotNull(compLevel))
		{
			throw new JCCbatFrameworkException("APLConst.propertiesに圧縮レベルの定義がありません。");
		}
		
		// 圧縮レベル（範囲チェック用）
		int intCompLevel = 0;
		try
		{
			intCompLevel = Integer.parseInt(compLevel);
		}
		catch (NumberFormatException e)
		{
			throw new JCCbatFrameworkException("圧縮レベルの定義が不正です。");
		}
		
		// 圧縮レベルの範囲確認
		if (intCompLevel < 1 || 9 < intCompLevel)
		{
			throw new JCCbatFrameworkException("圧縮レベルが範囲外の設定です。");
		}
		return compLevel;
	}
	
	/**
	 * ファイル登録処理<br>
	 * <p>
	 * <b>処理フロー</b><br>
	 * <pre>
	 *
	 * 1.DBアクセスを実行します。<br>
	 * 
	 * </pre>
	 * <p>
	 * @param paramList    SQLパラメータリスト
	 * @param dbDlFileKanri    テーブルアクセスクラス
	 * @throws Exception      業務サービス内で発生した例外全般。
	 */
	private static void createDlFileKanri(JBSbatCommonDBInterface paramList, JBSbatSQLAccess dbDlFileKanri) throws Exception
	{
	
		// DBアクセスを実行します
		dbDlFileKanri.executeBySqlDefine(paramList, DL_FILE_KANRI_INSERT_002);
	}
	
}
