1 7 package org.jfox.petstore.action; 8 9 import javax.ejb.EJB ; 10 11 import org.jfox.petstore.bo.ItemBO; 12 import org.jfox.petstore.domain.Cart; 13 import org.jfox.petstore.domain.CartItem; 14 import org.jfox.petstore.entity.Item; 15 import org.jfox.framework.annotation.Service; 16 import org.jfox.mvc.ActionSupport; 17 import org.jfox.mvc.Invocation; 18 import org.jfox.mvc.InvocationContext; 19 import org.jfox.mvc.PageContext; 20 import org.jfox.mvc.SessionContext; 21 import org.jfox.mvc.annotation.ActionMethod; 22 23 26 @Service(id = "cart") 27 public class CartAction extends ActionSupport { 28 29 public static String CART_SESSION_KEY = "__CART__"; 30 31 @EJB 32 ItemBO itemBO; 33 34 @ActionMethod(successView = "Cart.vhtml") 35 public void doGetView(InvocationContext invocationContext) throws Exception { 36 SessionContext sessionContext = invocationContext.getSessionContext(); 37 Cart cart = (Cart)sessionContext.getAttribute(CART_SESSION_KEY); 38 if (cart == null) { 39 cart = new Cart(); 40 sessionContext.setAttribute(CART_SESSION_KEY, cart); 41 } 42 43 PageContext pageContext = invocationContext.getPageContext(); 44 pageContext.setAttribute("cart", cart); 45 } 46 47 @ActionMethod(successView = "Cart.vhtml", invocationClass = CartInvocation.class) 48 public void doGetAddItem(InvocationContext invocationContext) throws Exception { 49 CartInvocation invocation = (CartInvocation)invocationContext.getInvocation(); 50 51 SessionContext sessionContext = invocationContext.getSessionContext(); 52 Cart cart = (Cart)sessionContext.getAttribute(CART_SESSION_KEY); 53 if (cart == null) { 54 cart = new Cart(); 55 sessionContext.setAttribute(CART_SESSION_KEY, cart); 56 } 57 if (!cart.containsItemId(invocation.getWorkingItemId())) { 58 Item item = itemBO.getItem(invocation.getWorkingItemId()); 59 cart.addItem(item, itemBO.isItemInStock(invocation.getWorkingItemId())); 60 } 61 else { 62 cart.incrementQuantityByItemId(invocation.getWorkingItemId()); 63 } 64 65 PageContext pageContext = invocationContext.getPageContext(); 66 pageContext.setAttribute("cart", cart); 67 } 68 69 @ActionMethod(successView = "Cart.vhtml", invocationClass = CartInvocation.class) 70 public void doGetRemoveItem(InvocationContext invocationContext) throws Exception { 71 CartInvocation invocation = (CartInvocation)invocationContext.getInvocation(); 72 SessionContext sessionContext = invocationContext.getSessionContext(); 73 Cart cart = (Cart)sessionContext.getAttribute(CART_SESSION_KEY); 74 if (cart == null) { 75 cart = new Cart(); 76 sessionContext.setAttribute(CART_SESSION_KEY, cart); 77 } 78 if (cart.containsItemId(invocation.getWorkingItemId())) { 79 cart.removeItemById(invocation.getWorkingItemId()); 80 } 81 82 PageContext pageContext = invocationContext.getPageContext(); 83 pageContext.setAttribute("cart", cart); 84 85 } 86 87 @ActionMethod(successView = "Cart.vhtml", invocationClass = Invocation.class) 88 public void doPostUpdate(InvocationContext invocationContext) throws Exception { 89 Invocation invocation = invocationContext.getInvocation(); 90 SessionContext sessionContext = invocationContext.getSessionContext(); 91 Cart cart = (Cart)sessionContext.getAttribute(CART_SESSION_KEY); 92 if (cart == null) { 93 cart = new Cart(); 94 sessionContext.setAttribute(CART_SESSION_KEY, cart); 95 } 96 for (String key : invocation.attributeKeys()) { 97 CartItem cartItem = cart.getCartItem(key); 98 if (cartItem != null) { 99 try { 100 int quantity = Integer.parseInt((String )invocation.getAttribute(key)); 101 cartItem.setQuantity(quantity); 102 } 103 catch (NumberFormatException e) { 104 e.printStackTrace(); 105 } 106 } 107 } 108 109 PageContext pageContext = invocationContext.getPageContext(); 110 pageContext.setAttribute("cart", cart); 111 112 } 113 114 @ActionMethod(successView = "Checkout.vhtml") 115 public void doGetCheckout(InvocationContext invocationContext) throws Exception { 116 SessionContext sessionContext = invocationContext.getSessionContext(); 117 Cart cart = (Cart)sessionContext.getAttribute(CART_SESSION_KEY); 118 PageContext pageContext = invocationContext.getPageContext(); 119 pageContext.setAttribute("cart", cart); 120 } 121 122 public static class CartInvocation extends Invocation { 123 private String workingItemId; 124 125 public String getWorkingItemId() { 126 return workingItemId; 127 } 128 129 public void setWorkingItemId(String workingItemId) { 130 this.workingItemId = workingItemId; 131 } 132 } 133 134 public static void main(String [] args) { 135 136 } 137 } 138 | Popular Tags |