/**
 * Copyright (c) 2006 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 * 
 * Initial Developers: Greg Lapouchnian
 *                     Patrick Smith
 *
 */

package olstore.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import olstore.domain.logic.OlstoreFacade;

import org.springframework.beans.support.PagedListHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;


/**
 * Controller used for the list items displays.
 */
public class ListItemsController implements Controller {

    // This form's olstore instance that is provided via Spring.
	private OlstoreFacade olstore;

	/**
	 * Spring injection setter method for the olstore instance.
	 * @param olstore the olstore bean instance for this form.
	 */
	public void setOlstore(OlstoreFacade olstore) {
		this.olstore = olstore;
	}

	/**
	 * When spring intercepts a request to the specified path (in olstore-servlet.xml) the 
	 * request is handled here.
	 * 
	 * @param request the HttpServletRequest object.
	 * @param response The HttpServletResponse object.
	 * @throws Exception 
	 */
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String type = request.getParameter("type");
        if(type == null) {
            type = "all";
        }
        Map model = ControllerHelper.initModel(request, olstore);
        
        model.put("page_title", "Viewing " + type + " items - Olstore");
        
        Collection items;
        PagedListHolder productList;
        if(request.getParameter("page") == null) {
            if ("all".equals(type)) {
                items = olstore.getAll();
            } else {
                items = olstore.getItemsByType(type);
            }
            model.put("friendsMap", this.olstore.getFriendsMap(request.getRemoteUser(), items));
            
            productList = new PagedListHolder(new ArrayList(items));
            productList.setPageSize(5);
            request.getSession().setAttribute("ListItemsController_type", type);
            request.getSession().setAttribute("ListItemsController_productList", productList);
        }
        else {
            String page = request.getParameter("page");  
            productList = (PagedListHolder) request.getSession().getAttribute("ListItemsController_productList");
            if ("next".equals(page)) {
               productList.nextPage( );
            }
            else if ("previous".equals(page)) {
               productList.previousPage( );
            }     
        }
        
        model.put("type", type);
        model.put("productList", productList);
        return new ModelAndView("listItems", model);        
    }
}
