/**
 * 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.Map;

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

import olstore.domain.logic.OlstoreFacade;
import olstore.domain.manager.CartManager;
import olstore.dto.ItemValue;
import olstore.dto.PictureValue;

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

/**
 * A Spring controller class that handles adding an item to the shopping cart.
 */
public class AddItemToCartController implements Controller {

    // Instance of the olstore bean injected via Spring.
	private OlstoreFacade olstore;

	// Spring injection setter method for olstore instance.
	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);
        
        String itemId = request.getParameter("itemId");
		ItemValue item = this.olstore.getItemById(itemId);
        PictureValue pic = this.olstore.getPictureById(itemId);
        
		model.put("item", item);
		model.put("picture", pic);
        
        // add item to the cart if it's not already in it
        CartManager cartManager = (CartManager) request.getSession().getAttribute("cart");
        if(!cartManager.containsItem(Integer.valueOf(itemId))) {
            cartManager.updateQuantity(Integer.valueOf(itemId), new Integer(1));
            model.put("message", "The item has been added to your shopping cart.");   
        } else {
            model.put("error", "Item already in shopping cart.");
        }
        
        model.put("page_title", "Viewing '" + item.getName() + "' - Olstore");
        return new ModelAndView("viewItem", model);
	}

}
