コード例 #1
0
  /**
   * Example URL: http://localhost:8080/webstore/products/add
   *
   * <p>Called when a POST is requested
   *
   * @param newProduct
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public String processAddNewProductForm(
      @ModelAttribute("newProduct") @Valid Product newProduct,
      BindingResult result,
      HttpServletRequest request) {
    if (result.hasErrors()) {
      return "addProduct";
    }

    String[] suppressedFields = result.getSuppressedFields();
    if (suppressedFields.length > 0) {
      throw new RuntimeException(
          "Attempting to bind disallowed fields: "
              + StringUtils.arrayToCommaDelimitedString(suppressedFields));
    }

    MultipartFile productImage = newProduct.getProductImage();
    MultipartFile productManual = newProduct.getProductManual();

    String rootDirectory = request.getSession().getServletContext().getRealPath("/");

    if (productImage != null && !productImage.isEmpty()) {
      try {
        productImage.transferTo(
            new File(rootDirectory + "resources\\images\\" + newProduct.getProductId() + ".jpg"));
      } catch (Exception e) {
        throw new RuntimeException("Product Image saving failed", e);
      }
    }

    if (productManual != null && !productManual.isEmpty()) {
      try {
        productManual.transferTo(
            new File(rootDirectory + "resources\\pdf\\" + newProduct.getProductId() + ".pdf"));
      } catch (Exception e) {
        throw new RuntimeException("Product Manual saving failed", e);
      }
    }

    productService.addProduct(newProduct);
    return "redirect:/products";
  }
コード例 #2
0
 public Product getProductById(String productId) {
   // TODO Auto-generated method stub
   Product productById = null;
   for (Product product : listOfProducts) {
     if (product != null
         && product.getProductId() != null
         && product.getProductId().equals(productId)) {
       productById = product;
       break;
     }
   }
   if (productById == null) {
     throw new ProductNotFoundException("No products found with the product id: " + productId);
   }
   return productById;
 }
コード例 #3
0
  @Override
  public Product getProductById(String productID) {
    Product productById = null;
    for (Product product : listOfProducts) {
      if (product != null
          && product.getProductId() != null
          && product.getProductId().equals(productID)) {
        productById = product;
        break;
      }
    }

    if (productById == null) {
      throw new ProductNotFoundException("No products found withthe product id: " + productID);
    }
    return productById;
  }
コード例 #4
0
  public Product getProductById(String productId) {
    Product productById = null;

    for (Product product : listOfProducts) {
      if (product != null
          && product.getProductId() != null
          && product.getProductId().equals(productId)) {
        productById = product;
        break;
      }
    }

    if (productById == null) {
      throw new IllegalArgumentException("No products found with the product id: " + productId);
    }

    return productById;
  }