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

📄 trademanagementsessionfacadebean.java

📁 Oracle的J2EE Sample
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * @author  : Pushkala
 * @version : 1.0
 *
 * Development Environment : Oracle9i JDeveloper
 *
 * Name of the File : TradeManagementSessionFacadeBean.java
 *
 * Creation / Modification History
 *    Pushkala        26-Apr-2002        Created
 *    Elangovan       19-Aug-2003        Modified
 *
 */
package oracle.otnsamples.ibfbs.trademanagement.ejb;

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

import java.sql.SQLException;

import java.rmi.RemoteException;

// EJB Packages
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;

import javax.jms.QueueSession;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueConnection;
import javax.jms.Queue;
import javax.jms.Message;
import javax.jms.JMSException;

import javax.naming.InitialContext;

import oracle.toplink.exceptions.DatabaseException;

// Required TradeManagement Classes
import oracle.otnsamples.ibfbs.trademanagement.ejb.PortfolioLocal;
import oracle.otnsamples.ibfbs.trademanagement.ejb.PortfolioHomeLocal;
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeDetailsLocal;
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeDetailsHomeLocal;
import oracle.otnsamples.ibfbs.trademanagement.ejb.PortfolioInfo;
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeDetailsInfo;
import oracle.otnsamples.ibfbs.trademanagement.ejb.TradeHelper;
import oracle.otnsamples.ibfbs.trademanagement.ejb.PageByPage;
import oracle.otnsamples.ibfbs.toplink.Symbol;
import oracle.otnsamples.ibfbs.usermanagement.ejb.UserAccountLocal;
import oracle.otnsamples.ibfbs.usermanagement.ejb.UserAccountHomeLocal;


/**
 * This Class contains the bean implementation for the TradeManagement
 * Stateful Session EJB. This contains all the methods required for Trade
 * Management in the application. This Session Facade Bean coordinates all
 * the Trade Management Related Functionality and client invokes all this
 * functionality only through this Session Bean.
 *
 * The SessionFacadeDesign pattern is used here to provide a simple interface
 * to the complex subsystem of enterprise beans and to reduce communication
 * and dependencies between client objects and enterprise beans.
 *
 * The methods in Session Facade Bean provide the following functionality :
 * a) Getting / Setting Portfolio
 * b) Getting / Setting TradeDetails
 * c) Adding Portfolio / TradeDetails
 * d) Updating Portfolio / TradeDetails
 * e) Rollback of Portfolio / TradeDetails
 * f) Interacting with UserManagement EJBs to get User Account Information,
 *    Contact Information
 *
 * UserAccount Local EJB Object has 1:N relationship with other Local
 * Entity Beans namely PreferencesBean, AlertsBean, PortfolioBean and
 * TradeDetails Bean. This forms a wrapper to the two Entity Beans
 * PortfolioBean and TradeDetails Bean. It acts as a connecting link
 * between the TradeManagementHelper class and the entity beans.
 *  
 *  @see TradeHelper.java
 */
public class TradeManagementSessionFacadeBean implements javax.ejb.SessionBean {

  /**
   *  Instance variable to hold reference to User Account local home interface
   *  Note that using Home interfaces, we can get references to remote interfaces
   */
  private UserAccountHomeLocal  userAccountHomeLocal;

  /** Instance variable to hold reference to Trade Details local home interface */
  private TradeDetailsHomeLocal tradeDetailsHomeLocal;

  /** Instance variable to hold reference to Portfolio local home interface */
  private PortfolioHomeLocal    portfolioHomeLocal;

  /** Session Context object */
  private SessionContext sc;
  
  /** Trade Helper */
  private TradeHelper tradeHelper = null;
  
  /** Exchange Queue Connection factory instance */
  private QueueConnectionFactory exchageQConnfactory = null;;
  
  /** Exchange Queue handle */
  private Queue                  exchangeQueue       = null;

  /**
   * This is the method called by the EJB Container when
   * TradeManagementSessionFacadeBean is instantiated ie,. when the client
   * invokes the create() method on the Home interface
   * TradeManagementSessionHomeRemote. Here we call the initialize method
   * to set the objects required for TradeManagement processing.
   * @since 1.0
   */
  public void ejbCreate() {
    initialize();
  }

  /**
   * This method sets the initial context, and initializes the values of
   * references to Home Interfaces. This also sets the DataSource object.
   * @since 1.0
   */
  private void initialize() {
    try {
      InitialContext ic = new InitialContext();

      // Look up the Home interface of Entity Beans used in this Session Bean
      userAccountHomeLocal = (UserAccountHomeLocal)
                            ic.lookup("java:comp/env/ejb/UserAccountHomeLocal");
      
      tradeDetailsHomeLocal = (TradeDetailsHomeLocal)
                           ic.lookup("java:comp/env/ejb/TradeDetailsHomeLocal");

      portfolioHomeLocal = (PortfolioHomeLocal)
                              ic.lookup("java:comp/env/ejb/PortfolioHomeLocal");

      tradeHelper = TradeHelper.getInstance();

    } catch(javax.naming.NamingException ne) {
      System.err.println(" TMSFB.ejbCreate(): Error during JNDI lookup : "+ne.toString());
    } catch(Exception ex) {
      System.err.println(" TMSFB.ejbCreate(): Generic Error : "+ex.toString());
    }
  }

