/* ===================================================================
 * ImportRecipeMLForm.java
 * 
 * Created Feb 15, 2005 6:24:27 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: ImportRecipeMLForm.java 77 2009-05-18 00:11:32Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

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

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

import magoffin.matt.ieat.domain.Recipe;
import magoffin.matt.ieat.domain.RecipeIngredient;
import magoffin.matt.ieat.domain.UiData;
import magoffin.matt.ieat.domain.UiEdit;
import magoffin.matt.ieat.util.ImportRecipeCommand;
import magoffin.matt.ieat.validation.PagedValidator;
import magoffin.matt.util.TemporaryFile;
import magoffin.matt.util.TemporaryFileMultipartFileEditor;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

/**
 * Form controller to import a RecipeML recipe.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 77 $ $Date: 2009-05-18 12:11:32 +1200 (Mon, 18 May 2009) $
 */

public class ImportRecipeMLForm extends AddRecipeForm {
    
	@Override
	@SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request, Object command,
		Errors errors, int page) throws Exception {
		ImportRecipeCommand irCommand = (ImportRecipeCommand)command;
		Map<String, Object> model = new LinkedHashMap<String, Object>();
		
		if ( irCommand.getImportRecipe() != null ) {
			// first create a temporary UiEdit object to pass to super class
			UiEdit edit = getDomainObjectFactory().getUiEditInstance();
			edit.setRecipe(irCommand.getImportRecipe());
			model = super.referenceData(request, edit, errors, page);
			
			// now add our import recipe to the reference data's UiData object (created in super class)
			UiData data = (UiData)model.get("uidata");
			data.getRecipe().add(irCommand.getImportRecipe());
			
			// and add edit recipe so XSL has both to work with and compare
			if ( irCommand.getRecipe() != null ) {
			    data.getRecipe().add(irCommand.getRecipe());

			    // look for any recipe ingredients that must be added
				for ( Iterator itr = irCommand.getRecipe().getIngredient().iterator(); itr.hasNext(); ) {
					RecipeIngredient ri = (RecipeIngredient)itr.next();
					if ( ri.getRecipe() != null && ri.getRecipe().getRecipeId() != null && ri.getRecipe().getRecipeId().intValue() > 0 ) {
						Recipe relatedRecipe = recipeBiz.getRecipeById(ri.getRecipe().getRecipeId());
						data.getRecipe().add(relatedRecipe);
					}
				}
			}
			
		}
		
		return model;
	}
	
	@Override
	protected void validatePage(Object command, Errors errors, int page) {
	    PagedValidator pv = (PagedValidator)getValidator();
	    pv.validate(command,errors,page);
	}
	
	@Override
    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {
		// ensure logged in user...
		WebUtil.getBizContext(request, true);
    		
    	// override AddRecipeForm's implementation
        return createCommand();
    }
    
   @Override
	protected ModelAndView processFinish(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// store recipe in backend!
		ImportRecipeCommand irCommand = (ImportRecipeCommand)command;
		Recipe r = irCommand.getRecipe();
		return processFinish(request,response,command,errors,r);
	}
    
	@Override
    protected ModelAndView processCancel(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        // assume no model required for cancel 
        return new ModelAndView(getCancelView());
    }
    
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		super.initBinder(request, binder);
		
		// register our Multipart TemporaryFile binder...
		binder.registerCustomEditor(TemporaryFile.class, new TemporaryFileMultipartFileEditor());
	}
	
}
