protected UserGroup importUserGroup(
      long companyId, Attributes attributes, Properties groupMappings) throws Exception {

    AttributesTransformer attributesTransformer = AttributesTransformerFactory.getInstance();

    attributes = attributesTransformer.transformGroup(attributes);

    LDAPGroup ldapGroup =
        _ldapToPortalConverter.importLDAPGroup(companyId, attributes, groupMappings);

    UserGroup userGroup = null;

    try {
      userGroup = UserGroupLocalServiceUtil.getUserGroup(companyId, ldapGroup.getGroupName());

      UserGroupLocalServiceUtil.updateUserGroup(
          companyId,
          userGroup.getUserGroupId(),
          ldapGroup.getGroupName(),
          ldapGroup.getDescription());
    } catch (NoSuchUserGroupException nsuge) {
      if (_log.isDebugEnabled()) {
        _log.debug("Adding user group to portal " + ldapGroup.getGroupName());
      }

      long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);

      LDAPUserGroupTransactionThreadLocal.setOriginatesFromLDAP(true);

      try {
        userGroup =
            UserGroupLocalServiceUtil.addUserGroup(
                defaultUserId, companyId, ldapGroup.getGroupName(), ldapGroup.getDescription());
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to create user group " + ldapGroup.getGroupName());
        }

        if (_log.isDebugEnabled()) {
          _log.debug(e, e);
        }
      } finally {
        LDAPUserGroupTransactionThreadLocal.setOriginatesFromLDAP(false);
      }
    }

    addRole(companyId, ldapGroup, userGroup);

    return userGroup;
  }
  protected User importUser(
      long companyId,
      Attributes attributes,
      Properties userMappings,
      Properties userExpandoMappings,
      Properties contactMappings,
      Properties contactExpandoMappings,
      String password)
      throws Exception {

    LDAPUserTransactionThreadLocal.setOriginatesFromLDAP(true);

    try {
      AttributesTransformer attributesTransformer = AttributesTransformerFactory.getInstance();

      attributes = attributesTransformer.transformUser(attributes);

      LDAPUser ldapUser =
          _ldapToPortalConverter.importLDAPUser(
              companyId,
              attributes,
              userMappings,
              userExpandoMappings,
              contactMappings,
              contactExpandoMappings,
              password);

      User user = getUser(companyId, ldapUser);

      if ((user != null) && user.isDefaultUser()) {
        return user;
      }

      if (user == null) {
        user = addUser(companyId, ldapUser, password);
      }

      String modifiedDate = LDAPUtil.getAttributeString(attributes, "modifyTimestamp");

      user = updateUser(companyId, ldapUser, user, password, modifiedDate);

      updateExpandoAttributes(user, ldapUser);

      return user;
    } finally {
      LDAPUserTransactionThreadLocal.setOriginatesFromLDAP(false);
    }
  }