001: /**
002: * Copyright (c) 2006 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: Greg Lapouchnian
022: * Patrick Smith
023: *
024: */package olstore.controller;
025:
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import olstore.domain.logic.OlstoreFacade;
033: import olstore.domain.manager.CartManager;
034: import olstore.dto.ShoppingCartItem;
035:
036: import org.springframework.web.servlet.ModelAndView;
037: import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
038:
039: /**
040: * A controller that handles shopping cart related actions.
041: */
042: public class ShoppingCartController extends MultiActionController {
043:
044: // The olstore instance provided via Spring.
045: private OlstoreFacade olstore;
046:
047: /**
048: * The Spring injection setter method for the olstore instance.
049: * @param olstore the instance provided via spring.
050: */
051: public void setOlstore(OlstoreFacade olstore) {
052: this .olstore = olstore;
053: }
054:
055: /**
056: * Returns a new model and view for this controller.
057: * @param request the HttpServletRequest
058: * @param response the HttpServletResponse
059: * @return a new model and view that represents the data for this controller.
060: * @throws Exception
061: */
062: public ModelAndView view(HttpServletRequest request,
063: HttpServletResponse response) throws Exception {
064:
065: Map model = ControllerHelper.initModel(request, olstore);
066: CartManager cartManager = (CartManager) request.getSession()
067: .getAttribute("cart");
068:
069: model.put("items", cartManager.getItems());
070:
071: request.setAttribute("page_title",
072: "Shopping Cart Contents - Olstore");
073: return new ModelAndView("cart", model);
074: }
075:
076: /**
077: * Called when we are in an update cart action.
078: * @param request the HttpServletRequest.
079: * @param response the HttpServletResponse.
080: * @return a model and view for representing the updating of a cart.
081: * @throws Exception
082: */
083: public ModelAndView update(HttpServletRequest request,
084: HttpServletResponse response) throws Exception {
085: Map model = ControllerHelper.initModel(request, olstore);
086: CartManager cartManager = (CartManager) request.getSession()
087: .getAttribute("cart");
088: model.put("items", cartManager.getItems());
089: request.setAttribute("page_title",
090: "Shopping Cart Contents - Olstore");
091:
092: Iterator i = cartManager.getItems().iterator();
093: while (i.hasNext()) {
094: int itemId = ((ShoppingCartItem) i.next()).getItemId()
095: .intValue();
096: int quantity;
097: try {
098: quantity = Integer.valueOf(
099: request.getParameter(String.valueOf(itemId)))
100: .intValue();
101: cartManager.updateQuantity(new Integer(itemId),
102: new Integer(quantity));
103: } catch (NumberFormatException e) {
104: model.put("error", "Quantity invalid. Smarten-up.");
105: }
106: }
107: return new ModelAndView("cart", model);
108: }
109:
110: /**
111: * Called when we are performing an order action.
112: * @param request the HttpServletRequest.
113: * @param response the HttpServletResponse.
114: * @return a model and view to represent the updating order.
115: * @throws Exception
116: */
117: public ModelAndView order(HttpServletRequest request,
118: HttpServletResponse response) throws Exception {
119: Map model = ControllerHelper.initModel(request, olstore);
120: CartManager cartManager = (CartManager) request.getSession()
121: .getAttribute("cart");
122:
123: cartManager.placeOrder();
124:
125: model.put("page_title", "Shopping Cart Contents - Olstore");
126: model
127: .put("message",
128: "Your order has been placed. Thank you for shopping with us.");
129:
130: return new ModelAndView("cart", model);
131: }
132:
133: }
|