package foodplanet.web.controller;

import foodplanet.data.dao.FoodItemDao;
import foodplanet.data.model.FoodItem;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class FoodItemListController extends AbstractController {
    private static Log log = LogFactory.getLog(CategoryListController.class);
    private String viewName;
    private FoodItemDao itemDao;
    
    public FoodItemListController() {
    }
    
    public void setViewName(String view) {
        this.viewName = view;
    }
    public void setFoodItemDao(FoodItemDao dao) {
        this.itemDao = dao;
    }
    
    private Long getCatId(HttpServletRequest req) {
        long result = 0;
        try{
            result = Long.parseLong(req.getParameter("catid"));
        }catch(NumberFormatException e){}
        return result;
    }
    
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        log.info("***** Food Item Controller: Processing Request ******");
        List<FoodItem> items = itemDao.findAllFoodItems(getCatId(request));
        request.setAttribute("listSize", items.size());
        request.setAttribute("list", items);
        return new ModelAndView(viewName);
    }
}
