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
41 private static final long serialVersionUID = -4038684592582714235L;
42
43
44 private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
45
46 private static final String CHECK_OUT = "/WEB-INF/jsp/cart/Checkout.jsp";
47
48
49 @SpringBean
50 private transient CatalogService catalogService;
51
52
53 private Cart cart = new Cart();
54
55 private String workingItemId;
56
57
58
59
60
61
62 public Cart getCart() {
63 return cart;
64 }
65
66
67
68
69
70
71
72 public void setCart(Cart cart) {
73 this.cart = cart;
74 }
75
76
77
78
79
80
81
82 public void setWorkingItemId(String workingItemId) {
83 this.workingItemId = workingItemId;
84 }
85
86
87
88
89
90
91 public Resolution addItemToCart() {
92
93 if (workingItemId == null || workingItemId.trim().isEmpty()) {
94 setMessage("Invalid item ID: cannot add item to cart.");
95 return new ForwardResolution(ERROR);
96 }
97
98 if (cart.containsItemId(workingItemId)) {
99 cart.incrementQuantityByItemId(workingItemId);
100 } else {
101
102
103
104 boolean isInStock = catalogService.isItemInStock(workingItemId);
105 Item item = catalogService.getItem(workingItemId);
106 cart.addItem(item, isInStock);
107 }
108
109 return new ForwardResolution(VIEW_CART);
110 }
111
112
113
114
115
116
117 public Resolution removeItemFromCart() {
118
119 if (workingItemId == null || workingItemId.trim().isEmpty()) {
120 setMessage("Invalid item ID: cannot remove item from cart.");
121 return new ForwardResolution(ERROR);
122 }
123
124 Item item = cart.removeItemById(workingItemId);
125
126 if (item == null) {
127 setMessage("Attempted to remove null CartItem from Cart.");
128 return new ForwardResolution(ERROR);
129 } else {
130 return new ForwardResolution(VIEW_CART);
131 }
132 }
133
134
135
136
137
138
139 public Resolution updateCartQuantities() {
140 HttpServletRequest request = context.getRequest();
141
142 Iterator<CartItem> cartItems = getCart().getAllCartItems();
143 while (cartItems.hasNext()) {
144 CartItem cartItem = cartItems.next();
145 String itemId = cartItem.getItem().getItemId();
146 try {
147 int quantity = Integer.parseInt(request.getParameter(itemId));
148 getCart().setQuantityByItemId(itemId, quantity);
149 if (quantity < 1) {
150 cartItems.remove();
151 }
152 } catch (NumberFormatException e) {
153
154 }
155 }
156
157 return new ForwardResolution(VIEW_CART);
158 }
159
160
161
162
163
164
165 public ForwardResolution viewCart() {
166 return new ForwardResolution(VIEW_CART);
167 }
168
169
170
171
172
173
174 public ForwardResolution checkOut() {
175 return new ForwardResolution(CHECK_OUT);
176 }
177
178
179
180
181 public void clear() {
182 cart = new Cart();
183 workingItemId = null;
184 }
185
186 }