KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > jpetstore > presentation > OrderBean


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