/* ===================================================================
 * EmailRecipeForm.java
 * 
 * Created Mar 5, 2005 6:38:05 PM
 * 
 * Copyright (c) 2005 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: EmailRecipeForm.java 28 2009-05-04 01:19:45Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

import java.util.LinkedHashMap;
import java.util.Map;

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

import magoffin.matt.ieat.biz.RecipeIOBiz;
import magoffin.matt.ieat.domain.Recipe;
import magoffin.matt.ieat.domain.UiData;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.util.EmailRecipeCommand;

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

/**
 * Form controller for emailing a recipe to someone.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 28 $ $Date: 2009-05-04 13:19:45 +1200 (Mon, 04 May 2009) $
 */
public class EmailRecipeForm extends AbstractEatForm {
	
	private RecipeIOBiz recipeIOBiz;
	
	@SuppressWarnings("unchecked")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		WebUtil.getBizContext(request,true);
		
		Map<String, Object> m = new LinkedHashMap<String, Object>();
		
		UiData ui = getDomainObjectFactory().getUiDataInstance();
		m.put("uidata",ui);
		
		// add recipe to UI
		EmailRecipeCommand emailCommand = (EmailRecipeCommand)command;
		Recipe r = recipeBiz.getRecipeById(emailCommand.getRecipeId());
		ui.getRecipe().add(r);
		
		if ( emailCommand.getIndex() != null || emailCommand.getCriteria() != null ) {
			UiSearchResults sr = getDomainObjectFactory().getEmbeddedSearchResultsInstance();
			WebUtil.initSearchResults(sr,emailCommand.getIndex(),emailCommand.getCriteria(), 
					getDomainObjectFactory());
			ui.setSearchResults(sr);
		}
		return m;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		EmailRecipeCommand emailCommand = (EmailRecipeCommand)command;
		
		// get the recipe
		Recipe recipe = recipeBiz.getRecipeById(emailCommand.getRecipeId());
		emailCommand.setRecipe(recipe);
		recipeIOBiz.emailRecipe(emailCommand,WebUtil.getBizContext(request,true));

		// save message that email has been sent
		ObjectError msg = new ObjectError(null,
				new String[] {"email-recipe.sent"},null,
				"The email has been sent.");
		WebUtil.saveError(request,msg);
		
		Map<String, Object> model = new LinkedHashMap<String, Object>();
		WebUtil.saveRecipeCommand(model,emailCommand);
		return new ModelAndView(getSuccessView(),model);
	}

	@Override
	protected ModelAndView processCancel(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors) {
		EmailRecipeCommand emailCommand = (EmailRecipeCommand)command;
		Map<String, Object> model = new LinkedHashMap<String, Object>();
		WebUtil.saveRecipeCommand(model,emailCommand);
		return new ModelAndView(getCancelView(),model);
	}

	/**
	 * @return the recipeIOBiz
	 */
	public RecipeIOBiz getRecipeIOBiz() {
		return recipeIOBiz;
	}

	/**
	 * @param recipeIOBiz the recipeIOBiz to set
	 */
	public void setRecipeIOBiz(RecipeIOBiz recipeIOBiz) {
		this.recipeIOBiz = recipeIOBiz;
	}

}
