package com.sun.j2ee.blueprints.cart.ejb;
import java.util.Locale;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collection;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import com.sun.j2ee.blueprints.util.tracer.Debug;
import com.sun.j2ee.blueprints.cart.model.CartItem;
import com.sun.j2ee.blueprints.catalog.client.CatalogHelper;
import com.sun.j2ee.blueprints.catalog.model.Item;
import com.sun.j2ee.blueprints.catalog.client.CatalogException;
public class ShoppingCartLocalEJB implements SessionBean {
private HashMap cart;
private Locale locale = Locale.US;
public ShoppingCartLocalEJB() {
cart = new HashMap();
}
public HashMap getDetails() {
return cart;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public Collection getItems() {
CatalogHelper catalog = new CatalogHelper(false);
HashMap map = getDetails();
ArrayList items = new ArrayList();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key = (String)it.next();
Integer value = (Integer)map.get(key);
Item item = null;
try {
item = catalog.getItem(key, locale);
CartItem ci = new CartItem(item.getItemId(),
item.getProductId(),
item.getCategory(),
item.getProductName(),
item.getAttribute(),
value.intValue(),
item.getListCost());
items.add(ci);
} catch (CatalogException cce) {
System.out.println("ShoppingCartEJB caught: " + cce);
}
}
return items;
}
public void addItem (String itemID) {
cart.put(itemID, new Integer(1));
}
public void addItem (String itemID,int qty) {
cart.put(itemID, new Integer(qty));
}
public void deleteItem (String itemID) {
cart.remove(itemID);
}
public void updateItemQuantity (String itemID, int newQty) {
cart.remove(itemID);
if (newQty > 0) cart.put(itemID, new Integer(newQty));
}
public Integer getCount() {
return new Integer(cart.size());
}
public Double getSubTotal() {
Collection items = getItems();
if (items == null) return null;
double ret = 0.0d;
for (Iterator it =items.iterator(); it.hasNext(); ) {
CartItem i = (CartItem) it.next();
ret += (i.getUnitCost() * i.getQuantity()) ;
}
return new Double(ret);
}
public void empty () {
cart.clear();
}
public void ejbCreate() {
cart = new HashMap();
}
public void setSessionContext(SessionContext sc) {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
}