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

📄 usermanagementsessionfacadebean.java

📁 Oracle的J2EE Sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    try {
      // Find the User Account Local Entity Bean by passing the Email
      Collection uaColl = userAccountHomeLocal.findByEmail(email);
      Iterator   uaIter = uaColl.iterator();
      if (uaIter.hasNext()) {
        UserAccountLocal userAccountLocal = (UserAccountLocal) uaIter.next();

        // Add the queried values to the Hashtable
        userHash.put("ACCOUNTNUMBER", userAccountLocal.getAccountNumber());
        userHash.put("PASSWORD", userAccountLocal.getPassword());
        userHash.put("FIRSTNAME", userAccountLocal.getFirstName());
        userHash.put("LASTNAME", userAccountLocal.getLastName());
      }
    } catch (FinderException ex) {  // Trap Errors While Finding EJB Object
      System.out.println("Finder Exception in getUserAccount : "
                         + ex.toString());
      throw new RemoteException("Finder Exception While Getting User Account = " +
           ex.toString());

    }

    return userHash;
  }

  /**
   * Method to call the EJB-QL method of UserAccountBean to check whether
   * Admin account has been created. Creates a new account if Admin user
   * does not exist and initializes Timer for TradeDetails Updates
   * with default values of '0' hour and '15' minutes.
   *
   * @return accountNumber of Admin user (if new account was created)
   *
   * @exception RemoteException if creating Admin account fails
   * @since 1.0
   */
  public Integer createAdminAccount() throws RemoteException {

    int accountNumber = 0;

    try {

      // Find the User Account Local Entity Bean by passing the UserType as 'A'
      Collection uaColl = userAccountHomeLocal.findByUserType("A");

      if (uaColl.iterator().hasNext())  {

        // Account already exists, do nothing

      } else {

        // Create Admin account

        // Admin account is assumed to be One (always)
        UserAccountLocal userAccountLocal
                   = userAccountHomeLocal.create(helper.getNextID("ACCOUNTNUMBER_SEQ"),
                                                 "welcome",
                                                 "John","Doe","IBFBS"," "," ",
                                                 " "," "," ",0,"admin@fbs.com",
                                                 "A",new Integer(0),"E"," ");
        accountNumber = 1;

        // Initialize Timer Values to default
        int hour = 0;
        int mins = 15;

    		// Calculate interval in milli seconds
        long timeout = (hour * 60 + mins) * 60 * 1000;

        // Initialize Timer on SLSB for Tradedetails updates
        this.initializeTimer(timeout, "Tradedetails Updates");

      }

    } catch (FinderException findEx) {  // Handle Errors While Finding EJB Object

       System.out.println("Finder Exception while creating admin account : "
                           + findEx.toString());

       accountNumber = -1;

    } catch(CreateException createEx) {

       System.out.println("Create Exception while creating admin account : "
                           + createEx.toString());

       accountNumber = -1;

    } catch (Exception ex) {

      throw new RemoteException("Generic Exception while creating admin account : "
                                + ex.toString());

    }

    return new Integer(accountNumber);

  }


  /**
   * This method gets all the TradeDetails between the input dates, calls the 
   * format method to format the message and then creates an instance of the 
   * MailService bean to send detailed mail to the Administrator.
   * 
   * @param   timerInfo   Information about the timer 
   * @param   toDate      Start Date 
   * @param   sysDate     End Date
   *
   * @since 1.0
   */
  private void sendMail(String timerInfo, Date toDate, Date sysDate) {

    Collection tdColl  = null;

    try  {
      // Execute an EJB-QL to retrieve all tradedetails BETWEEN the input dates
      tdColl = (Collection) tradedetailsHomeLocal.TradeDetails(toDate, sysDate);
    } catch (Exception ex) {
      System.out.println(" Exception while ejbSelectTradeDetails "+ex);
    }

    if (tdColl != null) {
      // Iterate through the tradedetails collection 
      Iterator tdIter  = tdColl.iterator();

      if (tdIter.hasNext()) {
        // Format Message
        String      message     = formatHTMLMessage(tdColl, toDate, sysDate);
        MailService mailService = null;

        try {
          InitialContext ic = new InitialContext();
          // Initialize the MailSender
          MailServiceHome mailServiceHome =
            (MailServiceHome) ic.lookup("MailService");

          mailService = mailServiceHome.create();

          // Send mail the Administrator's mailid specified in ConnectionParams
          mailService.sendMail(notificationMailId,
                               "tradeupdates@fbs.com",
                               " TradeDetails Updates ",
                               message);

        } catch (Exception ex) {
          System.out.println(
                 " Couldn't initialize mail service to send trade details updates :"+
                 ex.toString());
        }
      }
    }
  }

  /**
   * This method prepares the HTML format of the message to be sent to the 
   * FBS administrator about the trading activity.
   * 
   * @param   tdColl      Collection of trade details
   * @param   toDate      Start Date 
   * @param   sysDate     End Date
   * 
   * @return  The formatted HTML string
   *
   * @since 1.0
   */
  private String formatHTMLMessage(Collection tdColl,
                                   Date toDate, Date sysDate) {

    StringBuffer sb = new StringBuffer();

    sb.append("<html><body>");

    sb.append("<table width=\"80%\"><tr><td>");
    sb.append("Hello, <br><br>  Details of all the Stock Trades carried out " );
    sb.append("BETWEEN ");
    sb.append(toDate);
    sb.append(" AND ");
    sb.append(sysDate);
    sb.append("   are as follows : ");
    sb.append("</td></tr></table><br>");

    sb.append("<table width=\"80%\" border=\"1\" >");

    // Set the iterator for the Collection
    Iterator tdIter  = tdColl.iterator();

    int rowCount = 0; // initialize row counter

    // Iterate through the collection
    while (tdIter.hasNext()) {

       // Retrieve each tradedetails record
      TradeDetailsLocal tdv = (TradeDetailsLocal)tdIter.next();

      rowCount += 1;

      // Display the Table header only once when the row count is 1
      if (rowCount == 1) {

        String tabHeader[] = new String[] {
                                            "SYMBOL",
                                            "ACTION",
                                            "PRICE ($)",
                                            "QUANTITY"
                                          };

        sb.append("<tr> ");
        for (int count=0; count < 4 ; count++) {
          sb.append("   <th> ");
          sb.append("      <div align=\"center\">  ");
          sb.append(tabHeader[count]);
          sb.append("      </div>  ");
          sb.append("   </th>  ");
        }
        sb.append("</tr> ");

      }

      // Display the values in the Collection
      sb.append("<tr> ");

      sb.append("<td height=\"30\"> ");
      sb.append("  <div align=\"left\"> ");
      sb.append(tdv.getSymbol());
      sb.append("  </div>");
      sb.append("</td>");

      sb.append("<td height=\"30\"> ");
      sb.append("  <div align=\"left\"> ");

      String action = tdv.getAction();
      if (action.equals("B")) action = "BOUGHT";
      else if (action.equals("S")) action = "SOLD";

      sb.append(action);
      sb.append("  </div>");
      sb.append("</td>");

      sb.append("<td height=\"30\"> ");
      sb.append("  <div align=\"right\"> ");
      sb.append(tdv.getPrice());
      sb.append("  </div>");
      sb.append("</td>");
      sb.append("<td height=\"30\"> ");
      sb.append("  <div align=\"right\"> ");
      sb.append(tdv.getQuantity());
      sb.append("  </div>");
      sb.append("</td>");

      sb.append("</tr> ");
    }

    sb.append("</table>");

    sb.append("<br><br>");
    sb.append("<table width=\"80%\"><tr><td>");
    sb.append("Report Time : ");
    sb.append(new Date());
    sb.append("<br><br>-- EJBTimer Reporting.");
    sb.append("<br><br><B>Note : </B>This Report is automatically generated by ");
    sb.append("EJB Container with the Timer values set by the Administrator. ");
    sb.append("To change this : Log on as FBS Administrator ");
    sb.append("and Set the required Report Interval. <br>");
    sb.append("</td></tr></table>");

    sb.append("</body></html>");

    return sb.toString();

  }

  /**
   * Method to call the EJB-QL method of UserAccountBean to check whether
   * Admin account has been created. If Admin user already exists,
   * initializes Timer for TradeDetails Updates with the input values.
   * 
   * @param timerInfo  TimerInfo value object
   *
   * @return String indicating if the updation of Timer was successful or not
   *
   * @exception RemoteException if updating Admin account fails
   * @since 1.0
   */
  public String updateAdminAccount(TimerInfo timerInfo)
      throws RemoteException {

    // Initialize the Status Message
    String retMessage = "NOSUCCESS";

    try {

      // Find the User Account Local Entity Bean by passing the UserType as 'A'
      Collection uaColl = userAccountHomeLocal.findByUserType("A");

      if (uaColl.iterator().hasNext())  {

        int hour = timerInfo.getTimerHours().intValue();
        int mins = timerInfo.getTimerMinutes().intValue();

        if ( (hour > 0) || (mins > 0) ) {

      		// Calculate interval in milli seconds
          long timeout = (hour * 60 + mins) * 60 * 1000;

          // Re-Initialize Timer on SLSB for Tradedetails updates
          this.initializeTimer(timeout, "Tradedetails Updates");

          retMessage = "SUCCESS";

        }

      } else {

        // Account does not exist
        throw new RemoteException("Admin Account does not exist : ");

      }

    } catch (FinderException findEx) {  // Handle Errors While Finding EJB Object

       System.out.println("Finder Exception while updating admin account : "
                           + findEx.toString());

    } catch (Exception ex) {

      throw new RemoteException("Genric Exception while creating admin account : "
                         + ex.toString());

    }

    return retMessage;

  }

  /**
   * Other Standard Call Back Methods
   */
  public void ejbRemove() {}

  public void ejbActivate() {}

  public void ejbPassivate() {}

  public void setSessionContext(javax.ejb.SessionContext cntx) {
    this.sctx = cntx;
  }

  public void unsetSessionContext() {
    this.sctx = null;
  }
}

⌨️ 快捷键说明

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