/*
 
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.monitor.http;

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.monitor.ExportMonitorNumeric;
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.http.bean.MonitorHttpInfo;
import com.clustercontrol.http.ejb.session.MonitorHttpController;
import com.clustercontrol.monitor.run.bean.MonitorInfo;
import com.clustercontrol.monitor.run.bean.MonitorTypeConstant;

/**
 * HTTP監視情報を取得するクラス<br>
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
public class ExportMonitorHttp extends ExportMonitorNumeric {

	// ログ出力
	private static Log log = LogFactory.getLog(ExportMonitorHttp.class);

    public ExportMonitorHttp() {
		super(ExportMonitorNumeric.INPUT_VERIFICATION_POSITIVE_INTEGER);
	}

	/**
     * HTTP監視情報をExportする。<br>
     */
    public void exportHttp() {
    	log.info(Messages.getMsg("ExportTool.Http.Start"));
    	
    	//CSV出力配列を用意
    	ArrayList<ArrayList<String>> csvOutput = new ArrayList<ArrayList<String>>();
    	
    	//監視項目リスト取得
    	ArrayList idList = getHttpList();
    	
    	if(idList instanceof ArrayList){
			Iterator itr = idList.iterator();
			while(itr.hasNext()){
				ArrayList line = (ArrayList)itr.next();
				
				int monitorType = MonitorTypeConstant.stringToType((String)line.get(3));
				if(monitorType == MonitorTypeConstant.TYPE_NUMERIC){
					Object[] args = {(String)line.get(0)};
					log.info(Messages.getMsg("ExportTool.MonitorID", args));
					
					MonitorInfo info = getHttp((String)line.get(0), monitorType);
					csvOutput.add(createCsvFormat(info));
				}
			}
			
			//監視項目IDでソート
			Collections.sort(csvOutput, new StringListComparator(0));
    	}
    	
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
		
		WriteCsvFile.writeCsvFile(
				Config.getConfig("Output.Dir") + "/HTTP_" + format.format(new Date()) + ".csv", 
				csvOutput);
		
		log.info(Messages.getMsg("ExportTool.Http.End"));
    }
    
	/**
	 * HTTP監視情報をExportする。<br>
	 * 
	 * @param monitorInfo HTTP監視情報
	 * @return CSVフォーマット文字列
	 * 
     * @see jp.co.intellilink.hinemos.export.monitor.ExportMonitorNumeric#createCsvFormat(com.clustercontrol.monitor.run.bean.MonitorInfo)
     */
    @Override
	public ArrayList<String> createCsvFormat(MonitorInfo monitorInfo) {
		ArrayList<String> csvFormat = new ArrayList<String>();
		
		//監視基本情報
		csvFormat.addAll(createMonitorBase(monitorInfo));
		
		//監視条件情報
		csvFormat.addAll(createMonitorRule(monitorInfo));
		
		//監視情報
		csvFormat.addAll(createMonitorInfo(monitorInfo));
		
		//監視判定情報
		csvFormat.addAll(createMonitorJudgement(monitorInfo));
		
		//監視通知情報
		csvFormat.addAll(createMonitorNotify(monitorInfo));

        //アプリケーション
        if (monitorInfo.getApplication() != null)
        	csvFormat.add(monitorInfo.getApplication());
        else
        	csvFormat.add("");
        
        //有効/無効
        csvFormat.add(ValidConstant.typeToString(monitorInfo.getValid()));
		
		return csvFormat;
	}

	/**
	 * HTTP監視情報をExportする。<br>
	 * 
	 * @param monitorInfo HTTP監視情報
	 * @return CSVフォーマット文字列
	 * 
     * @see jp.co.intellilink.hinemos.export.monitor.ExportMonitor#createMonitorInfo(com.clustercontrol.monitor.run.bean.MonitorInfo)
     */
    @Override
	public ArrayList<String> createMonitorInfo(MonitorInfo monitorInfo) {
		ArrayList<String> csvFormat = new ArrayList<String>();
		
        MonitorHttpInfo httpInfo = (MonitorHttpInfo)monitorInfo.getCheckInfo();
        if(httpInfo == null){
        	httpInfo = new MonitorHttpInfo();
        }
        //URL
        if (httpInfo.getRequestUrl() != null)
        	csvFormat.add(httpInfo.getRequestUrl());
        else
        	csvFormat.add("");
        //タイムアウト
        csvFormat.add(String.valueOf(httpInfo.getTimeout()));

		return csvFormat;
	}

    /**
     * HTTP監視情報を取得する。<br>
     * 
    /**
     * @param monitorId 取得対象の監視項目ID
     * @param monitorType 取得対象の監視種別
     * @return HTTP監視情報
     */
    protected MonitorInfo getHttp(String monitorId, int monitorType) {

    	MonitorHttpController http = EjbConnectionManager.getConnectionManager().getMonitorHttpController();

        MonitorInfo info = null;
        try {
        	info = http.getHttpInfo(monitorId, monitorType);
        } catch (Exception e) {
			log.error(Messages.getMsg("ExportTool.ConnectManagerFailed"), e);
			System.exit(14);
        }
        return info;
    }
    
    /**
     * 監視項目リストを取得する。<br>
     * 
     * @return 監視項目一覧
     */
    protected ArrayList getHttpList() {

    	MonitorHttpController http = EjbConnectionManager.getConnectionManager().getMonitorHttpController();

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