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.ArrayList;
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.dto.ItemValue;
034:
035: import org.springframework.web.servlet.ModelAndView;
036: import org.springframework.web.servlet.mvc.Controller;
037: import org.springframework.web.servlet.view.RedirectView;
038:
039: /**
040: * A controller used for when an individual item is viewed.
041: */
042: public class ViewItemController implements Controller {
043:
044: // The olstore bean object injected via Spring.
045: private OlstoreFacade olstore;
046:
047: /**
048: * The Spring injection method for setting the olstore bean instance.
049: * @param olstore the bean instance.
050: */
051: public void setOlstore(OlstoreFacade olstore) {
052: this .olstore = olstore;
053: }
054:
055: /**
056: * Handle the request to display a single item in the store.
057: *
058: * @param request the HttpServletRequest.
059: * @param response the HttpServletResponse.
060: * @return a new model and view to to display this item
061: */
062: public ModelAndView handleRequest(HttpServletRequest request,
063: HttpServletResponse response) throws Exception {
064: Map model = ControllerHelper.initModel(request, olstore);
065:
066: String itemId = request.getParameter("itemId");
067: if (itemId != null && itemId.length() > 0) {
068: try {
069: ItemValue item = this .olstore.getItemById(itemId);
070: ArrayList items = new ArrayList();
071: items.add(item);
072: model.put("friendsMap", this .olstore.getFriendsMap(
073: request.getRemoteUser(), items));
074: model.put("item", item);
075: model.put("picture", this .olstore
076: .getPictureById(itemId));
077: model.put("page_title", "Viewing '" + item.getName()
078: + "' - Olstore");
079:
080: String interesting = request
081: .getParameter("interesting");
082: if (interesting != null && interesting.equals("true")) {
083: try {
084: this .olstore.markItemAsInteresting(item
085: .getItemId(), request.getRemoteUser());
086: model
087: .put("message",
088: "Item successfully marked as interesting.");
089: } catch (Exception e) {
090: model
091: .put("error",
092: "There was an error when trying to mark this item as interesting.");
093: }
094: }
095: } catch (Exception e) {
096: e.printStackTrace();
097: model.put("page_title", "Item not found - Olstore");
098: model.put("message",
099: "The item you requested could not be found.");
100: }
101: return new ModelAndView("viewItem", model);
102: } else {
103: return new ModelAndView(new RedirectView("index.do"));
104: }
105: }
106:
107: }
|