package com.eactive.eai.agent.adapter;

import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.adapter.wca.util.RuntimeUtil;
import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.util.Logger;

public class RemoveAdapterPropGroupCommand extends Command {

	private static final long serialVersionUID = 1L;

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

	public Object execute() throws CommandException {
		Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
		String SNA_PROP_GROUP_NAME = "SNAAdapter{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 {
			AdapterPropManager manager = AdapterPropManager.getInstance();
			String key = null;

			if (args != null) {
				key = (String) args;
				manager.removeAdapterPropGroupVO(key);

				/**
				 * SNA Adapter notifyAllSoftlink
				 */
				if (SNA_PROP_GROUP_NAME.equals(key)) {
					RuntimeUtil snaUtil = RuntimeUtil.getInstance();
					snaUtil.notifyAllSoftlink();
				}
			}
		} 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 {
//	   test(String args[]);
//}

//private static void test(String args[]) throws Exception {
//        String selectedName = null;
//             
//        if(args.length ==1)
//            selectedName = args[0];
//            
//         if(selectedName == null || selectedName.length() ==0){
//            selectedName = "ALL_TEST";
//        }
//        
//        if(selectedName != null){
////            System.out.println("RemoveAdapterPropGroupCommand ] vo is not null.... remove start");
//            
//            Command command = new RemoveAdapterPropGroupCommand();
//            command.setArgs(selectedName);
//            AgentUtil.broadcast(command);
//        }
//
//    }
}
