/**
 * 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.Collection;
import java.util.Map;

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

import olstore.domain.logic.OlstoreFacade;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * The controller used for the view index action.
 */
public class ViewIndexController implements Controller {

    // The olstore bean instance supplied via Spring.
	private OlstoreFacade olstore;

	/**
	 * The Spring injection method to set the olstore bean's instance.
	 * @param olstore the bean instance set via Spring.
	 */
	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 {
		Map model = ControllerHelper.initModel(request, olstore);
        model.put("user", olstore.getUser(request.getRemoteUser()));
        model.put("friendSet", ((Collection) olstore.findByMainPageAndFriendsOf(request.getRemoteUser())));
        model.put("specialSet", ((Collection) olstore.findByMainPageAndSpecialOffer()));
        model.put("otherSet", ((Collection) olstore.findByMainPage()));
		model.put("page_title", "RHAPS Olstore Demo");
        return new ModelAndView("index", model);
	}

}
