/**
   * Retrieve the current portfolio of stock holdings for the given trader Dispatch to the Trade
   * Portfolio JSP for display
   *
   * @param userID The User requesting to view their portfolio
   * @param ctx the servlet context
   * @param req the HttpRequest object
   * @param resp the HttpResponse object
   * @param results A short description of the results/success of this web request provided on the
   *     web page
   * @exception javax.servlet.ServletException If a servlet specific exception is encountered
   * @exception javax.io.IOException If an exception occurs while writing results back to the user
   */
  void doPortfolio(
      ServletContext ctx,
      HttpServletRequest req,
      HttpServletResponse resp,
      String userID,
      String results)
      throws ServletException, IOException {

    try {
      // Get the holdiings for this user

      Collection quoteDataBeans = new ArrayList();
      Collection holdingDataBeans = tAction.getHoldings(userID);

      // Walk through the collection of user
      //  holdings and creating a list of quotes
      if (holdingDataBeans.size() > 0) {

        Iterator it = holdingDataBeans.iterator();
        while (it.hasNext()) {
          HoldingDataBean holdingData = (HoldingDataBean) it.next();
          QuoteDataBean quoteData = tAction.getQuote(holdingData.getQuoteID());
          quoteDataBeans.add(quoteData);
        }
      } else {
        results = results + ".  Your portfolio is empty.";
      }
      req.setAttribute("results", results);
      req.setAttribute("holdingDataBeans", holdingDataBeans);
      req.setAttribute("quoteDataBeans", quoteDataBeans);
      requestDispatch(ctx, req, resp, userID, TradeConfig.getPage(TradeConfig.PORTFOLIO_PAGE));
    } catch (java.lang.IllegalArgumentException e) { // this is a user error so I will
      // forward them to another page rather than throw a 500
      req.setAttribute("results", results + "illegal argument:" + e.getMessage());
      requestDispatch(ctx, req, resp, userID, TradeConfig.getPage(TradeConfig.PORTFOLIO_PAGE));
      // log the exception with an error level of 3 which means, handled exception but would
      // invalidate a automation run
      Log.error(
          e,
          "TradeServletAction.doPortfolio(...)",
          "illegal argument, information should be in exception string",
          "user error");
    } catch (Exception e) {
      // log the exception with error page
      throw new ServletException(
          "TradeServletAction.doPortfolio(...)" + " exception user =" + userID, e);
    }
  }
  /**
   * Logout a Trade User Dispatch to the Trade Welcome JSP for display
   *
   * @param userID The User to logout
   * @param ctx the servlet context
   * @param req the HttpRequest object
   * @param resp the HttpResponse object
   * @param results A short description of the results/success of this web request provided on the
   *     web page
   * @exception javax.servlet.ServletException If a servlet specific exception is encountered
   * @exception javax.io.IOException If an exception occurs while writing results back to the user
   */
  void doLogout(ServletContext ctx, HttpServletRequest req, HttpServletResponse resp, String userID)
      throws ServletException, IOException {
    String results = "";

    try {
      tAction.logout(userID);

    } catch (java.lang.IllegalArgumentException e) { // this is a user error so I will
      // forward them to another page, at the end of the page.
      req.setAttribute("results", results + "illegal argument:" + e.getMessage());

      // log the exception with an error level of 3 which means, handled exception but would
      // invalidate a automation run
      Log.error(
          e,
          "TradeServletAction.doLogout(...)",
          "illegal argument, information should be in exception string",
          "treating this as a user error and forwarding on to a new page");
    } catch (Exception e) {
      // log the exception and foward to a error page
      Log.error(
          e,
          "TradeServletAction.doLogout(...):",
          "Error logging out" + userID,
          "fowarding to an error page");
      // set the status_code to 500
      throw new ServletException(
          "TradeServletAction.doLogout(...)" + "exception logging out user " + userID, e);
    }
    HttpSession session = req.getSession();
    if (session != null) {
      session.invalidate();
    }

    Object o = req.getAttribute("TSS-RecreateSessionInLogout");
    if (o != null && ((Boolean) o).equals(Boolean.TRUE)) {
      // Recreate Session object before writing output to the response
      // Once the response headers are written back to the client the opportunity
      // to create a new session in this request may be lost
      // This is to handle only the TradeScenarioServlet case
      session = req.getSession(true);
    }
    requestDispatch(ctx, req, resp, userID, TradeConfig.getPage(TradeConfig.WELCOME_PAGE));
  }
  /**
   * Login a Trade User. Dispatch to the Trade Home JSP for display
   *
   * @param userID The User to login
   * @param passwd The password supplied by the trader used to authenticate
   * @param ctx the servlet context
   * @param req the HttpRequest object
   * @param resp the HttpResponse object
   * @param results A short description of the results/success of this web request provided on the
   *     web page
   * @exception javax.servlet.ServletException If a servlet specific exception is encountered
   * @exception javax.io.IOException If an exception occurs while writing results back to the user
   */
  void doLogin(
      ServletContext ctx,
      HttpServletRequest req,
      HttpServletResponse resp,
      String userID,
      String passwd)
      throws javax.servlet.ServletException, java.io.IOException {
    System.out.println("Login userID: " + userID);
    String results = "";
    try {
      // Got a valid userID and passwd, attempt login

      AccountDataBean accountData = tAction.login(userID, passwd);

      if (accountData != null) {
        HttpSession session = req.getSession(true);
        session.setAttribute("uidBean", userID);
        session.setAttribute("sessionCreationDate", new java.util.Date());
        if (("true").equals(req.getParameter("stress"))) {
          // fib(35);
          char[] s = new char[10 * 1024 * 1000];
          String str = String.copyValueOf(s);
          session.setAttribute("someobject", str);
        }
        results = "Ready to Trade";
        doHome(ctx, req, resp, userID, results);
        return;
      } else {
        req.setAttribute("results", results + "\nCould not find account for + " + userID);
        // log the exception with an error level of 3 which means, handled exception but would
        // invalidate a automation run
        Log.log(
            "TradeServletAction.doLogin(...)",
            "Error finding account for user " + userID + "",
            "user entered a bad username or the database is not populated");
      }
    } catch (java.lang.IllegalArgumentException e) { // this is a user error so I will
      // forward them to another page rather than throw a 500
      req.setAttribute("results", results + "illegal argument:" + e.getMessage());
      // log the exception with an error level of 3 which means, handled exception but would
      // invalidate a automation run
      Log.error(
          e,
          "TradeServletAction.doLogin(...)",
          "illegal argument, information should be in exception string",
          "treating this as a user error and forwarding on to a new page");

    } catch (Exception e) {
      doWelcome(
          ctx,
          req,
          resp,
          "User not found! Is the database <a href = 'config?action=buildDB'>populated </a>?");
      return;
      /*			throw new ServletException(
      "TradeServletAction.doLogin(...)"
      	+ "Exception logging in user "
      	+ userID
      	+ "with password"
      	+ passwd
      	,e);
      	*/

    }

    requestDispatch(ctx, req, resp, userID, TradeConfig.getPage(TradeConfig.WELCOME_PAGE));
  }