/* ===================================================================
 * SearchForRecipesForm.java
 * 
 * Created Nov 1, 2004 4:47:55 PM
 * 
 * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * This program 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 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
 * 02111-1307 USA
 * ===================================================================
 * $Id: SearchForRecipesForm.java 72 2009-05-14 02:11:56Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

import java.util.Map;

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

import magoffin.matt.ieat.biz.BizContext;
import magoffin.matt.ieat.biz.RecipeSearchBiz;
import magoffin.matt.ieat.domain.RecipeSearchCriteria;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.util.RecipeCommand;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

/**
 * Form controller for performing recipe search.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 72 $ $Date: 2009-05-14 14:11:56 +1200 (Thu, 14 May 2009) $
 */
public class SearchForRecipesForm extends AbstractEatForm {
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		WebUtil.getBizContext(request,false);
		if ( isSessionForm() ) {
			// we want to persist the search command object in session not only during form processing
			HttpSession session = request.getSession(false);
			if (session != null) {
				String formAttrName = getFormSessionAttributeName(request);
				Object sessionFormObject = session.getAttribute(formAttrName);
				if (sessionFormObject != null) {
					return sessionFormObject;
				}
			}
		}
		return super.formBackingObject(request);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		RecipeCommand recipeCommand = (RecipeCommand)command;

		// save command object for future returns to search page
		if (isSessionForm()) {
			request.getSession().setAttribute(getFormSessionAttributeName(request), recipeCommand);
		}

		RecipeSearchCriteria criteria = recipeCommand.getCriteria();
		if ( recipeCommand.getMode() == null ) {
			if ( criteria != null && criteria.getMode() != null ) {
				// use mode from criteria
				recipeCommand.setMode(criteria.getMode());
			} else {
				// default to "simple" search
				recipeCommand.setMode(RecipeSearchBiz.RECIPE_SEARCH_MODE_SIMPLE);
			}
		}
	
		boolean userRequired = false;
		if ( RecipeSearchBiz.RECIPE_SEARCH_MODE_ADVANCED.equals(recipeCommand.getMode()) ) {
			// must be logged in here
			userRequired = true;
		}
		BizContext context = WebUtil.getBizContext(request,userRequired);
		
		if ( criteria != null ) {
			criteria.setMode(recipeCommand.getMode());
		}
		
		Map<String, Object> m = errors.getModel();

		UiSearchResults results = recipeSearchBiz.findRecipes(criteria, context);
		results.setMode(recipeCommand.getMode());
		m.put(WebConstants.DEFALUT_MODEL_OBJECT,results);
		
		return new ModelAndView(getSuccessView(),m);
	}
	
	@Override
	protected ModelAndView processCancel(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors) {
		return new ModelAndView(getCancelView());
	}
	
	@Override
	protected boolean isFormSubmission(HttpServletRequest request) {
		// treat all requests as form post
		return true;
	}
	
}
