/**
   * @see PermissionManager#isNameInUse(java.lang.String)
   * @deprecated save method has already checked it.
   */
  @Deprecated
  public boolean isNameInUse(String name) {
    Permission perm = new Permission();
    perm.setName(name);
    logger.debug("check for permission name '{}'", name);

    return getDao().exists(perm, "name"); // 如果给定name不唯一,则表示正在使用
  }
  /** @see PermissionManager#savePerm(Permission) */
  @Override
  @Transactional
  public void save(Permission perm) {
    Assert.notNull(perm);

    if (!getDao().exists(perm, "name")) {
      if (perm.getId() != null) { // 更新
        Permission old = get(perm.getId());
        BeanUtils.copyProperties(perm, old, new String[] {"resources", "roles"});
        if (perm.getStatus() != null) {
          old.setStatus(perm.getStatus());
        }
        getDao().update(old);
      } else { // 新建
        perm.setStatus(Constants.STATUS_AVAILABLE);
        getDao().save(perm);
      }
      if (acegiCacheManager != null) {
        acegiCacheManager.onPermissionChanged(perm);
      }
    } else {
      throw new ApplicationException("Reduplicate permission name [" + perm.getName() + "]");
    }
  }
  /** @see BaseManager#remove(java.lang.Object) */
  @Override
  @Transactional
  public void remove(Permission perm) {
    Assert.notNull(perm);
    if (perm.getId() == null) {
      return;
    }

    // 如果是系统许可则不删除
    if (StringUtils.equals(Constants.STATUS_AVAILABLE, perm.getIsSys())) {
      logger.warn("Can't delete system permission.");
      return;
    }
    // 删除与资源的关联关系
    Set<Resource> resSet = perm.getResources();
    Resource[] resources = resSet.toArray(new Resource[] {});
    logger.debug("Remove the relations between Persmission and Resource");
    for (int i = 0; i < resources.length; i++) {
      resources[i].getPermissions().remove(perm);
      perm.getResources().remove(resources[i]);
      // saveObject(resources[i]);
    }
    // 删除与角色的关联关系
    Set<Role> roleSet = perm.getRoles();
    Role[] roles = roleSet.toArray(new Role[] {});
    logger.debug("Remove the relations between Persmission and Roles");
    for (int i = 0; i < roles.length; i++) {
      roles[i].getPermissions().remove(perm);
      perm.getRoles().remove(roles[i]);
      // saveObject(roles[i]);
    }
    // 同步缓存
    if (acegiCacheManager != null) {
      acegiCacheManager.beforePermissionRemoved(perm);
    }

    super.remove(perm);
  }