KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > shop > ShoppingCartBean


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package shop;
59
60 import javax.ejb.CreateException JavaDoc;
61 import javax.ejb.EJBException JavaDoc;
62 import javax.ejb.SessionBean JavaDoc;
63 import javax.ejb.SessionContext JavaDoc;
64 /**
65  * Creation date: (8/4/2001 2:25:19 PM)
66  * @author <a HREF="mailto:gpfau@de.ibm.com">Gerhard Pfau</a>
67  */

68 public class ShoppingCartBean implements SessionBean JavaDoc {
69     private javax.ejb.SessionContext JavaDoc mySessionCtx = null;
70
71     private int fieldTotal = 0;
72
73     private String JavaDoc fieldFirstName;
74     private String JavaDoc fieldLastName;
75     private Address fieldAddress;
76     private String JavaDoc fieldCustomerNumber;
77
78     private Order fieldOrder;
79
80     public Item addItem(String JavaDoc itemNumber, String JavaDoc itemName, int itemQuantity)
81         throws javax.ejb.EJBException JavaDoc, InvalidItemException, OutOfStockException {
82         Item item = new Item();
83
84         if (itemQuantity > 10)
85             throw new OutOfStockException(
86                 "There are less then '"
87                     + itemQuantity
88                     + "' pieces of '"
89                     + itemName
90                     + "' in stock");
91
92         if ((itemNumber == null)
93             || (itemName == null)
94             || (itemNumber.equals(""))
95             || (itemName.equals("")))
96             throw new InvalidItemException(
97                 "Invalid item: Number = '" + itemNumber + "' Name = '" + itemName + "'");
98
99         item.setNumber(itemNumber);
100         item.setName(itemName);
101         item.setQuantity(itemQuantity);
102         item.setPrice(100); // at the moment everything costs 100 currency units :-)
103

104         fieldOrder.addItem(itemNumber, item);
105
106         fieldTotal += item.getPrice();
107
108         return item;
109     }
110
111     /**
112      * ejbActivate method comment
113      * @exception java.rmi.RemoteException The exception description.
114      */

115     public void ejbActivate() throws javax.ejb.EJBException JavaDoc {
116     }
117
118     public void ejbCreate()
119         throws javax.ejb.CreateException JavaDoc, javax.ejb.EJBException JavaDoc {
120         fieldFirstName = null;
121         fieldLastName = null;
122         fieldAddress = null;
123         fieldCustomerNumber = null;
124         fieldOrder = null;
125     }
126
127     public void ejbCreate(
128         String JavaDoc firstName,
129         String JavaDoc lastName,
130         Address address,
131         String JavaDoc customerNumber)
132         throws javax.ejb.CreateException JavaDoc, javax.ejb.EJBException JavaDoc, shop.CreateException {
133         fieldFirstName = firstName;
134         fieldLastName = lastName;
135         fieldAddress = address;
136         fieldCustomerNumber = customerNumber;
137
138         fieldOrder = new Order(customerNumber);
139     }
140
141     /**
142      * ejbPassivate method comment
143      * @exception java.rmi.RemoteException The exception description.
144      */

145     public void ejbPassivate() throws javax.ejb.EJBException JavaDoc {
146     }
147     /**
148      * ejbRemove method comment
149      * @exception java.rmi.RemoteException The exception description.
150      */

151
152     public void ejbRemove() throws javax.ejb.EJBException JavaDoc {
153     }
154     /**
155      * setSessionContext method comment
156      * @param ctx javax.ejb.SessionContext
157      * @exception java.rmi.RemoteException The exception description.
158      */

159
160     public void setSessionContext(javax.ejb.SessionContext JavaDoc ctx)
161         throws javax.ejb.EJBException JavaDoc {
162         mySessionCtx = ctx;
163     }
164
165     public SubmitOrderResult submitOrder(
166         CreditCardInfo creditCardInfo,
167         AirMilesContainer airMilesContainer)
168         throws javax.ejb.EJBException JavaDoc {
169         long orderConfirmationNumber = System.currentTimeMillis();
170         fieldOrder.setConfirmationNumber(orderConfirmationNumber);
171         // fieldAllOrders.add(fieldOrder) ;
172
fieldOrder = null;
173         airMilesContainer.addMiles(1000);
174
175         return new SubmitOrderResult(airMilesContainer, orderConfirmationNumber);
176     }
177
178     public void emptyOrder(String JavaDoc customerNumber) throws javax.ejb.EJBException JavaDoc {
179         //ignore the customer number;
180
fieldOrder.empty();
181         fieldTotal = 0;
182     }
183 }
184
Popular Tags