/*********************************************************************
*  All Rights reserved,Copyright (c) K-Opticom						 *
**********************************************************************
*＜プログラム内容＞
*	システム名			：eo顧客基幹システム
*	モジュール名		：JBSbatKKAbstractHistoryDeleteTool
*	ソースファイル名	：JBSbatKKAbstractHistoryDeleteTool.java
*	作成者				：富士通　
*	作成日				：2014年07月03日
*＜機能概要＞
*　履歴削除ツールの親クラスです。
*＜修正履歴＞
*	バージョン	修正日		修正者		修正内容
*	v1.00.00	2014/07/03  FJ)山下		新規作成
*********************************************************************/
package eo.business.service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;

import eo.business.common.JBSbatBusinessService;
import eo.framework.file.JBSbatInputFileUtil;

/**
* 
* 履歴削除ツールの親クラスです。
* <BR>
* @author 富士通
*/
public abstract class JBSbatKKAbstractHistoryDeleteTool extends
		JBSbatBusinessService {

	/** 入力定義ファイルキー：ファイルパス */
	public static final String INFILE_KEY = "FILE_PATH";

	/** ツール起動時間 */
	protected Integer startUpTime = null;
	
	/** ツール開始時間 */
	protected Long startTimeMills = null;
	
	/** テーブル毎の削除件数情報 */
	protected LinkedHashMap<String, Integer> tableDeleteCountMap = null;
	
	/** 入力ファイル毎の削除キー情報 */
	protected LinkedHashMap<String, ArrayList<LinkedHashMap<String, String>>> tableDeleteInfoMap = null;

	/** ダブルコーテーション */
	public static final String DOUBLE_QUOTE = "\"";
	
	/** ブランク */
	public static final String BLANK = "";
	
	/** カンマ */
	public static final String COMMA = ",";
	
	/**
	 * テーブル毎の削除件数情報を取得します。
	 * @return テーブル毎の削除件数情報
	 */
	public LinkedHashMap<String, Integer> getTableDeleteCountMap() {
		return tableDeleteCountMap;
	}

	/**
	 * テーブル毎の削除件数情報を設定します。
	 * @param tableDeleteCountMap
	 */
	public void setTableDeleteCountMap(String tableName, int delCount) {
		
		if (null == this.tableDeleteCountMap)
		{
			this.tableDeleteCountMap = new LinkedHashMap<String, Integer>();
		}
		
		if (this.tableDeleteCountMap.containsKey(tableName))
		{
			Integer count = this.tableDeleteCountMap.get(tableName);
			
			this.tableDeleteCountMap.put(tableName, count + delCount);
		}
		else
		{
			this.tableDeleteCountMap.put(tableName, delCount);
		}
	}

	/**
	 * 入力ファイル毎の削除キー情報を取得します。
	 * @return
	 */
	public LinkedHashMap<String, ArrayList<LinkedHashMap<String, String>>> getTableDeleteInfoMap() {
		return tableDeleteInfoMap;
	}

	/**
	 * 入力ファイル毎の削除キー情報を設定します。
	 * @param inFileName 入力ファイル名
	 * @param deleteMap キー情報を格納したLinkedHashMap
	 */
	public void setTableDeleteInfoMap(String inFileName, LinkedHashMap<String, String> deleteMap) {
		if (null == this.tableDeleteInfoMap)
		{
			this.tableDeleteInfoMap = new LinkedHashMap<String, ArrayList<LinkedHashMap<String,String>>>();
		}
		
		if (this.tableDeleteInfoMap.containsKey(inFileName))
		{
			if (!this.tableDeleteInfoMap.get(inFileName).contains(deleteMap))
			{
				this.tableDeleteInfoMap.get(inFileName).add(deleteMap);
			}
		}
		else
		{
			ArrayList<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String, String>>();
			list.add(deleteMap);
			
			this.tableDeleteInfoMap.put(inFileName, list);
		}
	}

	/**
	 * ツール起動時間を取得します。
	 * @return ツール起動時間
	 */
	public Integer getStartUpTime() {
		return startUpTime;
	}

	/**
	 * ツール起動時間を設定します。
	 * @param startUpTime ツール起動時間
	 */
	public void setStartUpTime(Integer startUpTime) {
		this.startUpTime = startUpTime;
	}

	/**
	 * ツール開始時間を取得します。
	 * @return ツール起動開始時間
	 */
	public Long getStartTimeMills() {
		return startTimeMills;
	}

	/**
	 * ツール開始時間を設定します。
	 * @param startTimeMills ツール開始時間
	 */
	public void setStartTimeMills(Long startTimeMills) {
		this.startTimeMills = startTimeMills;
	}
	
	/**
	 * 入力ファイルを最終までスキップします。
	 * @throws IOException
	 * @throws SQLException
	 */
	public void skip() throws IOException, SQLException
	{
		JBSbatInputFileUtil inFile = super.commonItem.getInPutFile();
		
		if (null == inFile)
		{
			return;
		}
		
		if (inFile.ready())
		{
			while(true)
			{
				String line = inFile.readLine();
				if(line == null)
				{
					break;
				}
			}
		}
	}
	
	/**
	 * シェル起動時に指定した時間を超過した場合のログを出力します。
	 */
	public void printTimeOverLog()
	{
		this.logPrint.printDebugLog("起動時間が指定時間を超過した為、処理を終了します。");
	}
	
	/**
	 * FreeItemのチェックを行います。
	 * @return 判定結果
	 */
	public boolean checkFreeItem()
	{
		String freeItem = commonItem.getFreeItem();
		
		if (null == freeItem || "".equals(freeItem))
		{
			super.logPrint.printDebugLog("引数に起動時間(分)が設定されていません。");
			return false;
		}
		
		try
		{
			Integer.parseInt(freeItem);
		}
		catch (NumberFormatException nfe)
		{
			super.logPrint.printDebugLog("引数の起動時間(分)が不正です。 " + freeItem);
			return false;
		
		}
		
		return true;
	}
	
	/**
	 * ツール起動時間を超えているかどうか判定します。
	 * @return 判定結果
	 */
	public boolean isTimeOver()
	{
		// 現在時間を取得
		long currentTimeMills = System.currentTimeMillis();
		
		// 分に変換
		Long min = ((currentTimeMills - getStartTimeMills()) / 1000) / 60;
		
		int iMin = min.intValue();
		
		// 起動時間との比較を返却
		return ( iMin > getStartUpTime() );
	}

	
	/**
	 * 物理削除のログを出力します。
	 * @param tableName 物理削除対象テーブル
	 * @param svcKeiNo サービス契約番号
	 * @param geneAddDtm 世代年月日時分秒
	 * @param delCount 削除件数
	 */
	public void printDeleteLog(String tableName, LinkedHashMap<String, String> deleteMap, int delCount, String inFileName)
	{		
		
		Iterator<String> ite = deleteMap.keySet().iterator();
		
		StringBuffer logBuf = new StringBuffer();
		
		logBuf.append(tableName);
		logBuf.append(" 削除件数：");
		logBuf.append(delCount);
		logBuf.append("件 ");
		
		logBuf.append("【 ");
		
		while(ite.hasNext())
		{
			String key = ite.next();
			
			logBuf.append(key);
			logBuf.append(":");
			logBuf.append(deleteMap.get(key));
			logBuf.append(" ");
		}
		
		logBuf.append("】");
		
		if (0 < delCount)
		{
			// テーブル毎の削除件数を設定
			this.setTableDeleteCountMap(tableName, delCount);
		
			// テーブル毎の削除キー情報を設定
			this.setTableDeleteInfoMap(inFileName, deleteMap);
		}
		
		super.logPrint.printDebugLog(logBuf.toString());
	}
	
	/**
	 * 物理削除のログを出力します。
	 */
	public void printTotalDeleteLog()
	{
		if (null == this.tableDeleteCountMap)
		{
			return;
		}
		
		Iterator<String> ite = this.tableDeleteCountMap.keySet().iterator();
		
		StringBuffer logBuf = new StringBuffer();
		
		logBuf.append("削除結果：");
		
		Integer totalDelCount = 0;
		while(ite.hasNext())
		{
			String key = ite.next();
			
			logBuf.append(key);
			logBuf.append(":");
			logBuf.append(this.tableDeleteCountMap.get(key));
			logBuf.append("件 ");
			
			totalDelCount += this.tableDeleteCountMap.get(key);
		}
		
		logBuf.append("合計：");
		logBuf.append(totalDelCount);
		logBuf.append("件");
		
		super.logPrint.printDebugLog(logBuf.toString());
	}
	
	/**
	 * 入力ファイルのチェックを行います。
	 * @param path 入力ファイルパス
	 * @return 判定結果
	 */
	public boolean checkInFile(String path)
	{
		
		if (null == path)
		{
			return false;
		}
		
		File f = new File(path);
		
		if (!f.exists() || !f.isFile())
		{
			return false;
		}
		
		String fileName = f.getName();
		if (9 > fileName.length())
		{
			return false;
		}
		
		if (!"KKIFM6160".equals(fileName.substring(0, 9)))
		{
			return false;
		}
		
		return true;
	}
	
	/**
	 * 削除したキー情報を除いた入力ファイルを作成します。
	 * @param filePath 入力ファイルパス
	 * @throws IOException
	 */
	abstract void createNewInFile(String filePath) throws IOException;
}