  /**
   * This method constructs a new Collection from the input collection,
   * since we need to send the following details to the client
   * 1) boolean value to indicate if the page need iteration
   * 2) Size of the Collection
   * 3) boolean value to indicate if the records have previous set
   * 4) boolean value to indicate if the records have next set
   * 5) Portfolio Details Collection
   * @param allRecords    Collection of all records
   * @param recordNumber  Record Number
   * @param linesPerPage  Lines per page
   * @since 1.0
   */
  private static Collection createNewCollection(Collection allRecords,
                                                String recordNumber,
                                                int linesPerPage) {
    Collection newColl = new ArrayList();

    // Size of the input collection
    int collSize = allRecords.size();

    if (recordNumber == null) {
      recordNumber = "1";
    }

    boolean needsPageIteration = false;

    // Page Iteration is required only if number of rows
    // retrieved exceeds linesPerPage
    if (collSize == 0 || collSize <= linesPerPage) {
      needsPageIteration = false;
    } else {
      needsPageIteration = true;
    }

    if (needsPageIteration) {
      PageByPage pbp = new PageByPage();
      // Get the collection before getting hasPrev, hasNext
      // since the variables are set by the getCollection method
      Collection temp = pbp.getCollection((List)allRecords,
                                          recordNumber,
                                          linesPerPage);
      newColl.add(pbp.hasPrevRecords());
      newColl.add(pbp.hasNextRecords());
      newColl.add(temp);
    } else {
      newColl.add("false");
      newColl.add("false");
      newColl.add(allRecords);
    }

    return newColl;
  }

  /**
   * This method calls the methods of the Entity Beans to get the Portfolio
   * details corresponding to the input User Account Number and returns the
   * Portfolio details as a Collection.
   *
   * @param accountNumber Account Number
   * @param recordNumber  Record Number
   * @param linesPerPage  Lines Per Page
   * @return The Collection of Portfolio Details for the input Account Number
   * @exception RemoteException If error occurs while getting the details
   * @since 1.0
   */
  public Collection getPortfolio(Integer accountNumber, String recordNumber,
                                 int linesPerPage)
      throws RemoteException {

    Collection allPortfolio = new ArrayList();
    Collection pfColl       = null;

    try {
      // Find the User Account Entity for the input account number
      UserAccountLocal userAccountLocal =
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Get the Collection of Portfolio corresponding to the user account
      pfColl = (Collection)userAccountLocal.getPortfolio();

      // Create an iterator for the above Collection
      Iterator portfolioLocalIter = pfColl.iterator();

      while(portfolioLocalIter.hasNext()) {

        // If the iterator has more values, get the next portfolio information
        PortfolioLocal pf = (PortfolioLocal)portfolioLocalIter.next();

        if(pf.getQuantity().intValue() > 0) {
        
          Symbol s = tradeHelper.getSymbol(pf.getSymbol());

          // If the Portfolio Quantity is greater than 0,
          // Add the Portfolio information to a new collection
          allPortfolio.add(new PortfolioInfo(pf.getLineNo(), pf.getQuantity(),
                                             pf.getPrice(), pf.getSymbol(),
                                             s.getCompanyname(),
                                             pf.getPurchaseDate(),
                                             pf.getPurchaseMode(),
                                             pf.getTradeId()));
        }
      }
    } catch(FinderException ex) {
      throw (new RemoteException("Finder Exception in get Portfolio : " +
                                 ex.toString()));
    } catch(DatabaseException ex) {
      throw (new RemoteException("SQL Exception in get Portfolio : " +
                                 ex.toString()));
    }

    //  return the new collection created
    return createNewCollection(allPortfolio, recordNumber, linesPerPage);
  }


  /**
   * This method gets the Portfolio Valuation Information for the input
   * Account Number. A User can have multiple Portfolio details, so a
   * collection of Portfolio Valuation Information is returned.
   *
   * @param accountNumber Account Number
   * @param recordNumber  Record Number
   * @param linesPerPage  Lines Per Page
   * @return The Collection of Portfolio Valuation Information for the
   *         input Account Number
   * @exception RemoteException
   * @since 1.0
   */
  public Collection getPortfolioValuation(Integer accountNumber,
                                          String recordNumber,
                                          int linesPerPage)
      throws RemoteException {

    Collection pfValue    = new ArrayList();
    Collection pfColl     = null;

    float totalAssetValue = 0; // The Total Asset Value

    try {
      // Find the User Account Entity for the input account number
      UserAccountLocal userAccountLocal =
        userAccountHomeLocal.findByPrimaryKey(accountNumber);

      // Get the Collection of Portfolio corresponding to the user account
      pfColl = (Collection)userAccountLocal.getPortfolio();

      // Create an iterator for the above Collection
      Iterator portfolioLocalIter = pfColl.iterator();

      while(portfolioLocalIter.hasNext()) {

        // If the iterator has more values, get the next portfolio information
        PortfolioLocal pf = (PortfolioLocal)portfolioLocalIter.next();

        if(pf.getQuantity().intValue() > 0) {

          // If the Portfolio Quantity is greater than 0,
          // Add the Portfolio information to a new collection
          float currPrice  = tradeHelper.getLatestRate(pf.getSymbol()).getLowprice().floatValue();
          float buyValue   = pf.getQuantity().floatValue() * pf.getPrice();
          float currValue  = pf.getQuantity().floatValue() * currPrice;
          float gainorloss = currValue - buyValue;
          float percent    = (gainorloss / buyValue) * 100;
          Symbol s = tradeHelper.getSymbol(pf.getSymbol());

          pfValue.add(new PortfolioValue(pf.getLineNo(), pf.getQuantity(),
                                         pf.getPrice(), pf.getSymbol(),
                                         s.getCompanyname(),
                                         pf.getPurchaseDate(),
                                         pf.getPurchaseMode(),
                                         pf.getTradeId(), currPrice,
                                         gainorloss, percent, currValue));

          // Compute the Total Asset Value
          totalAssetValue = totalAssetValue + currValue;

⌨️ 快捷键说明

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