/* ===================================================================
 * MealValidator.java
 * 
 * Copyright (c) 2004 Matt Magoffin. Created Aug 18, 2004 8:42:27 PM.
 * 
 * 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: MealValidator.java 27 2009-05-03 23:40:50Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.validation;

import java.util.List;

import magoffin.matt.ieat.domain.Meal;
import magoffin.matt.ieat.domain.MealRecipe;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Validator for Meal objects.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 27 $ $Date: 2009-05-04 11:40:50 +1200 (Mon, 04 May 2009) $
 */
public class MealValidator implements Validator {

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public boolean supports(Class clazz) {
		return Meal.class.isAssignableFrom(clazz);
	}

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@SuppressWarnings("unchecked")
	public void validate(Object obj, Errors errors) {
		Meal meal = (Meal)obj;
		if ( meal == null ) return;
		if ( meal.getName() == null || meal.getName().trim().length() < 1 ) {
			errors.rejectValue("meal.name", "error.required", null, "name.displayName");
		}
		if ( meal.getRecipe() != null && meal.getRecipe().size() > 0 ) {
			List<MealRecipe> recipes = meal.getRecipe();
			for ( int i = 0; i < recipes.size(); i++ ) {
				MealRecipe mr = recipes.get(i);
				if ( mr.getQuantity() <= 0 ) {
					errors.rejectValue("meal.recipe["+i+"].quantity",
							"error.required",null,"meal.recipe.quantity.displayName");
				}
				
				if ( mr.getCourse() == null || mr.getCourse().getCourseId() == null 
						|| mr.getCourse().getCourseId().intValue() < 1 ) {
					errors.rejectValue("meal.recipe["+i+"].course.courseId",
							"error.required",null,"meal.recipe.course.displayName");
				}
				
				if ( mr.getRecipe() == null || mr.getRecipe().getRecipeId() == null
						|| mr.getRecipe().getRecipeId().intValue() < 1) {
					errors.rejectValue("meal.recipe["+i+"].recipe.recipeId",
							"error.required",null,"meal.recipe.recipe.displayName");
				}
			}
		}
	}

}
