public Category editCategory(
      Long categoryId, String name, String description, String overpassKeyValue, User updateUser) {
    if (updateUser == null || !updateUser.isCategoryWrite() || name.isEmpty()) {
      return null;
    }

    EntityTransaction transaction = startSaveTransaction();

    Category category = getCategoryById(categoryId);
    if (category != null) {
      category.setName(name);
      category.setDescription(description);
      category.setOverpassKeyValue(overpassKeyValue);
      category.setUpdateUser(updateUser);
      category.setUpdateTimestamp(new Timestamp(new Date().getTime()));
    }

    try {
      entityManager.persist(category);
      transaction.commit();
      return category;
    } catch (Exception e) {
      transaction.rollback();
    }

    return null;
  }
  @RequestMapping(value = "/edit-category/{categoryId}", method = RequestMethod.POST)
  public String doEditCategory(
      @ModelAttribute("category") @Valid Category category,
      Errors errors,
      @PathVariable String categoryId,
      @RequestParam("name") String name,
      @RequestParam("description") String description,
      Model model) {

    model.addAttribute("title", "Edit Category");

    if (errors.hasErrors()) {
      model.addAttribute("category", category);
      return "edit-category";
    }

    category = categoryService.getCategory(Integer.parseInt(HtmlUtils.htmlEscape(categoryId)));

    if (category == null) {
      return "redirect:manage-category.html";
    }
    category.setDescription(description);
    category.setName(name);

    categoryService.updateCategory(category);
    model.addAttribute(
        "info",
        "<div class=\"info\">Succesfully Updated <p> <a href='../manage-category.html'>Go back to Management Page</a></div>");

    return "edit-category";
  }
  @RequestMapping(value = "/create-category", method = RequestMethod.POST)
  public String createCategory(
      Model model,
      @ModelAttribute("category") @Valid Category category,
      Errors errors,
      @RequestParam("name") String name,
      @RequestParam("description") String description) {

    model.addAttribute("title", "Create Category");

    Category newCategory =
        new Category(HtmlUtils.htmlEscape(name), HtmlUtils.htmlEscape(description));

    if (errors.hasErrors()) {
      model.addAttribute("category", category);
      return "create-category";
    }

    /*
     * What to do if there is no error
     * We need a DAO and Service to add category into the database
     */
    try {
      categoryService.createCategory(newCategory);
      model.addAttribute(
          "success",
          "<div class=\"info\">Succesfully Created <p> <a href='manage-category.html'>Go back to Management Page</a></div>"); // Add success message
      category.setName(""); // Reset form field
      category.setDescription(""); // Reset form field
    } catch (EJBException e) {
      model.addAttribute("error", e.getMessage());
    }

    return "create-category";
  }
Exemple #4
0
 private CV createCV() {
   CV cv = new CV();
   Category category = new Category();
   category.setName("Technology");
   cv.setCategory(Collections.singletonList(category));
   User user = new User();
   user.setGender(Gender.FEMALE);
   user.setName("Ann");
   cv.setOwner(user);
   cv.setText("Python, Java, Ruby");
   cv.setTitle("Software developer");
   return cv;
 }
 public static Category toCategory(CategoryDTO categoryDTO) {
   Category c = new Category();
   c.setId(categoryDTO.id);
   c.setName(categoryDTO.name);
   return c;
 }