예제 #1
0
  @RequestMapping(value = "/edit", method = RequestMethod.GET)
  public String prepareEdit(
      @RequestParam(value = "key", required = false) Integer key, Model model) {
    if (key == null) {
      ProductForm productForm = new ProductForm();
      model.addAttribute("productForm", productForm);
    } else {
      Product product = this.productDAO.findById(key);
      ProductForm productForm = new ProductForm(product);
      model.addAttribute("productForm", productForm);
    }

    // TODO Move this code in ControllerHelper
    //  store all units for combo
    List<Unit> units = unitDAO.findAll();
    Map<Integer, String> allUnits = new LinkedHashMap<Integer, String>();
    for (Unit unit : units) {
      allUnits.put(unit.getId(), unit.getName());
    }
    model.addAttribute("allUnits", allUnits);

    List<ProductCategory> categories = this.categoryDAO.findAll();
    Map<Integer, String> categoryForms = new LinkedHashMap<Integer, String>();
    for (ProductCategory category : categories) {
      categoryForms.put(category.getId(), category.getName());
    }
    model.addAttribute("allCategories", categoryForms);

    return "products/edit";
  }
예제 #2
0
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  public String processUpdate(
      @Validated ProductForm formProduct, BindingResult result, Model model) {
    if (result.hasErrors()) {
      return "products/edit";
    }

    Product product = (Product) formProduct.getProduct();
    if (product.getId() != null) {
      // product update
      product = productDAO.findById(formProduct.getId());

      product.setName(formProduct.getName());
      product.setShared(formProduct.getShared());
      product.setAmount(formProduct.getAmount());
      product.setMaxPoints(formProduct.getMaxPoints());
      product.setPoints(formProduct.getPoints());
      product.setDescription(formProduct.getDescription());
    }

    //  Unit returned by formProduct contains only the ID in the name property of Unit instance.
    Unit unit = unitDAO.findById(formProduct.getUnitId());
    product.setUnit(unit);

    //  Category returned by formProduct contains only the ID in the name property of Category
    // instance.
    ProductCategory category = this.categoryDAO.findById(formProduct.getCategoryId());
    product.setCategory(category);

    this.productDAO.update(product);
    return "redirect:/products/list";
  }