示例#1
0
 public ModelAndView viewOrders(
     HttpServletRequest request, HttpServletResponse response, HttpSession session) {
   ModelAndView mv = new ModelAndView("orders");
   Customer cust = (Customer) session.getAttribute("CUST");
   if (cust != null) {
     List<Order> orders = orderService.query("BYCUSTOMER", cust.getCustomerId());
     System.out.println(orders.size());
     request.setAttribute("ORDERS", orders);
   } else {
     mv.addObject("msg", "Error retriving orders, please do a checkout first");
   }
   return mv;
 }
示例#2
0
  @RequestMapping(path = "/checkOut", method = RequestMethod.POST)
  @ResponseBody
  public String checkOut(@RequestBody Customer customer, HttpSession session) {
    String response = null;
    System.out.print("custName " + customer.getName() + "," + customer.getEmail());
    customer.setCustomerId(customer.getEmail());
    Gson gson = new Gson();
    Order order = new Order();
    // save customer
    // customer=customerService.add(customer);
    /*		List<Customer> currentCustomer=customerService.query("BY_EMAIL",customer.getCustomerId());
    		if (currentCustomer.size()==1) {

    			customer=currentCustomer.get(0);
    		}
    */
    order.setCustomer(customer);
    // hack
    order.setCustomerId(customer.getCustomerId());
    order.setOrderDate(new Date());
    order.setName("ORD_" + customer.getName());
    Cart cart = (Cart) session.getAttribute("CART");
    order.setTotalPrice(cart.getTotalPrice());
    // save customer to session
    session.setAttribute("CUST", customer);
    Iterator<CartItem> cartItems = cart.getItems().iterator();

    while (cartItems.hasNext()) {
      CartItem cartItem = cartItems.next();
      OrderItem orderItem = new OrderItem();
      orderItem.setName(cartItem.getItemName());
      Product product = new Product();
      product.setId(Long.valueOf(cartItem.getItemId()));
      orderItem.setProduct(product);
      orderItem.setQty(cartItem.getQty());
      orderItem.setLineCost(BigDecimal.valueOf(cartItem.getLinePrice()));
      orderItem.setOrder(order);
      order.getItems().add(orderItem);
      cartItems.remove();
    }
    orderService.add(order);
    // clear cart items
    // cart.setItems(new ArrayList<CartItem>());
    cart.setTotalPrice(BigDecimal.ZERO);
    session.setAttribute("CART", cart);
    return gson.toJson(customer);
  }