View Javadoc
1   /*
2    *    Copyright 2010-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * The Class OrderActionBean.
34   *
35   * @author Eduardo Macarron
36   */
37  @SessionScope
38  public class OrderActionBean extends AbstractActionBean {
39  
40    /** The serial version uid. */
41    private static final long serialVersionUID = -6171288227470176272L;
42  
43    /** The confirm order. */
44    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
45    /** The list orders. */
46    private static final String LIST_ORDERS = "/WEB-INF/jsp/order/ListOrders.jsp";
47    /** The new order. */
48    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
49    /** The shipping. */
50    private static final String SHIPPING = "/WEB-INF/jsp/order/ShippingForm.jsp";
51    /** The view order. */
52    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
53  
54    /** The card type list. */
55    private static final List<String> CARD_TYPE_LIST;
56  
57    /** The order service. */
58    @SpringBean
59    private transient OrderService orderService;
60  
61    /** The order. */
62    private Order order = new Order();
63    /** The shipping address required. */
64    private boolean shippingAddressRequired;
65    /** The confirmed. */
66    private boolean confirmed;
67    /** The order list. */
68    private List<Order> orderList;
69  
70    static {
71      CARD_TYPE_LIST = Collections.unmodifiableList(Arrays.asList("Visa", "MasterCard", "American Express"));
72    }
73  
74    /**
75     * Gets the order id.
76     *
77     * @return the order id
78     */
79    public int getOrderId() {
80      return order.getOrderId();
81    }
82  
83    /**
84     * Sets the order id.
85     *
86     * @param orderId
87     *          the order id
88     */
89    public void setOrderId(int orderId) {
90      order.setOrderId(orderId);
91    }
92  
93    /**
94     * Gets the order.
95     *
96     * @return the order
97     */
98    public Order getOrder() {
99      return order;
100   }
101 
102   /**
103    * Sets the order.
104    *
105    * @param order
106    *          the order
107    */
108   public void setOrder(Order order) {
109     this.order = order;
110   }
111 
112   /**
113    * Checks if is shipping address required.
114    *
115    * @return true, if successful
116    */
117   public boolean isShippingAddressRequired() {
118     return shippingAddressRequired;
119   }
120 
121   /**
122    * Sets the shipping address required.
123    *
124    * @param shippingAddressRequired
125    *          the shipping address required
126    */
127   public void setShippingAddressRequired(boolean shippingAddressRequired) {
128     this.shippingAddressRequired = shippingAddressRequired;
129   }
130 
131   /**
132    * Checks if is confirmed.
133    *
134    * @return true, if successful
135    */
136   public boolean isConfirmed() {
137     return confirmed;
138   }
139 
140   /**
141    * Sets the confirmed.
142    *
143    * @param confirmed
144    *          the confirmed
145    */
146   public void setConfirmed(boolean confirmed) {
147     this.confirmed = confirmed;
148   }
149 
150   /**
151    * Gets the credit card types.
152    *
153    * @return the credit card types
154    */
155   public List<String> getCreditCardTypes() {
156     return CARD_TYPE_LIST;
157   }
158 
159   /**
160    * Gets the order list.
161    *
162    * @return the order list
163    */
164   public List<Order> getOrderList() {
165     return orderList;
166   }
167 
168   /**
169    * List orders.
170    *
171    * @return the resolution
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    * New order form.
182    *
183    * @return the resolution
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    * New order.
205    *
206    * @return the resolution
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    * View order.
234    *
235    * @return the resolution
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    * Clear.
255    */
256   public void clear() {
257     order = new Order();
258     shippingAddressRequired = false;
259     confirmed = false;
260     orderList = null;
261   }
262 
263 }