VerifyFundsImpl.jcs Sample

This topic inludes the source code for the VerifyFundsImpl.jcs Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/JavaControlProject/verifyFunds/

Sample Source Code


001 package verifyFunds; 
002 
003 import com.bea.control.ControlException;
004 import examples.ejb20.basic.containerManaged.Account;
005 import examples.ejb20.basic.containerManaged.ProcessingErrorException;
006 import java.rmi.RemoteException;
007 import javax.ejb.RemoveException;
008 
009 /**
010  * A Java control demonstrating how you can use multiple Java controls
011  * to coordinate potentially complex business logic. While this control
012  * is identical to a control in the localControls folder of the 
013  * WebServices sample project, its placement here illustrates how
014  * you can package a control in a control project for greater 
015  * portability. You can build this project (right-click JavaControlProject,
016  * then click Build JavaControlProject) to generate a JavaControlProject.jar.
017  * That file can in turn be used in multiple other applications. The JAR
018  * file will be placed in the Libraries folder.
019  
020  * @jcs:jc-jar 
021  *  label="VerifyFunds"
022  *  palette-priority="3" 
023  *  group-name="SampleControls"  
024  *  version="1.0" 
025  *  description="A control that contains other controls." 
026  * @editor-info:code-gen control-interface="true"
027  */
028 public class VerifyFundsImpl implements VerifyFunds, com.bea.control.ControlSource
029 {     
030     public Callback callback;
031 
032     /**
033      * A utility control for formatting the PO number.
034      
035      * @common:control
036      */
037     private verifyFunds.poUtil.POUtil poUtilJC;
038 
039     /**
040      * An EJB control for accessing the customer's account.
041      
042      * @common:control
043      */
044     private verifyFunds.CustomerAccountEJB customerAccountEJB;
045 
046     /**
047      * A Database control for accessing the item inventory.
048      
049      * @common:control
050      */
051     private verifyFunds.ItemsDatabase itemsDB;
052 
053     /**
054      * A global variable to hold the balance available.
055      */
056     public boolean m_isBalanceAvailable = false;
057 
058     /**
059      * A global variable to hold the inventory available.
060      */
061   public boolean m_isInventoryAvailable = false;
062 
063     /**
064      * Submits a new purchase order, using an EJB to check the customer's 
065      * balance and a database to check item inventory. <br/>
066      
067      * All of the parameter values are set by the client of this 
068      * control. For most, the values may be any number. For the
069      * the itemNumber and quantityRequested parameters, the value
070      * matters because they are needed for database queries. The item numbers
071      * and quantities known to the database are stored in following table. <br/>
072      
073      <table style="font-size: 8 pt">
074      <tr><td style="width: 100px">itemNumber</td><td style="width: 100px">quantityRequested</td></tr>
075      
076      <tr><td>624</td><td>5</td></tr>
077      <tr><td>625</td><td>6</td></tr>
078      <tr><td>629</td><td>10</td></tr>
079      <tr><td>631</td><td>15</td></tr>
080      <tr><td>640</td><td>1</td></tr>
081      
082      </table>
083      
084      @param  poNumberString  The number to use for this new purchase order. 
085      * Any number will do here.
086      @param  customerIDString  The customer's unique ID. May be anything.
087      @param  itemNumber  A number corresponding to an item stored in the 
088      * database.
089      @param  quantityRequested  The number of items to purchase. Used to 
090      * query for available inventory.
091      @param  startingBalance  The amount of money the customer has. Used to
092      * create an EJB representing the customer's account.
093      
094      * @common:operation
095      */
096     public void submitPO(String poNumberString, String customerIDString, 
097         int itemNumber, int quantityRequested, double startingBalance)
098     {
099         String accountResult = this.openAccount(startingBalance, customerIDString);
100 
101         int poNumber = poUtilJC.formatNumber(poNumberString);
102         int customerID = poUtilJC.formatNumber(customerIDString);
103 
104     double itemPrice = itemsDB.selectItemPrice(itemNumber);
105         int quantityAvailable = itemsDB.checkInventory(itemNumber);
106     double totalAmount = itemPrice * quantityRequested;              
107 
108         try
109         {
110             /** Get the customer's available balance. */
111             double balance = customerAccountEJB.balance();
112 
113             /**
114              * If the balance is greater than the request's total, set the 
115              * "balance available" flag to true.
116              */
117             if (customerAccountEJB.balance() > totalAmount
118             
119                    m_isBalanceAvailable = true
120             }
121             /**
122              * If the inventory is greater than the request's total, set the 
123              * "inventory available" flag to true.
124              */
125             if (quantityAvailable >= quantityRequested)
126             {
127                 m_isInventoryAvailable = true;
128             }
129             /**
130              * If both "inventory available" and "balance available" flags are
131              * true, submit the purchase order and decrement both inventory and 
132              * balance. If either is false, roll back the transaction.
133              */
134             if (m_isBalanceAvailable && m_isInventoryAvailable
135             {                
136                 customerAccountEJB.withdraw(totalAmount);
137                 itemsDB.updateInventory(itemNumber, quantityAvailable - quantityRequested);
138                 callback.onTransactionComplete("Transaction successful.", m_isBalanceAvailable,
139                     m_isInventoryAvailable);
140             }
141             else 
142             {
143                 callback.onTransactionComplete("Unable to complete the transaction."
144                     m_isBalanceAvailable, m_isInventoryAvailable);
145             }
146 
147             /**
148              * Call the EJB's remove method to delete the bean's record from 
149              * the database. This allows you to test repeatedly with the same
150              * customer number.
151              */
152             customerAccountEJB.remove();
153         }
154         catch (RemoveException rve)
155         {
156             throw new ControlException ("VerifyFunds: Error removing Account EJB. " +
157                 "Transaction canceled.", rve);
158         }
159         catch (RemoteException re)
160         {
161             throw new ControlException ("VerifyFunds: Error getting information about the " +
162                 "account. Transaction canceled.", re);
163         
164         catch (ProcessingErrorException pe)
165         {
166             throw new ControlException ("VerifyFunds: Error withdrawing funds. " +
167                 "Transaction canceled.", pe);
168         }
169 
170     }
171 
172     /**
173      * Used to create a new instance of an Account EJB representing the
174      * customer's account. Purchase order details are calculated and the
175      * resulting total is checked against the amount of money the customer
176      * has.
177      
178      @param  initialBalance  The amount of money the customer can spend.
179      @param  accountID  The customer's unique ID.
180      @return  A message indicating whether or not the account
181      * was created.
182      */
183     private String openAccount(double initialBalance, String accountID
184     {
185     String result = null;        
186         String accountType = "checking";
187 
188     try 
189         {
190       Account buyerAccount = customerAccountEJB.create(accountID, 
191                 initialBalance, accountType);
192       result = "Created account: " + accountID;
193     
194     catch (Exception e
195         {
196       throw new RuntimeException("Create account failed for account ID:" + accountID);
197     }
198         return result;
199     }
200 
201