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

📄 usermanagementsessionfacadebean.java

📁 Oracle的J2EE Sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * @author : Umesh Kulkarni
 * @version 1.0
 *
 * Development Environment : Oracle9i JDeveloper
 *
 * Name of the File : UserManagementSessionFacadeBean.java
 *
 * Creation / Modification History
 *    Umesh           26-Apr-2002        Created
 *    Pushkala        01-Aug-2003        Modified and added Timer functionality
 *
 */
package oracle.otnsamples.ibfbs.usermanagement.ejb;

// Import Required Packages
import javax.naming.InitialContext; // JNDI
import java.rmi.RemoteException;    // RMI

// EJB Classes
import javax.ejb.CreateException;
import javax.ejb.FinderException;

// Utility Classes
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Date;

// Timer related classes
import javax.ejb.TimedObject;
import javax.ejb.TimerService;
import javax.ejb.Timer;
import javax.ejb.TimerHandle;
import javax.ejb.SessionContext;

import oracle.toplink.exceptions.DatabaseException;

import oracle.otnsamples.ibfbs.admin.ejb.MailServiceHome;
import oracle.otnsamples.ibfbs.admin.ejb.MailService;

// UserManagement Classes
import oracle.otnsamples.ibfbs.usermanagement.ejb.AlertsLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.PreferencesLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.AlertsHomeLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.PreferencesHomeLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.UserAccountHomeLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.UserAccountLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.AlertsInfo;
import oracle.otnsamples.ibfbs.usermanagement.ejb.AccountInfo;
import oracle.otnsamples.ibfbs.usermanagement.ejb.TimerInfo;
import oracle.otnsamples.ibfbs.usermanagement.ejb.PreferencesInfo;

// TradeManagement Classes
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeDetailsHomeLocal;
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeDetailsLocal;


/**
 * This Class is the Bean implementation of UserManagementSession Bean.
 * This Session Facade Bean coordinates all the User Management Related
 * Functionality and client invokes all this functionality only through
 * this Session Bean.
 *
 * In order to provide the User Management Functionality such as
 *   a) Create a New Account, b) Add/Delete/Change Preferences
 *   c) Add/Delete/Change Alerts etc, this Bean takes the help of "UserAccount"
 *      Local EJB Object.
 *
 * UserAccount Local EJB Object has 1:N relationship with other Local entity
 * beans namely PreferencesBean, AlertsBean, PortfolioBean and TradeDetails Bean.
 * 
 * This Bean also implements the EJB2.1 feature : Timer Service on StateLess 
 * Session Bean (SLSB) by implementing the TimedObject and ejbTimeOut method() 
 * on it. The FBS Administrator is associated with a Timer to send periodic 
 * Report of the Trading activity on the site. The timeout interval can be set 
 * by logging on as FBS Adminstrator.
 * 
 * @see UserHelper.java
 */
