/*
 * @author  : Rajat Gupta
 * @version : 1.0
 *
 * Development Environment : Oracle9i JDeveloper

 * Name of the File        : InvokeEJB.java
 *
 * Creation / Modification History
 *    Rajat Gupta    15-Jul-2003      Created
 *
 */

package oracle.otnsamples.ejbql.classes;

// SQL Imports
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;


// Util Imports
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;

// Naming Imports
import javax.naming.InitialContext;


// Javax Imports
import javax.sql.DataSource;

// Other Imports
import oracle.otnsamples.ejbql.UserAccount;
import oracle.otnsamples.ejbql.UserAccountLocalHome;

/**

 * This class forms the interface between the JSPs and Entity EJBs.
 * This class contains methods which call the methods on the UserAccount
 * Entity EJB to create a new UserAccount or Search for an existing
 * UserAccount with the specific criteria selected by the User in the JSP.
 */
public class InvokeEJB  {

  // Create reference to the EJB Home Object
  private UserAccountLocalHome accountHome = null;


  // Create reference to the DataSource Name
  private DataSource dsn = null;


  /**
   * Empty Default Constructor
   */
  public InvokeEJB() {
  }

  /**

   * Creates a new UserAccount entity with the input details of the User.
   *
   * @param firstName   First Name
   * @param lastName    Last Name
   * @param creditLimit Credit Limit
   *
   * @return Account Number of the newly created Entity
   *
   * @exception Generic Exception
   */
  public String createAccount(String firstName, String lastName,
                              String creditLimit)

      throws Exception {

     // If DSN and EJB Home objects are not available, then
     // initiate them
     if ((accountHome == null) || (dsn == null)){
       this.initialize();
     }

    // Initialize Objects
    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    String accountNumber = null;


    // Gets the Connection
    conn = (Connection)dsn.getConnection();

    // Create the Statement
    stmt = conn.createStatement();

    // Execute the Query
    rset = stmt.executeQuery("Select AccountNumber_Seq.nextval from dual");

    // Loop through the ResultSet and get the Account Information
    while (rset.next()){
        accountNumber  = rset.getString(1);
    }


    // Get the Calendar Instance
    Calendar calendar = Calendar.getInstance();

    // Set the TimeStamp for the date to 00:00:00 (hours:mintutes:seconds)
    calendar.set(Calendar.AM_PM , Calendar.AM);
    calendar.set(Calendar.MINUTE , 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.HOUR, 0);

    // Create a new Entity Bean with the User Inputs. This creates a
    // corrosponding row in the table
    UserAccount ua = accountHome.create(new Long(accountNumber),
                                        calendar.getTime(),
                                        firstName, lastName,

                                        new Long(creditLimit));

    return accountNumber;
  }

  /**
   * Initializes the reference to the EJB Home Object and Datasource object.
   *
   * @exception Generic Exception
   */
  public void initialize() throws Exception {

    // Get the Initial Context Object
    InitialContext ctx = new InitialContext();

    // Lookup and get the EJB Home object

    accountHome = (UserAccountLocalHome)
                       ctx.lookup("java:comp/env/UserAccount");

    // Look up the JNDI Connection.
    dsn = (DataSource)ctx.lookup("jdbc/OracleECoreJBQLDS");

    return ;
  }

  /**
   * Find All the UserAccounts created.
   *
   * @return Collection of all UserAccounts.
   *
   * @exception Generic Exception
   */
  public Collection findByAllAccounts() throws Exception {


    // If EJB Home is not available, then initialize it
    if (accountHome == null){
      this.initialize();
    }

    // Invoke the EJB and get the Information for all the accounts.
    // Return this Collection.
    return accountHome.findByAllAccounts();
  }


  /**
   * Find the UserAccounts which have credit limit between the
   * input Minimun and Maximum.
   *
   * @param minAmount   Minimum Amount
   * @param maxAmount   Maximum Amount

   *
   * @return Collection of all UserAccounts which have credit limit
   *         between the input values.
   *
   * @exception Generic Exception
   */
  public Collection findByMinMaxCreditLimit(String minAmount,
                                            String maxAmount)
      throws Exception {

    // If EJB Home is not available, then initialize it
    if (accountHome == null){
      this.initialize();
    }

    // Invoke the EJB and get the Information for all the accounts
    // that fall within the specified Credit Limit.
    // Return this Collection
    return accountHome.findByMinMaxCreditLimit(new Long(minAmount),

                                               new Long(maxAmount));
  }

  /**
   * Find the UserAccounts created on the input date.
   *
   * @param d   Created Date
   *
   * @return Collection of all UserAccounts created on the input date.
   *
   * @exception Generic Exception
   */
  public Collection findByDate(Date d) throws Exception {

    // If EJB Home is not available, then initialize it
    if (accountHome == null){
      this.initialize();
    }

    // Invoke the EJB and get the Information for all the accounts

    // that were created on the specififed Date.
    // Return this Collection
    return accountHome.findByCreatedDate(d);
  }


  /**
   * Find the Top <input number of> UserAccounts with highest Credit Limit.
   *
   * @param numberOfAccounts Number of Accounts
   *
   * @return Collection of all UserAccounts with highest credit limits.
   *
   * @exception Generic Exception
   */
  public Collection getTopAccounts(String numberOfAccounts)
      throws Exception {

    // If EJB Home is not available, then initialize it
    if (accountHome == null){
      this.initialize();

    }

    // Invoke the EJB and get the Information for all the accounts
    // that have the highest Credit Limit.
    // Return this Collection
    return accountHome.TopAccounts(numberOfAccounts);
  }

  /**
   * Find the Credit Limit of the input Account Number.
   *
   * @param accountNumber User Account Number
   *
   * @return Credit Limit value.
   *
   * @exception Generic Exception
   */
  public Long getCreditLimit(String accountNumber) throws Exception {

    Long creditLimit = null;

    try {

      // If EJB Home is not available, then initialize it
      if (accountHome == null){
        this.initialize();
      }

      // Invoke the EJB and get the Credit Limit for the specified Account.
      creditLimit = accountHome.CreditLimit(new Long(accountNumber));

    } catch (Exception finderex) {
      // Do Nothing here
    }

    // Return the Credit Limit
    return creditLimit;
  }
}
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy