예제 #1
0
  @RequestMapping(value = "/submit", method = RequestMethod.POST)
  public String index(
      @Valid @ModelAttribute("orderForm") OrderFormDTO orderForm,
      Model m,
      BindingResult result,
      RedirectAttributes redirectAttributes,
      UriComponentsBuilder uriBuilder) {

    List<Long> serviceIds = orderForm.getServiceIds();
    List<Long> tireIds = orderForm.getTireIds();
    OrderDTO order = new OrderDTO();

    for (Long tireId : tireIds) {
      OrderItemDTO tireItem = new OrderItemDTO();
      tireItem.setItem(tireFacade.getTireById(tireId));
      order.addOrderItem(tireItem);
    }

    for (Long serviceId : serviceIds) {
      OrderItemDTO serviceItem = new OrderItemDTO();
      serviceItem.setItem(serviceFacade.findServiceById(serviceId));
      order.addOrderItem(serviceItem);
    }

    String email = orderForm.getEmail();
    if (email != null) {
      CustomerDTO customer = new CustomerDTO();
      customer.setEmail(email);
      customerFacade.createCustomer(customer);
    }

    redirectAttributes.addFlashAttribute("alert_success", "Order was successfully submitted");
    return "redirect:" + uriBuilder.path("/").toUriString();
  }
  /**
   * Action to process submit order form
   *
   * @param orderForm
   * @param m
   * @param result
   * @param request
   * @param redirectAttributes
   * @return
   */
  @RequestMapping(value = "/submit", method = RequestMethod.POST)
  public String submitOrder(
      @Valid @ModelAttribute("orderForm") OrderFormDTO orderForm,
      Model m,
      BindingResult result,
      HttpServletRequest request,
      RedirectAttributes redirectAttributes,
      UriComponentsBuilder uriBuilder) {

    List<Long> serviceIds = orderForm.getServiceIds();
    List<Long> tireIds = orderForm.getTireIds();
    OrderDTO order = new OrderDTO();

    Collection<OrderItemDTO> orderItemCollection = new ArrayList<OrderItemDTO>();

    for (Long tireId : tireIds) {
      OrderItemDTO tireItem = new OrderItemDTO();
      tireItem.setItem(tireFacade.getTireById(tireId));
      orderItemCollection.add(tireItem);
    }

    for (Long serviceId : serviceIds) {
      OrderItemDTO serviceItem = new OrderItemDTO();
      serviceItem.setItem(serviceFacade.findServiceById(serviceId));
      orderItemCollection.add(serviceItem);
    }

    CustomerDTO customer = (CustomerDTO) request.getSession().getAttribute("auth");
    if (customer != null) {
      order.setCustomer(customer);
    }

    order.setOrderItems(orderItemCollection);

    try {
      orderFacade.createOrder(order);
    } catch (PneuFacadeException ex) {
      Logger.getLogger(OrderController.class.getName()).log(Level.SEVERE, ex.getMessage());
      redirectAttributes.addFlashAttribute(
          "alert_danger", "Error occured " + "while submitting the order. Please, try again.");
      return "redirect:/";
    }

    redirectAttributes.addFlashAttribute("alert_success", "Order was successfully submitted");
    return "redirect:/";
  }