/*
 
Copyright (C) 2009 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.conf.monitor;

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

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

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

import com.clustercontrol.monitor.run.bean.MonitorInfo;
import com.clustercontrol.performance.bean.CollectorItemInfo;
import com.clustercontrol.performance.monitor.bean.MonitorPerfCheckInfo;
import com.clustercontrol.performance.monitor.ejb.session.MonitorPerfController;

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

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

    public ExportMonitorPerformance() {
		super(ExportMonitorNumeric.INPUT_VERIFICATION_REAL_NUMBER);
	}

	/**
     * リソース監視情報をExportする。<br>
     */
    public void exportPerformance() {
    	log.info(Messages.getMsg("Resource.Start", new String[]{Messages.getMsg("ExportTool.Export")}));
    	
    	//CSV出力配列を用意
    	ArrayList<ArrayList<String>> csvOutput = new ArrayList<ArrayList<String>>();
    	
    	//監視項目リスト取得
    	ArrayList idList = getPerformanceList();
    	
    	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("MonitorID", args));
				
				MonitorInfo info = getPerformance((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("Export.Dir") + "/RESOURCE_" + format.format(new Date()) + ".csv", 
				csvOutput);
		
		log.info(Messages.getMsg("Resource.End", new String[]{Messages.getMsg("ExportTool.Export")}));
    }
    
    /**
	 * リソース監視情報をExportする。<br>
	 * 
	 * @param monitorInfo プロセス監視情報
	 * @return CSVフォーマット文字列
	 */
    @Override
	public ArrayList<String> createMonitorInfo(MonitorInfo monitorInfo) {
		ArrayList<String> csvFormat = new ArrayList<String>();
		csvFormat.add("");
		
        // 性能監視情報
        MonitorPerfCheckInfo perfCheckInfo = (MonitorPerfCheckInfo)monitorInfo.getCheckInfo();

        // 監視対象の情報が設定されていない場合はなにもしない
        if(perfCheckInfo != null){
        	List<CollectorItemInfo> itemInfoList = 
        		CollectorItemCodeFactory.getAvailableCollectorItemList(monitorInfo.getFacilityId());
        	
        	Iterator<CollectorItemInfo> itr = itemInfoList.iterator();
        	while(itr.hasNext()){
        		CollectorItemInfo itemInfo = itr.next();
        		
        		if(itemInfo.getItemCode().equals(perfCheckInfo.getItemCode()) &&
        				itemInfo.getDisplayName().equals(perfCheckInfo.getDisplayName())){
        			String itemName = CollectorItemCodeFactory.getFullItemName(itemInfo);
        			csvFormat.set(0, itemName);
        			break;
        		}
        	}
        }

		return csvFormat;
	}

    /**
     * リソース監視情報を取得する。<br>
     * 
     * @param monitorId 取得対象の監視項目ID
     * @return リソース監視情報
     */
    protected MonitorInfo getPerformance(String monitorId) {

    	MonitorPerfController perf = EjbConnectionManager.getConnectionManager().getMonitorPerfController();

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

    	MonitorPerfController perf = EjbConnectionManager.getConnectionManager().getMonitorPerfController();

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