/* ===================================================================
 * SearchForIngredientsController.java
 * 
 * Created May 8, 2009 2:29:25 PM
 * 
 * Copyright (c) 2009 Matt Magoffin.
 * 
 * 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: SearchForIngredientsController.java 58 2009-05-11 07:42:09Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

import java.util.List;
import java.util.ListIterator;

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.biz.RecipeSearchBiz.IngredientCriteria;
import magoffin.matt.ieat.domain.RecipeSearchCriteria;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.util.IngredientCriteriaImpl;

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

/**
 * Ingredient search controller.
 *
 * @author matt
 * @version $Revision: 58 $ $Date: 2009-05-11 19:42:09 +1200 (Mon, 11 May 2009) $
 */
public class SearchForIngredientsController extends AbstractEatCommandController {
	
	/**
	 * Extension of {@link IngredientCriteriaImpl} to work with jQuery autocomplete plugin.
	 */
	public static class Command extends IngredientCriteriaImpl 
	implements IngredientCriteria { // note this is specified here for proxy in RecipeSearchBizLuceneImpl
		
		private Integer limit;
		private boolean recipeSearch = false;
		
		/**
		 * Set the query term.
		 * @param query the query
		 */
		public void setQ(String query) {
			setIngredientName(query);
		}

		/**
		 * @return the limit
		 */
		public Integer getLimit() {
			return limit;
		}

		/**
		 * @param limit the limit to set
		 */
		public void setLimit(Integer limit) {
			this.limit = limit;
		}

		/**
		 * @return the recipeSearch
		 */
		public boolean isRecipeSearch() {
			return recipeSearch;
		}

		/**
		 * @param recipeSearch the recipeSearch to set
		 */
		public void setRecipeSearch(boolean recipeSearch) {
			this.recipeSearch = recipeSearch;
		}
		
	}
	
	@Override
	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Command criteria = (Command)command;
		UiSearchResults results = null;
		List<?> matchList = null;
		if ( criteria.isRecipeSearch() ) {
			BizContext context = WebUtil.getBizContext(request, false);
			RecipeSearchCriteria rsCriteria = getDomainObjectFactory().getRecipeCriteriaInstance();
			rsCriteria.setSimpleQuery(criteria.getIngredientName());
			rsCriteria.setMode(RecipeSearchBiz.RECIPE_SEARCH_MODE_SIMPLE);
			results = recipeSearchBiz.findRecipes(rsCriteria, context);
			matchList = results.getRecipe();
		} else {
			results = recipeSearchBiz.findIngredientsSimilarlyNamed(criteria);
			matchList = results.getIngredient();
		}
		if ( criteria.getLimit() != null && criteria.getLimit() > 0 
				&& results.getTotalResults() > criteria.getLimit() ) {
			for ( ListIterator<?> itr = matchList.listIterator(criteria.getLimit()); 
					itr.hasNext(); ) {
				itr.next();
				itr.remove();
			}
		}
		return new ModelAndView(getSuccessView(), WebConstants.DEFALUT_MODEL_OBJECT, results);
	}

}