public class UserManagementSessionFacadeBean
    implements javax.ejb.SessionBean, TimedObject {

  // Reference to Local Home Interface of UserAccountBean
  private   UserAccountHomeLocal  userAccountHomeLocal  = null; 

  // Reference to Local Home Interface of PreferencesBean
  private   PreferencesHomeLocal  preferencesHomeLocal  = null; 

  // Reference to Local Home Interface of AlertsBean                                        
  private   AlertsHomeLocal       alertsHomeLocal       = null; 

  // Reference to Local Home Interface of TradeDetailsBean                                    
  private   TradeDetailsHomeLocal tradedetailsHomeLocal = null; 
  
  private   String                notificationMailId    = null;

  // Handle of the Timer (Serializable)
  private   TimerHandle           timerHandle           = null; 

  // Session Context 
  private   SessionContext        sctx;   
  
  private   UserHelper            helper                = null;

  /**
   * Method which is called by the EJB Container, when the client invokes
   * create() method on the Home Interface namely
   * 'UserManagementSessionHomeRemote.java. This method initializes the
   * values of the references to Home Interfaces for later use.
   *
   * @since 1.0
   */
  public void ejbCreate() {
    try {
      InitialContext ic = new InitialContext();

      // Look Up the Local Home Interfaces
      userAccountHomeLocal =
      (UserAccountHomeLocal)ic.lookup("java:comp/env/ejb/UserAccountHomeLocal");

      preferencesHomeLocal =
      (PreferencesHomeLocal)ic.lookup("java:comp/env/ejb/PreferencesHomeLocal");

      alertsHomeLocal      =
      (AlertsHomeLocal) ic.lookup("java:comp/env/ejb/AlertsHomeLocal");

      tradedetailsHomeLocal =
      (TradeDetailsHomeLocal)ic.lookup("java:comp/env/ejb/TradeDetailsHomeLocal");

      helper = UserHelper.getInstance();
      
      notificationMailId = (String) ic.lookup("NTFY_MAIL");
      
    } catch (javax.naming.NamingException ne) {
      ne.printStackTrace();
    }
  }

  /**
   * Method to create a New User Account. After creating the new User Account,
   * the account Number is returned back. Note that the parameters passed to
   * this method are Value Objects.
   *
   * @param  contactInfo The Contact Information Value Object
   *         For more information @see ContactInfo.java
   * @param  accountInfo The account Information Value Object
   *         For more information @see AccountInfo.java
   * @return Account Number of created Account.
   * @exception RemoteException Exception raised when error occurs during
   *           creating a new account
   * @since 1.0

   */
  public Integer createNewAccount(ContactInfo contactInfo,
                                  AccountInfo accountInfo) throws RemoteException {

    Integer accountNumber = new Integer(0);

    try {
      // Get the Next Account Number Value from the Database Sequence
      accountNumber = helper.getNextID("ACCOUNTNUMBER_SEQ");

      // Create the New Account By invoking Method on the
      // UserAccount Local EJB Object
      UserAccountLocal userAccountLocal = userAccountHomeLocal.create(accountNumber,
                           accountInfo.getPassword(),
                           contactInfo.getFirstName(),
                           contactInfo.getLastName(),
                           contactInfo.getOrganization(),
                           contactInfo.getAddress(),
                           contactInfo.getCity(),
                           contactInfo.getState(),
                           contactInfo.getCountry(),
                           contactInfo.getPhone(),
                           accountInfo.getAccountBalance(),
                           contactInfo.getEmail(),
                           accountInfo.getUserType(),
                           accountInfo.getLinesPerPage(),
                           accountInfo.getAlertMode(),
                           contactInfo.getMobileEmail());

    } catch (CreateException ex) {  // Trap Creation Errors
      System.err.println("Create Exception = " + ex.toString());
      accountNumber = new Integer(-1);
      throw new RemoteException("Exception While creating account : " +
           ex.toString());
    } catch (DatabaseException ex) { // Trap SQL Errors
      throw new RemoteException("SQL Exception While Creating New Account : " +
          ex.toString());
    }

    return accountNumber;
  }

  /**
   * Method to get Contact Information for a particular User. Note that the
   * parameters returned by this method is ContactInfo Value Object.
   *
   * @param accountNumber Account Number of the User Account.
   * @return  The Contact Information Value Object for the given user.
   *          For more information @see ContactInfo.java
   * @exception RemoteException If error occurs during getting Contact Info
   *
   * @since 1.0

   */
  public ContactInfo getContactInfo(Integer accountNumber) throws RemoteException {

    ContactInfo contactInfo = null;
    try {
      // Find the Appropriate User Account Bean
      UserAccountLocal userAccountLocal = userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Construct the Value Object contactInfo to be passed back to the client
      contactInfo = new ContactInfo(userAccountLocal.getFirstName(),
                                    userAccountLocal.getLastName(),
                                    userAccountLocal.getOrganization(),
                                    userAccountLocal.getAddress(),
                                    userAccountLocal.getCity(),
                                    userAccountLocal.getState(),
                                    userAccountLocal.getCountry(),
                                    userAccountLocal.getPhone(),
                                    userAccountLocal.getEmail(),
                                    userAccountLocal.getMobileEmail());
    } catch (FinderException ex) {  // Trap Any Errors While Finding Bean
      System.err.println("Finder Exception " + ex.toString());
      throw new RemoteException("Finder Exception While Getting Contact Info : " +
           ex.toString());
    }

    return contactInfo;  // return Contact Information
  }

  /**
   * Method to initialize Timer on the SLSB associated with Admin user 
   * to send periodic Trade updates to the Administrator.
   *
   * @param timeout Timeout value of the timer
   * @param info    Timer information
   *
   * @since 1.0
   */
  private void initializeTimer(long timeout, String info) {

    try {

      // Get an instance of TimerService associated with the 
      // SessionContext of the SLSB
      TimerService ts = sctx.getTimerService();

      // If there are existing Timer associated with this Bean,
      // Cancel it before initializing another one
      checkAndCancelTimers(ts.getTimers());

      long initialDuration  = timeout;
      long intervalDuration = timeout;

      // Create Timer with required interval
      // The timeout value is also set in the infor field for convenience
      Timer timer = ts.createTimer(initialDuration, intervalDuration, 
                                   info + " Timeout : " + timeout);

      // Store the TimerHandle which is seriablizable and which can be used 
      // to retrieve the timer values whenever required later
      timerHandle = timer.getHandle();

      System.out.println("Timer created at " +
                         new Date(System.currentTimeMillis()) +
	                     " with a timeout: " + intervalDuration +
                         " ms and with info: " + info);

    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return;

  }

  /**
   * Method to check and cancel timers if any on the SLSB.
   *
   * @param allTimers  Collection of all timers associated with this SLSB 
   *                   obtained from the TimerService
   *
   * @since 1.0
   */
  private void checkAndCancelTimers(Collection allTimers) {

    Iterator timerIter = allTimers.iterator(); // Set an iterator

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

      Timer  timer = (Timer) timerIter.next(); // Get next timer

      try {
        timer.cancel(); // Cancel existing timers if any
      } catch (IllegalStateException ise) {
        // Ignore already cancelled timers
        System.out.println("Error cancelling timer : "+ise.toString());
      }
    }

  }

  /**
   * This method implements the callback method defined on the TimedObject 
   * interface. This method is called by the EJB container when the timer 
   * expires (goes off). This is used by the EJB container to deliver 
   * timer expiration notifications.
   * 
   * This method contains all the business logic to send timed reports of the 
   * trading activity on FBS site to the FBS Administrator.
   * 
   * Note : This method has to be implemented by an entity bean or stateless 
   * session bean or message-driven bean class which implements the TimedObject 
   * interface to process the timer expirations. 
   *
   * @param timer  The Timer instance which has expired
   *
   * @since 1.0
   */
  public void ejbTimeout(Timer timer) {

    Date   sysDate   = new Date(System.currentTimeMillis());
    Date   toDate    = new Date(System.currentTimeMillis() - timer.getTimeRemaining());

    // Get the timer information
    String timerInfo = (String) timer.getInfo();
    
    System.out.println("ejbTimeout() called at: " +
                        sysDate + " with info: " + timerInfo);

    // Call the method to send mail containing information of the 
    // trading activity on the FBS site to the administrator 
    sendMail(timerInfo, toDate, sysDate);

    return;
  }

  /**
   * Method to set the New Value of Contact Information for a particular User.
   * Note that the one of the parameter passed to this method is ContactInfo
   * Value Object.
   *
   * @param accountNumber Account Number of the User Account.
   * @param contactInfo  The Contact Information Value Object for the given user.
   *                     For more information @see ContactInfo.java
   * @return Status of this operation
   * @exception RemoteException If error occurs during setting Contact Info
   * @since 1.0

⌨️ 快捷键说明

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