package com.eactive.eai.agent.transformer;

import java.util.Vector;

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.transformer.layout.LayoutManager;

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

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

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

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

		try {
			LayoutManager manager = LayoutManager.getManager();

			Vector layoutNameVt = (Vector) args;
			String layoutName = "";
			for (int i = 0; i < layoutNameVt.size(); i++) {
				layoutName = (String) layoutNameVt.get(i);

				if (layoutName != null) {
					manager.reload(layoutName);
					manager.permanentReload(layoutName);
					if (logger.isWarn())
						logger.warn("ReloadLayoutListCommand ] " + layoutName + " Layout info Reload.");
				}
			}
		} 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();
//}

// private static void test() throws Exception {
//        LayoutManager manager = LayoutManager.getManager();
//        Vector vt = new Vector();
//        Command command = new ReloadLayoutListCommand();
//        command.setArgs(vt);
//        AgentUtil.broadcast(command);
//        
//    }
}
