⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanagementsessionfacadebean.java

📁 Oracle的J2EE Sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    try {
      // Find the User Account Local Entity Bean by passing the account Number
      // Primary Key
      UserAccountLocal userAccountLocal = 
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Get All the Preferences for the User Account and loop through them
      Iterator prefIter = (userAccountLocal.getPreferences()).iterator();
      while (prefIter.hasNext()) {

        // Get the next handle to PreferencesBean Local EJB Object
        PreferencesLocal pl = (PreferencesLocal) prefIter.next();

        // Check if the passed Symbol is same as the symbol in Preferences Info
        // If not, add to final list.
        if (!pl.getSymbol().equals(symbol)) {
          allPreferences.add(pl);
        }
      }

      // Set the final Set Of Preferences for User Account
      userAccountLocal.setPreferences(allPreferences);
    } catch (FinderException ex) {  // Trap Errors While Finding EJB Object
      System.err.println("Delete Preferences Info Method : Finder Exception "
                         + ex.toString());

      message = "NOSUCCESS";
      throw new RemoteException("Finder Exception While Deleting Preferences " +
           ex.toString());

    }

    return message;
  }

  /**
   * Method to retrieve Alerts for a given User Account. As a single user
   * account can have multiple alerts, this method returns a collection
   * of AlertsInfo Value Object.
   *
   * @param accountNumber Account Number of the User Account.
   * @return A Collection of AlertsInfo Value Object. This represents all
   *         the alerts for the User Account.
   * @exception RemoteException If error occurs during getting Alerts Info

   * @since 1.0
   */
  public Collection getAlerts(Integer accountNumber) throws RemoteException {
    Collection allAlerts = new ArrayList();
    try {
      // Find the User Account Local Entity Bean by passing the
      // Account Number Primary Key
      UserAccountLocal userAccountLocal = 
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Loop through all the Alerts
      Iterator alertIter = (userAccountLocal.getAlerts()).iterator();

      while (alertIter.hasNext()) {
        // Get the next handle to AlertsBean Local EJB Object
        AlertsLocal al = (AlertsLocal) alertIter.next();

        // Add a new instance of AlertsInfo Value Object to final list
        allAlerts.add(new AlertsInfo(al.getSymbol(), al.getMinLimit(),
                                     al.getMaxLimit()));
      }
    } catch (FinderException ex) {  // Trap Errors While Finding EJB Object
      System.err.println("Get Alerts Method : Finder Exception "
                         + ex.toString());
      throw new RemoteException("Finder Exception While Getting Alerts = " +
           ex.toString());
    }

    return allAlerts;  // Return All Alerts
  }


  /**
   * Method to add an Alert for a given User Account.
   *
   * @param accountNumber Account Number of the User Account.
   * @return Status of this Operation
   * @exception RemoteException If error occurs during adding Alert Info
   * @since 1.0
   */
  public String addAlert(Integer accountNumber, AlertsInfo alertsInfo)
    throws RemoteException {
    boolean    symbolPresent = false;
    boolean    validSymbol = false;
    String     message = null;

    try {
      // Find the User Account Local Entity Bean by passing the account Number
      // Primary Key
      UserAccountLocal userAccountLocal = 
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Check Whether a Valid Symbol
      validSymbol = helper.checkSymbolExists(alertsInfo.getSymbol());


      // Get All the Alerts for this User Account and loop through them
      ArrayList allAlertsIter = new ArrayList(userAccountLocal.getAlerts());
      int size = allAlertsIter.size();
      int i = 0;
      while (i < size) {

        // Get the next handle to AlertsBean Local EJB Object
        AlertsLocal al = (AlertsLocal) allAlertsIter.get(i);

        // Check whether the Symbol already exists in the given set of
        // Alerts
        if (al.getSymbol().equals(alertsInfo.getSymbol())) {
          symbolPresent = true;
          //break;
        }

        allAlertsIter.set(i,al);
        i++;
      }

      // If Symbol was not already present then add this Alert to the
      // existing list of Alerts
      if (!symbolPresent && validSymbol ) {
        AlertsLocal alertsLocal = 
            alertsHomeLocal.create(helper.getNextID("ALERTS_SEQ"),
                                   accountNumber,
                                   alertsInfo.getSymbol(),
                                   alertsInfo.getMinLimit(),
                                   alertsInfo.getMaxLimit());

        allAlertsIter.add(alertsLocal);
      }

      userAccountLocal.setAlerts(allAlertsIter);
    } catch (FinderException ex) {  // Trap the errors while finding the EJB
      System.err.println("Finder Exception : Add Alerts Method : "
                         + ex.toString());
      throw new RemoteException("Finder Exception While Adding Alert = " +
           ex.toString());
    } catch (CreateException ex) {  // Trap the errors while creating the EJB
      System.err.println("Create Exception : Add Alerts Method : "
                         + ex.toString());
      throw new RemoteException("Create Exception While Adding Alert = " +
           ex.toString());
    } catch (DatabaseException ex) { // Trap SQL Errors
      throw new RemoteException("SQL Exception While Creating New Account : " +
          ex.toString());
    }

    // If Symbol Already Exists then return a proper message
    if (!symbolPresent && validSymbol) {
      message = "SUCCESS";
    } else if (symbolPresent && validSymbol) {
      message = "Symbol Already Present. Can not add. Change it";
    } else if (!validSymbol) {
      message = "Improper Symbol Value. Please Enter proper Symbol Value";
    }

    return message;
  }



  /**
   * Method to Update a Single Alert Record for a given User Account.
   *
   * @param accountNumber Account Number of the User Account.
   * @param alertInfo Value Object for Alert Information to be updated.
   * @return Status of this Operation
   * @exception RemoteException If error occurs during Changing Alert Info
   * @since 1.0
   */
  public String changeAlertInfo(Integer accountNumber, AlertsInfo alertInfo)
    throws RemoteException {
    String     message   = "SUCCESS";

    try {
      // Find the User Account Local Entity Bean by passing the account Number
      // Primary Key
      UserAccountLocal userAccountLocal = 
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Get All the Alerts
      ArrayList alertIter = new ArrayList(userAccountLocal.getAlerts());
      int size = alertIter.size();
      int i    = 0;
      // Loop through all the alerts
      while ( i < size) {
        // Get the next handle to AlertsBean Local EJB Object
        AlertsLocal al = (AlertsLocal) alertIter.get(i);

        // Check whether the passed Symbol is same as Alerts Symbol
        if (al.getSymbol().equals(alertInfo.getSymbol())) {

          // Update the Values of Max Limit and Min Limit
          al.setMaxLimit(alertInfo.getMaxLimit());
          al.setMinLimit(alertInfo.getMinLimit());
        }

        // Add Alert Back to the final List
        alertIter.set(i,al);
        i++;
      }

      // Set the Alerts
      userAccountLocal.setAlerts(alertIter);
    } catch (FinderException ex) {
      // Trap Errors While Finding Local EJB Objects
      System.err.println("Change Alert Info Method : Finder Exception "
                         + ex.toString());
      message = "NOSUCCESS";
      throw new RemoteException("Finder Exception While Changing Alert Info = " +
                                ex.toString());

    }

    return message;
  }

  /**
   * Method to Delete an Alert for the given User Account.
   *
   * @param accountNumber Account Number of the User Account.
   * @return Status of this Operation
   * @exception RemoteException If error occurs during deleting Alert
   * @since 1.0
   */
  public String deleteAlert(Integer accountNumber, String symbol)
    throws RemoteException {

    String     message   = "SUCCESS";
    Collection allAlerts = new ArrayList();

    try {
      // Find the User Account Local Entity Bean by passing the
      // Account Number Primary Key
      UserAccountLocal userAccountLocal = 
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Get All the Alerts for the User Account
      Iterator alertIter = userAccountLocal.getAlerts().iterator();
      // Loop through all the alerts
      while ( alertIter.hasNext()) {
        // Get the next handle to AlertsBean Local EJB Object
        AlertsLocal al = (AlertsLocal) alertIter.next();

        // Check if the passed Symbol is same as the symbol in Alerts Info
        // If no, add it to the final list.
        if (!al.getSymbol().equals(symbol)) {
           allAlerts.add(al);
        }
      }

      // Set the Value of Alerts
      userAccountLocal.setAlerts(allAlerts);
    } catch (FinderException ex) {  // Trap Errors While Finding EJB Object
      System.out.println("Delete Alert Info Method : Finder Exception "
                         + ex.toString());

      message = "NOSUCCESS";
      throw new RemoteException("Finder Exception While Deleting Alerts = " +
           ex.toString());

    }

    return message;
  }


  /**
   * Method to check whether the password provided by the user is correct.
   * @param accountNumber Account Number of the User Account.
   * @param password User Supplied Password
   * @return Boolean Flag indicating whether user provided Valid Password
   * @exception RemoteException If error occurs during Validating User
   * @since 1.0
   */
  public boolean validUser(Integer accountNumber, String password)
    throws RemoteException {

    boolean valid = false;

    try {
      // Find the User Account Local Entity Bean by passing the
      // Account Number Primary Key
      UserAccountLocal userAccountLocal = 
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Get the User's Password stored in the database
      String userPassword = userAccountLocal.getPassword();

      //Check whether the passwords are the same
      if (userPassword.equalsIgnoreCase(password)) {
        valid = true;
      }
    } catch (FinderException ex) {  // Trap Errors While Finding EJB Object
      throw new RemoteException("Invalid Account Number. " + 
                                "Please Enter Valid Account Number. ");
    }

    return valid;
  }

  /**
   * Method to change User's Password Value.
   * @param accountNumber Account Number of the User Account.
   * @param newPassword   New Password Value
   * @return Status of this Operation
   * @exception RemoteException If error occurs during changing Password Info
   * @since 1.0
   */
  public String changePassword(Integer accountNumber, String newPassword, 
                               String oldPassword)
    throws RemoteException {

    String result = "NOSUCCESS";

    try {
      // Check whether user has entered old password correctly
      if (this.validUser(accountNumber,oldPassword)) {

        // Find the User Account Local Entity Bean by passing the account Number
        // Primary Key
        UserAccountLocal userAccountLocal = 
          userAccountHomeLocal.findByPrimaryKey(accountNumber);

        // Set the New Password
        userAccountLocal.setPassword(newPassword);

        result = "SUCCESS";
      }
    } catch (FinderException ex) {  // Trap Errors While Finding EJB Object
      System.out.println("Valid User Method : " + ex.toString());
      throw new RemoteException("Finder Exception While Changing Password = " +
        ex.toString());

    }

    return result;
  }

  /**
   * Method to call the EJB-QL method of UserAccountBean and get the Hashtable
   * with AccountNumber and Password details of the user.
   *
   * @param email Email is input by the user.
   * @return Hashtable with AccountNumber and Password
   *
   * @exception RemoteException Remote Error when getting a User Account Information
   * @since 1.0
   */
  public Hashtable getUserAccount(String email) throws RemoteException {
    Hashtable userHash = new Hashtable();

⌨️ 快捷键说明

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