POVerifyImpl.jcs Sample

This topic inludes the source code for the POVerifyImpl.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/tags/

Sample Source Code


001 package localControls.tags; 
002 
003 import com.bea.control.ControlContext;
004 
005 /**
006  * The POVerify control is designed to
007  * accept the details of an item associated with a purchase order,
008  * and to insert the details into two database tables. The control
009  * has the capability to make the database submission conditional based on 
010  * whether there is enough inventory for the item. It exposes a 
011  * "checkInventory" attribute through which a developer
012  * using the control can request this feature.<br/><br/>
013  
014  * @jcs:jc-jar 
015  *  label="POVerify"
016  *  palette-priority="3" 
017  *  group-name="SampleControls" 
018  *  version="1.0" 
019  *  description="Checks inventory before submitting a purchase order." 
020  * @editor-info:code-gen control-interface="true"
021  * @jcs:control-tags file="POVerify-tags.xml"
022  */
023 public class POVerifyImpl implements POVerify, com.bea.control.ControlSource
024 {
025     /**
026      * @common:control
027      */
028     private localControls.tags.ItemsDatabaseControl itemsDB;
029 
030     /**
031      * @common:context
032      */
033     ControlContext context;
034 
035     /**
036      * As with web services built with WebLogic Workshop, a control method 
037      * exposed to clients is annotated with a common:operation tag. The 
038      * addPurchaseOrder method does most of the work in this control. It is
039      * the only method exposed to clients. 
040      
041      * @common:operation
042      */
043     public boolean addPurchaseOrder(String poNumberString, int customerID, 
044         int itemNumber, int quantity)
045     {
046         /* 
047          * Use the helper function at the bottom of this code to format the
048          * incoming purchase order number to an int that will be accepted by the
049          * database.
050          */
051         int poNumber = formatPO(poNumberString);
052 
053         /* 
054          * Use the ControlContext object's getControlAttribute method to retrieve the 
055          * checkInventory attribute's value set by the developer using the control.
056          * The return type of getControlAttribute is always a String, so convert it to
057          * a boolean through which you can check the value.
058          */      
059         String checkInventoryString = context.getControlAttribute("jc:databaseActions"
060             "checkInventory");
061         boolean checkInventory = new Boolean(checkInventoryString).booleanValue();
062 
063         /* 
064          * If the checkInventory attribute has been set to true, then
065          * check inventory using the Database control before inserting the 
066          * rows pertaining to this request.
067          */
068         if (checkInventory)
069         {
070 
071             /* 
072              * Use the Database control to get the number of items available.
073              */
074             int quantityAvailable = itemsDB.checkInventory(itemNumber);
075 
076             /* 
077              * If the number of items available is equal to or greater than 
078              * the number requested, then go ahead and update the database.
079              */
080             if (quantityAvailable >= quantity
081             {
082                 itemsDB.insertItemCustomer(poNumber, customerID);
083                 itemsDB.insertPOItem(poNumber, itemNumber, quantity);
084                 return true;
085             }
086             
087             /* 
088              * If the number of items available is not equal to or greater
089              * than the number requested, return false to indicated that the 
090              * update was not successful.
091              */
092             else 
093             
094                 return false;             
095             }
096         }
097 
098         /*
099          * If the checkInventory attribute is set to false, then 
100          * simply perform the database inserts and return true to indicate it 
101          * has been done.
102          */
103         else 
104         {
105             itemsDB.insertItemCustomer(poNumber, customerID);
106             itemsDB.insertPOItem(poNumber, itemNumber, quantity);
107             return true;
108         }
109     }
110 
111     /**
112      * Formats a purchase order number, removing
113      * non-numeric characters.
114      */
115     private int formatPO(String stringNumber)
116     {
117         StringBuffer cleanString = new StringBuffer();
118 
119         for (int i = 0; i < stringNumber.length(); i++
120         {
121             if (!(stringNumber.charAt(i'0' || stringNumber.charAt(i'9'))
122                 cleanString.append(stringNumber.charAt(i));
123         }
124         int number = new Integer(cleanString.toString()).intValue();
125         return number;
126     }
127 }