protected User setGoogleCredentials(HttpSession session, long companyId, Credential credential)
      throws Exception {

    Userinfo userinfo = getUserInfo(credential);

    if (userinfo == null) {
      return null;
    }

    User user = null;

    String emailAddress = userinfo.getEmail();

    if ((user == null) && Validator.isNotNull(emailAddress)) {
      user = UserLocalServiceUtil.fetchUserByEmailAddress(companyId, emailAddress);

      if ((user != null) && (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE)) {

        session.setAttribute("GOOGLE_USER_EMAIL_ADDRESS", emailAddress);
      }
    }

    if (user != null) {
      if (user.getStatus() == WorkflowConstants.STATUS_INCOMPLETE) {
        session.setAttribute("GOOGLE_INCOMPLETE_USER_ID", userinfo.getId());

        user.setEmailAddress(userinfo.getEmail());
        user.setFirstName(userinfo.getGivenName());
        user.setLastName(userinfo.getFamilyName());

        return user;
      }

      user = updateUser(user, userinfo);
    } else {
      user = addUser(session, companyId, userinfo);
    }

    if (DeployManagerUtil.isDeployed(_GOOGLE_DRIVE_CONTEXT)) {
      updateCustomFields(user, userinfo, credential.getAccessToken(), credential.getRefreshToken());
    }

    return user;
  }
  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);
  }
  protected User addUser(HttpSession session, long companyId, Userinfo userinfo) throws Exception {

    long creatorUserId = 0;
    boolean autoPassword = true;
    String password1 = StringPool.BLANK;
    String password2 = StringPool.BLANK;
    boolean autoScreenName = true;
    String screenName = StringPool.BLANK;
    String emailAddress = userinfo.getEmail();
    String openId = StringPool.BLANK;
    Locale locale = LocaleUtil.getDefault();
    String firstName = userinfo.getGivenName();
    String middleName = StringPool.BLANK;
    String lastName = userinfo.getFamilyName();
    int prefixId = 0;
    int suffixId = 0;
    boolean male = Validator.equals(userinfo.getGender(), "male");
    int birthdayMonth = Calendar.JANUARY;
    int birthdayDay = 1;
    int birthdayYear = 1970;
    String jobTitle = StringPool.BLANK;
    long[] groupIds = null;
    long[] organizationIds = null;
    long[] roleIds = null;
    long[] userGroupIds = null;
    boolean sendEmail = true;

    ServiceContext serviceContext = new ServiceContext();

    User user =
        UserLocalServiceUtil.addUser(
            creatorUserId,
            companyId,
            autoPassword,
            password1,
            password2,
            autoScreenName,
            screenName,
            emailAddress,
            0,
            openId,
            locale,
            firstName,
            middleName,
            lastName,
            prefixId,
            suffixId,
            male,
            birthdayMonth,
            birthdayDay,
            birthdayYear,
            jobTitle,
            groupIds,
            organizationIds,
            roleIds,
            userGroupIds,
            sendEmail,
            serviceContext);

    user = UserLocalServiceUtil.updateLastLogin(user.getUserId(), user.getLoginIP());

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

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

    session.setAttribute("GOOGLE_USER_EMAIL_ADDRESS", emailAddress);

    return user;
  }