Example usage for org.springframework.social.facebook.api User getId

List of usage examples for org.springframework.social.facebook.api User getId

Introduction

In this page you can find the example usage for org.springframework.social.facebook.api User getId.

Prototype

public String getId() 

Source Link

Document

The user's Facebook ID

Usage

From source file:gr.abiss.calipso.userDetails.service.impl.UserDetailsServiceImpl.java

/**
 * @see org.springframework.social.connect.ConnectionSignUp#execute(org.springframework.social.connect.Connection)
 *///from   ww  w  .j a  v a 2s  .co m
@Override
@Transactional(readOnly = false)
public String execute(Connection<?> connection) {
    //if(LOGGER.isDebugEnabled()){
    LOGGER.info("ConnectionSignUp#execute, connection: " + connection);
    //}
    //String localUsername = null;
    String accessToken = connection.createData().getAccessToken();
    UserProfile profile = connection.fetchUserProfile();
    ConnectionData data = connection.createData();

    String socialUsername = profile.getUsername();
    if (StringUtils.isBlank(socialUsername)) {
        LOGGER.info("blank username for profile class: " + profile.getClass());
        Object api = connection.getApi();
        if (SocialMediaService.FACEBOOK.toString().equalsIgnoreCase(connection.createData().getProviderId())) {
            Facebook fbApi = new FacebookTemplate(accessToken);
            User fbProfile = fbApi.userOperations().getUserProfile();
            if (fbProfile != null) {
                socialUsername = fbProfile.getId();
                LOGGER.debug("ConnectionSignUp#execute, Got facebook id: " + socialUsername);
            }
        } else if (SocialMediaService.LINKEDIN.toString()
                .equalsIgnoreCase(connection.createData().getProviderId())) {
            LinkedIn liApi = new LinkedInTemplate(accessToken);
            LinkedInProfile liProfile = liApi.profileOperations().getUserProfile();
            if (liProfile != null) {
                socialUsername = liProfile.getId();
                LOGGER.debug("ConnectionSignUp#execute, Got linkedin id: " + socialUsername);
            }
        }
    }
    String socialName = profile.getName();
    String socialEmail = profile.getEmail();
    String socialFirstName = profile.getFirstName();
    String socialLastName = profile.getLastName();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("ConnectionSignUp#execute, profile: " + profile + ", data: " + data + ", socialUsername: "
                + socialUsername + ", socialName: " + socialName + ", socialEmail: " + socialEmail
                + ", socialFirstName: " + socialFirstName + ", socialLastName: " + socialLastName
                + ", accessToken: " + accessToken);
    }
    // get email from github if empty
    //      if (StringUtils.isNullOrEmpty(socialEmail)) {
    //         Object api = connection.getApi();
    //         if (SocialMediaService.GITHUB.toString().equalsIgnoreCase(connection.createData().getProviderId())) {
    //            GitHub githubApi = new GitHubTemplate(accessToken);//(GitHub) api;
    //            GitHubUserProfile githubProfile = githubApi.userOperations().getUserProfile();
    //            LOGGER.debug("ConnectionSignUp#execute, Got github profile: " + githubProfile + ", authorized: " + githubApi.isAuthorized());
    //            if (githubProfile != null) {
    //               socialEmail = githubProfile.getEmail();
    //               LOGGER.debug("ConnectionSignUp#execute, Got github email: " + socialEmail);
    //            }
    //         }
    //      }
    LocalUser user = null;
    if (!StringUtils.isBlank(socialEmail)) {
        // LOGGER.debug("ConnectionSignUp#execute, Social email accessible, looking for local user match");

        user = localUserService.findByUserNameOrEmail(socialEmail);
        // 

        if (user != null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(
                        "ConnectionSignUp#execute, Email matches existing local user, no need to create one");
            }
            //localUsername = user.getUsername();
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(
                        "ConnectionSignUp#execute, Email did not match an local user, trying to create one");
            }

            user = new SimpleLocalUser();
            user.setActive(true);
            user.setEmail(socialEmail);
            user.setUsername(socialEmail);
            user.setFirstName(socialFirstName);
            user.setLastName(socialLastName);
            user.setPassword(UUID.randomUUID().toString());
            try {
                user = localUserService.createForImplicitSignup(user);

                //localUsername = user.getUsername();
            } catch (DuplicateEmailException e) {
                LOGGER.error("ConnectionSignUp#executeError while implicitly registering user", e);
            }

        }
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "ConnectionSignUp#execute, Social email was not accessible, unable to implicitly sign in user");
        }
    }
    //localUserService.createAccount(account);
    String result = user != null && user.getId() != null ? user.getId().toString() : null;
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("ConnectionSignUp#execute, returning result: " + result);
    }
    return result;
}