/** * Request method to get form for category * * @param categoryId category to edit * @return view form */ @RequestMapping(value = "/edit/categoryform/{categoryId}", method = RequestMethod.GET) public ModelAndView editCategoryForm(@PathVariable int categoryId) { ModelAndView view = new ModelAndView("/admin-pages/category_form"); view.addObject("category", categoryService.getCategory(categoryId)); view.addObject("edit", true); return view; }
/** * Request method to add or edit product * * @param productId * @param name * @param description * @param price * @param onPromotion * @param categories * @param p * @return * @throws ParseException * @throws IOException */ @RequestMapping(value = "/product", method = RequestMethod.POST) public String addProductPost( @RequestParam int productId, @RequestParam String name, @RequestParam String description, @RequestParam double price, @RequestParam(defaultValue = "false") boolean onPromotion, @RequestParam("cat") String[] categories, Product p) throws ParseException, IOException { small = p.getFileSmallImage().getFileItem(); large = p.getFileLargeImage().getFileItem(); FileOutputStream smallImageOutputStream = null; FileOutputStream largeImageOutputStream = null; if (small.getSize() > 0) { smallImageOutputStream = new FileOutputStream(new File(FILEPATH + "t" + small.getName())); smallImageOutputStream.write(small.get()); smallImageOutputStream.flush(); smallImageOutputStream.close(); } if (large.getSize() > 0) { largeImageOutputStream = new FileOutputStream(new File(FILEPATH + large.getName())); largeImageOutputStream.write(large.get()); largeImageOutputStream.flush(); largeImageOutputStream.close(); } if (name != "" && description != "" && price == (double) price && onPromotion == true || onPromotion == false) { if (productId == 0) { Product product = new Product(); ArrayList<Category> categoryList = new ArrayList<Category>(); product.setName(name); product.setDescription(description); product.setPrice(price); product.setOnPromotion(onPromotion); product.setSmallImage("t" + small.getName()); product.setLargeImage(large.getName()); for (String i : categories) { categoryList.add(categoryService.getCategory(Integer.parseInt(i))); } product.setCategories(categoryList); productService.addProduct(product); return "redirect:/admin/products"; } else { Product product = productService.getProduct(productId); ArrayList<Category> categoryList = new ArrayList<Category>(); product.setName(name); product.setDescription(description); product.setPrice(price); product.setOnPromotion(onPromotion); product.setCategories(categoryList); if (!"".equals(small.getName())) { product.setSmallImage("t" + small.getName()); } if (!"".equals(large.getName())) { product.setLargeImage(large.getName()); } for (String i : categories) { categoryList.add(categoryService.getCategory(Integer.parseInt(i))); } productService.updateProduct(product); return "redirect:/admin/products"; } } else { return "redirect:/admin/product"; } }