/* ===================================================================
 * ExportRecipeForm.java
 * 
 * Created Feb 24, 2005 6:02:04 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: ExportRecipeForm.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.UiMetaSupport;
import magoffin.matt.ieat.domain.UiParameter;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.util.RecipeCommand;

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

/**
 * Form controller for exporting a recipe.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 28 $ $Date: 2009-05-04 13:19:45 +1200 (Mon, 04 May 2009) $
 */
public class ExportRecipeForm extends AbstractEatForm {
	
	private RecipeIOBiz rioBiz = null;
	private UiMetaSupport formats = null;

	@SuppressWarnings("unchecked")
	@Override
	protected void initApplicationContext() {
		super.initApplicationContext();
		if ( rioBiz == null ) {
			throw new RuntimeException("The rioBiz property is not configured.");
		}
		
		// create our UiMetaSupport list now so we don't need to each request
		String[] formatStrings = rioBiz.getSupportedExportFormats();
		formats = getDomainObjectFactory().getMetaSupportInstance();
		for ( int i = 0; i < formatStrings.length; i++ ) {
			UiParameter meta = getDomainObjectFactory().getParameterInstance();
			meta.setKey("export-format");
			meta.setValue(formatStrings[i]);
			formats.getMeta().add(meta);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		RecipeCommand recipeCommand = (RecipeCommand)command;
		Map<String, Object> data = new LinkedHashMap<String, Object>();
		UiData ui = getDomainObjectFactory().getUiDataInstance();
		
		Recipe recipe = recipeBiz.getRecipeById(recipeCommand.getRecipeId());
		ui.getRecipe().add(recipe);
		ui.setMetaSupport(formats);
		
		if ( recipeCommand.getIndex() != null || recipeCommand.getCriteria() != null ) {
			UiSearchResults sr = getDomainObjectFactory().getEmbeddedSearchResultsInstance();
			WebUtil.initSearchResults(sr,recipeCommand.getIndex(),recipeCommand.getCriteria(), 
					getDomainObjectFactory());
			ui.setSearchResults(sr);
		}

		data.put("uidata",ui);
		return data;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
		    HttpServletResponse response, Object command, BindException errors)
		    throws Exception {
		RecipeCommand recipeCommand = (RecipeCommand)command;
		
		Recipe recipe = recipeBiz.getRecipeById(recipeCommand.getRecipeId());

		response.setHeader("Content-Disposition","attachment;filename=\"" 
				+rioBiz.getFileNameForExport(recipe,recipeCommand.getMode()) +"\"");
		response.setContentType(rioBiz.getMimeForExport(recipe,recipeCommand.getMode()));

		rioBiz.exportRecipe(recipe,request.getLocale(),
				response.getOutputStream(),recipeCommand.getMode());
		
		return null;
	}

	/**
	 * @return the rioBiz
	 */
	public RecipeIOBiz getRioBiz() {
		return rioBiz;
	}

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