/** * 更新新菜品分类 * * @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; }
/** * 更新功能 * * @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 菜品分类命名重复异常 */ @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; }
/** 检查一个菜品的设定是否合法 */ 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; }
/** * 添加新会员等级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; }