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.Collection;
028: import java.util.Map;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import olstore.domain.logic.OlstoreFacade;
034:
035: import org.springframework.beans.support.PagedListHolder;
036: import org.springframework.web.servlet.ModelAndView;
037: import org.springframework.web.servlet.mvc.Controller;
038:
039: /**
040: * Controller used for the list items displays.
041: */
042: public class ListItemsController implements Controller {
043:
044: // This form's olstore instance that is provided via Spring.
045: private OlstoreFacade olstore;
046:
047: /**
048: * Spring injection setter method for the olstore instance.
049: * @param olstore the olstore bean instance for this form.
050: */
051: public void setOlstore(OlstoreFacade olstore) {
052: this .olstore = olstore;
053: }
054:
055: /**
056: * When spring intercepts a request to the specified path (in olstore-servlet.xml) the
057: * request is handled here.
058: *
059: * @param request the HttpServletRequest object.
060: * @param response The HttpServletResponse object.
061: * @throws Exception
062: */
063: public ModelAndView handleRequest(HttpServletRequest request,
064: HttpServletResponse response) throws Exception {
065: String type = request.getParameter("type");
066: if (type == null) {
067: type = "all";
068: }
069: Map model = ControllerHelper.initModel(request, olstore);
070:
071: model.put("page_title", "Viewing " + type + " items - Olstore");
072:
073: Collection items;
074: PagedListHolder productList;
075: if (request.getParameter("page") == null) {
076: if ("all".equals(type)) {
077: items = olstore.getAll();
078: } else {
079: items = olstore.getItemsByType(type);
080: }
081: model.put("friendsMap", this .olstore.getFriendsMap(request
082: .getRemoteUser(), items));
083:
084: productList = new PagedListHolder(new ArrayList(items));
085: productList.setPageSize(5);
086: request.getSession().setAttribute(
087: "ListItemsController_type", type);
088: request.getSession().setAttribute(
089: "ListItemsController_productList", productList);
090: } else {
091: String page = request.getParameter("page");
092: productList = (PagedListHolder) request.getSession()
093: .getAttribute("ListItemsController_productList");
094: if ("next".equals(page)) {
095: productList.nextPage();
096: } else if ("previous".equals(page)) {
097: productList.previousPage();
098: }
099: }
100:
101: model.put("type", type);
102: model.put("productList", productList);
103: return new ModelAndView("listItems", model);
104: }
105: }
|