001: package com.ibatis.jpetstore.presentation;
002:
003: import com.ibatis.jpetstore.domain.Cart;
004: import com.ibatis.jpetstore.domain.CartItem;
005: import com.ibatis.jpetstore.domain.Item;
006: import com.ibatis.jpetstore.service.CatalogService;
007: import com.ibatis.struts.ActionContext;
008: import com.ibatis.struts.BaseBean;
009:
010: import java.util.Iterator;
011: import java.util.Map;
012:
013: import org.huihoo.jfox.soaf.container.ServiceFactory;
014:
015: public class CartBean extends BaseBean {
016:
017: /* Constants */
018:
019: private CatalogService catalogService = (CatalogService) ServiceFactory
020: .getInstance().getService(CatalogService.class);
021:
022: /* Private Fields */
023:
024: private Cart cart = new Cart();
025:
026: private String workingItemId;
027:
028: private String pageDirection;
029:
030: /* JavaBeans Properties */
031:
032: public Cart getCart() {
033: return cart;
034: }
035:
036: public void setCart(Cart cart) {
037: this .cart = cart;
038: }
039:
040: public String getWorkingItemId() {
041: return workingItemId;
042: }
043:
044: public void setWorkingItemId(String workingItemId) {
045: this .workingItemId = workingItemId;
046: }
047:
048: public String getPageDirection() {
049: return pageDirection;
050: }
051:
052: public void setPageDirection(String pageDirection) {
053: this .pageDirection = pageDirection;
054: }
055:
056: /* Public Methods */
057:
058: public String addItemToCart() {
059: if (cart.containsItemId(workingItemId)) {
060: cart.incrementQuantityByItemId(workingItemId);
061: } else {
062: // isInStock is a "real-time" property that must be updated
063: // every time an item is added to the cart, even if other
064: // item details are cached.
065: boolean isInStock = catalogService
066: .isItemInStock(workingItemId);
067: Item item = catalogService.getItem(workingItemId);
068: cart.addItem(item, isInStock);
069: }
070:
071: return "success";
072: }
073:
074: public String removeItemFromCart() {
075:
076: Item item = cart.removeItemById(workingItemId);
077:
078: if (item == null) {
079: ActionContext.getActionContext().setSimpleMessage(
080: "Attempted to remove null CartItem from Cart.");
081: return "failure";
082: } else {
083: return "success";
084: }
085: }
086:
087: public String updateCartQuantities() {
088: Map parameterMap = ActionContext.getActionContext()
089: .getParameterMap();
090:
091: Iterator cartItems = getCart().getAllCartItems();
092: while (cartItems.hasNext()) {
093: CartItem cartItem = (CartItem) cartItems.next();
094: String itemId = cartItem.getItem().getItemId();
095: try {
096: int quantity = Integer.parseInt((String) parameterMap
097: .get(itemId));
098: getCart().setQuantityByItemId(itemId, quantity);
099: if (quantity < 1) {
100: cartItems.remove();
101: }
102: } catch (Exception e) {
103: //ignore on purpose
104: }
105: }
106:
107: return "success";
108: }
109:
110: public String switchCartPage() {
111: if ("next".equals(pageDirection)) {
112: cart.getCartItemList().nextPage();
113: } else if ("previous".equals(pageDirection)) {
114: cart.getCartItemList().previousPage();
115: }
116: return "success";
117: }
118:
119: public String viewCart() {
120: return "success";
121: }
122:
123: public void clear() {
124: cart = new Cart();
125: workingItemId = null;
126: pageDirection = null;
127: }
128:
129: }
|