1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.web.actions;
17
18 import java.util.Iterator;
19
20 import javax.servlet.http.HttpServletRequest;
21
22 import net.sourceforge.stripes.action.ForwardResolution;
23 import net.sourceforge.stripes.action.Resolution;
24 import net.sourceforge.stripes.action.SessionScope;
25 import net.sourceforge.stripes.integration.spring.SpringBean;
26
27 import org.mybatis.jpetstore.domain.Cart;
28 import org.mybatis.jpetstore.domain.CartItem;
29 import org.mybatis.jpetstore.domain.Item;
30 import org.mybatis.jpetstore.service.CatalogService;
31
32
33
34
35
36
37 @SessionScope
38 public class CartActionBean extends AbstractActionBean {
39
40 private static final long serialVersionUID = -4038684592582714235L;
41
42 private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
43 private static final String CHECK_OUT = "/WEB-INF/jsp/cart/Checkout.jsp";
44
45 @SpringBean
46 private transient CatalogService catalogService;
47
48 private Cart cart = new Cart();
49 private String workingItemId;
50
51 public Cart getCart() {
52 return cart;
53 }
54
55 public void setCart(Cart cart) {
56 this.cart = cart;
57 }
58
59 public void setWorkingItemId(String workingItemId) {
60 this.workingItemId = workingItemId;
61 }
62
63
64
65
66
67
68 public Resolution addItemToCart() {
69
70 if (workingItemId == null || workingItemId.trim().isEmpty()) {
71 setMessage("Invalid item ID: cannot add item to cart.");
72 return new ForwardResolution(ERROR);
73 }
74
75 if (cart.containsItemId(workingItemId)) {
76 cart.incrementQuantityByItemId(workingItemId);
77 } else {
78
79
80
81 boolean isInStock = catalogService.isItemInStock(workingItemId);
82 Item item = catalogService.getItem(workingItemId);
83 cart.addItem(item, isInStock);
84 }
85
86 return new ForwardResolution(VIEW_CART);
87 }
88
89
90
91
92
93
94 public Resolution removeItemFromCart() {
95
96 if (workingItemId == null || workingItemId.trim().isEmpty()) {
97 setMessage("Invalid item ID: cannot remove item from cart.");
98 return new ForwardResolution(ERROR);
99 }
100
101 Item item = cart.removeItemById(workingItemId);
102
103 if (item == null) {
104 setMessage("Attempted to remove null CartItem from Cart.");
105 return new ForwardResolution(ERROR);
106 } else {
107 return new ForwardResolution(VIEW_CART);
108 }
109 }
110
111
112
113
114
115
116 public Resolution updateCartQuantities() {
117 HttpServletRequest request = context.getRequest();
118
119 Iterator<CartItem> cartItems = getCart().getAllCartItems();
120 while (cartItems.hasNext()) {
121 CartItem cartItem = cartItems.next();
122 String itemId = cartItem.getItem().getItemId();
123 try {
124 int quantity = Integer.parseInt(request.getParameter(itemId));
125 getCart().setQuantityByItemId(itemId, quantity);
126 if (quantity < 1) {
127 cartItems.remove();
128 }
129 } catch (NumberFormatException e) {
130
131 }
132 }
133
134 return new ForwardResolution(VIEW_CART);
135 }
136
137 public ForwardResolution viewCart() {
138 return new ForwardResolution(VIEW_CART);
139 }
140
141 public ForwardResolution checkOut() {
142 return new ForwardResolution(CHECK_OUT);
143 }
144
145 public void clear() {
146 cart = new Cart();
147 workingItemId = null;
148 }
149
150 }