/**
   * Updates the role with the primary key.
   *
   * @param roleId the primary key of the role
   * @param name the role's new name
   * @param titleMap the new localized titles (optionally <code>null</code>) to replace those
   *     existing for the role
   * @param descriptionMap the new localized descriptions (optionally <code>null</code>) to replace
   *     those existing for the role
   * @param subtype the role's new subtype (optionally <code>null</code>)
   * @return the role with the primary key
   * @throws PortalException if a role with the primary could not be found or if the role's name was
   *     invalid
   * @throws SystemException if a system exception occurred
   */
  public Role updateRole(
      long roleId,
      String name,
      Map<Locale, String> titleMap,
      Map<Locale, String> descriptionMap,
      String subtype)
      throws PortalException, SystemException {

    Role role = rolePersistence.findByPrimaryKey(roleId);

    validate(roleId, role.getCompanyId(), role.getClassNameId(), name);

    if (PortalUtil.isSystemRole(role.getName())) {
      name = role.getName();
      subtype = null;
    }

    role.setName(name);
    role.setTitleMap(titleMap);
    role.setDescriptionMap(descriptionMap);
    role.setSubtype(subtype);

    rolePersistence.update(role, false);

    return role;
  }
  @Override
  protected PortletPreferences doDeleteData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences)
      throws Exception {

    if (portletDataContext.addPrimaryKey(RolesAdminPortletDataHandler.class, "deleteData")) {

      return portletPreferences;
    }

    List<Role> roles = RoleLocalServiceUtil.getRoles(portletDataContext.getCompanyId());

    for (Role role : roles) {
      if (!PortalUtil.isSystemRole(role.getName())) {
        RoleLocalServiceUtil.deleteRole(role);
      }
    }

    return portletPreferences;
  }
  /**
   * Deletes the role and its associated permissions.
   *
   * @param role the role
   * @return the deleted role
   * @throws PortalException if the role is a default system role or if the role's resource could
   *     not be found
   * @throws SystemException if a system exception occurred
   */
  @Override
  public Role deleteRole(Role role) throws PortalException, SystemException {
    if (PortalUtil.isSystemRole(role.getName())) {
      throw new RequiredRoleException();
    }

    // Resources

    String className = role.getClassName();
    long classNameId = role.getClassNameId();

    if ((classNameId <= 0) || className.equals(Role.class.getName())) {
      resourceLocalService.deleteResource(
          role.getCompanyId(),
          Role.class.getName(),
          ResourceConstants.SCOPE_INDIVIDUAL,
          role.getRoleId());
    }

    if ((role.getType() == RoleConstants.TYPE_ORGANIZATION)
        || (role.getType() == RoleConstants.TYPE_SITE)) {

      userGroupRoleLocalService.deleteUserGroupRolesByRoleId(role.getRoleId());

      userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByRoleId(role.getRoleId());
    }

    // Role

    rolePersistence.remove(role);

    // Permission cache

    PermissionCacheUtil.clearCache();

    return role;
  }