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

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

package oracle.otnsamples.ejbql;

import java.util.Date;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;


import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;


/**
 * This Class is the Bean Implementation for UserAccount
 * which is a local EJB Object.

 */
public abstract class UserAccountBean implements EntityBean  {

  // Entity Context
  private EntityContext context;

  /**
   * Method to create UserAccountBean Local EJB Object.
   *

   * @param accountNumber  Account Number of the User Account
   * @param createddate    Created Date
   * @param firstname      First Name associated with the User Account
   * @param lastname       Last Name associated with the User Account
   * @param creditlimit    Credit Limit associated with the User Account
   *
   * @return Local EJB Object implementing UserAccountLocal interface
   */
  public Long ejbCreate(Long accountnumber, Date createddate,
                        String firstname, String lastname,

                        Long creditlimit) {

    setAccountnumber(accountnumber);
    setCreateddate(createddate);
    setFirstname(firstname);
    setLastname(lastname);
    setCreditlimit(creditlimit);

    return accountnumber;
  }


  /**
   * Method which is called by the Container after invoking of ejbCreate().
   * This method has the same signature as ejbCreate() method.
   *
   * @param accountNumber  Account Number of the User Account
   * @param createddate    Created Date
   * @param firstname      First Name associated with the User Account
   * @param lastname       Last Name associated with the User Account
   * @param creditlimit    Credit Limit associated with the User Account
   */
  public void ejbPostCreate(Long accountnumber, Date createddate,
                            String firstname, String lastname,

                            Long creditlimit) {
  }

  /**
   * Method to retrieve the value of AccountNumber
   * associated with the User Account
   *
   * @return  The Account Number Value
   */
  public abstract Long getAccountnumber();

  /**
   * Method to assign the new AccountNumber value in the User Account.

   *
   * @param newAccountnumber New Account Number Value
   */
  public abstract void setAccountnumber(Long newAccountnumber);

  /**
   * Method to retrieve the value of created date
   * associated with the User Account
   *
   * @return  The Created Date Value
   */
  public abstract Date getCreateddate();

  /**

   * Method to assign the new Created Date value for the User Account.
   *
   * @param newCreateddate New Created Date Value
   */
  public abstract void setCreateddate(Date newCreateddate);

  /**
   * Method to retrieve the value of First Name
   * associated with the User Account
   *
   * @return  The First Name Value
   */
  public abstract String getFirstname();

  /**

   * Method to assign the new First Name value in the User Account.
   *
   * @param newFirstname New First Name Value
   */
  public abstract void setFirstname(String newFirstname);

  /**
   * Method to retrieve the value of Last Name
   * associated with the User Account
   *
   * @return  The Last Name Value
   */
  public abstract String getLastname();

  /**
   * Method to assign the new Last Name value in the User Account.

   *
   * @param newLastname New Last Name Value
   */
  public abstract void setLastname(String newLastname);

  /**
   * Method to retrieve the value of Credit Limit
   * associated with the User Account
   *
   * @return  The Credit Limit Value
   */
  public abstract Long getCreditlimit();

  /**
   * Method to assign the new Credit Limit value in the User Account.
   *
   * @param newCreditlimit New Credit Limit Value

   */
  public abstract void setCreditlimit(Long newCreditlimit);

  /**
   * Method to select all the UserAccounts.
   *
   * @return Collection of all UserAccounts
   */
  public abstract Collection ejbSelectByTopAccounts() throws FinderException;

