package actions;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import beans.*;
import java.util.*;
public final class AddToCartServlet extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionForward nextPage = null;
HttpSession session = request.getSession();
ArrayList theCart = new ArrayList();
if (session.getAttribute("cart") != null){
theCart = (ArrayList)session.getAttribute("cart");
}
beans.ProductFormBean theBean = (ProductFormBean)form;
managers.ProductManager manager = new managers.ProductManager();
if (theBean.getProductId() == 0) {
request.setAttribute("message", "Product Id is required");
nextPage = mapping.findForward("failure");
}
else if (theBean.getQuantity() == 0) {
request.setAttribute("message", "Quantity is required");
nextPage = mapping.findForward("failure");
}
else{
try{
theBean = manager.find(theBean);
if (theBean == null){
nextPage = mapping.findForward("failure");
request.setAttribute("message", "Error locating product");
}
else{
request.setAttribute("message", "Product Added to Cart");
theCart.add(theBean);
//request.setAttribute("cart", theCart);
totaller bob = new totaller();
String total = bob.total(theCart);
session.setAttribute("cart", theCart);
nextPage = mapping.findForward("success");
}
}
catch (Exception e){
request.setAttribute("message", e+" "+"exception error");
nextPage = mapping.findForward("failure");
}
// request.setAttribute("cart", theBean);
}
return nextPage;
}
}