001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.petstore.action;
008:
009: import javax.ejb.EJB;
010:
011: import org.jfox.framework.annotation.Service;
012: import org.jfox.mvc.ActionContext;
013: import org.jfox.mvc.ActionSupport;
014: import org.jfox.mvc.Invocation;
015: import org.jfox.mvc.PageContext;
016: import org.jfox.mvc.SessionContext;
017: import org.jfox.mvc.annotation.ActionMethod;
018: import org.jfox.petstore.bo.ItemBO;
019: import org.jfox.petstore.domain.Cart;
020: import org.jfox.petstore.domain.CartItem;
021: import org.jfox.petstore.entity.Item;
022:
023: /**
024: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
025: */
026: @Service(id="cart")
027: public class CartAction extends ActionSupport {
028:
029: public static String CART_SESSION_KEY = "__CART__";
030:
031: @EJB
032: ItemBO itemBO;
033:
034: @ActionMethod(name="view",successView="Cart.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
035: public void doGetView(ActionContext actionContext) throws Exception {
036: SessionContext sessionContext = actionContext
037: .getSessionContext();
038: Cart cart = (Cart) sessionContext
039: .getAttribute(CART_SESSION_KEY);
040: if (cart == null) {
041: cart = new Cart();
042: sessionContext.setAttribute(CART_SESSION_KEY, cart);
043: }
044:
045: PageContext pageContext = actionContext.getPageContext();
046: pageContext.setAttribute("cart", cart);
047: }
048:
049: @ActionMethod(name="additem",successView="Cart.vhtml",invocationClass=CartInvocation.class,httpMethod=ActionMethod.HttpMethod.GET)
050: public void doGetAddItem(ActionContext actionContext)
051: throws Exception {
052: CartInvocation invocation = (CartInvocation) actionContext
053: .getInvocation();
054:
055: SessionContext sessionContext = actionContext
056: .getSessionContext();
057: Cart cart = (Cart) sessionContext
058: .getAttribute(CART_SESSION_KEY);
059: if (cart == null) {
060: cart = new Cart();
061: sessionContext.setAttribute(CART_SESSION_KEY, cart);
062: }
063: if (!cart.containsItemId(invocation.getWorkingItemId())) {
064: Item item = itemBO.getItem(invocation.getWorkingItemId());
065: cart.addItem(item, itemBO.isItemInStock(invocation
066: .getWorkingItemId()));
067: } else {
068: cart.incrementQuantityByItemId(invocation
069: .getWorkingItemId());
070: }
071:
072: PageContext pageContext = actionContext.getPageContext();
073: pageContext.setAttribute("cart", cart);
074: }
075:
076: @ActionMethod(name="removeitem",successView="Cart.vhtml",invocationClass=CartInvocation.class,httpMethod=ActionMethod.HttpMethod.GET)
077: public void doGetRemoveItem(ActionContext actionContext)
078: throws Exception {
079: CartInvocation invocation = (CartInvocation) actionContext
080: .getInvocation();
081: SessionContext sessionContext = actionContext
082: .getSessionContext();
083: Cart cart = (Cart) sessionContext
084: .getAttribute(CART_SESSION_KEY);
085: if (cart == null) {
086: cart = new Cart();
087: sessionContext.setAttribute(CART_SESSION_KEY, cart);
088: }
089: if (cart.containsItemId(invocation.getWorkingItemId())) {
090: cart.removeItemById(invocation.getWorkingItemId());
091: }
092:
093: PageContext pageContext = actionContext.getPageContext();
094: pageContext.setAttribute("cart", cart);
095:
096: }
097:
098: @ActionMethod(name="update",successView="Cart.vhtml",invocationClass=Invocation.class,httpMethod=ActionMethod.HttpMethod.POST)
099: public void doPostUpdate(ActionContext actionContext)
100: throws Exception {
101: Invocation invocation = actionContext.getInvocation();
102: SessionContext sessionContext = actionContext
103: .getSessionContext();
104: Cart cart = (Cart) sessionContext
105: .getAttribute(CART_SESSION_KEY);
106: if (cart == null) {
107: cart = new Cart();
108: sessionContext.setAttribute(CART_SESSION_KEY, cart);
109: }
110: for (String key : invocation.attributeKeys()) {
111: CartItem cartItem = cart.getCartItem(key);
112: if (cartItem != null) {
113: try {
114: int quantity = Integer.parseInt((String) invocation
115: .getAttribute(key));
116: cartItem.setQuantity(quantity);
117: } catch (NumberFormatException e) {
118: e.printStackTrace();
119: }
120: }
121: }
122:
123: PageContext pageContext = actionContext.getPageContext();
124: pageContext.setAttribute("cart", cart);
125:
126: }
127:
128: @ActionMethod(name="checkout",successView="Checkout.vhtml",httpMethod=ActionMethod.HttpMethod.GET)
129: public void doGetCheckout(ActionContext actionContext)
130: throws Exception {
131: SessionContext sessionContext = actionContext
132: .getSessionContext();
133: Cart cart = (Cart) sessionContext
134: .getAttribute(CART_SESSION_KEY);
135: PageContext pageContext = actionContext.getPageContext();
136: pageContext.setAttribute("cart", cart);
137: }
138:
139: public static class CartInvocation extends Invocation {
140: private String workingItemId;
141:
142: public String getWorkingItemId() {
143: return workingItemId;
144: }
145:
146: public void setWorkingItemId(String workingItemId) {
147: this .workingItemId = workingItemId;
148: }
149: }
150:
151: public static void main(String[] args) {
152:
153: }
154: }
|