001: // OrderPlacementSLR.java
002: // Stateless Session bean
003:
004: package org.objectweb.jonas.stests.appli;
005:
006: import java.rmi.RemoteException;
007: import java.util.Enumeration;
008: import java.util.Vector;
009:
010: import javax.ejb.CreateException;
011: import javax.ejb.SessionBean;
012: import javax.ejb.SessionContext;
013: import javax.naming.InitialContext;
014: import javax.rmi.PortableRemoteObject;
015:
016: import org.objectweb.jonas.common.Log;
017: import org.objectweb.util.monolog.api.BasicLevel;
018: import org.objectweb.util.monolog.api.Logger;
019:
020: /**
021: *
022: */
023: public class OrderPlacementSLR implements SessionBean {
024:
025: static private Logger logger = null;
026: private transient SessionContext ctx;
027:
028: // ------------------------------------------------------------------
029: // SessionBean implementation
030: // ------------------------------------------------------------------
031:
032: public void setSessionContext(SessionContext ctxt) {
033: if (logger == null) {
034: logger = Log.getLogger("org.objectweb.jonas_tests");
035: }
036: logger.log(BasicLevel.DEBUG, "");
037: ctx = ctxt;
038: }
039:
040: public void ejbRemove() {
041: logger.log(BasicLevel.DEBUG, "");
042: }
043:
044: public void ejbCreate() throws CreateException {
045: logger.log(BasicLevel.DEBUG, "");
046: }
047:
048: public void ejbPassivate() {
049: logger.log(BasicLevel.DEBUG, "");
050: }
051:
052: public void ejbActivate() {
053: logger.log(BasicLevel.DEBUG, "");
054: }
055:
056: // ------------------------------------------------------------------
057: // OrderPlacement implementation
058: // ------------------------------------------------------------------
059:
060: // Implementation of business methods defined in remote interface OrderPlacement
061: /**
062: * Convenience method to place an order using all Strings for the IDs.
063: * Converts district id String to an int and calls the appropriate placeOrder method.
064: * @param wID String Warehouse ID
065: * @param dID String District ID
066: * @param cID Integer Customer ID
067: * @param orderLines Vector of OrderDetail objects.
068: * Each OrderDetail object represents one line item on the order.
069: * @exception javax.ejb.RemoteException
070: */
071: public float placeOrder(String wID, String dID, Integer cID,
072: Vector orderLines) throws RemoteException {
073:
074: try {
075: int districtInt = Integer.parseInt(dID);
076: return placeOrder(wID, districtInt, cID, orderLines);
077: } catch (NumberFormatException nfe) {
078: throw new RemoteException("Unable to covert district id ("
079: + cID + ") to an int.");
080: }
081:
082: }
083:
084: /**
085: * Place an order.
086: * @param wID String Warehouse ID
087: * @param dID int District ID
088: * @param cID Integer Customer ID
089: * @param orderLines Vector of OrderDetail objects.
090: * Each OrderDetail object represents one line item on the order.
091: * @return The order number assigned to this order.
092: * @exception javax.ejb.RemoteException
093: */
094: public float placeOrder(String wID, int dID, Integer cID,
095: Vector orderLines) throws RemoteException {
096: float oID = 0;
097: logger.log(BasicLevel.DEBUG, "");
098: try {
099: // The InitialContext will let us retrieve references to the entity beans we need.
100: InitialContext initCtx = new InitialContext();
101:
102: //Get the Order Number from the District entity bean.
103: DistrictHome dHome = (DistrictHome) PortableRemoteObject
104: .narrow(initCtx.lookup("DistrictHome"),
105: DistrictHome.class);
106: DistrictID districtID = new DistrictID(dID, wID);
107: District district = (District) dHome
108: .findByPrimaryKey(districtID);
109: //'true' tells the District to increment the order id.
110: oID = district.getNextOrderId(true);
111: logger.log(BasicLevel.DEBUG,
112: "(District)dHome.findByPrimaryKey(" + districtID
113: + "); OK");
114:
115: //Update the Stock level for each item in an order line using the Stock entity bean
116: StockHome sHome = (StockHome) PortableRemoteObject.narrow(
117: initCtx.lookup("StockHome"), StockHome.class);
118: Enumeration lines = orderLines.elements();
119: float orderTotal = 0;
120: while (lines.hasMoreElements()) {
121: OrderDetail od = (OrderDetail) lines.nextElement();
122: Integer itemID = od.getItemID();
123: int itemQty = od.getItemQty();
124: //Calculate the order total while we are going through the orders.
125: orderTotal += od.getItemAmount();
126: StockID stockID = new StockID(wID, itemID);
127: Stock stock = (Stock) sHome.findByPrimaryKey(stockID);
128: logger.log(BasicLevel.DEBUG,
129: "(Stock)sHome.findByPrimaryKey(" + stockID
130: + ");OK ");
131: stock.decreaseStockQuantity(itemQty);
132: logger.log(BasicLevel.DEBUG,
133: "stock.decreaseStockQuantity(" + itemQty + ")");
134: }
135:
136: //Save the Order to the database by creating an Order entity bean
137: OrderHome oHome = (OrderHome) PortableRemoteObject.narrow(
138: initCtx.lookup("OrderHome"), OrderHome.class);
139: Order o = oHome.create(wID, dID, cID, oID, orderLines);
140: System.out.println(">>>>>>>> CID= " + o.getCustomerID()
141: + " LC= " + o.getOrderLineCount());
142: logger.log(BasicLevel.DEBUG, "OrderHome.create(" + wID
143: + "," + dID + "," + cID + "," + oID + ","
144: + orderLines + "); OK");
145: //Update the Customer records using the Customer entity bean
146: CustomerHome cHome = (CustomerHome) PortableRemoteObject
147: .narrow(initCtx.lookup("CustomerHome"),
148: CustomerHome.class);
149:
150: Customer customer = (Customer) cHome.findByPrimaryKey(cID);
151: customer.updateBalance(orderTotal);
152: logger.log(BasicLevel.DEBUG,
153: "(Customer)cHome.findByPrimaryKey(" + cID
154: + "); OK");
155: //Write the order to the data queue.
156: try {
157: writeDataQueue(wID, dID, cID, oID);
158: } catch (Exception e) {
159: System.out.println("WriteDataQueue error: "
160: + e.getMessage());
161: throw new RemoteException(e.getMessage());
162: }
163:
164: } catch (Exception e) {
165: throw new RemoteException(e.getMessage());
166: }
167: logger.log(BasicLevel.DEBUG,
168: "OrderPacementBean: placeOrder return " + oID);
169: return oID;
170: }
171:
172: private void writeDataQueue(String wID, int dID, Integer cID,
173: float oID) throws Exception {
174: }
175: }
|