/*

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.importtool.conf.maintenance;

import java.util.ArrayList;
import java.util.Iterator;

import jp.co.intellilink.hinemos.util.EjbConnectionManager;
import jp.co.intellilink.hinemos.util.Messages;

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

import com.clustercontrol.maintenance.ejb.session.MaintenanceController;

/**
 * メンテナンス機能情報を削除するクラス<br>
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
public class DeleteMaintenance {

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

	/**
	 * メンテナンス監視情報を削除する<br>
	 */
	@SuppressWarnings("unchecked")
	public void delete() {
		log.info(Messages.getMsg("Maintenance.Start", new String[]{Messages.getMsg("ImportTool.Delete")}));

		//監視項目リスト取得
		ArrayList idList = getMaintenanceList();

		if(idList instanceof ArrayList){
			Iterator itr = idList.iterator();
			while(itr.hasNext()){
				ArrayList line = (ArrayList)itr.next();

				deleteMaintenanceInfo((String)line.get(0));
			}
		}

		log.info(Messages.getMsg("Maintenance.End", new String[]{Messages.getMsg("ImportTool.Delete")}));
	}

	/**
	 * メンテナンス情報を削除する。<br>
	 * 
	 * @param maintenanceId メンテナンスID
	 */
	protected void deleteMaintenanceInfo(String maintenanceId) {

		MaintenanceController maintenance = EjbConnectionManager.getConnectionManager().getMaintenanceController();

		try {
			Object[] args = {maintenanceId};
			log.info(Messages.getMsg("MaintenanceID", args) + 
					" (" + Messages.getMsg("delete") + ")");

			maintenance.deleteMaintenance(maintenanceId);
			maintenance.deleteSchedule(maintenanceId);
		} catch (Exception e) {
			log.error(Messages.getMsg("ConnectManagerFailed"), e);
			System.exit(14);
		}
	}

	/**
	 * メンテナンスリストを取得する。<br>
	 * 
	 * @return メンテナンス一覧
	 */
	@SuppressWarnings("unchecked")
	protected ArrayList getMaintenanceList() {

		MaintenanceController maintenance = EjbConnectionManager.getConnectionManager().getMaintenanceController();

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