/**
  * Request method to delete category
  *
  * @param id of category to delete
  * @return view
  */
 @RequestMapping(value = "/delete/category", method = RequestMethod.GET)
 public ModelAndView deleteCategory(@RequestParam int id) {
   categoryService.deleteCategory(id);
   ModelAndView view = new ModelAndView("redirect:/admin/categories");
   view.addObject("categories", categoryService.getAllCategories());
   return view;
 }
 /**
  * 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 show all categories
   *
   * @param page number of categories
   * @return view
   */
  @RequestMapping(value = "/categories", method = RequestMethod.GET)
  public ModelAndView categoriesGet(@RequestParam(defaultValue = "1") int page) {
    int pageCount = categoryService.getCategoriesCount("");
    if (page < 1 || page > pageCount) {
      page = 1;
    }

    List<Category> listCategories = categoryService.getCategoriesInRange(page, "");

    ModelAndView view = new AdminModelAndView("categories");
    view.addObject("categories", listCategories);
    view.addObject("category", new Category());
    view.addObject("pageCount", pageCount);
    view.addObject("page", page);
    return view;
  }
  /**
   * Request method to add or update category
   *
   * @param category to add or update
   * @return view
   */
  @RequestMapping(value = "/add/category", method = RequestMethod.POST)
  public ModelAndView addCategory(@ModelAttribute Category category) {

    categoryService.addOrUpdateCategory(category);
    ModelAndView view = new ModelAndView("redirect:/admin/categories");

    return view;
  }
 /**
  * Admin root path
  *
  * @return view
  */
 @RequestMapping(method = RequestMethod.GET)
 public ModelAndView adminHome() {
   ModelAndView view = new AdminModelAndView("admin");
   view.addObject("usersCount", userService.getAllUsers().size());
   view.addObject("productsCount", productService.getAllProducts().size());
   view.addObject("categoriesCount", categoryService.getAllCategories().size());
   view.addObject("purchasesCount", purchaseService.getAllPurchases().size());
   view.addObject("promotions", productService.getOnPromotion());
   return view;
 }
 /**
  * Request method to show one product
  *
  * @param productId id of product to show
  * @return view
  */
 @RequestMapping(value = "/product", method = RequestMethod.GET)
 public ModelAndView addOrEditProductGet(@RequestParam int productId) {
   ModelAndView view = new AdminModelAndView("add_product");
   if (productId == 0) {
     view.addObject("product", new Product());
   } else {
     view.addObject("product", productService.getProduct(productId));
   }
   view.addObject("categoriesList", categoryService.getAllCategories());
   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";
    }
  }