package Juno2;
import javax.servlet.http.HttpServletRequest;

public class CmdEventFactory implements RequestToEventFactory {

	public WebEvent extractEvent(HttpServletRequest request) throws Exception {
		String command = request.getParameter("cmd");
		// System.out.println("Parameter extractEvent for command: " + command);
		CmdVO info = new CmdVO();

		if (command.startsWith("login")) {
			info.setUserId(request.getParameter("username"));
			info.setUserPassword(request.getParameter("password"));

			////System.out.println(
			////	"Extract event using username="
			////		+ request.getParameter("username")
			////		+ " password="
			////		+ request.getParameter("password")
			////		+ " from REQUEST.");

			return new CmdCreateEvent(info);
		} else
			if (command.startsWith("acct")) {
				info.setUserId((String) request.getSession().getAttribute("userId"));
				info.setUserPassword(
					(String) request.getSession().getAttribute("userPassword"));

				// if we put this back it should use session and not parameter
				// System.out.println("Extract event using username=" + request.getParameter("username") +
				// " password=" + request.getParameter("password") + " from SESSION.");

				return new CmdCreateEvent(info);
			} else
				if (command.startsWith("pos")) {
					info.setUserId((String) request.getSession().getAttribute("userId"));
					info.setUserPassword(
						(String) request.getSession().getAttribute("userPassword"));

					info.setAccountId(request.getParameter("acct"));
					////System.out.println("AccountId set to: " + info.getAccountId());

					return new CmdCreateEvent(info);
				} else
					if (command.startsWith("sell")) {
						info.setUserId((String) request.getSession().getAttribute("userId"));
						info.setUserPassword(
							(String) request.getSession().getAttribute("userPassword"));

						info.setAccountId(request.getParameter("acct"));
						info.setSymbol(request.getParameter("sym"));

						////System.out.println(
						////	"SELL account="
						////		+ request.getParameter("acct")
						////		+ " position="
						////		+ request.getParameter("sym"));
						// System.out.println("Splat Sell CmdVO: " + info);

						return new CmdCreateEvent(info);
					} else {
						////throw Exception("Unknown command: " + command);
						return null;
					}
	}
}