KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > services > cart > ejb > CartBean


1 package xpetstore.services.cart.ejb;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.ejb.Local JavaDoc;
11 import javax.ejb.Stateful JavaDoc;
12 import javax.ejb.Remove JavaDoc;
13
14 import javax.persistence.EntityManager;
15 import javax.persistence.PersistenceContext;
16
17 import org.jboss.annotation.ejb.LocalBinding;
18
19 import xpetstore.domain.catalog.ejb.Item;
20 import xpetstore.domain.catalog.ejb.Product;
21
22 import xpetstore.services.cart.model.CartItem;
23 import xpetstore.services.petstore.ejb.Petstore;
24
25 import xpetstore.util.Debug;
26
27
28 /**
29  *
30  * @ ejb.bean
31  * name="Cart"
32  * type="Stateful"
33  * view-type="local"
34  * @ ejb.transaction
35  * type="Required"
36  * @ ejb.ejb-ref
37  * ejb-name="Item"
38  * view-type="local"
39  * ref-name="ejb/ItemLocal"
40  */

41 @Stateful JavaDoc(name="Cart")
42 @LocalBinding(jndiBinding="ejb/Cart")
43 @Local JavaDoc(Cart.class)
44 public class CartBean
45    implements Cart
46 {
47
48    @PersistenceContext
49    private EntityManager manager;
50    
51     /** Map of item quantities indexed by itemId */
52     private Map JavaDoc _details = new HashMap JavaDoc( );
53
54    public CartBean()
55    {
56       
57    }
58
59     /**
60      * @ ejb.interface-method
61      */

62     public void addItem( String JavaDoc itemId )
63     {
64         addItem( itemId, 1 );
65     }
66
67     /**
68      * @ ejb.interface-method
69      */

70     public void addItem( String JavaDoc itemId,
71                          int qty )
72     {
73         Integer JavaDoc curQty = ( Integer JavaDoc ) _details.get( itemId );
74         if ( curQty == null )
75         {
76             _details.put( itemId, new Integer JavaDoc( qty ) );
77         }
78         else
79         {
80             _details.put( itemId, new Integer JavaDoc( qty + curQty.intValue( ) ) );
81         }
82     }
83
84     /**
85      * @ ejb.interface-method
86      */

87     public void removeItem( String JavaDoc itemId )
88     {
89         _details.remove( itemId );
90     }
91
92     /**
93      * @ ejb.interface-method
94      */

95     public void updateItems( String JavaDoc itemId[],
96                              int newQty[] )
97     {
98         for ( int i = 0; i < itemId.length; i++ )
99         {
100             String JavaDoc id = itemId[ i ];
101             int qty = newQty[ i ];
102
103             if ( _details.containsKey( id ) )
104             {
105                 if ( qty > 0 )
106                 {
107                     _details.put( id, new Integer JavaDoc( qty ) );
108                 }
109             }
110             else
111             {
112                 Debug.print( " can't update item[" + id + "]. This item not in the cart" );
113             }
114         }
115     }
116
117     /**
118      * @ ejb.interface-method
119      */

120     public int getCount( )
121     {
122         return _details.size( );
123     }
124
125     /**
126      * @ ejb.interface-method
127      */

128     public double getTotal( )
129     {
130         double ret = 0.0d;
131         Iterator JavaDoc it = getCartItems( ).iterator( );
132         for ( ; it.hasNext( ); )
133         {
134             CartItem i = ( CartItem ) it.next( );
135             ret += ( i.getUnitCost( ) * i.getQuantity( ) );
136         }
137
138         return ret;
139     }
140
141     /**
142      * @ ejb.interface-method
143      */

144     public void empty( )
145     {
146         _details.clear( );
147     }
148
149     /**
150      * @ return Return a Map of quantities indexed by itemId
151      *
152      * @ ejb.interface-method
153      * @ ejb.transaction-type
154      * type="NotSupported"
155      */

156     public Map JavaDoc getDetails( )
157     {
158         return _details;
159     }
160
161     /**
162      * @ return Return a list of {@link CartItem} objects
163      *
164      * @ ejb.interface-method
165      * @ ejb.transaction-type
166      * type="NotSupported"
167      */

168     public Collection JavaDoc getCartItems( )
169     {
170         try
171         {
172             ArrayList JavaDoc items = new ArrayList JavaDoc( );
173             Iterator JavaDoc it = _details.keySet( ).iterator( );
174             while ( it.hasNext( ) )
175             {
176                 String JavaDoc key = ( String JavaDoc ) it.next( );
177                 Integer JavaDoc value = ( Integer JavaDoc ) _details.get( key );
178                 try
179                 {
180                     Item item = manager.find( Item.class, key );
181              
182                     Product prod = item.getProduct( );
183
184                     CartItem ci = new CartItem( item.getItemId( ), prod.getProductId( ), prod.getName( ), item.getDescription( ), value.intValue( ), item.getListPrice( ) );
185
186                     items.add( ci );
187                 }
188                 catch ( Exception JavaDoc cce )
189                 {
190                     cce.printStackTrace( );
191                 }
192             }
193
194             // Sort the items
195
Collections.sort( items, new CartItem.ItemIdComparator( ) );
196             return items;
197         }
198         catch ( Exception JavaDoc e )
199         {
200             return Collections.EMPTY_LIST;
201         }
202     }
203     
204     @Remove JavaDoc
205     public void remove()
206     {
207        
208     }
209
210 }
211
Popular Tags