示例#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 = "/list", method = RequestMethod.GET)
  public String prepareList(
      @RequestParam(value = "queryName", required = false) String queryName,
      @RequestParam(value = "category", required = false) Integer categoryId,
      @RequestParam(value = "page", required = false) Integer pageIndex,
      Model model) {

    ProductCategory category = null;

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

    if (!StringUtils.isEmpty(queryName) && queryName.endsWith(",")) {
      //  browser returns the param followed by a comma ??
      queryName = queryName.substring(0, queryName.indexOf(','));

      if (queryName.startsWith("%")) {
        model.addAttribute("queryName", queryName);
      }
    }
    model.addAttribute("category", categoryId);

    Page<Product> products = this.productDAO.findByNameAndCategory(queryName, category, pageIndex);
    List<ProductForm> forms = new ArrayList<ProductForm>(products.size());
    for (Product product : products) {
      forms.add(new ProductForm(product));
    }
    model.addAttribute("products", forms);

    model.addAttribute("currentPage", products.getCurrentPage());
    model.addAttribute("pageCount", products.getPageCount());

    if (!model.containsAttribute("finderMode")) {
      model.addAttribute("finderMode", false);
    }

    return "products/list";
  }
  @Override
  @Transactional
  public void update(ProductCategory category) {
    if (category.getId() == null) {
      insert(category);
    }
    ;

    Session session = sessionFactory.getCurrentSession();
    session.update(category);
  }