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

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

import olstore.domain.logic.OlstoreFacade;
import olstore.dto.ItemValue;

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

/**
 * A controller used for when an individual item is viewed.
 */
public class ViewItemController implements Controller {

    // The olstore bean object injected via Spring.
	private OlstoreFacade olstore;

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

    /**
     * Handle the request to display a single item in the store.
     * 
     * @param request the HttpServletRequest.
     * @param response the HttpServletResponse.
     * @return a new model and view to to display this item
     */
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map model = ControllerHelper.initModel(request, olstore);
        
		String itemId = request.getParameter("itemId");
        if(itemId != null && itemId.length() > 0) {
            try {                
                ItemValue item = this.olstore.getItemById(itemId);
                ArrayList items = new ArrayList();
                items.add(item);
                model.put("friendsMap", this.olstore.getFriendsMap(request.getRemoteUser(), items));
        		model.put("item", item);
        		model.put("picture", this.olstore.getPictureById(itemId));
                model.put("page_title", "Viewing '" + item.getName() + "' - Olstore");
                
                String interesting = request.getParameter("interesting");
                if(interesting != null && interesting.equals("true")) {
                    try {
                        this.olstore.markItemAsInteresting(item.getItemId(), request.getRemoteUser());
                        model.put("message", "Item successfully marked as interesting.");
                    } catch (Exception e) {
                        model.put("error", "There was an error when trying to mark this item as interesting.");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                model.put("page_title", "Item not found - Olstore");
                model.put("message", "The item you requested could not be found.");
            }
            return new ModelAndView("viewItem", model);
        }
        else {
            return new ModelAndView(new RedirectView("index.do"));
        }
	}

}
