package com.eactive.eai.agent.messagekey;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.messagekey.MessageKeyManager;
import com.eactive.eai.common.util.Logger;

public class RemoveMessageKeyCommand extends Command {
	private static final long serialVersionUID = 1L;

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

	public Object execute() throws CommandException {
		Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
		if (logger.isInfo())
			logger.info(this.name + " is executed");

		if (!(args instanceof String)) {
			String rspErrorCode = "RECEAIMCM001";
			String msg = makeException(rspErrorCode, null);
			if (logger.isError())
				logger.error(msg);
			throw new CommandException(msg);
		}

		try {
			String keyName = null;
			MessageKeyManager manager = MessageKeyManager.getInstance();

			keyName = (String) args;
			if (keyName != null) {
				String[] keys = keyName.split(",");
				String keyCode = keys[0] + keys[1];
				manager.removeMessageKey(keyCode);
			}
		} catch (Exception e) {
			String rspErrorCode = "RECEAIMCM002";
			String msg = makeException(rspErrorCode, e);
			if (logger.isError())
				logger.error(msg, e);
			throw new CommandException(msg);
		}
		return "success";

	}

//    public static void main(String[] args)throws Exception{
//        String selectedName = null;
//        String updateData   = null;
//             
//        if(args.length ==1)
//            selectedName = args[0];
//            
//         if(selectedName == null || selectedName.length() ==0){
//            selectedName = "HttpAdapter_STD_IN_TEST";
//        }
//        
//        MessageKeyManager manager = MessageKeyManager.getInstance();
//        MessageKeyVO vo = new MessageKeyVO(selectedName);
//        
//        if(vo != null){
//            System.out.println("RemoveMessageKeyCommand ] vo is not null.... remove start");
//            
//            Command command = new RemoveMessageKeyCommand();
//            command.setArgs(vo);
//            AgentUtil.broadcast(command);
//        }
//        else{
//            System.out.println("RemoveMessageKeyCommand ] main() vo is null");
//        }
//    }
}
