KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > session > ShoppingCartBean


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 /*
28  * ShoppingCartBean.java
29  *
30  * Created on Aug 16, 2004
31  *
32  */

33 package olstore.session;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.Set;
41 import java.math.BigDecimal;
42
43 import javax.ejb.SessionBean;
44
45 import olstore.dto.ShoppingCartItem;
46 import olstore.framework.EJBHomeFactory;
47
48 import olstore.entity.ItemLocal;
49 import olstore.entity.ItemLocalHome;
50 import olstore.entity.OrderLocalHome;
51 import olstore.entity.UserLocal;
52
53 /* The costs stored in the session bean would be unformatted float values,
54  * @author mwringe
55  *
56  *
57  *
58  */

59 /* it is the presentation layer's responsibility to enforce the formatting
60  * @author mwringe
61  *
62  *
63  *
64  */

65 /* the display of the costs.
66  */

67
68 /**
69  * @ejb.bean name="ShoppingCart"
70  * type="Stateful"
71  * transaction-type="Container"
72  * view-type="local"
73  * local-jndi-name="ShoppingCartLocal_L"
74  *
75  * @ejb.ejb-ref
76  * ejb-name="User"
77  * view-type="local"
78  * ref-name="ejb/UserLocal"
79  *
80  * @ejb.ejb-ref
81  * ejb-name="Item"
82  * view-type="local"
83  * ref-name="ejb/ItemLocal"
84  *
85  * @ejb.ejb-ref
86  * ejb-name="Order"
87  * view-type="local"
88  * ref-name="ejb/OrderLocal"
89  *
90  * @ejb.transaction
91  * type="Required"
92  *
93  *--
94  * This is needed for JOnAS.
95  * If you are not using JOnAS you can safely remove the tags below.
96  * @jonas.bean ejb-name="ShoppingCart"
97  * jndi-name="ShoppingCartLocal"
98  *
99  *--
100  **/

