@Override
  protected Document doGetDocument(Object obj) throws Exception {
    User user = (User) obj;

    Document document = getBaseModelDocument(CLASS_NAME, user);

    long[] organizationIds = user.getOrganizationIds();

    document.addKeyword(Field.COMPANY_ID, user.getCompanyId());
    document.addKeyword(Field.GROUP_ID, user.getGroupIds());
    document.addDate(Field.MODIFIED_DATE, user.getModifiedDate());
    document.addKeyword(Field.SCOPE_GROUP_ID, user.getGroupIds());
    document.addKeyword(Field.STATUS, user.getStatus());
    document.addKeyword(Field.USER_ID, user.getUserId());
    document.addKeyword(Field.USER_NAME, user.getFullName());

    document.addKeyword(
        "ancestorOrganizationIds", getAncestorOrganizationIds(user.getOrganizationIds()));
    document.addText("emailAddress", user.getEmailAddress());
    document.addText("firstName", user.getFirstName());
    document.addText("fullName", user.getFullName());
    document.addKeyword("groupIds", user.getGroupIds());
    document.addText("jobTitle", user.getJobTitle());
    document.addText("lastName", user.getLastName());
    document.addText("middleName", user.getMiddleName());
    document.addKeyword("organizationIds", organizationIds);
    document.addKeyword("organizationCount", String.valueOf(organizationIds.length));
    document.addKeyword("roleIds", user.getRoleIds());
    document.addText("screenName", user.getScreenName());
    document.addKeyword("teamIds", user.getTeamIds());
    document.addKeyword("userGroupIds", user.getUserGroupIds());

    populateAddresses(document, user.getAddresses(), 0, 0);

    return document;
  }
  public void testUpdateExisting() throws Exception {
    long pk = nextLong();

    User newUser = _persistence.create(pk);

    newUser.setUuid(randomString());
    newUser.setCompanyId(nextLong());
    newUser.setCreateDate(nextDate());
    newUser.setModifiedDate(nextDate());
    newUser.setDefaultUser(randomBoolean());
    newUser.setContactId(nextLong());
    newUser.setPassword(randomString());
    newUser.setPasswordEncrypted(randomBoolean());
    newUser.setPasswordReset(randomBoolean());
    newUser.setPasswordModifiedDate(nextDate());
    newUser.setDigest(randomString());
    newUser.setReminderQueryQuestion(randomString());
    newUser.setReminderQueryAnswer(randomString());
    newUser.setGraceLoginCount(nextInt());
    newUser.setScreenName(randomString());
    newUser.setEmailAddress(randomString());
    newUser.setFacebookId(nextLong());
    newUser.setOpenId(randomString());
    newUser.setPortraitId(nextLong());
    newUser.setLanguageId(randomString());
    newUser.setTimeZoneId(randomString());
    newUser.setGreeting(randomString());
    newUser.setComments(randomString());
    newUser.setFirstName(randomString());
    newUser.setMiddleName(randomString());
    newUser.setLastName(randomString());
    newUser.setJobTitle(randomString());
    newUser.setLoginDate(nextDate());
    newUser.setLoginIP(randomString());
    newUser.setLastLoginDate(nextDate());
    newUser.setLastLoginIP(randomString());
    newUser.setLastFailedLoginDate(nextDate());
    newUser.setFailedLoginAttempts(nextInt());
    newUser.setLockout(randomBoolean());
    newUser.setLockoutDate(nextDate());
    newUser.setAgreedToTermsOfUse(randomBoolean());
    newUser.setStatus(nextInt());

    _persistence.update(newUser, false);

    User existingUser = _persistence.findByPrimaryKey(newUser.getPrimaryKey());

    assertEquals(existingUser.getUuid(), newUser.getUuid());
    assertEquals(existingUser.getUserId(), newUser.getUserId());
    assertEquals(existingUser.getCompanyId(), newUser.getCompanyId());
    assertEquals(
        Time.getShortTimestamp(existingUser.getCreateDate()),
        Time.getShortTimestamp(newUser.getCreateDate()));
    assertEquals(
        Time.getShortTimestamp(existingUser.getModifiedDate()),
        Time.getShortTimestamp(newUser.getModifiedDate()));
    assertEquals(existingUser.getDefaultUser(), newUser.getDefaultUser());
    assertEquals(existingUser.getContactId(), newUser.getContactId());
    assertEquals(existingUser.getPassword(), newUser.getPassword());
    assertEquals(existingUser.getPasswordEncrypted(), newUser.getPasswordEncrypted());
    assertEquals(existingUser.getPasswordReset(), newUser.getPasswordReset());
    assertEquals(
        Time.getShortTimestamp(existingUser.getPasswordModifiedDate()),
        Time.getShortTimestamp(newUser.getPasswordModifiedDate()));
    assertEquals(existingUser.getDigest(), newUser.getDigest());
    assertEquals(existingUser.getReminderQueryQuestion(), newUser.getReminderQueryQuestion());
    assertEquals(existingUser.getReminderQueryAnswer(), newUser.getReminderQueryAnswer());
    assertEquals(existingUser.getGraceLoginCount(), newUser.getGraceLoginCount());
    assertEquals(existingUser.getScreenName(), newUser.getScreenName());
    assertEquals(existingUser.getEmailAddress(), newUser.getEmailAddress());
    assertEquals(existingUser.getFacebookId(), newUser.getFacebookId());
    assertEquals(existingUser.getOpenId(), newUser.getOpenId());
    assertEquals(existingUser.getPortraitId(), newUser.getPortraitId());
    assertEquals(existingUser.getLanguageId(), newUser.getLanguageId());
    assertEquals(existingUser.getTimeZoneId(), newUser.getTimeZoneId());
    assertEquals(existingUser.getGreeting(), newUser.getGreeting());
    assertEquals(existingUser.getComments(), newUser.getComments());
    assertEquals(existingUser.getFirstName(), newUser.getFirstName());
    assertEquals(existingUser.getMiddleName(), newUser.getMiddleName());
    assertEquals(existingUser.getLastName(), newUser.getLastName());
    assertEquals(existingUser.getJobTitle(), newUser.getJobTitle());
    assertEquals(
        Time.getShortTimestamp(existingUser.getLoginDate()),
        Time.getShortTimestamp(newUser.getLoginDate()));
    assertEquals(existingUser.getLoginIP(), newUser.getLoginIP());
    assertEquals(
        Time.getShortTimestamp(existingUser.getLastLoginDate()),
        Time.getShortTimestamp(newUser.getLastLoginDate()));
    assertEquals(existingUser.getLastLoginIP(), newUser.getLastLoginIP());
    assertEquals(
        Time.getShortTimestamp(existingUser.getLastFailedLoginDate()),
        Time.getShortTimestamp(newUser.getLastFailedLoginDate()));
    assertEquals(existingUser.getFailedLoginAttempts(), newUser.getFailedLoginAttempts());
    assertEquals(existingUser.getLockout(), newUser.getLockout());
    assertEquals(
        Time.getShortTimestamp(existingUser.getLockoutDate()),
        Time.getShortTimestamp(newUser.getLockoutDate()));
    assertEquals(existingUser.getAgreedToTermsOfUse(), newUser.getAgreedToTermsOfUse());
    assertEquals(existingUser.getStatus(), newUser.getStatus());
  }
  protected User updateUser(
      long companyId, LDAPUser ldapUser, User user, String password, String modifiedDate)
      throws Exception {

    Date ldapUserModifiedDate = null;

    try {
      if (Validator.isNull(modifiedDate)) {
        if (_log.isInfoEnabled()) {
          _log.info("LDAP entry never modified, skipping user " + user.getEmailAddress());
        }

        return user;
      } else {
        ldapUserModifiedDate = LDAPUtil.parseDate(modifiedDate);
      }

      if (ldapUserModifiedDate.equals(user.getModifiedDate()) && ldapUser.isAutoPassword()) {

        if (_log.isDebugEnabled()) {
          _log.debug("User is already synchronized, skipping user " + user.getEmailAddress());
        }

        return user;
      }
    } catch (ParseException pe) {
      if (_log.isDebugEnabled()) {
        _log.debug("Unable to parse LDAP modify timestamp " + modifiedDate, pe);
      }
    }

    boolean passwordReset = ldapUser.isPasswordReset();

    if (PrefsPropsUtil.getBoolean(
        companyId, PropsKeys.LDAP_EXPORT_ENABLED, PropsValues.LDAP_EXPORT_ENABLED)) {

      passwordReset = user.isPasswordReset();
    }

    if (!PropsValues.LDAP_IMPORT_USER_PASSWORD_ENABLED) {
      password = PropsValues.LDAP_IMPORT_USER_PASSWORD_DEFAULT;

      if (password.equalsIgnoreCase(_USER_PASSWORD_SCREEN_NAME)) {
        password = ldapUser.getScreenName();
      }
    }

    if (Validator.isNull(ldapUser.getScreenName())) {
      ldapUser.setAutoScreenName(true);
    }

    if (ldapUser.isAutoScreenName()) {
      ScreenNameGenerator screenNameGenerator = ScreenNameGeneratorFactory.getInstance();

      ldapUser.setScreenName(
          screenNameGenerator.generate(companyId, user.getUserId(), ldapUser.getEmailAddress()));
    }

    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();

    birthdayCal.setTime(user.getContact().getBirthday());

    int birthdayMonth = birthdayCal.get(Calendar.MONTH);
    int birthdayDay = birthdayCal.get(Calendar.DAY_OF_MONTH);
    int birthdayYear = birthdayCal.get(Calendar.YEAR);

    if (ldapUser.isUpdatePassword()) {
      UserLocalServiceUtil.updatePassword(
          user.getUserId(), password, password, passwordReset, true);
    }

    Contact contact = user.getContact();

    Set<String> ldapIgnoreAttributes = SetUtil.fromArray(PropsValues.LDAP_USER_IGNORE_ATTRIBUTES);

    for (String attribute : ldapIgnoreAttributes) {
      Object value = BeanPropertiesUtil.getObjectSilent(user, attribute);

      if (value == null) {
        value = BeanPropertiesUtil.getObjectSilent(contact, attribute);
      }

      if (value != null) {
        BeanPropertiesUtil.setProperty(ldapUser, attribute, value);
      }
    }

    updateLDAPUser(ldapUser.getUser(), ldapUser.getContact(), user);

    user =
        UserLocalServiceUtil.updateUser(
            user.getUserId(),
            password,
            StringPool.BLANK,
            StringPool.BLANK,
            passwordReset,
            ldapUser.getReminderQueryQuestion(),
            ldapUser.getReminderQueryAnswer(),
            ldapUser.getScreenName(),
            ldapUser.getEmailAddress(),
            ldapUser.getFacebookId(),
            ldapUser.getOpenId(),
            ldapUser.getLanguageId(),
            ldapUser.getTimeZoneId(),
            ldapUser.getGreeting(),
            ldapUser.getComments(),
            ldapUser.getFirstName(),
            ldapUser.getMiddleName(),
            ldapUser.getLastName(),
            ldapUser.getPrefixId(),
            ldapUser.getSuffixId(),
            ldapUser.isMale(),
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            ldapUser.getSmsSn(),
            ldapUser.getAimSn(),
            ldapUser.getFacebookSn(),
            ldapUser.getIcqSn(),
            ldapUser.getJabberSn(),
            ldapUser.getMsnSn(),
            ldapUser.getMySpaceSn(),
            ldapUser.getSkypeSn(),
            ldapUser.getTwitterSn(),
            ldapUser.getYmSn(),
            ldapUser.getJobTitle(),
            ldapUser.getGroupIds(),
            ldapUser.getOrganizationIds(),
            ldapUser.getRoleIds(),
            ldapUser.getUserGroupRoles(),
            ldapUser.getUserGroupIds(),
            ldapUser.getServiceContext());

    if (ldapUserModifiedDate != null) {
      user = UserLocalServiceUtil.updateModifiedDate(user.getUserId(), ldapUserModifiedDate);
    }

    if (ldapUser.isUpdatePortrait()) {
      byte[] portraitBytes = ldapUser.getPortraitBytes();

      if ((portraitBytes != null) && (portraitBytes.length > 0)) {
        UserLocalServiceUtil.updatePortrait(user.getUserId(), portraitBytes);
      } else {
        UserLocalServiceUtil.deletePortrait(user.getUserId());
      }
    }

    return user;
  }