All pastes #284114 Raw Edit

Untitled

public text v1 · immutable
#284114 ·published 2006-12-18 21:38 UTC
rendered paste body
/*
 * AddCakesToOrder.java
 *
 * Created on 13 December 2006, 13:50
 */

package jgibb16.cbcc.actions.customers;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jgibb16.cbcc.model.Order;
import jgibb16.cbcc.model.ProductQuantityDescriptor;
import jgibb16.cbcc.model.StockList;
import jgibb16.cbcc.model.product.ProductInterface;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;
/**
 *
 * @author Jonno
 * @version
 */

public class AddCakesToOrder extends Action {
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // create action errors incase login fails
        ActionMessages errors = new ActionMessages();        
        
        HttpSession session = request.getSession();
        Order order = (Order) session.getAttribute("order");
        
        DynaValidatorForm  theForm = (DynaValidatorForm) form;
        int[] quantities = (int[]) theForm.get("quantity");
         
        List stock;
         
        if(theForm.get("type").equals("cakes"))
        {
           stock = ((StockList)this.getServlet().getServletContext().getAttribute("stockList")).getCakes();
        }
        else
        {
            stock = ((StockList)this.getServlet().getServletContext().getAttribute("stockList")).getAles();            
        }
        
        boolean hasErrors = false;
        
        // check all quantities are within stock levels
        for(int i=0; i<quantities.length; i++) {
            ProductQuantityDescriptor descriptor = ((ProductQuantityDescriptor) stock.get(i));
            ProductInterface tmpProduct = descriptor.getProduct();
            
            if(quantities[i] > 0) {
                if(quantities[i] > descriptor.getQuantity()) {
                    errors.add("qtyToHigh", new ActionMessage("order.qtyToHigh"));
                    hasErrors = true;
                    break;
                }
            }
        }
        
        if(hasErrors) {
            saveErrors(request,errors);
            return mapping.findForward("error");
        }
        
        // loop to add lines to order
        for(int i=0; i<quantities.length; i++) {
            ProductQuantityDescriptor descriptor = ((ProductQuantityDescriptor) stock.get(i));
            ProductInterface tmpProduct = descriptor.getProduct();
            
            if(quantities[i] > 0) {
                order.addLine(tmpProduct.getProductID(), quantities[i]);
                descriptor.setQuantity(descriptor.getQuantity()-quantities[i]);
            }
        }
        
        return mapping.findForward("success");
        
    }
}