/* ===================================================================
 * RecipeCommentsForm.java
 * 
 * Created Mar 9, 2005 10:10:07 AM
 * 
 * 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: RecipeCommentsForm.java 28 2009-05-04 01:19:45Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

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

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

import magoffin.matt.ieat.biz.BizContext;
import magoffin.matt.ieat.biz.RecipeSearchBiz;
import magoffin.matt.ieat.domain.Recipe;
import magoffin.matt.ieat.domain.RecipeComment;
import magoffin.matt.ieat.domain.UiData;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.util.EmailRecipeCommand;
import magoffin.matt.ieat.util.RecipeCommand;
import magoffin.matt.ieat.util.UserCriteriaImpl;
import magoffin.matt.util.StringUtil;

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

/**
 * Form controller for adding and viewing recipe comments.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 28 $ $Date: 2009-05-04 13:19:45 +1200 (Mon, 04 May 2009) $
 */
public class RecipeCommentsForm extends AbstractEatForm {
	
	@SuppressWarnings("unchecked")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		// anonymous user ok here
		WebUtil.getBizContext(request,false);
		
		Map<String, Object> m = new LinkedHashMap<String, Object>();
		
		UiData ui = getDomainObjectFactory().getUiDataInstance();
		m.put("uidata",ui);
		
		// add recipe to UI
		RecipeCommand recipeCommand = (RecipeCommand)command;
		Recipe r = recipeBiz.getRecipeById(recipeCommand.getRecipeId());
		ui.getRecipe().add(r);
		
		// we'll store user matches and the possible recipe index here
		UiSearchResults sr = getDomainObjectFactory().getEmbeddedSearchResultsInstance();
		ui.setSearchResults(sr);

		// for any/all comments, get users and add them to UI so can display name
		if ( !r.getComment().isEmpty() ) {
			UserCriteriaImpl userCriteria = new UserCriteriaImpl();
			userCriteria.setSearchMode(RecipeSearchBiz.UserCriteria.MODE_USERS_BY_ID);
			for ( RecipeComment rc : (List<RecipeComment>)r.getComment() ) {
				if ( rc.getUserId() != null ) {
					userCriteria.addUserId(rc.getUserId());
				}
			}
			sr.getUser().addAll(recipeSearchBiz.searchForUsers(userCriteria));
		}
		
		if ( recipeCommand.getIndex() != null || recipeCommand.getCriteria() != null ) {
			WebUtil.initSearchResults(sr,recipeCommand.getIndex(),recipeCommand.getCriteria(), 
					getDomainObjectFactory());
		}
		return m;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// we require a logged in here user
		BizContext context = WebUtil.getBizContext(request,true);
		
		RecipeCommand recipeCommand = (RecipeCommand)command;
		
		String comment = StringUtil.trimToNull(recipeCommand.getComment());
		recipeBiz.addCommentToRecipe(recipeCommand.getRecipeId(),comment,context);
		
		// save message that comment has been saved
		ObjectError msg = new ObjectError(null,
				new String[] {"recipe-comments.saved"},null,
				"The recipe comment has been sent.");
		WebUtil.saveError(request,msg);
		
		Map<String, Object> model = new LinkedHashMap<String, Object>();
		WebUtil.saveRecipeCommand(model,recipeCommand);
		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);
	}

}
