View Javadoc
1   /*
2    *    Copyright 2010-2022 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    private static final long serialVersionUID = -6171288227470176272L;
41  
42    private static final String CONFIRM_ORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
43    private static final String LIST_ORDERS = "/WEB-INF/jsp/order/ListOrders.jsp";
44    private static final String NEW_ORDER = "/WEB-INF/jsp/order/NewOrderForm.jsp";
45    private static final String SHIPPING = "/WEB-INF/jsp/order/ShippingForm.jsp";
46    private static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
47  
48    private static final List<String> CARD_TYPE_LIST;
49  
50    @SpringBean
51    private transient OrderService orderService;
52  
53    private Order order = new Order();
54    private boolean shippingAddressRequired;
55    private boolean confirmed;
56    private List<Order> orderList;
57  
58    static {
59      CARD_TYPE_LIST = Collections.unmodifiableList(Arrays.asList("Visa", "MasterCard", "American Express"));
60    }
61  
62    public int getOrderId() {
63      return order.getOrderId();
64    }
65  
66    public void setOrderId(int orderId) {
67      order.setOrderId(orderId);
68    }
69  
70    public Order getOrder() {
71      return order;
72    }
73  
74    public void setOrder(Order order) {
75      this.order = order;
76    }
77  
78    public boolean isShippingAddressRequired() {
79      return shippingAddressRequired;
80    }
81  
82    public void setShippingAddressRequired(boolean shippingAddressRequired) {
83      this.shippingAddressRequired = shippingAddressRequired;
84    }
85  
86    public boolean isConfirmed() {
87      return confirmed;
88    }
89  
90    public void setConfirmed(boolean confirmed) {
91      this.confirmed = confirmed;
92    }
93  
94    public List<String> getCreditCardTypes() {
95      return CARD_TYPE_LIST;
96    }
97  
98    public List<Order> getOrderList() {
99      return orderList;
100   }
101 
102   /**
103    * List orders.
104    *
105    * @return the resolution
106    */
107   public Resolution listOrders() {
108     HttpSession session = context.getRequest().getSession();
109     AccountActionBean accountBean = (AccountActionBean) session.getAttribute("/actions/Account.action");
110     orderList = orderService.getOrdersByUsername(accountBean.getAccount().getUsername());
111     return new ForwardResolution(LIST_ORDERS);
112   }
113 
114   /**
115    * New order form.
116    *
117    * @return the resolution
118    */
119   public Resolution newOrderForm() {
120     HttpSession session = context.getRequest().getSession();
121     AccountActionBean accountBean = (AccountActionBean) session.getAttribute("/actions/Account.action");
122     CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
123 
124     clear();
125     if (accountBean == null || !accountBean.isAuthenticated()) {
126       setMessage("You must sign on before attempting to check out.  Please sign on and try checking out again.");
127       return new ForwardResolution(AccountActionBean.class);
128     } else if (cartBean != null) {
129       order.initOrder(accountBean.getAccount(), cartBean.getCart());
130       return new ForwardResolution(NEW_ORDER);
131     } else {
132       setMessage("An order could not be created because a cart could not be found.");
133       return new ForwardResolution(ERROR);
134     }
135   }
136 
137   /**
138    * New order.
139    *
140    * @return the resolution
141    */
142   public Resolution newOrder() {
143     HttpSession session = context.getRequest().getSession();
144 
145     if (shippingAddressRequired) {
146       shippingAddressRequired = false;
147       return new ForwardResolution(SHIPPING);
148     } else if (!isConfirmed()) {
149       return new ForwardResolution(CONFIRM_ORDER);
150     } else if (getOrder() != null) {
151 
152       orderService.insertOrder(order);
153 
154       CartActionBean cartBean = (CartActionBean) session.getAttribute("/actions/Cart.action");
155       cartBean.clear();
156 
157       setMessage("Thank you, your order has been submitted.");
158 
159       return new ForwardResolution(VIEW_ORDER);
160     } else {
161       setMessage("An error occurred processing your order (order was null).");
162       return new ForwardResolution(ERROR);
163     }
164   }
165 
166   /**
167    * View order.
168    *
169    * @return the resolution
170    */
171   public Resolution viewOrder() {
172     HttpSession session = context.getRequest().getSession();
173 
174     AccountActionBean accountBean = (AccountActionBean) session.getAttribute("accountBean");
175 
176     order = orderService.getOrder(order.getOrderId());
177 
178     if (accountBean.getAccount().getUsername().equals(order.getUsername())) {
179       return new ForwardResolution(VIEW_ORDER);
180     } else {
181       order = null;
182       setMessage("You may only view your own orders.");
183       return new ForwardResolution(ERROR);
184     }
185   }
186 
187   /**
188    * Clear.
189    */
190   public void clear() {
191     order = new Order();
192     shippingAddressRequired = false;
193     confirmed = false;
194     orderList = null;
195   }
196 
197 }