/*
 * Created on 2005-12-19
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.huihoo.jfox.soaf.samples.struts.action;

import java.util.Collection;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.huihoo.jfox.soaf.samples.struts.model.Cheese;
import org.huihoo.jfox.soaf.samples.struts.service.CheeseService;

/**
 * @author peter
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class CheeseAction extends Action {
	private final CheeseService cheeseService;

	public CheeseAction(CheeseService cheeseService) {
		this.cheeseService = cheeseService;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CheeseForm cheeseForm = (CheeseForm) form;

		if (!isEmpty(cheeseForm.getName())) {
			Cheese cheese = new Cheese(cheeseForm.getName(), cheeseForm
					.getCountry());
			cheeseService.save(cheese);
		}

		Collection cheeses = cheeseService.getCheeses();
		request.setAttribute("cheesesOfTheWord", cheeses);

		return mapping.findForward("next");
	}

	private boolean isEmpty(String s) {
		return s == null || "".equals(s.trim());
	}

}