/*
 
Copyright (C) 2008 NTT DATA INTELLILINK CORPORATION
 
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License 
as published by the Free Software Foundation, version 2.
 
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE.  See the GNU General Public License for more details.
 
*/

package jp.co.intellilink.hinemos.export.logtransfer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;

import jp.co.intellilink.hinemos.export.util.Config;
import jp.co.intellilink.hinemos.export.util.StringListComparator;
import jp.co.intellilink.hinemos.export.util.EjbConnectionManager;
import jp.co.intellilink.hinemos.export.util.Messages;
import jp.co.intellilink.hinemos.export.util.WriteCsvFile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.bean.ValidConstant;
import com.clustercontrol.bean.YesNoConstant;
import com.clustercontrol.logtransfer.bean.LogTransferFileInfo;
import com.clustercontrol.logtransfer.bean.LogTransferInfo;
import com.clustercontrol.logtransfer.ejb.session.LogTransferController;

/**
 * 通知情報を取得するクラス<br>
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
public class ExportLogTransfer {

	// ログ出力
	private static Log log = LogFactory.getLog(ExportLogTransfer.class);
	
    /**
     * ログ転送情報をExportする。<br>
     */
    public void exportLogTransfer() {
    	log.info(Messages.getMsg("ExportTool.LogTransfer.Start"));
    	
    	//CSV出力配列を用意
    	ArrayList<ArrayList<String>> csvOutput = new ArrayList<ArrayList<String>>();
    	
    	//通知IDリスト取得
    	ArrayList idList = getLogTransferIdList();
    	
    	if(idList instanceof ArrayList){
			Iterator itr = idList.iterator();
			while(itr.hasNext()){
				ArrayList line = (ArrayList)itr.next();
				
				Object[] args = {(String)line.get(0)};
				log.info(Messages.getMsg("ExportTool.TransferID", args));
				
				LogTransferInfo info = getLogTransfer((String)line.get(0));
				csvOutput.add(createCsvFormat(info));
			}
			
			//転送IDでソート
			Collections.sort(csvOutput, new StringListComparator(0));
    	}
    	
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
		
		WriteCsvFile.writeCsvFile(
				Config.getConfig("Output.Dir") + "/LOGTRANSFER_" + format.format(new Date()) + ".csv", 
				csvOutput);
		
		log.info(Messages.getMsg("ExportTool.LogTransfer.End"));
    }
    
    /**
	 * ログ転送情報をExportする。<br>
	 * 
	 * @param logTransferInfo ログ転送情報
	 * @return CSVフォーマット文字列
	 */
	public ArrayList<String> createCsvFormat(LogTransferInfo logTransferInfo) {
		ArrayList<String> csvFormat = new ArrayList<String>();
		
		//転送ID
		csvFormat.add(logTransferInfo.getTransferId());
		//説明
		csvFormat.add(logTransferInfo.getDescription());
		//ファシリティID
		csvFormat.add(logTransferInfo.getFacilityId());
		
		for(LogTransferFileInfo fileInfo : logTransferInfo.getFileInfo()){
			//ファイルパス
			csvFormat.add(fileInfo.getFilePath());
			//動作間隔
			csvFormat.add(String.valueOf(fileInfo.getRunInterval()));
			//最初にファイルをチェック
			csvFormat.add(YesNoConstant.typeToString(fileInfo.getExistenceFlg()));
			//有効・無効
			csvFormat.add(ValidConstant.typeToString(fileInfo.getValid()));
			break;
		}
		
		//有効/無効
		csvFormat.add(ValidConstant.typeToString(logTransferInfo.getValid()));
		
		return csvFormat;
	}
	
    /**
     * ログ転送情報を取得する。<br>
     * 
     * @param transferId 取得対象の転送ID
     * @return ログ転送情報
     */
    protected LogTransferInfo getLogTransfer(String transferId) {

    	LogTransferController transfer = EjbConnectionManager.getConnectionManager().getLogTransferController();

    	LogTransferInfo info = null;
        try {
        	info = transfer.getLogTransfer(transferId);
        } catch (Exception e) {
			log.error(Messages.getMsg("ExportTool.ConnectManagerFailed"), e);
			System.exit(14);
        }
        return info;
    }
    
    /**
     * 転送IDリストを取得する。<br>
     * 
     * @return 転送ID一覧
     */
    protected ArrayList getLogTransferIdList() {

    	LogTransferController transfer = EjbConnectionManager.getConnectionManager().getLogTransferController();

        ArrayList records = null;
        try {
            records = transfer.getLogTransferList();
        } catch (Exception e) {
			log.error(Messages.getMsg("ExportTool.ConnectManagerFailed"), e);
			System.exit(14);
        }
        return records;
    }
}