View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
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    *      http://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 com.ibatis.jpetstore.presentation;
17  
18  import com.ibatis.jpetstore.domain.Account;
19  import com.ibatis.jpetstore.domain.Order;
20  import com.ibatis.jpetstore.service.AccountService;
21  import com.ibatis.jpetstore.service.OrderService;
22  import com.ibatis.struts.ActionContext;
23  import com.ibatis.struts.BaseBean;
24  import com.ibatis.common.util.PaginatedList;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Collections;
30  
31  public class OrderBean extends BaseBean {
32  
33    /* Constants */
34  
35    private static final AccountService accountService = AccountService.getInstance();
36    private static final OrderService orderService = OrderService.getInstance();
37  
38    private static final List CARD_TYPE_LIST;
39  
40    /* Private Fields */
41  
42    private Order order;
43    private int orderId;
44    private boolean shippingAddressRequired;
45    private boolean confirmed;
46    private PaginatedList orderList;
47    private String pageDirection;
48  
49    /* Static Initializer */
50  
51    static {
52      List cardList = new ArrayList();
53      cardList.add("Visa");
54      cardList.add("MasterCard");
55      cardList.add("American Express");
56      CARD_TYPE_LIST = Collections.unmodifiableList(cardList);
57    }
58  
59    /* Constructors */
60  
61    public OrderBean() {
62      this.order = new Order();
63      this.shippingAddressRequired = false;
64      this.confirmed = false;
65    }
66  
67    /* JavaBeans Properties */
68  
69    public int getOrderId() {
70      return orderId;
71    }
72  
73    public void setOrderId(int orderId) {
74      this.orderId = orderId;
75    }
76  
77    public Order getOrder() {
78      return order;
79    }
80  
81    public void setOrder(Order order) {
82      this.order = order;
83    }
84  
85    public boolean isShippingAddressRequired() {
86      return shippingAddressRequired;
87    }
88  
89    public void setShippingAddressRequired(boolean shippingAddressRequired) {
90      this.shippingAddressRequired = shippingAddressRequired;
91    }
92  
93    public boolean isConfirmed() {
94      return confirmed;
95    }
96  
97    public void setConfirmed(boolean confirmed) {
98      this.confirmed = confirmed;
99    }
100 
101   public List getCreditCardTypes() {
102     return CARD_TYPE_LIST;
103   }
104 
105   public List getOrderList() {
106     return orderList;
107   }
108 
109   public String getPageDirection() {
110     return pageDirection;
111   }
112 
113   public void setPageDirection(String pageDirection) {
114     this.pageDirection = pageDirection;
115   }
116 
117   /* Public Methods */
118 
119   public String newOrderForm() {
120     Map sessionMap = ActionContext.getActionContext().getSessionMap();
121     AccountBean accountBean = (AccountBean) sessionMap.get("accountBean");
122     CartBean cartBean = (CartBean) sessionMap.get("cartBean");
123 
124     clear();
125     if (accountBean == null || !accountBean.isAuthenticated()){
126       ActionContext.getActionContext().setSimpleMessage("You must sign on before attempting to check out.  Please sign on and try checking out again.");
127       return "signon";
128     } else if (cartBean != null) {
129       // Re-read account from DB at team's request.
130       Account account = accountService.getAccount(accountBean.getAccount().getUsername());
131       order.initOrder(account, cartBean.getCart());
132       return "success";
133     } else {
134       ActionContext.getActionContext().setSimpleMessage("An order could not be created because a cart could not be found.");
135       return "failure";
136     }
137   }
138 
139   public String newOrder() {
140     Map sessionMap = ActionContext.getActionContext().getSessionMap();
141 
142     if (shippingAddressRequired) {
143       shippingAddressRequired = false;
144       return "shipping";
145     } else if (!isConfirmed()) {
146       return "confirm";
147     } else if (getOrder() != null) {
148 
149       orderService.insertOrder(order);
150 
151       CartBean cartBean = (CartBean)sessionMap.get("cartBean");
152       cartBean.clear();
153 
154       ActionContext.getActionContext().setSimpleMessage("Thank you, your order has been submitted.");
155 
156       return "success";
157     } else {
158       ActionContext.getActionContext().setSimpleMessage("An error occurred processing your order (order was null).");
159       return "failure";
160     }
161   }
162 
163   public String listOrders() {
164     Map sessionMap = ActionContext.getActionContext().getSessionMap();
165     AccountBean accountBean = (AccountBean) sessionMap.get("accountBean");
166     orderList = orderService.getOrdersByUsername(accountBean.getAccount().getUsername());
167     return "success";
168   }
169 
170   public String switchOrderPage() {
171     if ("next".equals(pageDirection)) {
172       orderList.nextPage();
173     } else if ("previous".equals(pageDirection)) {
174       orderList.previousPage();
175     }
176     return "success";
177   }
178 
179 
180   public String viewOrder() {
181     Map sessionMap = ActionContext.getActionContext().getSessionMap();
182     AccountBean accountBean = (AccountBean) sessionMap.get("accountBean");
183 
184     order = orderService.getOrder(orderId);
185 
186     if (accountBean.getAccount().getUsername().equals(order.getUsername())) {
187       return "success";
188     } else {
189       order = null;
190       ActionContext.getActionContext().setSimpleMessage("You may only view your own orders.");
191       return "failure";
192     }
193   }
194 
195   public void reset() {
196     shippingAddressRequired = false;
197   }
198 
199   public void clear() {
200     order = new Order();
201     orderId = 0;
202     shippingAddressRequired = false;
203     confirmed = false;
204     orderList = null;
205     pageDirection = null;
206   }
207 
208   public void validate() {
209     ActionContext ctx = ActionContext.getActionContext();
210 
211     if (!this.isShippingAddressRequired()) {
212       validateRequiredField(order.getCreditCard(), "FAKE (!) credit card number required.");
213       validateRequiredField(order.getExpiryDate(), "Expiry date is required.");
214       validateRequiredField(order.getCardType(), "Card type is required.");
215 
216       validateRequiredField(order.getShipToFirstName(), "Shipping Info: first name is required.");
217       validateRequiredField(order.getShipToLastName(), "Shipping Info: last name is required.");
218       validateRequiredField(order.getShipAddress1(), "Shipping Info: address is required.");
219       validateRequiredField(order.getShipCity(), "Shipping Info: city is required.");
220       validateRequiredField(order.getShipState(), "Shipping Info: state is required.");
221       validateRequiredField(order.getShipZip(), "Shipping Info: zip/postal code is required.");
222       validateRequiredField(order.getShipCountry(), "Shipping Info: country is required.");
223 
224       validateRequiredField(order.getBillToFirstName(), "Billing Info: first name is required.");
225       validateRequiredField(order.getBillToLastName(), "Billing Info: last name is required.");
226       validateRequiredField(order.getBillAddress1(), "Billing Info: address is required.");
227       validateRequiredField(order.getBillCity(), "Billing Info: city is required.");
228       validateRequiredField(order.getBillState(), "Billing Info: state is required.");
229       validateRequiredField(order.getBillZip(), "Billing Info: zip/postal code is required.");
230       validateRequiredField(order.getBillCountry(), "Billing Info: country is required.");
231     }
232 
233     if (ctx.isSimpleErrorsExist()) {
234       order.setBillAddress1(order.getShipAddress1());
235       order.setBillAddress2(order.getShipAddress2());
236       order.setBillToFirstName(order.getShipToFirstName());
237       order.setBillToLastName(order.getShipToLastName());
238       order.setBillCity(order.getShipCity());
239       order.setBillCountry(order.getShipCountry());
240       order.setBillState(order.getShipState());
241       order.setBillZip(order.getShipZip());
242     }
243 
244   }
245 
246 }