KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > action > OrderAction


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.action;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.List JavaDoc;
12 import javax.ejb.EJB JavaDoc;
13
14 import org.jfox.entity.EntityFactory;
15 import org.jfox.entity.dao.PKGenerator;
16 import org.jfox.framework.annotation.Service;
17 import org.jfox.mvc.ActionSupport;
18 import org.jfox.mvc.Invocation;
19 import org.jfox.mvc.InvocationContext;
20 import org.jfox.mvc.PageContext;
21 import org.jfox.mvc.SessionContext;
22 import org.jfox.mvc.annotation.ActionMethod;
23 import org.jfox.mvc.validate.LongValidation;
24 import org.jfox.petstore.bo.OrderBO;
25 import org.jfox.petstore.domain.Cart;
26 import org.jfox.petstore.domain.CartItem;
27 import org.jfox.petstore.entity.Account;
28 import org.jfox.petstore.entity.LineItem;
29 import org.jfox.petstore.entity.Order;
30
31 /**
32  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
33  */

34 @Service(id = "order")
35 public class OrderAction extends ActionSupport {
36     public static final String JavaDoc ORDER_SESSION_KEY = "__ORDER__";
37     private static List JavaDoc<String JavaDoc> creditCardTypes = new ArrayList JavaDoc<String JavaDoc>();
38
39     static {
40         creditCardTypes.add("Visa");
41         creditCardTypes.add("American Express");
42         creditCardTypes.add("mobile c");
43     }
44
45     @EJB JavaDoc
46     OrderBO orderBO;
47
48     @ActionMethod(successView = "NewOrder.vhtml")
49     public void doGetNew(InvocationContext invocationContext) throws Exception JavaDoc {
50         SessionContext sessionContext = invocationContext.getSessionContext();
51         Cart cart = (Cart)sessionContext.getAttribute(CartAction.CART_SESSION_KEY);
52
53         PageContext pageContext = invocationContext.getPageContext();
54
55         if(!sessionContext.containsAttribute(AccountAction.ACCOUNT_SESSION_KEY)) {
56             // not login
57
pageContext.setTargetView("signon.vhtml");
58             return;
59         }
60
61         Account account = (Account)sessionContext.getAttribute(AccountAction.ACCOUNT_SESSION_KEY);
62
63         Order order = EntityFactory.newEntityObject(Order.class);
64         order.setOrderId(PKGenerator.getInstance().nextPK());
65         order.setUsername(account.getUsername());
66         order.setOrderDate(new Date JavaDoc());
67
68         order.setShipToFirstName(account.getFirstName());
69         order.setShipToLastName(account.getLastName());
70         order.setShipAddress1(account.getAddress1());
71         order.setShipAddress2(account.getAddress2());
72         order.setShipCity(account.getCity());
73         order.setShipState(account.getState());
74         order.setShipZip(account.getZip());
75         order.setShipCountry(account.getCountry());
76
77         order.setBillToFirstName(account.getFirstName());
78         order.setBillToLastName(account.getLastName());
79         order.setBillAddress1(account.getAddress1());
80         order.setBillAddress2(account.getAddress2());
81         order.setBillCity(account.getCity());
82         order.setBillState(account.getState());
83         order.setBillZip(account.getZip());
84         order.setBillCountry(account.getCountry());
85
86         order.setTotalPrice(cart.getSubTotal());
87
88         order.setCreditCard("999 9999 9999 9999");
89         order.setExpiryDate("12/03");
90         order.setCardType("Visa");
91         order.setCourier("UPS");
92         order.setLocale("CA");
93         order.setStatus("P");
94
95         List JavaDoc<LineItem> lineItems = new ArrayList JavaDoc<LineItem>();
96
97         int i = 1;
98         for (CartItem cartItem : cart.getCartItemList()) {
99             LineItem lineItem = EntityFactory.newEntityObject(LineItem.class);
100             lineItem.setLineNumber(i);
101             lineItem.setOrderId(order.getOrderId());
102             lineItem.setQuantity(cartItem.getQuantity());
103             lineItem.setItemId(cartItem.getItem().getItemId());
104             lineItem.setUnitPrice(cartItem.getItem().getListPrice());
105             lineItem.setItem(cartItem.getItem());
106             lineItems.add(lineItem);
107             i++;
108         }
109         order.setLineItems(lineItems);
110
111         sessionContext.setAttribute(ORDER_SESSION_KEY, order);
112
113         pageContext.setAttribute("creditCardTypes", creditCardTypes);
114         pageContext.setAttribute("order", order);
115     }
116
117     @ActionMethod(successView = "ConfirmOrder.vhtml", invocationClass = NewOrderInvocation.class)
118     public void doPostNew(InvocationContext invocationContext) throws Exception JavaDoc {
119         NewOrderInvocation invocation = (NewOrderInvocation)invocationContext.getInvocation();
120         SessionContext sessionContext = invocationContext.getSessionContext();
121 // Account account = (Account)sessionContext.getAttribute(AccountAction.ACCOUNT_SESSION_KEY);
122
Order order = (Order)sessionContext.getAttribute(ORDER_SESSION_KEY);
123
124         order.setShipToFirstName(invocation.getBillToFirstName());
125         order.setShipToLastName(invocation.getBillToLastName());
126         order.setShipAddress1(invocation.getBillAddress1());
127         order.setShipAddress2(invocation.getBillAddress2());
128         order.setShipCity(invocation.getBillCity());
129         order.setShipState(invocation.getBillState());
130         order.setShipZip(invocation.getBillZip());
131         order.setShipCountry(invocation.getBillCountry());
132
133         order.setBillToFirstName(invocation.getBillToFirstName());
134         order.setBillToLastName(invocation.getBillToLastName());
135         order.setBillAddress1(invocation.getBillAddress1());
136         order.setBillAddress2(invocation.getBillAddress2());
137         order.setBillCity(invocation.getBillCity());
138         order.setBillState(invocation.getBillState());
139         order.setBillZip(invocation.getBillZip());
140         order.setBillCountry(invocation.getBillCountry());
141
142         order.setCardType(invocation.getCardType());
143         order.setCreditCard(invocation.getCreditCard());
144
145         if ("1".equals(invocation.getShippingAddressRequired())) {
146             // don't need input shipping address
147
invocationContext.getPageContext().setTargetView("Shipping.vhtml");
148         }
149
150         PageContext pageContext = invocationContext.getPageContext();
151         pageContext.setAttribute("order", order);
152     }
153
154     @ActionMethod(successView = "ConfirmOrder.vhtml", invocationClass = ShippingOrderInvocation.class)
155     public void doPostConfirmShipping(InvocationContext invocationContext) throws Exception JavaDoc {
156         ShippingOrderInvocation invocation = (ShippingOrderInvocation)invocationContext.getInvocation();
157         SessionContext sessionContext = invocationContext.getSessionContext();
158         Order order = (Order)sessionContext.getAttribute(ORDER_SESSION_KEY);
159
160         order.setShipToFirstName(invocation.getShipToFirstName());
161         order.setShipToLastName(invocation.getShipToLastName());
162         order.setShipAddress1(invocation.getShipAddress1());
163         order.setShipAddress2(invocation.getShipAddress2());
164         order.setShipCity(invocation.getShipCity());
165         order.setShipState(invocation.getShipState());
166         order.setShipZip(invocation.getShipZip());
167         order.setShipCountry(invocation.getShipCountry());
168
169         PageContext pageContext = invocationContext.getPageContext();
170         pageContext.setAttribute("order", order);
171     }
172
173     @ActionMethod(successView = "ViewOrder.vhtml", errorView = "Cart.vhtml")
174     public void doGetConfirm(InvocationContext invocationContext) throws Exception JavaDoc {
175         SessionContext sessionContext = invocationContext.getSessionContext();
176         PageContext pageContext = invocationContext.getPageContext();
177
178         Order order = (Order)sessionContext.getAttribute(ORDER_SESSION_KEY);
179         if (order != null) {
180             //insert order
181
try {
182                 orderBO.insertOrder(order);
183
184                 sessionContext.removeAttribute(ORDER_SESSION_KEY);
185                 sessionContext.removeAttribute(CartAction.CART_SESSION_KEY);
186                 pageContext.setAttribute("order", order);
187                 pageContext.setAttribute("orderUtil",OrderUtil.getInstance());
188             }
189             catch (Exception JavaDoc e) {
190                 Cart cart = (Cart)sessionContext.getAttribute(CartAction.CART_SESSION_KEY);
191                 pageContext.setAttribute("cart", cart);
192                 throw e;
193             }
194         }
195     }
196
197     @ActionMethod(successView = "ListOrders.vhtml")
198     public void doGetList(InvocationContext invocationContext) throws Exception JavaDoc {
199         SessionContext sessionContext = invocationContext.getSessionContext();
200         Account account = (Account)sessionContext.getAttribute(AccountAction.ACCOUNT_SESSION_KEY);
201         List JavaDoc<Order> orders = orderBO.getOrdersByUsername(account.getUsername());
202
203         PageContext pageContext = invocationContext.getPageContext();
204         pageContext.setAttribute("orders",orders);
205     }
206
207     @ActionMethod(successView = "ViewOrder.vhtml", invocationClass = ViewOrderInvocation.class)
208     public void doGetView(InvocationContext invocationContext) throws Exception JavaDoc {
209         ViewOrderInvocation invocation = (ViewOrderInvocation)invocationContext.getInvocation();
210         long orderId = invocation.getOrderId();
211         Order order = orderBO.getOrder(orderId);
212         PageContext pageContext = invocationContext.getPageContext();
213         pageContext.setAttribute("order",order);
214         pageContext.setAttribute("orderUtil",OrderUtil.getInstance());
215     }
216
217     public static class OrderUtil{
218         private static OrderUtil instance = new OrderUtil();
219         private OrderUtil(){
220
221         }
222         public static OrderUtil getInstance() {
223             return instance;
224         }
225
226         public double getTotalPrice(LineItem lineItem){
227             return lineItem.getUnitPrice() * lineItem.getQuantity();
228         }
229     }
230
231     public static class ViewOrderInvocation extends Invocation {
232
233         @LongValidation
234         private long orderId;
235
236         public long getOrderId() {
237             return orderId;
238         }
239
240         public void setOrderId(long orderId) {
241             this.orderId = orderId;
242         }
243     }
244
245     public static class NewOrderInvocation extends Invocation {
246         private String JavaDoc cardType;
247         private String JavaDoc creditCard;
248         private String JavaDoc expiryDate;
249         private String JavaDoc billToFirstName;
250         private String JavaDoc billToLastName;
251         private String JavaDoc billAddress1;
252         private String JavaDoc billAddress2;
253         private String JavaDoc billCity;
254         private String JavaDoc billState;
255         private String JavaDoc billZip;
256         private String JavaDoc billCountry;
257         private String JavaDoc shippingAddressRequired;
258
259         public String JavaDoc getBillAddress1() {
260             return billAddress1;
261         }
262
263         public void setBillAddress1(String JavaDoc billAddress1) {
264             this.billAddress1 = billAddress1;
265         }
266
267         public String JavaDoc getBillAddress2() {
268             return billAddress2;
269         }
270
271         public void setBillAddress2(String JavaDoc billAddress2) {
272             this.billAddress2 = billAddress2;
273         }
274
275         public String JavaDoc getBillCity() {
276             return billCity;
277         }
278
279         public void setBillCity(String JavaDoc billCity) {
280             this.billCity = billCity;
281         }
282
283         public String JavaDoc getBillCountry() {
284             return billCountry;
285         }
286
287         public void setBillCountry(String JavaDoc billCountry) {
288             this.billCountry = billCountry;
289         }
290
291         public String JavaDoc getBillState() {
292             return billState;
293         }
294
295         public void setBillState(String JavaDoc billState) {
296             this.billState = billState;
297         }
298
299         public String JavaDoc getBillToFirstName() {
300             return billToFirstName;
301         }
302
303         public void setBillToFirstName(String JavaDoc billToFirstName) {
304             this.billToFirstName = billToFirstName;
305         }
306
307         public String JavaDoc getBillToLastName() {
308             return billToLastName;
309         }
310
311         public void setBillToLastName(String JavaDoc billToLastName) {
312             this.billToLastName = billToLastName;
313         }
314
315         public String JavaDoc getBillZip() {
316             return billZip;
317         }
318
319         public void setBillZip(String JavaDoc billZip) {
320             this.billZip = billZip;
321         }
322
323         public String JavaDoc getCardType() {
324             return cardType;
325         }
326
327         public void setCardType(String JavaDoc cardType) {
328             this.cardType = cardType;
329         }
330
331         public String JavaDoc getCreditCard() {
332             return creditCard;
333         }
334
335         public void setCreditCard(String JavaDoc creditCard) {
336             this.creditCard = creditCard;
337         }
338
339         public String JavaDoc getExpiryDate() {
340             return expiryDate;
341         }
342
343         public void setExpiryDate(String JavaDoc expiryDate) {
344             this.expiryDate = expiryDate;
345         }
346
347         public String JavaDoc getShippingAddressRequired() {
348             return shippingAddressRequired;
349         }
350
351         public void setShippingAddressRequired(String JavaDoc shippingAddressRequired) {
352             this.shippingAddressRequired = shippingAddressRequired;
353         }
354     }
355
356     public static class ShippingOrderInvocation extends Invocation {
357         private String JavaDoc shipToFirstName;
358         private String JavaDoc shipToLastName;
359         private String JavaDoc shipAddress1;
360         private String JavaDoc shipAddress2;
361         private String JavaDoc shipCity;
362         private String JavaDoc shipState;
363         private String JavaDoc shipZip;
364         private String JavaDoc shipCountry;
365
366         public String JavaDoc getShipAddress1() {
367             return shipAddress1;
368         }
369
370         public void setShipAddress1(String JavaDoc shipAddress1) {
371             this.shipAddress1 = shipAddress1;
372         }
373
374         public String JavaDoc getShipAddress2() {
375             return shipAddress2;
376         }
377
378         public void setShipAddress2(String JavaDoc shipAddress2) {
379             this.shipAddress2 = shipAddress2;
380         }
381
382         public String JavaDoc getShipCity() {
383             return shipCity;
384         }
385
386         public void setShipCity(String JavaDoc shipCity) {
387             this.shipCity = shipCity;
388         }
389
390         public String JavaDoc getShipCountry() {
391             return shipCountry;
392         }
393
394         public void setShipCountry(String JavaDoc shipCountry) {
395             this.shipCountry = shipCountry;
396         }
397
398         public String JavaDoc getShipState() {
399             return shipState;
400         }
401
402         public void setShipState(String JavaDoc shipState) {
403             this.shipState = shipState;
404         }
405
406         public String JavaDoc getShipToFirstName() {
407             return shipToFirstName;
408         }
409
410         public void setShipToFirstName(String JavaDoc shipToFirstName) {
411             this.shipToFirstName = shipToFirstName;
412         }
413
414         public String JavaDoc getShipToLastName() {
415             return shipToLastName;
416         }
417
418         public void setShipToLastName(String JavaDoc shipToLastName) {
419             this.shipToLastName = shipToLastName;
420         }
421
422         public String JavaDoc getShipZip() {
423             return shipZip;
424         }
425
426         public void setShipZip(String JavaDoc shipZip) {
427             this.shipZip = shipZip;
428         }
429
430     }
431
432     public static void main(String JavaDoc[] args) {
433
434     }
435 }
436
Popular Tags