@Override
  public int compare(User user1, User user2) {
    String emailAddress1 = user1.getEmailAddress();
    String emailAddress2 = user2.getEmailAddress();

    int value = emailAddress1.compareTo(emailAddress2);

    if (_ascending) {
      return value;
    } else {
      return -value;
    }
  }
  public static String getLogin(boolean encodeLogin) throws Exception {
    String login = null;

    String authType = GetterUtil.getString(PropsUtil.get(PropsKeys.COMPANY_SECURITY_AUTH_TYPE));

    User user = TestPropsValues.getUser();

    if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
      login = user.getEmailAddress();

      if (encodeLogin) {
        login = HttpUtil.encodeURL(login);
      }
    } else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
      login = user.getScreenName();
    } else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
      login = Long.toString(TestPropsValues.getUserId());
    }

    return login;
  }
  protected void sendRedirect(
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      ThemeDisplay themeDisplay,
      User user,
      String password)
      throws Exception {

    String login = null;

    Company company = themeDisplay.getCompany();

    String authType = company.getAuthType();

    if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
      login = String.valueOf(user.getUserId());
    } else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
      login = user.getScreenName();
    } else {
      login = user.getEmailAddress();
    }

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

    String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

    if (Validator.isNotNull(redirect)) {
      HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

      AuthenticatedSessionManagerUtil.login(request, response, login, password, false, null);
    } else {
      PortletURL loginURL = LoginUtil.getLoginURL(request, themeDisplay.getPlid());

      loginURL.setParameter("login", login);

      redirect = loginURL.toString();
    }

    actionResponse.sendRedirect(redirect);
  }
  protected void addUser(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

    HttpSession session = request.getSession();

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

    Company company = themeDisplay.getCompany();

    boolean autoPassword = true;
    String password1 = null;
    String password2 = null;
    boolean autoScreenName = isAutoScreenName();
    String screenName = ParamUtil.getString(actionRequest, "screenName");
    String emailAddress = ParamUtil.getString(actionRequest, "emailAddress");
    long facebookId = ParamUtil.getLong(actionRequest, "facebookId");
    String openId = ParamUtil.getString(actionRequest, "openId");
    String languageId = ParamUtil.getString(actionRequest, "languageId");
    String firstName = ParamUtil.getString(actionRequest, "firstName");
    String middleName = ParamUtil.getString(actionRequest, "middleName");
    String lastName = ParamUtil.getString(actionRequest, "lastName");
    long prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
    long suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
    boolean male = ParamUtil.getBoolean(actionRequest, "male", true);
    int birthdayMonth = ParamUtil.getInteger(actionRequest, "birthdayMonth");
    int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
    int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
    String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
    long[] groupIds = null;
    long[] organizationIds = null;
    long[] roleIds = null;
    long[] userGroupIds = null;
    boolean sendEmail = true;

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(User.class.getName(), actionRequest);

    if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
      autoPassword = false;

      password1 = ParamUtil.getString(actionRequest, "password1");
      password2 = ParamUtil.getString(actionRequest, "password2");
    }

    boolean openIdPending = false;

    Boolean openIdLoginPending = (Boolean) session.getAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);

    if ((openIdLoginPending != null)
        && openIdLoginPending.booleanValue()
        && Validator.isNotNull(openId)) {

      sendEmail = false;
      openIdPending = true;
    }

    User user =
        _userService.addUserWithWorkflow(
            company.getCompanyId(),
            autoPassword,
            password1,
            password2,
            autoScreenName,
            screenName,
            emailAddress,
            facebookId,
            openId,
            LocaleUtil.fromLanguageId(languageId),
            firstName,
            middleName,
            lastName,
            prefixId,
            suffixId,
            male,
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            jobTitle,
            groupIds,
            organizationIds,
            roleIds,
            userGroupIds,
            sendEmail,
            serviceContext);

    if (openIdPending) {
      session.setAttribute(WebKeys.OPEN_ID_LOGIN, Long.valueOf(user.getUserId()));

      session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
    } else {

      // Session messages

      if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
        SessionMessages.add(request, "userAdded", user.getEmailAddress());
        SessionMessages.add(request, "userAddedPassword", user.getPasswordUnencrypted());
      } else {
        SessionMessages.add(request, "userPending", user.getEmailAddress());
      }
    }

    // Send redirect

    sendRedirect(actionRequest, actionResponse, themeDisplay, user, user.getPasswordUnencrypted());
  }
  protected void updateIncompleteUser(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    HttpServletRequest request =
        PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(actionRequest));

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

    boolean autoPassword = true;
    String password1 = null;
    String password2 = null;
    boolean autoScreenName = false;
    String screenName = ParamUtil.getString(actionRequest, "screenName");
    String emailAddress = ParamUtil.getString(actionRequest, "emailAddress");

    HttpSession session = request.getSession();

    long facebookId = GetterUtil.getLong(session.getAttribute(WebKeys.FACEBOOK_INCOMPLETE_USER_ID));
    String googleUserId =
        GetterUtil.getString(session.getAttribute(WebKeys.GOOGLE_INCOMPLETE_USER_ID));

    if (Validator.isNotNull(googleUserId)) {
      autoPassword = false;
    }

    if ((facebookId > 0) || Validator.isNotNull(googleUserId)) {
      password1 = PwdGenerator.getPassword();
      password2 = password1;
    }

    String openId = ParamUtil.getString(actionRequest, "openId");
    String firstName = ParamUtil.getString(actionRequest, "firstName");
    String middleName = ParamUtil.getString(actionRequest, "middleName");
    String lastName = ParamUtil.getString(actionRequest, "lastName");
    long prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
    long suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
    boolean male = ParamUtil.getBoolean(actionRequest, "male", true);
    int birthdayMonth = ParamUtil.getInteger(actionRequest, "birthdayMonth");
    int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
    int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
    String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
    boolean updateUserInformation = true;

    boolean sendEmail = true;

    if (Validator.isNotNull(googleUserId)) {
      sendEmail = false;
    }

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(User.class.getName(), actionRequest);

    User user =
        _userService.updateIncompleteUser(
            themeDisplay.getCompanyId(),
            autoPassword,
            password1,
            password2,
            autoScreenName,
            screenName,
            emailAddress,
            facebookId,
            openId,
            themeDisplay.getLocale(),
            firstName,
            middleName,
            lastName,
            prefixId,
            suffixId,
            male,
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            jobTitle,
            updateUserInformation,
            sendEmail,
            serviceContext);

    if (facebookId > 0) {
      session.removeAttribute(WebKeys.FACEBOOK_INCOMPLETE_USER_ID);

      updateUserAndSendRedirect(actionRequest, actionResponse, themeDisplay, user, password1);

      return;
    }

    if (Validator.isNotNull(googleUserId)) {
      _userLocalService.updateGoogleUserId(user.getUserId(), googleUserId);

      session.removeAttribute(WebKeys.GOOGLE_INCOMPLETE_USER_ID);

      updateUserAndSendRedirect(actionRequest, actionResponse, themeDisplay, user, password1);

      return;
    }

    // Session messages

    if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
      SessionMessages.add(request, "userAdded", user.getEmailAddress());
      SessionMessages.add(request, "userAddedPassword", user.getPasswordUnencrypted());
    } else {
      SessionMessages.add(request, "userPending", user.getEmailAddress());
    }

    // Send redirect

    sendRedirect(actionRequest, actionResponse, themeDisplay, user, user.getPasswordUnencrypted());
  }
  @Override
  public ActionForward execute(
      ActionMapping actionMapping,
      ActionForm actionForm,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    HttpSession session = request.getSession();

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

    String languageId = ParamUtil.getString(request, "languageId");

    Locale locale = LocaleUtil.fromLanguageId(languageId);

    if (LanguageUtil.isAvailableLocale(themeDisplay.getSiteGroupId(), locale)) {

      boolean persistState = ParamUtil.getBoolean(request, "persistState", true);

      if (themeDisplay.isSignedIn() && persistState) {
        User user = themeDisplay.getUser();

        Contact contact = user.getContact();

        AdminUtil.updateUser(
            request,
            user.getUserId(),
            user.getScreenName(),
            user.getEmailAddress(),
            user.getFacebookId(),
            user.getOpenId(),
            languageId,
            user.getTimeZoneId(),
            user.getGreeting(),
            user.getComments(),
            contact.getSmsSn(),
            contact.getFacebookSn(),
            contact.getJabberSn(),
            contact.getSkypeSn(),
            contact.getTwitterSn());
      }

      session.setAttribute(Globals.LOCALE_KEY, locale);

      LanguageUtil.updateCookie(request, response, locale);
    }

    // Send redirect

    String redirect = ParamUtil.getString(request, "redirect");

    String layoutURL = StringPool.BLANK;
    String queryString = StringPool.BLANK;

    int pos = redirect.indexOf(Portal.FRIENDLY_URL_SEPARATOR);

    if (pos == -1) {
      pos = redirect.indexOf(StringPool.QUESTION);
    }

    if (pos != -1) {
      layoutURL = redirect.substring(0, pos);
      queryString = redirect.substring(pos);
    } else {
      layoutURL = redirect;
    }

    Layout layout = themeDisplay.getLayout();

    if (isGroupFriendlyURL(layout.getGroup(), layout, layoutURL, locale)) {
      if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
        redirect = layoutURL;
      } else {
        redirect = PortalUtil.getGroupFriendlyURL(layout.getLayoutSet(), themeDisplay, locale);
      }
    } else {
      if (PropsValues.LOCALE_PREPEND_FRIENDLY_URL_STYLE == 0) {
        if (themeDisplay.isI18n()) {
          redirect = layout.getFriendlyURL(locale);
        } else {
          redirect = PortalUtil.getLayoutURL(layout, themeDisplay, locale);
        }
      } else {
        redirect = PortalUtil.getLayoutFriendlyURL(layout, themeDisplay, locale);
      }
    }

    if (layout.isTypeControlPanel()) {
      redirect = redirect + queryString;
    }

    response.sendRedirect(redirect);

    return null;
  }