/**
   * 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 doSellRandom(
      ServletContext ctx, HttpServletRequest req, HttpServletResponse resp, String userID)
      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();

          this.doSell(ctx, req, resp, userID, holdingData.getHoldingID());
          return;
        }
      } else {

      }

    } catch (java.lang.IllegalArgumentException e) {
    } catch (Exception e) {
    }
  }
  /**
   * 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);
    }
  }
  /**
   * Create the Trade Home page with personalized information such as the traders account balance
   * Dispatch to the Trade Home JSP for display
   *
   * @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 doHome(
      ServletContext ctx,
      HttpServletRequest req,
      HttpServletResponse resp,
      String userID,
      String results)
      throws javax.servlet.ServletException, java.io.IOException {
    BigDecimal balance;
    String result = "";
    try {
      AccountDataBean accountData = tAction.getAccountData(userID);
      Collection holdingDataBeans = tAction.getHoldings(userID);

      // Edge Caching:
      // Getting the MarketSummary has been moved to the JSP
      // MarketSummary.jsp. This makes the MarketSummary a
      // standalone "fragment", and thus is a candidate for
      // Edge caching.
      // marketSummaryData = tAction.getMarketSummary();

      req.setAttribute("accountData", accountData);
      req.setAttribute("holdingDataBeans", holdingDataBeans);
      // See Edge Caching above
      // req.setAttribute("marketSummaryData", marketSummaryData);
      req.setAttribute("results", results);
    } 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 + "check userID = " + userID + " and that the database is populated");
      requestDispatch(ctx, req, resp, userID, TradeConfig.getPage(TradeConfig.HOME_PAGE));
      // log the exception with an error level of 3 which means, handled exception but would
      // invalidate a automation run
      Log.error(
          "TradeServletAction.doHome(...)"
              + "illegal argument, information should be in exception string"
              + "treating this as a user error and forwarding on to a new page",
          e);
    }
    // ALPINE No support for EJB's yet
    /*catch (javax.ejb.FinderException e)
    {
    	//this is a user error so I will
    	//forward them to another page rather than throw a 500
    	req.setAttribute(
    		"results",
    		results + "\nCould not find account for + " + userID);
    	//requestDispatch(ctx, req, resp, TradeConfig.getPage(TradeConfig.HOME_PAGE));
    	//log the exception with an error level of 3 which means, handled exception but would invalidate a automation run
    	Log.error(
    		"TradeServletAction.doHome(...)" +
    		"Error finding account for user " + userID +
    		"treating this as a user error and forwarding on to a new page", e);
    }*/
    catch (Exception e) {
      // log the exception with error page
      throw new ServletException(
          "TradeServletAction.doHome(...)" + " exception user =" + userID, e);
    }

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