package com.eactive.eai.agent.rule.telegraminfo;

import java.util.HashMap;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.batch.rule.telegraminfo.TelegramInfoDAO;
import com.eactive.eai.batch.rule.telegraminfo.TelegramInfoManager;
import com.eactive.eai.batch.rule.telegraminfo.TelegramInfoVO;
import com.eactive.eai.common.dao.DAOFactory;
import com.eactive.eai.common.util.Logger;

public class AddTelegramInfoCommand extends Command
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public AddTelegramInfoCommand() {
        super("AddTelegramInfoCommand");
    }


    public Object execute() throws CommandException
    {
        Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
        logger.info(this.name + " Agent 가 호출되었습니다.");

        if ( args == null || !(args instanceof String) ) {
            String msg = makeException("BECEAICAG001", null);
            logger.error(this.name+" "+ msg);
            throw new CommandException(msg);
        }

        try {
            String strRuleCode = (String)args;
            logger.info(this.name +": (Agent Argument) RuleCode=["+strRuleCode+"]");

            TelegramInfoDAO dao = (TelegramInfoDAO)DAOFactory.newInstance().create(TelegramInfoDAO.class);
            HashMap<String, TelegramInfoVO> map = dao.getAllTelegramInfos();
            TelegramInfoVO  vo  = map.get(strRuleCode);
            if (vo == null) {
                String errMsg = this.name + ":  error >> RuleCode [" + strRuleCode + "] not found in DBMS";
                logger.error(errMsg);
                throw new CommandException(errMsg);
            }
            TelegramInfoManager.getInstance().setRuleCodeArray(strRuleCode, vo);

			logger.info(this.name + " Agent 가 성공적으로 완료되었습니다.");
            return "success";

        } catch (Exception ex) {
            String msg = makeException("BECEAICAG002", ex);
            logger.error(msg, ex);
            throw new CommandException(msg);
        }
    }
}
