KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > action > CheckOutAction


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27
28 package olstore.action;
29
30 import java.util.ArrayList;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.servlet.http.HttpSession;
35
36 import olstore.form.CheckoutForm;
37
38 import org.apache.struts.action.ActionError;
39 import org.apache.struts.action.ActionErrors;
40 import org.apache.struts.action.ActionForm;
41 import org.apache.struts.action.ActionForward;
42 import org.apache.struts.action.ActionMapping;
43 import org.apache.struts.action.ActionMessage;
44 import org.apache.struts.action.ActionMessages;
45
46 import olstore.session.ShoppingCartLocal;
47
48 public class CheckOutAction extends DemoBaseAction {
49     
50     /**
51      * Acts as a relay for all item related tasks with a default action
52      *
53      */

54     public ActionForward execute ( ActionMapping mapping,
55             ActionForm form,
56             HttpServletRequest request,
57             HttpServletResponse response
58     ) throws Exception {
59         
60         
61         String submitType = ((CheckoutForm)form).getSubmitType();
62         if ( submitType==null || submitType.equals("")) {
63             copyShoppingCartToForm ( mapping, form, request, response );
64         }else if(submitType.equals("update")){
65             copyFormToShoppingCart(mapping, form, request, response);
66             copyShoppingCartToForm ( mapping, form, request, response );
67         }else if( submitType.equals ( "submit" ) ) {
68             copyFormToShoppingCart(mapping, form, request, response);
69             copyShoppingCartToForm ( mapping, form, request, response );
70             resetForm(mapping, form, request, response);
71             try{
72                 placeOrders ( mapping, form, request, response );
73                 ActionMessages messages = new ActionMessages();
74                 ActionMessage msg = new ActionMessage("checkout.save.success");
75                 messages.add("success", msg);
76                 saveMessages(request, messages);
77             }catch(Exception e){
78                 ActionErrors errors = new ActionErrors();
79                 errors.add("error", new ActionError("errors.order.save", e.getMessage() ));
80                 saveErrors(request, errors);
81                 return (new ActionForward(mapping.getInput()));
82             }
83         }
84         
85         return mapping.findForward ( "/checkout/updateCheckout" );
86         
87     }
88     
89     public void resetForm ( ActionMapping mapping,
90             ActionForm form,
91             HttpServletRequest request,
92             HttpServletResponse response
93     ) throws Exception {
94         ((CheckoutForm) form).reset();
95     }
96     
97     public void copyShoppingCartToForm ( ActionMapping mapping,
98             ActionForm form,
99             HttpServletRequest request,
100             HttpServletResponse response
101     ) throws Exception {
102         HttpSession session = request.getSession(false);
103         ShoppingCartLocal shoppingCart = (ShoppingCartLocal)session.getAttribute("shoppingCart");
104         ArrayList cartEntries = shoppingCart.shoppingCartToDTOs();
105         ((CheckoutForm)form).setCartEntries ( cartEntries );
106         ((CheckoutForm)form).setTotalCost ( shoppingCart.getTotalCost());
107     }
108     
109     public void copyFormToShoppingCart ( ActionMapping mapping,
110             ActionForm form,
111             HttpServletRequest request,
112             HttpServletResponse response
113     ) throws Exception {
114         HttpSession session = request.getSession(false);
115         ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session.getAttribute("shoppingCart");
116         shoppingCart.DTOsToShoppingCart(((CheckoutForm) form).getCartEntries());
117     }
118     
119     
120     
121     /**
122      */

123     public void placeOrders ( ActionMapping mapping,
124             ActionForm form,
125             HttpServletRequest request,
126             HttpServletResponse response
127     ) throws Exception {
128         HttpSession session = request.getSession(false);
129         ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session.getAttribute("shoppingCart");
130         shoppingCart.createOrders();
131     }
132     
133     
134     
135     
136 }
137
Popular Tags