/**
   * 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;
  }
  /**
   * Adds a role with additional parameters. The user is reindexed after role is added.
   *
   * @param userId the primary key of the user
   * @param companyId the primary key of the company
   * @param name the role's name
   * @param titleMap the role's localized titles (optionally <code>null</code>)
   * @param descriptionMap the role's localized descriptions (optionally <code>null</code>)
   * @param type the role's type (optionally <code>0</code>)
   * @param className the name of the class for which the role is created (optionally <code>null
   *     </code>)
   * @param classPK the primary key of the class for which the role is created (optionally <code>0
   *     </code>)
   * @return the role
   * @throws PortalException if the class name or the role name were invalid, if the role is a
   *     duplicate, or if a user with the primary key could not be found
   * @throws SystemException if a system exception occurred
   */
  public Role addRole(
      long userId,
      long companyId,
      String name,
      Map<Locale, String> titleMap,
      Map<Locale, String> descriptionMap,
      int type,
      String className,
      long classPK)
      throws PortalException, SystemException {

    // Role

    className = GetterUtil.getString(className);
    long classNameId = PortalUtil.getClassNameId(className);

    long roleId = counterLocalService.increment();

    if ((classNameId <= 0) || className.equals(Role.class.getName())) {
      classNameId = PortalUtil.getClassNameId(Role.class);
      classPK = roleId;
    }

    validate(0, companyId, classNameId, name);

    Role role = rolePersistence.create(roleId);

    role.setCompanyId(companyId);
    role.setClassNameId(classNameId);
    role.setClassPK(classPK);
    role.setName(name);
    role.setTitleMap(titleMap);
    role.setDescriptionMap(descriptionMap);
    role.setType(type);

    rolePersistence.update(role, false);

    // Resources

    if (userId > 0) {
      resourceLocalService.addResources(
          companyId, 0, userId, Role.class.getName(), role.getRoleId(), false, false, false);

      if (!ImportExportThreadLocal.isImportInProcess()) {
        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);

        indexer.reindex(userId);
      }
    }

    return role;
  }