/* ===================================================================
 * RecipeValidator.java
 * 
 * Created Sep 16, 2004 9:11:10 AM
 * 
 * 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: RecipeValidator.java 39 2009-05-06 03:30:47Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.validation;

import java.util.List;

import magoffin.matt.ieat.domain.Recipe;
import magoffin.matt.ieat.domain.RecipeIngredient;
import magoffin.matt.ieat.domain.RecipeStep;
import magoffin.matt.ieat.domain.System;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

/**
 * Validator for Recipe objects.
 * 
 * <p>
 * Supports page-style validation. Set the <code>page</code>
 * attribute value to only validate data up to that "page".
 * </p>
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 39 $ $Date: 2009-05-06 15:30:47 +1200 (Wed, 06 May 2009) $
 */
public class RecipeValidator extends AbstractPagedValidator {
	
	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public boolean supports(Class clazz) {
		return Recipe.class.isAssignableFrom(clazz);
	}
	
	/* (non-Javadoc)
	 * @see magoffin.matt.ieat.validation.PagedValidator#validate(int, java.lang.Object, org.springframework.validation.Errors)
	 */
	@SuppressWarnings("unchecked")
	public void validate(Object obj, Errors errors, int page) {
		Recipe r = (Recipe)obj;
		
		System system = r.getSystem();
		if ( system == null || system.getSystemId() == null) {
			errors.rejectValue("recipe.system.systemId", "error.required", null, "recipe.system.displayName");
		}
		
		if ( page != 0 ) {
			if ( r.getName() == null || !StringUtils.hasLength(r.getName().trim()) ) {
				errors.rejectValue("recipe.name","error.required",null,"recipe.name.displayName");
			}
			if ( r.getExcerpt() == null || !StringUtils.hasLength(r.getExcerpt().trim()) ) {
				errors.rejectValue("recipe.excerpt","error.required",null,"recipe.excerpt.displayName");
			}
			if ( r.getServingSize() == null ) {
				errors.rejectValue("recipe.servingSize","error.required",
						null,"recipe.servings.displayName");
			}
			if ( r.getIngredient() != null && r.getIngredient().size() > 0 ) {
				List<RecipeIngredient> ingredients = r.getIngredient();
				for ( int i = 0; i < ingredients.size(); i++ ) {
					RecipeIngredient ri = ingredients.get(i);
					if ( ri.getQuantity() <= 0 ) {
						errors.rejectValue("recipe.ingredient["+i+"].quantity",
								"error.required",null,"recipe.ingredient.quantity.displayName");
					}
					
					boolean hasRecipe = false;
					if ( ri.getRecipe() != null ) {
						Long id = ri.getRecipe().getRecipeId();
						if ( id != null && id.intValue() > 0 ) {
							hasRecipe = true;
							ri.setIngredient(null); // remove "fake" ingredient
						} else {
							ri.setRecipe(null); // remove "fake" recipe
						}
					}
					
					if ( !hasRecipe && (ri.getIngredient() == null || ri.getIngredient().getIngredientId() == null
							|| ri.getIngredient().getIngredientId().intValue() < 1) ) {
						errors.rejectValue("recipe.ingredient["+i+"].ingredient.ingredientId",
								"error.required",null,"recipe.ingredient.ingredient.displayName");
					}
				}
			}
			if ( r.getStep() != null && r.getStep().size() > 0 ) {
				List steps = r.getStep();
				for ( int i = 0; i < steps.size(); i++ ) {
					RecipeStep step = (RecipeStep)steps.get(i);
					if ( step.getValue() == null || !StringUtils.hasLength(step.getValue().trim()) ) {
						errors.rejectValue("recipe.step["+i+"].value",
								"error.required",null,"recipe.step.value.displayName");
					}
				}
			}
		}
	}
	
}
