001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.action;
027:
028: import java.util.ArrayList;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.http.HttpSession;
033:
034: import olstore.form.CheckoutForm;
035:
036: import org.apache.struts.action.ActionError;
037: import org.apache.struts.action.ActionErrors;
038: import org.apache.struts.action.ActionForm;
039: import org.apache.struts.action.ActionForward;
040: import org.apache.struts.action.ActionMapping;
041: import org.apache.struts.action.ActionMessage;
042: import org.apache.struts.action.ActionMessages;
043:
044: import olstore.session.ShoppingCartLocal;
045:
046: public class CheckOutAction extends DemoBaseAction {
047:
048: /**
049: * Acts as a relay for all item related tasks with a default action
050: *
051: */
052: public ActionForward execute(ActionMapping mapping,
053: ActionForm form, HttpServletRequest request,
054: HttpServletResponse response) throws Exception {
055:
056: String submitType = ((CheckoutForm) form).getSubmitType();
057: if (submitType == null || submitType.equals("")) {
058: copyShoppingCartToForm(mapping, form, request, response);
059: } else if (submitType.equals("update")) {
060: copyFormToShoppingCart(mapping, form, request, response);
061: copyShoppingCartToForm(mapping, form, request, response);
062: } else if (submitType.equals("submit")) {
063: copyFormToShoppingCart(mapping, form, request, response);
064: copyShoppingCartToForm(mapping, form, request, response);
065: resetForm(mapping, form, request, response);
066: try {
067: placeOrders(mapping, form, request, response);
068: ActionMessages messages = new ActionMessages();
069: ActionMessage msg = new ActionMessage(
070: "checkout.save.success");
071: messages.add("success", msg);
072: saveMessages(request, messages);
073: } catch (Exception e) {
074: ActionErrors errors = new ActionErrors();
075: errors.add("error", new ActionError(
076: "errors.order.save", e.getMessage()));
077: saveErrors(request, errors);
078: return (new ActionForward(mapping.getInput()));
079: }
080: }
081:
082: return mapping.findForward("/checkout/updateCheckout");
083:
084: }
085:
086: public void resetForm(ActionMapping mapping, ActionForm form,
087: HttpServletRequest request, HttpServletResponse response)
088: throws Exception {
089: ((CheckoutForm) form).reset();
090: }
091:
092: public void copyShoppingCartToForm(ActionMapping mapping,
093: ActionForm form, HttpServletRequest request,
094: HttpServletResponse response) throws Exception {
095: HttpSession session = request.getSession(false);
096: ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session
097: .getAttribute("shoppingCart");
098: ArrayList cartEntries = shoppingCart.shoppingCartToDTOs();
099: ((CheckoutForm) form).setCartEntries(cartEntries);
100: ((CheckoutForm) form).setTotalCost(shoppingCart.getTotalCost());
101: }
102:
103: public void copyFormToShoppingCart(ActionMapping mapping,
104: ActionForm form, HttpServletRequest request,
105: HttpServletResponse response) throws Exception {
106: HttpSession session = request.getSession(false);
107: ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session
108: .getAttribute("shoppingCart");
109: shoppingCart.DTOsToShoppingCart(((CheckoutForm) form)
110: .getCartEntries());
111: }
112:
113: /**
114: */
115: public void placeOrders(ActionMapping mapping, ActionForm form,
116: HttpServletRequest request, HttpServletResponse response)
117: throws Exception {
118: HttpSession session = request.getSession(false);
119: ShoppingCartLocal shoppingCart = (ShoppingCartLocal) session
120: .getAttribute("shoppingCart");
121: shoppingCart.createOrders();
122: }
123:
124: }
|