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()));
  }
  protected User addUser() throws Exception {
    long pk = nextLong();

    User user = _persistence.create(pk);

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

    _persistence.update(user, false);

    return user;
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static User toModel(UserSoap soapModel) {
    User model = new UserImpl();

    model.setUuid(soapModel.getUuid());
    model.setUserId(soapModel.getUserId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setCreateDate(soapModel.getCreateDate());
    model.setModifiedDate(soapModel.getModifiedDate());
    model.setDefaultUser(soapModel.getDefaultUser());
    model.setContactId(soapModel.getContactId());
    model.setPassword(soapModel.getPassword());
    model.setPasswordEncrypted(soapModel.getPasswordEncrypted());
    model.setPasswordReset(soapModel.getPasswordReset());
    model.setPasswordModifiedDate(soapModel.getPasswordModifiedDate());
    model.setDigest(soapModel.getDigest());
    model.setReminderQueryQuestion(soapModel.getReminderQueryQuestion());
    model.setReminderQueryAnswer(soapModel.getReminderQueryAnswer());
    model.setGraceLoginCount(soapModel.getGraceLoginCount());
    model.setScreenName(soapModel.getScreenName());
    model.setEmailAddress(soapModel.getEmailAddress());
    model.setFacebookId(soapModel.getFacebookId());
    model.setOpenId(soapModel.getOpenId());
    model.setPortraitId(soapModel.getPortraitId());
    model.setLanguageId(soapModel.getLanguageId());
    model.setTimeZoneId(soapModel.getTimeZoneId());
    model.setGreeting(soapModel.getGreeting());
    model.setComments(soapModel.getComments());
    model.setFirstName(soapModel.getFirstName());
    model.setMiddleName(soapModel.getMiddleName());
    model.setLastName(soapModel.getLastName());
    model.setJobTitle(soapModel.getJobTitle());
    model.setLoginDate(soapModel.getLoginDate());
    model.setLoginIP(soapModel.getLoginIP());
    model.setLastLoginDate(soapModel.getLastLoginDate());
    model.setLastLoginIP(soapModel.getLastLoginIP());
    model.setLastFailedLoginDate(soapModel.getLastFailedLoginDate());
    model.setFailedLoginAttempts(soapModel.getFailedLoginAttempts());
    model.setLockout(soapModel.getLockout());
    model.setLockoutDate(soapModel.getLockoutDate());
    model.setAgreedToTermsOfUse(soapModel.getAgreedToTermsOfUse());
    model.setEmailAddressVerified(soapModel.getEmailAddressVerified());
    model.setStatus(soapModel.getStatus());

    return model;
  }
  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());
  }
  private void createAccount(WebForm form, HttpServletRequest request) throws Exception {

    User user =
        APILocator.getUserAPI()
            .loadByUserByEmail(form.getEmail(), APILocator.getUserAPI().getSystemUser(), false);
    User defaultUser = APILocator.getUserAPI().getDefaultUser();
    Date today = new Date();

    if (user.isNew() || (!user.isNew() && user.getLastLoginDate() == null)) {

      // ### CREATE USER ###
      Company company = PublicCompanyFactory.getDefaultCompany();
      user.setEmailAddress(form.getEmail().trim().toLowerCase());
      user.setFirstName(form.getFirstName() == null ? "" : form.getFirstName());
      user.setMiddleName(form.getMiddleName() == null ? "" : form.getMiddleName());
      user.setLastName(form.getLastName() == null ? "" : form.getLastName());
      user.setNickName("");
      user.setCompanyId(company.getCompanyId());
      user.setPasswordEncrypted(true);
      user.setGreeting("Welcome, " + user.getFullName() + "!");

      // Set defaults values
      if (user.isNew()) {
        // if it's a new user we set random password
        String pass = PublicEncryptionFactory.getRandomPassword();
        user.setPassword(PublicEncryptionFactory.digestString(pass));
        user.setLanguageId(defaultUser.getLanguageId());
        user.setTimeZoneId(defaultUser.getTimeZoneId());
        user.setSkinId(defaultUser.getSkinId());
        user.setDottedSkins(defaultUser.isDottedSkins());
        user.setRoundedSkins(defaultUser.isRoundedSkins());
        user.setResolution(defaultUser.getResolution());
        user.setRefreshRate(defaultUser.getRefreshRate());
        user.setLayoutIds("");
        user.setActive(true);
        user.setCreateDate(today);
      }
      APILocator.getUserAPI().save(user, APILocator.getUserAPI().getSystemUser(), false);
      // ### END CREATE USER ###

      // ### CREATE USER_PROXY ###
      UserProxy userProxy =
          com.dotmarketing.business.APILocator.getUserProxyAPI()
              .getUserProxy(user.getUserId(), APILocator.getUserAPI().getSystemUser(), false);
      userProxy.setPrefix("");
      userProxy.setTitle(form.getTitle());
      userProxy.setOrganization(form.getOrganization());
      userProxy.setUserId(user.getUserId());
      com.dotmarketing.business.APILocator.getUserProxyAPI()
          .saveUserProxy(userProxy, APILocator.getUserAPI().getSystemUser(), false);
      // ### END CRETE USER_PROXY ###

      // saving user inode on web form
      form.setUserInode(userProxy.getInode());
      if (UtilMethods.isSet(form.getFormType())) {
        HibernateUtil.saveOrUpdate(form);
      }

      ///// WE CAN DO THIS! BUT WE NEED TO ADD CATEGORIES TO WEBFORM AND ALSO CHANGE THE PROCESSES
      // THAT
      //// CREATE THE EXCEL DOWNLOAD FROM WEB FORMS. I DIDN'T ADD IT SO I COMMENTED THIS CODE FOR
      // NOW
      // get the old categories, wipe them out
      /*
      List<Category> categories = InodeFactory.getParentsOfClass(userProxy, Category.class);
      for (int i = 0; i < categories.size(); i++) {
      	categories.get(i).deleteChild(userProxy);
      }
       */
      // Save the new categories
      /*String[] arr = form.getCategories();
      if (arr != null) {
      	for (int i = 0; i < arr.length; i++) {
      		Category node = (Category) InodeFactory.getInode(arr[i], Category.class);
      		node.addChild(userProxy);
      	}
      }*/

      // ### CREATE ADDRESS ###
      try {
        List<Address> addresses = PublicAddressFactory.getAddressesByUserId(user.getUserId());
        Address address =
            (addresses.size() > 0 ? addresses.get(0) : PublicAddressFactory.getInstance());
        address.setStreet1(form.getAddress1() == null ? "" : form.getAddress1());
        address.setStreet2(form.getAddress2() == null ? "" : form.getAddress2());
        address.setCity(form.getCity() == null ? "" : form.getCity());
        address.setState(form.getState() == null ? "" : form.getState());
        address.setZip(form.getZip() == null ? "" : form.getZip());
        String phone = form.getPhone();
        address.setPhone(phone == null ? "" : phone);
        address.setUserId(user.getUserId());
        address.setCompanyId(company.getCompanyId());
        PublicAddressFactory.save(address);
      } catch (Exception ex) {
        Logger.error(this, ex.getMessage(), ex);
      }

      Role defaultRole =
          com.dotmarketing.business.APILocator.getRoleAPI()
              .loadRoleByKey(Config.getStringProperty("CMS_VIEWER_ROLE"));
      String roleId = defaultRole.getId();
      if (InodeUtils.isSet(roleId)) {
        com.dotmarketing.business.APILocator.getRoleAPI().addRoleToUser(roleId, user);
      }
    }
    // ### END CREATE ADDRESS ###

    // ### BUILD THE USER COMMENT ###
    addUserComments(user.getUserId(), form, request);
    // ### END BUILD THE USER COMMENT ###

    /* associate user with their clickstream request */
    if (Config.getBooleanProperty("ENABLE_CLICKSTREAM_TRACKING", false)) {
      ClickstreamFactory.setClickStreamUser(user.getUserId(), request);
    }
  }