Example #1
0
 /**
  * 更新功能
  *
  * @return
  */
 public String update() {
   try {
     // 1. 检查传入的会员等级属性是否合法
     if (!checkCategoryVO()) {
       return ERROR;
     } else {
       // 2. 更新新的会员等级
       categoryService.updateCategory(category);
       setMessage(MessageUtil.getMessage("category_update_success", category.getName()));
       category = new CategoryVO();
     }
   } catch (DuplicateNameException e) {
     this.setMessage(e.getMessage());
     return ERROR;
   } catch (CategoryNotFoundException e) {
     this.setMessage(e.getMessage());
     return ERROR;
   } catch (NumberFormatException e) {
     this.setMessage(e.getMessage());
     return ERROR;
   } finally {
     getCategoryList();
     refreshPageData();
   }
   return SUCCESS;
 }
  /**
   * 更新新菜品分类
   *
   * @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;
  }
  /**
   * 根据指定的菜品分类ID获取菜品分类信息
   *
   * @param categoryId 菜品分类ID
   * @return 返回菜品分类对象
   * @throws CategoryNotFoundException 菜品分类对象不存在异常
   */
  public Category getCategoryById(Long categoryId) throws CategoryNotFoundException {
    Category category = categoryDao.get(categoryId);

    if (null == category) {
      throw new CategoryNotFoundException(MessageUtil.getMessage("category_id", "" + categoryId));
    }
    return category;
  }
  /**
   * 根据指定的菜品分类名字查找菜品分类
   *
   * @param name 菜品分类名字
   * @return 菜品分类对象
   * @throws CategoryNotFoundException 菜品分类对象不存在异常
   */
  public Category getCategoryByName(String name) throws CategoryNotFoundException {
    Category category = categoryDao.getCategoryByName(name);

    if (null == category) {
      throw new CategoryNotFoundException(MessageUtil.getMessage("category_name_msg", name));
    }

    return category;
  }
Example #5
0
  /** 检查一个菜品的设定是否合法 */
  private boolean checkCategoryVO() {
    boolean isValidVO = true;

    if (null == category) {
      isValidVO = false;
      addFieldError("category.categoryvo", MessageUtil.getMessage("category_invalid_vo"));
      return isValidVO;
    }

    if (StringUtil.isEmpty(category.getName())) {
      isValidVO = false;
      addFieldError("category.name", MessageUtil.getMessage("category_name_rule"));
    } else {
      category.setName(category.getName());
    }

    if (StringUtil.isEmpty(category.getPicPath())) {
      isValidVO = false;
      addFieldError("category.pic.path", MessageUtil.getMessage("pic_path_rule"));
    }

    return isValidVO;
  }
Example #6
0
  /**
   * 删除功能
   *
   * @return
   */
  public String remove() {
    // 更新页面数据
    refreshPageData();

    if (null != id) {
      try {
        categoryService.deleteCategory(Long.parseLong(id));
      } catch (CategoryNotFoundException e) {
        this.setMessage(e.getMessage());
        return ERROR;
      }
    }

    this.setMessage(MessageUtil.getMessage("delete_success"));

    // 更新记录列表
    getCategoryList();

    return SUCCESS;
  }
  /**
   * 添加新菜品分类
   *
   * @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;
  }
Example #8
0
  /**
   * 添加新会员等级Action
   *
   * @return
   */
  public String save() {
    logger.debug(category);
    try {
      // 1. 检查传入的会员等级属性是否合法
      if (!checkCategoryVO()) {
        return ERROR;
      } else {
        // 2. 创建新的会员等级
        categoryService.addCategory(category);
        setMessage(MessageUtil.getMessage("category_save_success", category.getName()));
        category = new CategoryVO();
      }
    } catch (DuplicateNameException e) {
      logger.error(e.getMessage());
      this.setMessage(e.getMessage());
      return ERROR;
    } finally {
      getCategoryList();
      refreshPageData();
    }

    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);
  }
Example #10
0
  /**
   * 上传图片
   *
   * @return
   * @throws Exception
   */
  public String uploadImage() {
    // 得到当前时间自1970年1月1日0时0分0秒开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名。
    long now = new Date().getTime();

    // 得到保存上传文件的目录的真实路径
    String path = ServletActionContext.getServletContext().getRealPath(uploadDir);

    File dir = new File(path);
    // 如果这个目录不存在,则创建它。
    if (!dir.exists()) {
      dir.mkdir();
    }

    if (null != fileFileName && !"".equals(fileFileName.trim())) {
      int index = fileFileName.lastIndexOf('.');
      // 判断上传文件名是否有扩展名
      if (index != -1) {
        newFileName = now + fileFileName.substring(index);
      } else {
        newFileName = Long.toString(now);
      }
      this.setNewFileName(newFileName);

      BufferedOutputStream bos = null;
      BufferedInputStream bis = null;
      // 读取保存在临时目录下的上传文件,写入到新的文件中。
      try {
        FileInputStream fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);

        FileOutputStream fos = new FileOutputStream(new File(dir, newFileName));
        bos = new BufferedOutputStream(fos);

        byte[] buf = new byte[4096];

        int len = -1;
        while ((len = bis.read(buf)) != -1) {
          bos.write(buf, 0, len);
        }
      } catch (FileNotFoundException e) {
        this.setMessage(MessageUtil.getMessage("file_not_found_fxception", fileFileName));
        return INPUT;
      } catch (IOException e) {
        this.setMessage(MessageUtil.getMessage("io_exception", newFileName));
        return INPUT;
      } finally {
        refreshPageData();
        try {
          if (null != bis) {
            bis.close();
          }
          if (null != bos) {
            bos.close();
          }
        } catch (IOException e) {
          this.setMessage(MessageUtil.getMessage("io_exception", newFileName));
          return INPUT;
        }
      }
    }

    if (null == category) {
      category = new CategoryVO();
    }

    if (null != category) {
      category.setName(categoryName);
      if (null != categoryId) {
        category.setId(categoryId);
        setId(categoryId.toString());
      }
      category.setPicPath("/dish/" + newFileName);
    }

    this.setMessage(MessageUtil.getMessage("upload_picture_success_msg"));
    return SUCCESS;
  }
 public String getMessage() {
   return MessageUtil.getMessage(exceptionKey, queryCriteria);
 }