01: /**
02: * Copyright (c) 2006 Red Hat, Inc. All rights reserved.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or any later version.
08: *
09: * 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: Greg Lapouchnian
22: * Patrick Smith
23: *
24: */package olstore.controller;
25:
26: import java.util.Map;
27:
28: import javax.servlet.http.HttpServletRequest;
29: import javax.servlet.http.HttpServletResponse;
30:
31: import olstore.domain.logic.OlstoreFacade;
32: import olstore.domain.manager.CartManager;
33: import olstore.dto.ItemValue;
34: import olstore.dto.PictureValue;
35:
36: import org.springframework.web.servlet.ModelAndView;
37: import org.springframework.web.servlet.mvc.Controller;
38:
39: /**
40: * A Spring controller class that handles adding an item to the shopping cart.
41: */
42: public class AddItemToCartController implements Controller {
43:
44: // Instance of the olstore bean injected via Spring.
45: private OlstoreFacade olstore;
46:
47: // Spring injection setter method for olstore instance.
48: public void setOlstore(OlstoreFacade olstore) {
49: this .olstore = olstore;
50: }
51:
52: /**
53: * When spring intercepts a request to the specified path (in olstore-servlet.xml) the
54: * request is handled here.
55: *
56: * @param request the HttpServletRequest object.
57: * @param response The HttpServletResponse object.
58: * @throws Exception
59: */
60: public ModelAndView handleRequest(HttpServletRequest request,
61: HttpServletResponse response) throws Exception {
62: Map model = ControllerHelper.initModel(request, olstore);
63:
64: String itemId = request.getParameter("itemId");
65: ItemValue item = this .olstore.getItemById(itemId);
66: PictureValue pic = this .olstore.getPictureById(itemId);
67:
68: model.put("item", item);
69: model.put("picture", pic);
70:
71: // add item to the cart if it's not already in it
72: CartManager cartManager = (CartManager) request.getSession()
73: .getAttribute("cart");
74: if (!cartManager.containsItem(Integer.valueOf(itemId))) {
75: cartManager.updateQuantity(Integer.valueOf(itemId),
76: new Integer(1));
77: model.put("message",
78: "The item has been added to your shopping cart.");
79: } else {
80: model.put("error", "Item already in shopping cart.");
81: }
82:
83: model.put("page_title", "Viewing '" + item.getName()
84: + "' - Olstore");
85: return new ModelAndView("viewItem", model);
86: }
87:
88: }
|