  /**
   * Method to perform post-processing operations on all the
   * UserAccounts retrieved by calling the above method. This
   * method further process the retrieved UserAccounts and checks
   * for the Accounts with TopCredits (credit limits) and returns the
   * collection of input number of UserAccounts.
   * Post-processing information within the EJB container itself
   * has the following two advantages : 

   *   1) It improves performance as the application can now leverage 
   *      the advantage of the vast resources available to the server.
   *   2) The data-processing code should go into the business logic
   *      and not the web-tier. This helps in maintaining the code.
   * Above are the two design considerations when deciding between ejbFind and 
   * ejbSelect methods.
   *
   * @return Collection of <input number of> Top (credited) UserAccounts
   */
  public Collection ejbHomeTopAccounts(String accountNumbers)
      throws FinderException {

    // Invoke the ejbSelect method and get all the Account Information.
    Collection collection = this.ejbSelectByTopAccounts();

    // Get the Number of Accounts to be displayed
    int numberOfAccounts = Integer.parseInt(accountNumbers);

    // Initialize a boolean variable

    boolean isSet;

    // Initialize and ArrayList for storing the top Accounts
    ArrayList topAccounts = new ArrayList(numberOfAccounts);

    // Create a reference to the UserAccount bean
    UserAccount userAccount;

    // Check if any rows have been returned
    if (collection != null){
        // Initialize the iterator
        Iterator iter = collection.iterator();

        // Loop through the Iterator
        while (iter.hasNext()){
           // Get the UserAccount
           userAccount = (UserAccount)iter.next();

           // Set the boolean variable to false.
           isSet = false;


           // Loop through the ArrayList
           for (int i=0; i < topAccounts.size(); i++){
              // Check the Credit Limit of the UserAccount against the Credit
              // limit of the Account present in the ArrayList
              // This operation returns an integer value as follows :
              //   1) If the Credit Limit of the UserAccount is less than the
              //      Credit Limit of the Account present in the Arraylist,
              //      then a value greater than 0 is returned.
              //   2) If the Credit Limit of the UserAccount is more than the
              //      Credit Limit of the Account present in the Arraylist,
              //      then a value less than 0 is returned.
              //   3) If the Credit Limit of the UserAccount is equal to the
              //      Credit Limit of the Account present in the Arraylist,
              //      then the value 0 is returned.
              int creditValue = ((UserAccount)topAccounts.get(i))
                                      .getCreditlimit()
                                      .compareTo(userAccount.getCreditlimit());

              // If the returned value from the above operation is less than
              // or equal to 0, then add this account to the ArrayList. Also

              // set the boolean variable to true. This specifies that the
              // current has already been added to the ArrayList and no further
              // processing is required for this account.
              // Now, break the loop and check for the next account
              if ((creditValue < 0) || (creditValue == 0)){
                topAccounts.add(i, userAccount);
                isSet = true;
                break;
              }
             }

            // If the Number of Accounts to be returned has not been reached
            // and the Account has also not been added to the ArrayList, then
            // add it to the ArrayList now.
            if ((topAccounts.size() < numberOfAccounts) && (!isSet)){
               topAccounts.add(userAccount);
             }
           }
        }

        // If the Number of Accounts present in the Arraylist has exceeded the
        // limit, then remove the Accounts present at the end of the ArrayList.

        if (topAccounts.size() > numberOfAccounts){
          int size = topAccounts.size();
          for (int i = size-1; i >= numberOfAccounts; i--){
            topAccounts.remove(i);
          }
        }

    // Return all the top accounts
    return topAccounts;
  }

  /**
   * Method to select all the credit limit of the input UserAccount.
   *
   * @param accountnumber Account Number
   *
   * @return Credit Limit of the input UserAccount
   */
  public abstract Long ejbSelectCreditLimit(Long accountnumber)
      throws FinderException;

  /**
   * Method to call the above method and return the credit limit value

   * for the input accountnumber without post-processing
   * Please note that this method returns a Long instead of a collection
   * that is returned normally by the EJB container. This is a major 
   * advantage of ejbSelect methods. Using these methods, we can return
   * an object from 'within' the CMP instead of 'the' CMP. This way, the 
   * application uses the server and the EJB container resources more 
   * effeciently.
   *
   * @return Credit Limit of the input UserAccount
   */
  public Long ejbHomeCreditLimit(Long accountnumber)
      throws FinderException{

    // Return the Credit Limit of the specified Account
    return this.ejbSelectCreditLimit(accountnumber);

  }


  /**
   * standard call back methods
   */
  public void setEntityContext(EntityContext ctx) {
    this.context = ctx;

  }

  public void unsetEntityContext() {
    this.context = null;
  }

  public void ejbActivate() {
  }

  public void ejbLoad() {
  }

  public void ejbPassivate() {
  }

  public void ejbRemove() {
  }

  public void ejbStore() {
  }

}
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