usermanagementhelper.java

来自「Oracle的J2EE Sample」· Java 代码 · 共 1,235 行 · 第 1/4 页

JAVA
1,235
字号
    // Value Object and Store it in HashMap
    hm.put("PREFERENCESINFO", getPreferencesInfo(accountNo));

    // Get a list of alerts as a collection of AlertsInfo Value Object
    // and Store it in HashMap
    hm.put("ALERTSINFO", getAlertsInfo(accountNo));

    return hm;
  }

  /**
   * Method to populate a list of Preferences (PreferencesInfo )for a
   * particular user. After user logged in successfully, his/her
   * Account Number is set as LOGIN.RESPONSE attribute.
   *
   * @param req  Servlet Request Object
   * @return A HashMap data structure containing collection namely
   *         (a) Collection of PreferencesInfo Value Object
   * @exception UserManagementEventException Exception raised
   *          when getting Preferences Information
   * @since   1.0
   */
  public HashMap populatePreferences(HttpServletRequest req)
      throws UserManagementEventException {

    HashMap hm = new HashMap();

    Integer acctNo = (Integer) req.getSession().getAttribute("LOGIN.RESPONSE");

    // Get a list of preferences as a collection of PreferencesInfo
    // Value Object and Store it in HashMap
    hm.put("PREFERENCESINFO", getPreferencesInfo(acctNo));

    return hm;
  }

  /**
   * Method to populate a list of Alerts (AlertsInfo )for a particular
   * user. After user logged in successfully, his/her account number is set as
   * LOGIN.RESPONSE attribute.
   *
   * @param req  Servlet Request Object
   * @return A HashMap data structure containing collection namely
   *         (a) Collection of AlertsInfo Value Object
   * @exception UserManagementEventException Exception raised when
   *          getting Alerts Information
   * @since   1.0
   */
  public HashMap populateAlerts(HttpServletRequest req)
      throws UserManagementEventException {

    HashMap hm = new HashMap();

    Integer accountNo =
      (Integer) req.getSession().getAttribute("LOGIN.RESPONSE");

    // Get a list of alerts as a collection of AlertsInfo Value Object
    // and Store it in HashMap
    hm.put("ALERTSINFO", getAlertsInfo(accountNo));

    return hm;
  }

  /**
   * Method to populate a list of Stock News of User's Preferences.
   * Note that the User preference contains Stock Symbol of the company
   * in which he/she is interested in, and preferenceType. PreferenceType
   * can have following 3 Values
   *  'N' = News (User is only interested in Stock News)
   *  'S' = Stock Prices (User is only interested in Stock Prices)
   *  'B' = News and Stock Prices
   *        (User is interested in both News and Stock Prices)
   *
   * @param req  Servlet Request Object
   * @exception UserManagementEventException Exception raised
   *    when getting a NewsDAO
   * @since   1.0
   */
  public void populateNews(HttpServletRequest req)
      throws UserManagementEventException {

    // Initialize the Data Access Object Implementation which Connects
    // to appropriate Database and fetches the News
    NewsHelper newsHelper = new NewsHelper();
    Collection allNews    = new ArrayList();
    List       allSymbols = new ArrayList();

    // Get the User's Preferences
    HashMap hm = (HashMap) req.getSession().getAttribute("PREFS.RESPONSE");
    Collection prefCollection = (Collection) hm.get("PREFERENCESINFO");

    // Loop through all the Preferences for this User and check whether
    // the preference type is either "N" (for News) or "B" (for Both News and
    // Stock Prices)
    Iterator prefIter = prefCollection.iterator();

    while (prefIter.hasNext()) {

      PreferencesInfo pInfo  = (PreferencesInfo) prefIter.next();
      String          symbol = pInfo.getSymbol();

      // Check whether preferenceType is either "N" or "B"
      if ((pInfo.getPreferenceType().indexOf("N") != -1)
              || (pInfo.getPreferenceType().indexOf("B") != -1)) {

        // Add the Symbol to the final list of symbols
        allSymbols.add(symbol);
      }
    }

    // Fetch the News for all the Symbols
    allNews.addAll ( newsHelper.getLatestNews( (String[])allSymbols.toArray(new String[0]) ) );

    // Set the News as attribute of HTTP Session
    req.getSession().setAttribute("NEWS.RESPONSE", allNews);

  }

  /**
   * Method to populate a list of Stock Rates of User's Preferences. Note that
   * the User preference contains Stock Symbol of the company in which he/she
   * is interested in, and preferenceType. PreferenceType can have following
   * 3 Values
   *  'N' = News (User is only interested in Stock News)
   *  'S' = Stock Prices (User is only interested in Stock Prices)
   *  'B' = News and Stock Prices
   *        (User is interested in both News and Stock Prices)
   *
   * @param req  Servlet Request Object
   * @exception TradeManagementEventException Exception raised when getting
   *            a new StockRateDAO Object
   * @since   1.0
   */
  public void populateStockRates(HttpServletRequest req)
      throws TradeManagementEventException {

    // Initialize the Data Access Object Implementation which Connects
    // to appropriate Database and fetches the News
    StockRateHelper srHelper = new StockRateHelper();
    Collection   allRates   = new ArrayList();
    List         allSymbols = new ArrayList();

    // Get the User's Preferences
    HashMap hm = (HashMap) req.getSession().getAttribute("PREFS.RESPONSE");
    Collection prefCollection = (Collection) hm.get("PREFERENCESINFO");

    // Loop through all the Preferences for this User and check whether
    // the preference type is either "S" (for Stock Prices) or "B" (for Both
    // News and Stock Prices)
    Iterator prefIter = prefCollection.iterator();

    while (prefIter.hasNext()) {

      PreferencesInfo pInfo  = (PreferencesInfo) prefIter.next();
      String          symbol = pInfo.getSymbol();

      // Check whether preferenceType is either "S" or "B"
      if ((pInfo.getPreferenceType().indexOf("S") != -1)
              || (pInfo.getPreferenceType().indexOf("B") != -1)) {

        // Add the symbol to the final list of symbols
        allSymbols.add(symbol);
      }
    }

    // Fetch the Stock Rates for the Symbols
    allRates.addAll(srHelper.getLatestRate( (String[])allSymbols.toArray(new String[0]) ));

    // Set the Stock Rates as attribute of HTTP Session
    req.getSession().setAttribute("RATES.RESPONSE", allRates);
  }

  /**
   * A Single Method to populate both Stock News and Stock Rates.
   * This method in turn invokes individual methods to populate
   * Stock News and Stock Rates
   *
   * @param req Servlet Request Object
   * @exception UserManagementEventException Exception raised when
   *       populating News
   * @exception TradeManagementEventException Exception raised when
   *       populating Stock Rates
   * @since   1.0
   */
  public void populateNewsAndRates(HttpServletRequest req)
      throws UserManagementEventException, TradeManagementEventException {

    // Invoke Method to populate Stock News
    populateNews(req);

    // Invoke Method to populate Stock Prices/Rates
    populateStockRates(req);
  }

  /**
   * This method gets the User Account details corresponding to the input Email.
   * Formats the details as a HTML string and send an email to the user.
   *
   * @return String indicating whether sending email was successful
   * @exception UserManagementEventException
   * @since   1.0
   */
  public String sendEmail(HttpServletRequest req)
      throws UserManagementEventException {

    String    returnMessage = "User Account corresponding to the input " +
                              "Email address does not exist in the system";
    Hashtable userHash      = new Hashtable();

    try {

      String email = req.getParameter("EMAIL");

      // Access the EJB-QL method in TradeManagementSessionFacadeBean to get
      // the User Account Details corresponding to the input Email Address
      userHash = rmr.getUserAccount(email);

      Integer accountNumber = (Integer) userHash.get("ACCOUNTNUMBER");

      if (accountNumber != null) {

        // Format the HTML message
        String message = getHTMLMsg(accountNumber,
                                    (String) userHash.get("PASSWORD"),
                                    (String) userHash.get("FIRSTNAME"),
                                    (String) userHash.get("LASTNAME"));

        // Send the Message to the Email id of the User
        sendMessage(email, message);

        returnMessage = "Email has been sent to the entered Email Address " +
                        "with corresponding User Account Details";
      }
    } catch (RemoteException re) {  // Trap Remote Errors
      throw new UserManagementEventException(
        "Remote Exception while getting user account info : " + re.toString());
    } catch (Exception ex) {        // Trap Other Errors
      throw new UserManagementEventException(
        "Exception while getting user account info : " + ex.toString());
    }
    return returnMessage;
  }

  /**
   * Constructs the mail body using HTML tags.
   *
   * @param accountNumber Account number
   * @param password      Password
   * @param firstName     First Name
   * @param lastName      Last Name
   * @return mail body in HTML
   * @since   1.0
   */
  private String getHTMLMsg(Integer accountNumber, String password,
                            String firstName, String lastName) {

    StringBuffer sb = new StringBuffer();

    sb.append("<p>Hello " + firstName + " " + lastName + ",<br></p>");
    sb.append("<p>Please find herewith the Account Number and Password ");
    sb.append("to access OTN Financial Brokerage System<br>for Viewing ");
    sb.append("your Stock Information / Trading.<br>");
    sb.append("<br>Account Number : " + accountNumber + "<br>Password : ");
    sb.append( password + "<br></p>");
    sb.append("<p>Note : Please change the password once you login! </p>");
    sb.append("<br>Happy Trading !! <br> FBS Team.");

    return sb.toString();

  }

  /**
   * Sends the email reminder message to the input email.
   *
   * @param email   Email id input by the User
   * @param message Formatted HTML message string
   * @exception UserManagementEventException
   * @since   1.0
   */
  private void sendMessage(String email, String message)
      throws UserManagementEventException {

    try {

      InitialContext ctx = new InitialContext();

      // Lookup for the mailservice bean in the JNDI tree
      MailServiceHome mailServiceHome =
        (MailServiceHome) ctx.lookup("MailService");

      // Get an instance of  mailservice
      MailService mailService = mailServiceHome.create();

      // Send Mail to the input email address with the input message
      mailService.sendMail(email, "reminder@fbs.com",
                           "OTN FBS : AccountNumber/Password Reminder",
                           message);

    } catch (Exception ex) {  // Trap Other Errors
      throw new UserManagementEventException("Exception while sending mail : "
                                             + ex.toString());
    }
  }



}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?