protected void importGroups(
      long ldapServerId,
      long companyId,
      LdapContext ldapContext,
      Attributes attributes,
      User user,
      Properties userMappings,
      Properties groupMappings)
      throws Exception {

    List<Long> newUserGroupIds = new ArrayList<Long>();

    if (PrefsPropsUtil.getBoolean(companyId, PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER_ENABLED)) {

      String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);

      String baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN + postfix);

      Binding binding =
          PortalLDAPUtil.getUser(
              ldapServerId, companyId, user.getScreenName(), user.getEmailAddress());

      String fullUserDN = PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, binding);

      StringBundler sb = new StringBundler(9);

      sb.append(StringPool.OPEN_PARENTHESIS);
      sb.append(StringPool.AMPERSAND);
      sb.append(
          PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER + postfix));
      sb.append(StringPool.OPEN_PARENTHESIS);
      sb.append(groupMappings.getProperty("user"));
      sb.append(StringPool.EQUAL);
      sb.append(escapeValue(fullUserDN));
      sb.append(StringPool.CLOSE_PARENTHESIS);
      sb.append(StringPool.CLOSE_PARENTHESIS);

      byte[] cookie = new byte[0];

      while (cookie != null) {
        List<SearchResult> searchResults = new ArrayList<SearchResult>();

        String groupMappingsGroupName =
            GetterUtil.getString(groupMappings.getProperty("groupName")).toLowerCase();

        cookie =
            PortalLDAPUtil.searchLDAP(
                companyId,
                ldapContext,
                cookie,
                0,
                baseDN,
                sb.toString(),
                new String[] {groupMappingsGroupName},
                searchResults);

        for (SearchResult searchResult : searchResults) {
          String fullGroupDN =
              PortalLDAPUtil.getNameInNamespace(ldapServerId, companyId, searchResult);

          newUserGroupIds =
              importGroup(
                  ldapServerId,
                  companyId,
                  ldapContext,
                  fullGroupDN,
                  user,
                  groupMappings,
                  newUserGroupIds);
        }
      }
    } else {
      String userMappingsGroup = userMappings.getProperty("group");

      if (Validator.isNull(userMappingsGroup)) {
        return;
      }

      Attribute userGroupAttribute = attributes.get(userMappingsGroup);

      if (userGroupAttribute == null) {
        return;
      }

      for (int i = 0; i < userGroupAttribute.size(); i++) {
        String fullGroupDN = (String) userGroupAttribute.get(i);

        newUserGroupIds =
            importGroup(
                ldapServerId,
                companyId,
                ldapContext,
                fullGroupDN,
                user,
                groupMappings,
                newUserGroupIds);
      }
    }

    addUserGroupsNotAddedByLDAPImport(user.getUserId(), newUserGroupIds);

    for (long newUserGroupId : newUserGroupIds) {
      UserLocalServiceUtil.addUserGroupUsers(newUserGroupId, new long[] {user.getUserId()});
    }

    List<UserGroup> userUserGroups = UserGroupLocalServiceUtil.getUserUserGroups(user.getUserId());

    for (UserGroup userGroup : userUserGroups) {
      if (!newUserGroupIds.contains(userGroup.getUserGroupId())) {
        UserLocalServiceUtil.deleteUserGroupUser(userGroup.getUserGroupId(), user.getUserId());
      }
    }
  }
  protected void importUsers(
      long ldapServerId,
      long companyId,
      LdapContext ldapContext,
      Properties userMappings,
      Properties userExpandoMappings,
      Properties contactMappings,
      Properties contactExpandoMappings,
      long userGroupId,
      Attribute attribute)
      throws Exception {

    List<Long> newUserIds = new ArrayList<Long>(attribute.size());

    for (int i = 0; i < attribute.size(); i++) {
      String fullUserDN = (String) attribute.get(i);

      Attributes userAttributes = null;

      try {
        userAttributes =
            PortalLDAPUtil.getUserAttributes(ldapServerId, companyId, ldapContext, fullUserDN);
      } catch (NameNotFoundException nnfe) {
        _log.error("LDAP user not found with fullUserDN " + fullUserDN, nnfe);

        continue;
      }

      try {
        User user =
            importUser(
                companyId,
                userAttributes,
                userMappings,
                userExpandoMappings,
                contactMappings,
                contactExpandoMappings,
                StringPool.BLANK);

        if (user != null) {
          if (_log.isDebugEnabled()) {
            _log.debug("Adding " + user.getUserId() + " to group " + userGroupId);
          }

          UserLocalServiceUtil.addUserGroupUsers(userGroupId, new long[] {user.getUserId()});

          newUserIds.add(user.getUserId());
        }
      } catch (Exception e) {
        _log.error("Unable to load user " + userAttributes, e);
      }
    }

    List<User> userGroupUsers = UserLocalServiceUtil.getUserGroupUsers(userGroupId);

    for (User user : userGroupUsers) {
      if (!newUserIds.contains(user.getUserId())) {
        UserLocalServiceUtil.deleteUserGroupUser(userGroupId, user.getUserId());
      }
    }
  }