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/WebServices/localControls/nestedControls/

Sample Source Code


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