FileDocCategorySizeDatePackage
CartBean.javaAPI DocJBoss 4.2.14726Fri Jul 13 20:55:58 BST 2007xpetstore.services.cart.ejb

CartBean

public class CartBean extends Object implements Cart
ejb.bean name="Cart" type="Stateful" view-type="local"
ejb.transaction type="Required"
ejb.ejb-ref ejb-name="Item" view-type="local" ref-name="ejb/ItemLocal"

Fields Summary
private EntityManager
manager
private Map
_details
Map of item quantities indexed by itemId
Constructors Summary
public CartBean()


    
   
      
   
Methods Summary
public voidaddItem(java.lang.String itemId)

ejb.interface-method

        addItem( itemId, 1 );
    
public voidaddItem(java.lang.String itemId, int qty)

ejb.interface-method

        Integer curQty = ( Integer ) _details.get( itemId );
        if ( curQty == null )
        {
            _details.put( itemId, new Integer( qty ) );
        }
        else
        {
            _details.put( itemId, new Integer( qty + curQty.intValue(  ) ) );
        }
    
public voidempty()

ejb.interface-method

        _details.clear(  );
    
public java.util.CollectiongetCartItems()

return Return a list of {@link CartItem} objects
ejb.interface-method
ejb.transaction-type type="NotSupported"

        try
        {
            ArrayList     items = new ArrayList(  );
            Iterator      it = _details.keySet(  ).iterator(  );
            while ( it.hasNext(  ) )
            {
                String  key = ( String ) it.next(  );
                Integer value = ( Integer ) _details.get( key );
                try
                {
                    Item    item = manager.find( Item.class, key );
             
                    Product prod = item.getProduct(  );

                    CartItem     ci = new CartItem( item.getItemId(  ), prod.getProductId(  ), prod.getName(  ), item.getDescription(  ), value.intValue(  ), item.getListPrice(  ) );

                    items.add( ci );
                }
                catch ( Exception cce )
                {
                    cce.printStackTrace(  );
                }
            }

            // Sort the items
            Collections.sort( items, new CartItem.ItemIdComparator(  ) );
            return items;
        }
        catch ( Exception e )
        {
            return Collections.EMPTY_LIST;
        }
    
public intgetCount()

ejb.interface-method

        return _details.size(  );
    
public java.util.MapgetDetails()

return Return a Map of quantities indexed by itemId
ejb.interface-method
ejb.transaction-type type="NotSupported"

        return _details;
    
public doublegetTotal()

ejb.interface-method

        double   ret = 0.0d;
        Iterator it = getCartItems(  ).iterator(  );
        for ( ; it.hasNext(  ); )
        {
            CartItem i = ( CartItem ) it.next(  );
            ret += ( i.getUnitCost(  ) * i.getQuantity(  ) );
        }

        return ret;
    
public voidremove()

       
    
public voidremoveItem(java.lang.String itemId)

ejb.interface-method

        _details.remove( itemId );
    
public voidupdateItems(java.lang.String[] itemId, int[] newQty)

ejb.interface-method

        for ( int i = 0; i < itemId.length; i++ )
        {
            String id = itemId[ i ];
            int    qty = newQty[ i ];

            if ( _details.containsKey( id ) )
            {
                if ( qty > 0 )
                {
                    _details.put( id, new Integer( qty ) );
                }
            }
            else
            {
                Debug.print( " can't update item[" + id + "]. This item not in the cart" );
            }
        }