package com.leonard.actions;
import java.util.*;
import java.util.Map.Entry;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.MappingDispatchAction;
import com.leonard.actionforms.*;
import com.leonard.entity.*;
import com.leonard.service.CartService;
public class CartAction extends MappingDispatchAction {
public ActionForward input(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CartService cs = new CartService();
List<Product> productList = cs.getProducts();
if(productList==null)
{
ActionErrors errors = new ActionErrors();
ActionMessage msg = new ActionMessage("cart.non");
errors.add("loginerror", msg);
saveErrors(request, errors);
}
else
{
request.getSession().setAttribute("productList", productList);
}
return mapping.findForward("to");
}
public ActionForward addProduct(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ItemForm itf = (ItemForm)form;
Cart cart = (Cart)request.getSession().getAttribute("cart");
if(cart==null)
{
cart = new Cart();
}
List<Product> productList = (List)request.getSession().getAttribute("productList");
if(productList!=null)
{
Iterator<Product> pl = productList.iterator();
while(pl.hasNext())
{
Product pr = pl.next();
if(pr.getId()==Integer.parseInt(itf.getPid()))
{
cart.addItem(pr, 1);
break;
}
}
}
request.getSession().setAttribute("cart", cart);
return mapping.findForward("goback");
}
public ActionForward clearCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.getSession().removeAttribute("cart");
return mapping.findForward("next");
}
public ActionForward affordCart(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm lf = (LoginForm)form;
Cart cart = (Cart)request.getSession().getAttribute("cart");
User user = (User)request.getSession().getAttribute("user");
Order order = new Order();
HashMap<Integer,Item> map = cart.getItems();
Set<Entry<Integer, Item>> set = map.entrySet();
Iterator it = set.iterator();
order.setStatus(new Integer(1).shortValue());
//产生新USER 用来保存USER
User newUser = new User();
newUser.setId(user.getId());
newUser.setPassword(user.getPassword());
newUser.setName(user.getName());
newUser.setOrders(user.getOrders());
newUser.setPostcode(user.getPostcode());
newUser.setOffice_phone(user.getOffice_phone());
newUser.setHome_phone(user.getHome_phone());
newUser.setEmail(user.getEmail());
newUser.setCell_phone(user.getCell_phone());
newUser.setAddress(user.getAddress());
user.setPostcode(lf.getPostcode());
user.setOffice_phone(lf.getOffice_phone());
user.setHome_phone(lf.getHome_phone());
user.setEmail(lf.getEmail());
user.setCell_phone(lf.getCell_phone());
user.setAddress(lf.getAddress());
//用户和订单的双向关系
user.getOrders().add(order);
order.setUser(user);
double total = 0;
while(it.hasNext())
{
Entry<Integer,Item> en = (Entry<Integer, Item>) it.next();
Item item = en.getValue();
//订单和产品条目的双向关系
order.getItems().add(item);
item.setOrder(order);
total+=item.getCost();
}
order.setCost(total);
CartService cs = new CartService();
int i = cs.saveOrder(order,user);
if(i>0)
{
request.setAttribute("orderid", i);
request.getSession().setAttribute("user", user);
request.getSession().removeAttribute("cart");
}
else
{
request.getSession().setAttribute("user", newUser);
}
return mapping.findForward("next");
}
public ActionForward gotobuy(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = (User)request.getSession().getAttribute("user");
if(user==null)
{
request.getSession().setAttribute("buy", "buy");
return mapping.findForward("tologin");
}
return mapping.findForward("tobuy");
}
}