101
102 public abstract class ShoppingCartBean implements SessionBean {
103     private UserLocal customer;
104     private HashMap cart;
105     private javax.ejb.SessionContext ejbContext;
106     private javax.naming.Context initialContext;
107     
108     
109     /**
110      *
111      * @ejb.create-method
112      *
113      */

114     
115     public void ejbCreate(olstore.entity.UserLocal user){
116         customer = user;
117         cart = new HashMap();
118     }
119     
120     /**
121      *
122      * @return User
123      *
124      * @ejb.interface-method
125      *
126      */

127     
128     public olstore.entity.UserLocal getUser(){
129         return customer;
130     }
131     
132     /**
133      *
134      * Add items to shopping cart
135      *
136      * @param itemPK
137      * @param quantity
138      *
139      *
140      */

141      private void addItemToCart(Integer itemPK, Integer quantity){
142         ItemLocal item = null;
143         Integer qty;
144         
145         if(quantity.intValue() <= 0){
146             
147             throw new NumberFormatException("Quantity must be greater than 0:");
148         }
149         try{
150             EJBHomeFactory factory = EJBHomeFactory.getInstance();
151             ItemLocalHome home = (ItemLocalHome) factory.getLocalHome(EJBHomeFactory.ITEM);
152             item = home.findByPrimaryKey(itemPK);
153             if((qty = (Integer)cart.get(item))!=null){
154                 cart.remove(item);
155             }
156             cart.put(item, quantity);
157             
158         }catch(Exception e){
159             throw new javax.ejb.EJBException("Could not add item "
160                     + item , e);
161         }
162     }
163     
164     private BigDecimal recalculateTotal(){
165         Iterator it = cart.keySet().iterator();
166         BigDecimal totalCost = new BigDecimal(0.00f);
167         
168         while(it.hasNext())
169             totalCost = totalCost.add(getCostForItem(((ItemLocal) it.next()).getItemId()));
170         return totalCost;
171     }
172     
173     /**
174      *
175      * @param itemPK
176      * @return quantity of those items in the cart
177      *
178      * @ejb.interface-method
179      *
180      */

181     
182     public Integer getQuantity(Integer itemPK){
183         ItemLocal item = null;
184         
185         try{
186             EJBHomeFactory factory = EJBHomeFactory.getInstance();
187             ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
188             item = home.findByPrimaryKey(itemPK);
189             return ((Integer) cart.get(item));
190         }catch(Exception e){
191             throw new javax.ejb.EJBException("Could not get the quantity for item"
192                     + item , e);
193         }
194         
195     }
196     
197     /**
198      *
199      * Remove a specified number of items from the shopping cart
200      *
201      * @param itemPK
202      * @param quantity
203      *
204      */

205     
206     private void removeItemFromCart(Integer itemPK, Integer quantity){
207         ItemLocal item = null;
208         try{
209             EJBHomeFactory factory = EJBHomeFactory.getInstance();
210             ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
211             item = home.findByPrimaryKey(itemPK);
212             cart.remove(item);
213             cart.put(item, quantity);
214         }catch(Exception e){
215             throw new javax.ejb.EJBException("Could not remove item "
216                     + item , e);
217         }
218         
219     }
220     
221     /**
222      * Remove one item from the cart
223      *
224      * @param itemPK
225      *
226      * @ejb.interface-method
227      *
228      */

229     
230     public void removeItemFromCart(Integer itemPK){
231         ItemLocal item = null;
232         try{
233             EJBHomeFactory factory = EJBHomeFactory.getInstance();
234             ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
235             item = home.findByPrimaryKey(itemPK);
236             cart.remove(item);
237         }catch(Exception e){
238             throw new javax.ejb.EJBException("Could not remove item "
239                     + item , e);
240             
241         }
242     }
243     
244     /**
245      * Update the quantity of an item in the cart
246      *
247      * @param itemPK
248      * @param quantity
249      *
250      * @ejb.interface-method
251      *
252      */

253     
254     public void updateQuantity(Integer itemPK, Integer quantity){
255         ItemLocal item = null;
256         if(quantity.intValue() <= 0){
257             
258             throw new NumberFormatException("Quantity must be greater than 0:");
259         }
260         
261         try{
262             EJBHomeFactory factory = EJBHomeFactory.getInstance();
263             ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
264             item = home.findByPrimaryKey(itemPK);
265             int orig = 0; Integer origInt;
266             if((origInt = ((Integer) (cart.get(item))))!=null)
267                 orig = ((Integer) (cart.get(item))).intValue();
268             if(orig < quantity.intValue())
269                 addItemToCart(itemPK, quantity);
270             else
271                 removeItemFromCart(itemPK, quantity);
272             
273             
274         }catch(Exception e){
275             throw new javax.ejb.EJBException("Could not update item "
276                     + item , e);
277             
278         }
279     }
280     
281     /**
282      * Returns whether or not hte item is in the shopping cart
283      *
284      * @param itemPK
285      * @return whether or not the item is in the shopping cart
286      *
287      * @ejb.interface-method
288      *
289      */

290     
291     public boolean contains(Integer itemPK){
292         ItemLocal item = null;
293         try{
294             EJBHomeFactory factory = EJBHomeFactory.getInstance();
295             ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
296             item = home.findByPrimaryKey(itemPK);
297             return cart.containsKey(item);
298         }catch(Exception e){
299             throw new javax.ejb.EJBException("Could not find item "
300                     + itemPK , e);
301         }
302     }
303     
304     /**
305      * Returns the total number of items in the cart
306      *
307      * @return total number of items in the cart
308      *
309      * @ejb.interface-method
310      */

311     
312     public Integer listNumOfItems(){
313         return new Integer(cart.size());
314     }
315     
316     /**
317      * Returns the total cost of items in the cart
318      *
319      * @return total cost of items in the cart
320      *
321      * @ejb.interface-method
322      *
323      */

324     
325     public BigDecimal getTotalCost(){
326         
327         return recalculateTotal();
328         
329     }
330     
331     /**
332      * Returns the items in the cart
333      *
334      * @return the items in the cart
335      *
336      * @ejb.interface-method
337      *
338      */

339     
340     public Collection getItemsInCart(){
341         return cart.keySet();
342         
343     }
344     
345     
346     /**
347      * Crate orders from the items in the cart
348      *
349      * @ejb.interface-method
350      */

351     
352     public void createOrders(){
353         ItemLocal item = null;
354         String DATE_FORMAT = "yyyy-MM-dd";
355         Date date = new Date();
356         java.text.SimpleDateFormat sdf =
357             new java.text.SimpleDateFormat(DATE_FORMAT);
358         String strdate = sdf.format(date);
359         
360         try{
361             Set items = cart.keySet();
362             EJBHomeFactory factory = EJBHomeFactory.getInstance();
363             OrderLocalHome orderHome = (OrderLocalHome)factory.getLocalHome (EJBHomeFactory.ORDER);
364             Iterator it = items.iterator();
365             
366             Collection purchased = customer.getPurchased();
367             while(it.hasNext())
368             {
369                 item = ((ItemLocal) it.next());
370                 olstore.entity.OrderLocal order = orderHome.create(customer,
371                         item, (Integer)(cart.get(item)), new String("Ordered"),
372                         strdate, getCostForItem(item.getItemId()));
373                 if(!purchased.contains(item)){
374                     purchased.add(item);
375                 }
376             }
377             
378             customer.setPurchased(purchased);
379             clearContents();
380         }catch(Exception e){
381             throw new javax.ejb.EJBException("Could not add item "
382                     + item , e);
383         }
384     }
385     
386     private void clearContents(){
387         cart.clear();
388     }
389     
390     /**
391      * @ejb.interface-method
392      *
393      */

394     public ArrayList shoppingCartToDTOs(){
395         Iterator itemIterator = getItemsInCart().iterator();
396         ArrayList cartEntries = new ArrayList();
397         while(itemIterator.hasNext()){
398             ItemLocal currItemLocal = (ItemLocal)itemIterator.next();
399             ShoppingCartItem cartItem = new ShoppingCartItem(currItemLocal);
400             cartItem.setQuantity(getQuantity(cartItem.getItemId()).toString());
401             cartItem.setPrice(getCostForItem(cartItem.getItemId()));
402             cartEntries.add(cartItem);
403         }
404         
405         return cartEntries;
406     }
407     
408     
409     /**
410      * Takes updated information from the form and transfers to the Shopping Cart.
411      * @param cartEntries
412      *
413      * @ejb.interface-method
414      */

415     public void DTOsToShoppingCart(ArrayList cartEntries){
416         Iterator entriesIterator = cartEntries.iterator();
417         while(entriesIterator.hasNext()){
418             ShoppingCartItem cartItem = (ShoppingCartItem) entriesIterator.next();
419             
420             if(Integer.parseInt(cartItem.getQuantity())==0)
421                 removeItemFromCart(cartItem.getItemId());
422             else
423                 updateQuantity(cartItem.getItemId(), new Integer(cartItem.getQuantity()));
424         }
425         
426         recalculateTotal();
427     }
428     
429     
430     
431     /**
432      * @ejb.interface-method
433      *
434      */

435     
436     public BigDecimal getCostForItem(Integer itemPK){
437         ItemLocal item = null;
438         try{
439             EJBHomeFactory factory = EJBHomeFactory.getInstance();
440             ItemLocalHome home = (ItemLocalHome)factory.getLocalHome (EJBHomeFactory.ITEM);
441             item = home.findByPrimaryKey(itemPK);
442             return (item.getPrice().multiply(new BigDecimal(((Integer) cart.get(item)).toString())));
443         }catch(Exception e){
444             throw new javax.ejb.EJBException("Could not find item "
445                     + itemPK , e);
446         }
447         
448         
449     }
450     
451     public void ejbRemove(){
452     }
453     
454     public void ejbActivate(){
455     }
456     
457     public void ejbPassivate(){
458     }
459     
460     public void setSessionContext(javax.ejb.SessionContext cntx){
461         ejbContext = cntx;
462         try{
463             initialContext = new javax.naming.InitialContext();
464         }catch(javax.naming.NamingException ne){
465             throw new javax.ejb.EJBException(ne);
466         }
467     }
468 }
469     
470     
471
Popular Tags