コード例 #1
0
  @RequestMapping(value = "/userBooking", method = RequestMethod.GET)
  public String bookingCart(Model model, HttpSession session) {
    if (!SessionUtils.isUser(session)) {
      return "index";
    }
    UserDAO userDAO = (UserDAO) context.getBean("userDAO");
    User user = userDAO.get(SessionUtils.getUserIdFromSessionOrNull(session));
    CartDAO cartDAO = (CartDAO) context.getBean("cartDAO");
    Cart cart = cartDAO.getUserCart(user);

    ArrayList<CartBooking> bookings = createBookingsToDisplay(cart);
    model.addAttribute("bookings", bookings);
    model.addAttribute("user", user);

    return "userBooking";
  }
コード例 #2
0
  @ResponseBody
  @RequestMapping(value = "/userBooking/book", method = RequestMethod.POST)
  public String book(
      @RequestParam String type,
      @RequestParam String pizzeria,
      @RequestParam String date,
      @RequestParam String time,
      Model model,
      HttpSession session) {

    SimpleDateFormat sdfDate = new SimpleDateFormat("dd/M/yyyy");
    SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm");
    Date d = null;
    Date t = null;
    try {
      d = sdfDate.parse(date);
      t = sdfTime.parse(time);

    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    DateFormat dateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm");
    Date today = new Date();
    if (today.getTime() > d.getTime() + t.getTime()) return "dateError";
    // set correctly date
    UserDAO userDAO = (UserDAO) context.getBean("userDAO");
    BookingDAO bookingDAO = (BookingDAO) context.getBean("bookingDAO");
    CartDAO cartDAO = (CartDAO) context.getBean("cartDAO");
    PizzeriaDAO pizzeriaDAO = (PizzeriaDAO) context.getBean("pizzeriaDAO");
    OrderItemDAO orderItemDAO = (OrderItemDAO) context.getBean("orderItemDAO");

    User user = userDAO.get(SessionUtils.getUserIdFromSessionOrNull(session));
    Cart cart = cartDAO.getUserCart(user);
    ArrayList<OrderItem> toBook = itemToBook(pizzeria, cart);
    Pizzeria pizzerias = pizzeriaDAO.getByName(pizzeria);
    Booking booking = createBooking(type);
    booking.setConfirmed(false);
    booking.setDate(d);
    booking.setTime(t);
    booking.setPizzeria(pizzerias);
    booking.setUser(user);

    if (booking instanceof BookingDelivery)
      ((BookingDelivery) booking).setDeliveryAddress(user.getAddress());
    booking.setBill(BookingUserDisplayUtils.calculateBill(toBook));

    bookingDAO.create(booking);

    for (OrderItem orderItem : toBook) {
      orderItem.setBooking(booking);
      orderItem.setCart(null);
      if (orderItem instanceof PizzaOrderItem)
        OrderItemUtils.setPizzaOrderCost((PizzaOrderItem) orderItem, pizzerias);
      else if (orderItem instanceof BeverageOrderItem)
        OrderItemUtils.setBeverageOrderCost((BeverageOrderItem) orderItem, pizzerias);
      orderItemDAO.update(orderItem);
    }
    booking.setOrderItems(toBook);
    bookingDAO.update(BookingUtils.calculateBill(booking, pizzerias));
    // ArrayList<CartBooking> bookings = createBookingsToDisplay(cart);
    // model.addAttribute("bookings", bookings);
    model.addAttribute("user", user);
    return "{\"success\" : true}";
  }