/**
   * Updates the user group.
   *
   * @param companyId the primary key of the user group's company
   * @param userGroupId the primary key of the user group
   * @param name the user group's name
   * @param description the user group's description
   * @param serviceContext the service context to be applied (optionally <code>null</code>). Can set
   *     expando bridge attributes for the user group.
   * @return the user group
   */
  @Override
  public UserGroup updateUserGroup(
      long companyId,
      long userGroupId,
      String name,
      String description,
      ServiceContext serviceContext)
      throws PortalException {

    // User group

    validate(userGroupId, companyId, name);

    UserGroup userGroup = userGroupPersistence.findByPrimaryKey(userGroupId);

    userGroup.setName(name);
    userGroup.setDescription(description);
    userGroup.setExpandoBridgeAttributes(serviceContext);

    userGroupPersistence.update(userGroup);

    // Indexer

    Indexer<UserGroup> indexer = IndexerRegistryUtil.nullSafeGetIndexer(UserGroup.class);

    indexer.reindex(userGroup);

    return userGroup;
  }
  /**
   * Adds a user group.
   *
   * <p>This method handles the creation and bookkeeping of the user group, including its resources,
   * metadata, and internal data structures. It is not necessary to make subsequent calls to setup
   * default groups and resources for the user group.
   *
   * @param userId the primary key of the user
   * @param companyId the primary key of the user group's company
   * @param name the user group's name
   * @param description the user group's description
   * @param serviceContext the service context to be applied (optionally <code>null</code>). Can set
   *     expando bridge attributes for the user group.
   * @return the user group
   */
  @Override
  public UserGroup addUserGroup(
      long userId, long companyId, String name, String description, ServiceContext serviceContext)
      throws PortalException {

    // User group

    validate(0, companyId, name);

    User user = userPersistence.findByPrimaryKey(userId);

    long userGroupId = counterLocalService.increment();

    UserGroup userGroup = userGroupPersistence.create(userGroupId);

    if (serviceContext != null) {
      userGroup.setUuid(serviceContext.getUuid());
    }

    userGroup.setCompanyId(companyId);
    userGroup.setUserId(user.getUserId());
    userGroup.setUserName(user.getFullName());
    userGroup.setParentUserGroupId(UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
    userGroup.setName(name);
    userGroup.setDescription(description);
    userGroup.setAddedByLDAPImport(UserGroupImportTransactionThreadLocal.isOriginatesFromImport());
    userGroup.setExpandoBridgeAttributes(serviceContext);

    userGroupPersistence.update(userGroup);

    // Group

    groupLocalService.addGroup(
        userId,
        GroupConstants.DEFAULT_PARENT_GROUP_ID,
        UserGroup.class.getName(),
        userGroup.getUserGroupId(),
        GroupConstants.DEFAULT_LIVE_GROUP_ID,
        getLocalizationMap(String.valueOf(userGroupId)),
        null,
        0,
        true,
        GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
        null,
        false,
        true,
        null);

    // Resources

    resourceLocalService.addResources(
        companyId,
        0,
        userId,
        UserGroup.class.getName(),
        userGroup.getUserGroupId(),
        false,
        false,
        false);

    // Indexer

    Indexer<UserGroup> indexer = IndexerRegistryUtil.nullSafeGetIndexer(UserGroup.class);

    indexer.reindex(userGroup);

    return userGroup;
  }