/**
   * 获取所有分类
   *
   * @return 所有分类列表
   */
  public List<Category> getAllCategories() {
    List<Category> categories = categoryDao.loadAll();

    for (Category category : categories) {
      if (category.getName().equals(Constants.DEFAULT_CATEGORY)) {
        categories.remove(category);
        break;
      }
    }

    return categories;
  }
  /**
   * 根据菜品ID查找菜品列表
   *
   * @param categoryId 菜品ID
   * @return 菜品实体列表
   */
  public List<Dish> getDishesByCategoryId(Long categoryId) {
    List<Dish> result = new ArrayList<Dish>();

    Category category = categoryDao.get(categoryId);

    if (null != category) {
      for (Dish dish : category.getDishes()) {
        // REST服务只获取在售的菜品
        if (dish.isOnSell()) {
          result.add(dish);
        }
      }
    }

    return result;
  }
  /**
   * 更新新菜品分类
   *
   * @param categoryVO 菜品分类值对象,包含需要更新的数据
   * @return 更新后的菜品分类值对象
   * @throws DuplicateNameException 菜品分类命名重复异常
   * @throws CategoryNotFoundException 菜品分类不存在异常
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      readOnly = false,
      rollbackFor = Exception.class)
  public Category updateCategory(CategoryVO categoryVO)
      throws DuplicateNameException, CategoryNotFoundException {
    // 1. 检查需要更新的菜品分类是否存在
    Category category = this.getCategoryById(categoryVO.getId());

    Category checkedCategory = categoryDao.getCategoryByName(categoryVO.getName());
    // 2. 检查是否有同名菜品分类
    if (checkedCategory != null && checkedCategory.getId() != categoryVO.getId()) {
      throw new DuplicateNameException(
          MessageUtil.getMessage("category_name_msg", categoryVO.getName()));
    } else {
      // 3. 更新菜品分类
      category.setName(categoryVO.getName());
      category.setPicPath(categoryVO.getPicPath());
      category.setUpdateDate(new Date());

      categoryDao.update(category);
    }

    return category;
  }
  /**
   * 添加新菜品分类
   *
   * @param categoryVO 菜品分类值对象
   * @return 新创建的菜品分类对象
   * @throws DuplicateNameException 菜品分类命名重复异常
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      readOnly = false,
      rollbackFor = Exception.class)
  public Category addCategory(CategoryVO categoryVO) throws DuplicateNameException {
    Category category = null;

    // 1. 检查是否有同名菜品分类
    if (null == categoryDao.getCategoryByName(categoryVO.getName())) {
      category = new Category();
      category.setName(categoryVO.getName());
      category.setPicPath(categoryVO.getPicPath());
      category.setCreateDate(new Date());
      Long categoryId = categoryDao.save(category);
      category = categoryDao.get(categoryId);
    } else {
      throw new DuplicateNameException(
          MessageUtil.getMessage("category_name_msg", categoryVO.getName()));
    }

    return category;
  }
示例#5
0
  /**
   * 加载单个功能信息
   *
   * @return
   */
  public String edit() {
    // 刷新系统菜单
    refreshPageData();
    category = new CategoryVO();
    try {
      if (null != id && !"".equals(id.trim())) {
        Category categoryDB = categoryService.getCategoryById(Long.parseLong(id));
        category.setId(categoryDB.getId());
        category.setName(categoryDB.getName());
        category.setPicPath(categoryDB.getPicPath());
        category.setName(categoryDB.getName());
      } else {
        this.setMessage("ERROR");
        return ERROR;
      }
    } catch (CategoryNotFoundException e) {
      this.setMessage(e.getMessage());
      return ERROR;
    }

    return SUCCESS;
  }
  /**
   * 删除菜品分类
   *
   * @param categoryId 菜品分类id
   * @throws CategoryNotFoundException 菜品分类不存在异常
   */
  @Transactional(
      propagation = Propagation.REQUIRED,
      readOnly = false,
      rollbackFor = Exception.class)
  public void deleteCategory(Long categoryId) throws CategoryNotFoundException {
    // 1. 检查需要删除的菜品分类是否存在
    Category category = this.getCategoryById(categoryId);

    // 2. 将要删除菜品分类下的菜品重置为默认分类
    Category defaultCategory = categoryDao.getCategoryByName(Constants.DEFAULT_CATEGORY);

    if (null == defaultCategory) {
      throw new CategoryNotFoundException(
          MessageUtil.getMessage("category_name_msg", Constants.DEFAULT_CATEGORY));
    } else {
      for (Dish dish : category.getDishes()) {
        dish.setCategory(defaultCategory);
        dishDao.update(dish);
      }
    }

    // 3. 删除菜品分类
    categoryDao.delete(category);
  }