KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > web > webwork > action > cart > BaseCartAction


1 /*
2  * Created on 25-Feb-2003
3  *
4  * To change this generated comment go to
5  * Window>Preferences>Java>Code Generation>Code Template
6  */

7 package xpetstore.web.webwork.action.cart;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import cirrus.hibernate.Session;
15
16 import xpetstore.domain.Item;
17 import xpetstore.domain.OrderItem;
18
19 import xpetstore.web.webwork.action.BaseAction;
20
21
22 /**
23  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
24  */

25 public abstract class BaseCartAction
26     extends BaseAction
27 {
28     //~ Instance fields --------------------------------------------------------
29

30     private Collection JavaDoc _cartItems;
31
32     //~ Methods ----------------------------------------------------------------
33

34     /**
35      * Returns all the {@link xpetstore.web.cart.CartItem}s
36      * @return Collection
37      * @throws Exception
38      */

39     public Collection JavaDoc getCartItems( )
40         throws Exception JavaDoc
41     {
42         Session s = null;
43         Map JavaDoc cart = getCart( );
44
45         try
46         {
47             if ( _cartItems == null )
48             {
49                 _cartItems = new ArrayList JavaDoc( );
50
51                 if ( cart.size( ) > 0 )
52                 {
53                     s = getHibernateSession( );
54
55                     Iterator JavaDoc it = cart.keySet( ).iterator( );
56
57                     while ( it.hasNext( ) )
58                     {
59                         String JavaDoc itemId = ( String JavaDoc ) it.next( );
60                         Integer JavaDoc quantity = ( Integer JavaDoc ) cart.get( itemId );
61                         Item item = ( Item ) s.load( Item.class, itemId );
62
63                         _cartItems.add( new OrderItem( item, quantity.intValue( ) ) );
64                     }
65                 }
66             }
67
68             return _cartItems;
69         }
70         catch ( Exception JavaDoc e )
71         {
72             e.printStackTrace( );
73             throw e;
74         }
75         finally
76         {
77             if ( s != null )
78             {
79                 s.close( );
80             }
81         }
82     }
83 }
84
Popular Tags