com.folion.social.CouchbaseConnectionRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.folion.social.CouchbaseConnectionRepository.java

Source

/*
 * Copyright 2016 Folion Media, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.folion.social;

import com.folion.core.srv.SocialAccountService;
import com.folion.domain.SocialConnection;
import org.springframework.social.connect.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.List;

public class CouchbaseConnectionRepository implements ConnectionRepository {

    private SocialAccountService socialAccountService;
    private ConnectionFactoryLocator connectionFactoryLocator;

    public CouchbaseConnectionRepository(SocialAccountService socialAccountService,
            ConnectionFactoryLocator connectionFactoryLocator) {
        this.socialAccountService = socialAccountService;
        this.connectionFactoryLocator = connectionFactoryLocator;
    }

    @Override
    public MultiValueMap<String, Connection<?>> findAllConnections() {

        return new LinkedMultiValueMap<>();
    }

    @Override
    public List<Connection<?>> findConnections(String providerId) {

        return new ArrayList<>();
    }

    @Override
    public <A> List<Connection<A>> findConnections(Class<A> apiType) {
        List<?> connections = findConnections(getProviderId(apiType));
        return (List<Connection<A>>) connections;
    }

    @Override
    public MultiValueMap<String, Connection<?>> findConnectionsToUsers(
            MultiValueMap<String, String> providerUsers) {

        return new LinkedMultiValueMap<>();
    }

    @Override
    public Connection<?> getConnection(ConnectionKey connectionKey) {

        SocialConnection socialConnection = socialAccountService.getConnection(connectionKey.getProviderId(),
                connectionKey.getProviderUserId());
        return createConnection(socialConnection);
    }

    @Override
    public <A> Connection<A> getConnection(Class<A> apiType, String providerUserId) {

        String providerId = connectionFactoryLocator.getConnectionFactory(apiType).getProviderId();

        SocialConnection socialConnection = socialAccountService.getConnection(providerId, providerUserId);
        return (Connection<A>) createConnection(socialConnection);
    }

    @Override
    public <A> Connection<A> getPrimaryConnection(Class<A> apiType) {
        return null;
    }

    @Override
    public <A> Connection<A> findPrimaryConnection(Class<A> apiType) {
        return null;
    }

    @Override
    public void addConnection(Connection<?> connection) {

        System.out.println("Adding connection");
        System.out.println("Adding connection now");

        UserProfile userProfile = connection.fetchUserProfile();
        ConnectionData connectionData = connection.createData();

        SocialConnection socialConnection = new SocialConnection();

        socialConnection.setEmail(userProfile.getEmail());
        socialConnection.setFirstName(userProfile.getFirstName());
        socialConnection.setLastName(userProfile.getLastName());

        socialConnection.setProviderId(connectionData.getProviderId());
        socialConnection.setProviderUserId(connectionData.getProviderUserId());
        socialConnection.setAccessToken(connectionData.getAccessToken());
        socialConnection.setRefreshToken(connectionData.getRefreshToken());
        socialConnection.setExpireTime(connectionData.getExpireTime());
        socialConnection.setDisplayName(connectionData.getDisplayName());
        socialConnection.setImageUrl(connectionData.getImageUrl());

        //socialConnection.addConnection();

    }

    @Override
    public void updateConnection(Connection<?> connection) {

    }

    @Override
    public void removeConnections(String providerId) {

    }

    @Override
    public void removeConnection(ConnectionKey connectionKey) {

    }

    private <A> String getProviderId(Class<A> apiType) {
        return connectionFactoryLocator.getConnectionFactory(apiType).getProviderId();
    }

    private Connection<?> createConnection(SocialConnection socialConnection) {

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

        ConnectionFactory<?> connectionFactory = connectionFactoryLocator
                .getConnectionFactory(socialConnection.getProviderId());
        ConnectionData connectionData = new ConnectionData(socialConnection.getProviderId(),
                socialConnection.getProviderUserId(), socialConnection.getDisplayName(),
                socialConnection.getProfileUrl(), socialConnection.getImageUrl(), socialConnection.getAccessToken(),
                socialConnection.getSecret(), socialConnection.getRefreshToken(), socialConnection.getExpireTime());

        return connectionFactory.createConnection(connectionData);
    }
}