protected void updateLDAPUser(User ldapUser, Contact ldapContact, User user)
      throws PortalException, SystemException {

    Contact contact = user.getContact();

    ldapContact.setAimSn(GetterUtil.getString(contact.getAimSn()));
    ldapContact.setFacebookSn(GetterUtil.getString(contact.getFacebookSn()));
    ldapContact.setIcqSn(GetterUtil.getString(contact.getIcqSn()));
    ldapContact.setJabberSn(GetterUtil.getString(contact.getJabberSn()));
    ldapContact.setMale(GetterUtil.getBoolean(contact.getMale()));
    ldapContact.setMsnSn(GetterUtil.getString(contact.getMsnSn()));
    ldapContact.setMySpaceSn(GetterUtil.getString(contact.getMySpaceSn()));
    ldapContact.setPrefixId(GetterUtil.getInteger(contact.getPrefixId()));
    ldapContact.setSkypeSn(GetterUtil.getString(contact.getSkypeSn()));
    ldapContact.setSmsSn(GetterUtil.getString(contact.getSmsSn()));
    ldapContact.setSuffixId(GetterUtil.getInteger(contact.getSuffixId()));
    ldapContact.setTwitterSn(GetterUtil.getString(contact.getTwitterSn()));
    ldapContact.setYmSn(GetterUtil.getString(contact.getYmSn()));

    ldapUser.setComments(GetterUtil.getString(user.getComments()));
    ldapUser.setGreeting(GetterUtil.getString(user.getGreeting()));
    ldapUser.setJobTitle(GetterUtil.getString(user.getJobTitle()));
    ldapUser.setLanguageId(GetterUtil.getString(user.getLanguageId()));
    ldapUser.setMiddleName(GetterUtil.getString(user.getMiddleName()));
    ldapUser.setOpenId(GetterUtil.getString(user.getOpenId()));
    ldapUser.setTimeZoneId(GetterUtil.getString(user.getTimeZoneId()));
  }
  private static void _updateAdminUser(
      HttpServletRequest request, UnicodeProperties unicodeProperties) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    Company company = CompanyLocalServiceUtil.getCompanyById(themeDisplay.getCompanyId());

    String emailAddress =
        ParamUtil.getString(
            request,
            "adminEmailAddress",
            PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + StringPool.AT + company.getMx());

    PropsValues.ADMIN_EMAIL_FROM_ADDRESS = emailAddress;

    unicodeProperties.put(PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, emailAddress);

    ScreenNameGenerator screenNameGenerator = ScreenNameGeneratorFactory.getInstance();

    String screenName =
        GetterUtil.getString(PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX, "test");

    try {
      screenName = screenNameGenerator.generate(0, 0, emailAddress);
    } catch (Exception e) {
    }

    String firstName =
        ParamUtil.getString(request, "adminFirstName", PropsValues.DEFAULT_ADMIN_FIRST_NAME);
    String lastName =
        ParamUtil.getString(request, "adminLastName", PropsValues.DEFAULT_ADMIN_LAST_NAME);

    FullNameGenerator fullNameGenerator = FullNameGeneratorFactory.getInstance();

    String fullName = fullNameGenerator.getFullName(firstName, null, lastName);

    PropsValues.ADMIN_EMAIL_FROM_NAME = fullName;

    unicodeProperties.put(PropsKeys.ADMIN_EMAIL_FROM_NAME, fullName);

    User user = null;

    try {
      user = UserLocalServiceUtil.getUserByEmailAddress(themeDisplay.getCompanyId(), emailAddress);

      String greeting =
          LanguageUtil.format(
              themeDisplay.getLocale(), "welcome-x", StringPool.SPACE + fullName, false);

      Contact contact = user.getContact();

      Calendar birthdayCal = CalendarFactoryUtil.getCalendar();

      birthdayCal.setTime(contact.getBirthday());

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

      user =
          UserLocalServiceUtil.updateUser(
              user.getUserId(),
              StringPool.BLANK,
              StringPool.BLANK,
              StringPool.BLANK,
              false,
              user.getReminderQueryQuestion(),
              user.getReminderQueryAnswer(),
              screenName,
              emailAddress,
              user.getFacebookId(),
              user.getOpenId(),
              themeDisplay.getLanguageId(),
              user.getTimeZoneId(),
              greeting,
              user.getComments(),
              firstName,
              user.getMiddleName(),
              lastName,
              contact.getPrefixId(),
              contact.getSuffixId(),
              contact.isMale(),
              birthdayMonth,
              birthdayDay,
              birthdayYear,
              contact.getSmsSn(),
              contact.getAimSn(),
              contact.getFacebookSn(),
              contact.getIcqSn(),
              contact.getJabberSn(),
              contact.getMsnSn(),
              contact.getMySpaceSn(),
              contact.getSkypeSn(),
              contact.getTwitterSn(),
              contact.getYmSn(),
              contact.getJobTitle(),
              null,
              null,
              null,
              null,
              null,
              new ServiceContext());
    } catch (NoSuchUserException nsue) {
      UserLocalServiceUtil.addDefaultAdminUser(
          themeDisplay.getCompanyId(),
          screenName,
          emailAddress,
          themeDisplay.getLocale(),
          firstName,
          StringPool.BLANK,
          lastName);

      user = UserLocalServiceUtil.getUserByEmailAddress(themeDisplay.getCompanyId(), emailAddress);

      String defaultAdminEmailAddress =
          PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + PropsValues.COMPANY_DEFAULT_WEB_ID;

      if (!emailAddress.equals(defaultAdminEmailAddress)) {
        User testUser =
            UserLocalServiceUtil.fetchUserByEmailAddress(
                themeDisplay.getCompanyId(), defaultAdminEmailAddress);

        if (testUser != null) {
          UserLocalServiceUtil.updateStatus(
              testUser.getUserId(), WorkflowConstants.STATUS_INACTIVE);
        }
      }
    }

    user = UserLocalServiceUtil.updatePasswordReset(user.getUserId(), true);

    HttpSession session = request.getSession();

    session.setAttribute(WebKeys.EMAIL_ADDRESS, emailAddress);
    session.setAttribute(WebKeys.SETUP_WIZARD_PASSWORD_UPDATED, true);
    session.setAttribute(WebKeys.USER_ID, user.getUserId());
  }
  protected User updateUser(User user, Userinfo userinfo) throws Exception {
    String emailAddress = userinfo.getEmail();
    String firstName = userinfo.getGivenName();
    String lastName = userinfo.getFamilyName();
    boolean male = Validator.equals(userinfo.getGender(), "male");

    if (emailAddress.equals(user.getEmailAddress())
        && firstName.equals(user.getFirstName())
        && lastName.equals(user.getLastName())
        && (male == user.isMale())) {

      return user;
    }

    Contact contact = user.getContact();

    Calendar birthdayCal = CalendarFactoryUtil.getCalendar();

    birthdayCal.setTime(contact.getBirthday());

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

    long[] groupIds = null;
    long[] organizationIds = null;
    long[] roleIds = null;
    List<UserGroupRole> userGroupRoles = null;
    long[] userGroupIds = null;

    ServiceContext serviceContext = new ServiceContext();

    if (!StringUtil.equalsIgnoreCase(emailAddress, user.getEmailAddress())) {

      UserLocalServiceUtil.updateEmailAddress(
          user.getUserId(), StringPool.BLANK, emailAddress, emailAddress);
    }

    UserLocalServiceUtil.updateEmailAddressVerified(user.getUserId(), true);

    return UserLocalServiceUtil.updateUser(
        user.getUserId(),
        StringPool.BLANK,
        StringPool.BLANK,
        StringPool.BLANK,
        false,
        user.getReminderQueryQuestion(),
        user.getReminderQueryAnswer(),
        user.getScreenName(),
        emailAddress,
        0,
        user.getOpenId(),
        user.getLanguageId(),
        user.getTimeZoneId(),
        user.getGreeting(),
        user.getComments(),
        firstName,
        user.getMiddleName(),
        lastName,
        contact.getPrefixId(),
        contact.getSuffixId(),
        male,
        birthdayMonth,
        birthdayDay,
        birthdayYear,
        contact.getSmsSn(),
        contact.getAimSn(),
        contact.getFacebookSn(),
        contact.getIcqSn(),
        contact.getJabberSn(),
        contact.getMsnSn(),
        contact.getMySpaceSn(),
        contact.getSkypeSn(),
        contact.getTwitterSn(),
        contact.getYmSn(),
        contact.getJobTitle(),
        groupIds,
        organizationIds,
        roleIds,
        userGroupRoles,
        userGroupIds,
        serviceContext);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Contact newContact = _persistence.create(pk);

    newContact.setMvccVersion(RandomTestUtil.nextLong());

    newContact.setCompanyId(RandomTestUtil.nextLong());

    newContact.setUserId(RandomTestUtil.nextLong());

    newContact.setUserName(RandomTestUtil.randomString());

    newContact.setCreateDate(RandomTestUtil.nextDate());

    newContact.setModifiedDate(RandomTestUtil.nextDate());

    newContact.setClassNameId(RandomTestUtil.nextLong());

    newContact.setClassPK(RandomTestUtil.nextLong());

    newContact.setAccountId(RandomTestUtil.nextLong());

    newContact.setParentContactId(RandomTestUtil.nextLong());

    newContact.setEmailAddress(RandomTestUtil.randomString());

    newContact.setFirstName(RandomTestUtil.randomString());

    newContact.setMiddleName(RandomTestUtil.randomString());

    newContact.setLastName(RandomTestUtil.randomString());

    newContact.setPrefixId(RandomTestUtil.nextLong());

    newContact.setSuffixId(RandomTestUtil.nextLong());

    newContact.setMale(RandomTestUtil.randomBoolean());

    newContact.setBirthday(RandomTestUtil.nextDate());

    newContact.setSmsSn(RandomTestUtil.randomString());

    newContact.setFacebookSn(RandomTestUtil.randomString());

    newContact.setJabberSn(RandomTestUtil.randomString());

    newContact.setSkypeSn(RandomTestUtil.randomString());

    newContact.setTwitterSn(RandomTestUtil.randomString());

    newContact.setEmployeeStatusId(RandomTestUtil.randomString());

    newContact.setEmployeeNumber(RandomTestUtil.randomString());

    newContact.setJobTitle(RandomTestUtil.randomString());

    newContact.setJobClass(RandomTestUtil.randomString());

    newContact.setHoursOfOperation(RandomTestUtil.randomString());

    _contacts.add(_persistence.update(newContact));

    Contact existingContact = _persistence.findByPrimaryKey(newContact.getPrimaryKey());

    Assert.assertEquals(existingContact.getMvccVersion(), newContact.getMvccVersion());
    Assert.assertEquals(existingContact.getContactId(), newContact.getContactId());
    Assert.assertEquals(existingContact.getCompanyId(), newContact.getCompanyId());
    Assert.assertEquals(existingContact.getUserId(), newContact.getUserId());
    Assert.assertEquals(existingContact.getUserName(), newContact.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingContact.getCreateDate()),
        Time.getShortTimestamp(newContact.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingContact.getModifiedDate()),
        Time.getShortTimestamp(newContact.getModifiedDate()));
    Assert.assertEquals(existingContact.getClassNameId(), newContact.getClassNameId());
    Assert.assertEquals(existingContact.getClassPK(), newContact.getClassPK());
    Assert.assertEquals(existingContact.getAccountId(), newContact.getAccountId());
    Assert.assertEquals(existingContact.getParentContactId(), newContact.getParentContactId());
    Assert.assertEquals(existingContact.getEmailAddress(), newContact.getEmailAddress());
    Assert.assertEquals(existingContact.getFirstName(), newContact.getFirstName());
    Assert.assertEquals(existingContact.getMiddleName(), newContact.getMiddleName());
    Assert.assertEquals(existingContact.getLastName(), newContact.getLastName());
    Assert.assertEquals(existingContact.getPrefixId(), newContact.getPrefixId());
    Assert.assertEquals(existingContact.getSuffixId(), newContact.getSuffixId());
    Assert.assertEquals(existingContact.getMale(), newContact.getMale());
    Assert.assertEquals(
        Time.getShortTimestamp(existingContact.getBirthday()),
        Time.getShortTimestamp(newContact.getBirthday()));
    Assert.assertEquals(existingContact.getSmsSn(), newContact.getSmsSn());
    Assert.assertEquals(existingContact.getFacebookSn(), newContact.getFacebookSn());
    Assert.assertEquals(existingContact.getJabberSn(), newContact.getJabberSn());
    Assert.assertEquals(existingContact.getSkypeSn(), newContact.getSkypeSn());
    Assert.assertEquals(existingContact.getTwitterSn(), newContact.getTwitterSn());
    Assert.assertEquals(existingContact.getEmployeeStatusId(), newContact.getEmployeeStatusId());
    Assert.assertEquals(existingContact.getEmployeeNumber(), newContact.getEmployeeNumber());
    Assert.assertEquals(existingContact.getJobTitle(), newContact.getJobTitle());
    Assert.assertEquals(existingContact.getJobClass(), newContact.getJobClass());
    Assert.assertEquals(existingContact.getHoursOfOperation(), newContact.getHoursOfOperation());
  }