1 package xpetstore.services.cart.ejb; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.Collections ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import javax.ejb.Local ; 11 import javax.ejb.Stateful ; 12 import javax.ejb.Remove ; 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 41 @Stateful (name="Cart") 42 @LocalBinding(jndiBinding="ejb/Cart") 43 @Local (Cart.class) 44 public class CartBean 45 implements Cart 46 { 47 48 @PersistenceContext 49 private EntityManager manager; 50 51 52 private Map _details = new HashMap ( ); 53 54 public CartBean() 55 { 56 57 } 58 59 62 public void addItem( String itemId ) 63 { 64 addItem( itemId, 1 ); 65 } 66 67 70 public void addItem( String itemId, 71 int qty ) 72 { 73 Integer curQty = ( Integer ) _details.get( itemId ); 74 if ( curQty == null ) 75 { 76 _details.put( itemId, new Integer ( qty ) ); 77 } 78 else 79 { 80 _details.put( itemId, new Integer ( qty + curQty.intValue( ) ) ); 81 } 82 } 83 84 87 public void removeItem( String itemId ) 88 { 89 _details.remove( itemId ); 90 } 91 92 95 public void updateItems( String itemId[], 96 int newQty[] ) 97 { 98 for ( int i = 0; i < itemId.length; i++ ) 99 { 100 String 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 ( 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 120 public int getCount( ) 121 { 122 return _details.size( ); 123 } 124 125 128 public double getTotal( ) 129 { 130 double ret = 0.0d; 131 Iterator 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 144 public void empty( ) 145 { 146 _details.clear( ); 147 } 148 149 156 public Map getDetails( ) 157 { 158 return _details; 159 } 160 161 168 public Collection getCartItems( ) 169 { 170 try 171 { 172 ArrayList items = new ArrayList ( ); 173 Iterator it = _details.keySet( ).iterator( ); 174 while ( it.hasNext( ) ) 175 { 176 String key = ( String ) it.next( ); 177 Integer value = ( Integer ) _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 cce ) 189 { 190 cce.printStackTrace( ); 191 } 192 } 193 194 Collections.sort( items, new CartItem.ItemIdComparator( ) ); 196 return items; 197 } 198 catch ( Exception e ) 199 { 200 return Collections.EMPTY_LIST; 201 } 202 } 203 204 @Remove 205 public void remove() 206 { 207 208 } 209 210 } 211 | Popular Tags |