protected void setRolePermissions(Role role, String name, String[] actionIds)
      throws PortalException, SystemException {

    if (resourceBlockLocalService.isSupported(name)) {
      resourceBlockLocalService.setCompanyScopePermissions(
          role.getCompanyId(), name, role.getRoleId(), Arrays.asList(actionIds));
    } else {
      resourcePermissionLocalService.setResourcePermissions(
          role.getCompanyId(),
          name,
          ResourceConstants.SCOPE_COMPANY,
          String.valueOf(role.getCompanyId()),
          role.getRoleId(),
          actionIds);
    }
  }
  /**
   * 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;
  }
  protected void updateAction_1to5(
      Role role,
      long groupId,
      String selResource,
      String actionId,
      boolean selected,
      int scope,
      String[] groupIds)
      throws Exception {

    long roleId = role.getRoleId();

    if (selected) {
      if (scope == ResourceConstants.SCOPE_COMPANY) {
        PermissionServiceUtil.setRolePermission(
            roleId, groupId, selResource, scope, String.valueOf(role.getCompanyId()), actionId);
      } else if (scope == ResourceConstants.SCOPE_GROUP_TEMPLATE) {
        PermissionServiceUtil.setRolePermission(
            roleId,
            groupId,
            selResource,
            ResourceConstants.SCOPE_GROUP_TEMPLATE,
            String.valueOf(GroupConstants.DEFAULT_PARENT_GROUP_ID),
            actionId);
      } else {
        PermissionServiceUtil.unsetRolePermissions(
            roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP, actionId);

        for (String curGroupId : groupIds) {
          PermissionServiceUtil.setRolePermission(
              roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP, curGroupId, actionId);
        }
      }
    } else {

      // Remove company, group template, and group permissions

      PermissionServiceUtil.unsetRolePermissions(
          roleId, groupId, selResource, ResourceConstants.SCOPE_COMPANY, actionId);

      PermissionServiceUtil.unsetRolePermissions(
          roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP_TEMPLATE, actionId);

      PermissionServiceUtil.unsetRolePermissions(
          roleId, groupId, selResource, ResourceConstants.SCOPE_GROUP, actionId);
    }
  }
  protected void updateActions_Blocks(
      Role role,
      long scopeGroupId,
      String selResource,
      String actionId,
      boolean selected,
      int scope,
      String[] groupIds)
      throws Exception {

    long companyId = role.getCompanyId();
    long roleId = role.getRoleId();

    if (selected) {
      if (scope == ResourceConstants.SCOPE_GROUP) {
        ResourceBlockServiceUtil.removeAllGroupScopePermissions(
            scopeGroupId, companyId, selResource, roleId, actionId);
        ResourceBlockServiceUtil.removeCompanyScopePermission(
            scopeGroupId, companyId, selResource, roleId, actionId);

        for (String groupId : groupIds) {
          ResourceBlockServiceUtil.addGroupScopePermission(
              scopeGroupId, companyId, GetterUtil.getLong(groupId), selResource, roleId, actionId);
        }
      } else {
        ResourceBlockServiceUtil.removeAllGroupScopePermissions(
            scopeGroupId, companyId, selResource, roleId, actionId);
        ResourceBlockServiceUtil.addCompanyScopePermission(
            scopeGroupId, companyId, selResource, roleId, actionId);
      }
    } else {
      ResourceBlockServiceUtil.removeAllGroupScopePermissions(
          scopeGroupId, companyId, selResource, roleId, actionId);
      ResourceBlockServiceUtil.removeCompanyScopePermission(
          scopeGroupId, companyId, selResource, roleId, actionId);
    }
  }
  /**
   * 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;
  }