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.Arrays;
19 import java.util.Collections;
20 import java.util.List;
21
22 import javax.servlet.http.HttpSession;
23
24 import net.sourceforge.stripes.action.ForwardResolution;
25 import net.sourceforge.stripes.action.Resolution;
26 import net.sourceforge.stripes.action.SessionScope;
27 import net.sourceforge.stripes.integration.spring.SpringBean;
28
29 import org.mybatis.jpetstore.domain.Order;
30 import org.mybatis.jpetstore.service.OrderService;
31
32
33
34
35
36
37 @SessionScope
38 public class OrderActionBean extends AbstractActionBean {
39
40
41 private static final long serialVersionUID = -6171288227470176272L;
42
43
44 private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
45
46 private static final String LIST_ORDERS = "/WEB-INF/jsp/order/ListOrders.jsp";
47
48 private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
49
50 private static final String SHIPPING = "/WEB-INF/jsp/order/ShippingForm.jsp";
51
52 private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
53
54
55 private static final List<String> CARD_TYPE_LIST;
56
57
58 @SpringBean
59 private transient OrderService orderService;
60
61
62 private Order order = new Order();
63
64 private boolean shippingAddressRequired;
65
66 private boolean confirmed;
67
68 private List<Order> orderList;
69
70 static {
71 CARD_TYPE_LIST = Collections.unmodifiableList(Arrays.asList("Visa", "MasterCard", "American Express"));
72 }
73
74
75
76
77
78
79 public int getOrderId() {
80 return order.getOrderId();
81 }
82
83
84
85
86
87
88
89 public void setOrderId(int orderId) {
90 order.setOrderId(orderId);
91 }
92
93
94
95
96
97
98 public Order getOrder() {
99 return order;
100 }
101
102
103
104
105
106
107
108 public void setOrder(Order order) {
109 this.order = order;
110 }
111
112
113
114
115
116
117 public boolean isShippingAddressRequired() {
118 return shippingAddressRequired;
119 }
120
121
122
123
124
125
126
127 public void setShippingAddressRequired(boolean shippingAddressRequired) {
128 this.shippingAddressRequired = shippingAddressRequired;
129 }
130
131
132
133
134
135
136 public boolean isConfirmed() {
137 return confirmed;
138 }
139
140
141
142
143
144
145
146 public void setConfirmed(boolean confirmed) {
147 this.confirmed = confirmed;
148 }
149
150
151
152
153
154
155 public List<String> getCreditCardTypes() {
156 return CARD_TYPE_LIST;
157 }
158
159
160
161
162
163
164 public List<Order> getOrderList() {
165 return orderList;
166 }
167
168
169
170
171
172
173 public Resolution listOrders() {
174 HttpSession session = context.getRequest().getSession();
175 AccountActionBean accountBean = (AccountActionBean) session.getAttribute("/actions/Account.action");
176 orderList = orderService.getOrdersByUsername(accountBean.getAccount().getUsername());
177 return new ForwardResolution(LIST_ORDERS);
178 }
179
180
181
182
183
184
185 public Resolution newOrderForm() {
186 HttpSession session = context.getRequest().getSession();
187 AccountActionBean accountBean = (AccountActionBean) session.getAttribute("/actions/Account.action");
188 CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
189
190 clear();
191 if (accountBean == null || !accountBean.isAuthenticated()) {
192 setMessage("You must sign on before attempting to check out. Please sign on and try checking out again.");
193 return new ForwardResolution(AccountActionBean.class);
194 } else if (cartBean != null) {
195 order.initOrder(accountBean.getAccount(), cartBean.getCart());
196 return new ForwardResolution(NEW_ORDER);
197 } else {
198 setMessage("An order could not be created because a cart could not be found.");
199 return new ForwardResolution(ERROR);
200 }
201 }
202
203
204
205
206
207
208 public Resolution newOrder() {
209 HttpSession session = context.getRequest().getSession();
210
211 if (shippingAddressRequired) {
212 shippingAddressRequired = false;
213 return new ForwardResolution(SHIPPING);
214 } else if (!isConfirmed()) {
215 return new ForwardResolution(CONFIRM_ORDER);
216 } else if (getOrder() != null) {
217
218 orderService.insertOrder(order);
219
220 CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
221 cartBean.clear();
222
223 setMessage("Thank you, your order has been submitted.");
224
225 return new ForwardResolution(VIEW_ORDER);
226 } else {
227 setMessage("An error occurred processing your order (order was null).");
228 return new ForwardResolution(ERROR);
229 }
230 }
231
232
233
234
235
236
237 public Resolution viewOrder() {
238 HttpSession session = context.getRequest().getSession();
239
240 AccountActionBean accountBean = (AccountActionBean) session.getAttribute("accountBean");
241
242 order = orderService.getOrder(order.getOrderId());
243
244 if (accountBean.getAccount().getUsername().equals(order.getUsername())) {
245 return new ForwardResolution(VIEW_ORDER);
246 } else {
247 order = null;
248 setMessage("You may only view your own orders.");
249 return new ForwardResolution(ERROR);
250 }
251 }
252
253
254
255
256 public void clear() {
257 order = new Order();
258 shippingAddressRequired = false;
259 confirmed = false;
260 orderList = null;
261 }
262
263 }