/*
 * FoodItemStockController.java
 *
 * Created on October 31, 2006, 7:59 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package foodplanet.web.controller;

import foodplanet.data.dao.FoodItemDao;
import foodplanet.data.manager.DataManager;
import foodplanet.data.manager.SpringDataManager;
import foodplanet.data.model.FoodItem;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author vladimir
 */
public class FoodItemStockController extends AbstractController{
    private FoodItemDao itemDao;
    private String viewName;
    
    /** Creates a new instance of FoodItemStockController */
    public FoodItemStockController() {
    }
    
    
    public void setFoodItemDao(FoodItemDao dao) {
        this.itemDao = dao;
    }
    
    public void setViewName(String view) {
        this.viewName = view;
    }

    private Long getItemId(HttpServletRequest req) {
        String id = req.getParameter("id");
        return (id != null) ? new Long(id) : 0;
    }
    
    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 {
        long id = getItemId(request);
        
        itemDao.updateItemStock(id);

        List<FoodItem> items = itemDao.findAllFoodItems(getCatId(request));
        request.setAttribute("listSize", items.size());
        request.setAttribute("list", items);

        return new ModelAndView(this.viewName);
    }
    
}
