/* ===================================================================
 * EditRecipeForm.java
 * 
 * Created Oct 8, 2004 12:18:14 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: EditRecipeForm.java 61 2009-05-12 05:29:41Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import magoffin.matt.ieat.domain.Recipe;
import magoffin.matt.ieat.domain.RecipeIngredient;
import magoffin.matt.ieat.domain.UiEdit;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.domain.Unit;
import magoffin.matt.ieat.util.RecipeCommand;

import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;

/**
 * Form controller for editing an existing recipe.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 61 $ $Date: 2009-05-12 17:29:41 +1200 (Tue, 12 May 2009) $
 */
public class EditRecipeForm extends AddRecipeForm {

	@SuppressWarnings("unchecked")
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception 
	{
		// must be logged in
		WebUtil.getBizContext(request,true);
		
		UiEdit edit = getDomainObjectFactory().getUiEditInstance();

		// get the recipe ID to edit
		RecipeCommand recipeCommand = new RecipeCommand();
		ServletRequestDataBinder binder = createBinder(request, recipeCommand);
		binder.bind(request);
		
		Recipe recipe = recipeBiz.getRecipeById(recipeCommand.getRecipeId());
		
		// prepare ingredient units for edit, since edit form always submits
		// units but units might be empty from backend
		List<RecipeIngredient> l = recipe.getIngredient();
		for ( RecipeIngredient ri : l ) {
			if ( ri.getUnit() == null ) {
				Unit u = getDomainObjectFactory().getUnitInstance();
				u.setUnitId(new Integer(0));
				ri.setUnit(u);
			}
		}
		
		edit.setRecipe(recipe);
		
		if ( recipeCommand.getIndex() != null || recipeCommand.getCriteria() != null  ) {
			UiSearchResults sr = getDomainObjectFactory().getEmbeddedSearchResultsInstance();
			WebUtil.initSearchResults(sr,recipeCommand.getIndex(),recipeCommand.getCriteria(), getDomainObjectFactory());
			edit.setSearchResults(sr);
		}
		
		return edit;
	}
	
	@Override
	protected int getFormPageNumber() {
		return 0;
	}

	@Override
	protected void validatePage(Object command, Errors errors, int page) {
		super.validatePage(command, errors, (page+1)); // page + 1 because on edit no choose system
	}
	
}
