KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > appli > OrderPlacementSLR


1 // OrderPlacementSLR.java
2
// Stateless Session bean
3

4 package org.objectweb.jonas.stests.appli;
5
6 import java.rmi.RemoteException JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 import javax.ejb.CreateException JavaDoc;
11 import javax.ejb.SessionBean JavaDoc;
12 import javax.ejb.SessionContext JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.rmi.PortableRemoteObject JavaDoc;
15
16 import org.objectweb.jonas.common.Log;
17 import org.objectweb.util.monolog.api.BasicLevel;
18 import org.objectweb.util.monolog.api.Logger;
19
20
21 /**
22  *
23  */

24 public class OrderPlacementSLR implements SessionBean JavaDoc {
25
26     static private Logger logger = null;
27     private transient SessionContext JavaDoc ctx;
28
29     // ------------------------------------------------------------------
30
// SessionBean implementation
31
// ------------------------------------------------------------------
32

33
34     public void setSessionContext(SessionContext JavaDoc ctxt) {
35         if (logger == null) {
36             logger = Log.getLogger("org.objectweb.jonas_tests");
37         }
38         logger.log(BasicLevel.DEBUG, "");
39         ctx = ctxt;
40     }
41         
42
43     public void ejbRemove() {
44         logger.log(BasicLevel.DEBUG, "");
45     }
46         
47
48     public void ejbCreate() throws CreateException JavaDoc {
49         logger.log(BasicLevel.DEBUG, "");
50     }
51
52     public void ejbPassivate() {
53         logger.log(BasicLevel.DEBUG, "");
54     }
55
56     public void ejbActivate() {
57         logger.log(BasicLevel.DEBUG, "");
58     }
59     
60     // ------------------------------------------------------------------
61
// OrderPlacement implementation
62
// ------------------------------------------------------------------
63

64     // Implementation of business methods defined in remote interface OrderPlacement
65
/**
66      * Convenience method to place an order using all Strings for the IDs.
67      * Converts district id String to an int and calls the appropriate placeOrder method.
68      * @param wID String Warehouse ID
69      * @param dID String District ID
70      * @param cID Integer Customer ID
71      * @param orderLines Vector of OrderDetail objects.
72      * Each OrderDetail object represents one line item on the order.
73      * @exception javax.ejb.RemoteException
74      */

75     public float placeOrder(String JavaDoc wID, String JavaDoc dID, Integer JavaDoc cID, Vector JavaDoc orderLines)
76     throws RemoteException JavaDoc{
77
78     try {
79         int districtInt = Integer.parseInt(dID);
80         return placeOrder(wID, districtInt, cID, orderLines);
81     } catch (NumberFormatException JavaDoc nfe) {
82         throw new RemoteException JavaDoc("Unable to covert district id (" + cID + ") to an int.");
83     }
84             
85     }
86
87
88    /**
89      * Place an order.
90      * @param wID String Warehouse ID
91      * @param dID int District ID
92      * @param cID Integer Customer ID
93      * @param orderLines Vector of OrderDetail objects.
94      * Each OrderDetail object represents one line item on the order.
95      * @return The order number assigned to this order.
96      * @exception javax.ejb.RemoteException
97      */

98     public float placeOrder(String JavaDoc wID, int dID, Integer JavaDoc cID, Vector JavaDoc orderLines)
99         throws RemoteException JavaDoc {
100         float oID = 0;
101         logger.log(BasicLevel.DEBUG, "");
102         try {
103             // The InitialContext will let us retrieve references to the entity beans we need.
104
InitialContext JavaDoc initCtx = new InitialContext JavaDoc();
105             
106             //Get the Order Number from the District entity bean.
107
DistrictHome dHome = (DistrictHome) PortableRemoteObject.narrow(initCtx.lookup("DistrictHome"), DistrictHome.class);
108             DistrictID districtID = new DistrictID(dID, wID);
109             District district = (District)dHome.findByPrimaryKey(districtID);
110             //'true' tells the District to increment the order id.
111
oID = district.getNextOrderId(true);
112             logger.log(BasicLevel.DEBUG,"(District)dHome.findByPrimaryKey("+districtID+"); OK");
113
114             //Update the Stock level for each item in an order line using the Stock entity bean
115
StockHome sHome = (StockHome) PortableRemoteObject.narrow(initCtx.lookup("StockHome"), StockHome.class);
116             Enumeration JavaDoc lines = orderLines.elements();
117             float orderTotal = 0;
118             while(lines.hasMoreElements()){
119                 OrderDetail od = (OrderDetail)lines.nextElement();
120                 Integer JavaDoc itemID = od.getItemID();
121                 int itemQty = od.getItemQty();
122                 //Calculate the order total while we are going through the orders.
123
orderTotal += od.getItemAmount();
124                 StockID stockID = new StockID(wID, itemID);
125                 Stock stock = (Stock)sHome.findByPrimaryKey(stockID);
126                 logger.log(BasicLevel.DEBUG,"(Stock)sHome.findByPrimaryKey("+stockID+");OK ");
127                 stock.decreaseStockQuantity(itemQty);
128                 logger.log(BasicLevel.DEBUG,"stock.decreaseStockQuantity("+itemQty+")");
129             }
130             
131             //Save the Order to the database by creating an Order entity bean
132
OrderHome oHome = (OrderHome) PortableRemoteObject.narrow(initCtx.lookup("OrderHome"), OrderHome.class);
133             Order o = oHome.create(wID, dID, cID, oID, orderLines);
134             System.out.println(">>>>>>>> CID= "+o.getCustomerID()+ " LC= "+o.getOrderLineCount());
135             logger.log(BasicLevel.DEBUG,"OrderHome.create("+wID+","+ dID+","+ cID+","+ oID+","+ orderLines+"); OK");
136             //Update the Customer records using the Customer entity bean
137
CustomerHome cHome = (CustomerHome) PortableRemoteObject.narrow(initCtx.lookup("CustomerHome"), CustomerHome.class);
138            
139             Customer customer = (Customer)cHome.findByPrimaryKey(cID);
140             customer.updateBalance(orderTotal);
141             logger.log(BasicLevel.DEBUG,"(Customer)cHome.findByPrimaryKey("+cID+"); OK");
142             //Write the order to the data queue.
143
try{
144                 writeDataQueue(wID, dID, cID, oID);
145             } catch(Exception JavaDoc e){
146                 System.out.println("WriteDataQueue error: " + e.getMessage());
147                 throw new RemoteException JavaDoc(e.getMessage());
148             }
149             
150         } catch (Exception JavaDoc e) {
151             throw new RemoteException JavaDoc(e.getMessage());
152         }
153         logger.log(BasicLevel.DEBUG,"OrderPacementBean: placeOrder return "+oID);
154         return oID;
155     }
156
157     private void writeDataQueue(String JavaDoc wID, int dID, Integer JavaDoc cID, float oID) throws Exception JavaDoc {}
158 }
159
160
Popular Tags