// Path: social-redis-spring-boot-starter/src/main/java/be/ordina/jworks/social/redis/connection/data/SocialRedisConnection.java
// @Getter
// @Setter
// @AllArgsConstructor
// @RedisHash("socialRedisConnection")
// public class SocialRedisConnection {
// 
//     @Id
//     @Indexed
//     private String providerUserId;
// 
//     @Indexed
//     private String userId;
// 
//     @Indexed
//     private String providerId;
// 
//     private String displayName;
// 
//     private String profileUrl;
// 
//     private String imageUrl;
// 
//     private String accessToken;
// 
//     private String secret;
// 
//     private String refreshToken;
// 
//     private Long expireTime;
// }
// 
// Path: social-redis-spring-boot-starter/src/main/java/be/ordina/jworks/social/redis/connection/data/SocialRedisConnectionRepository.java
// @Repository
// public interface SocialRedisConnectionRepository extends CrudRepository<SocialRedisConnection, String> {
// 
//     Iterable<SocialRedisConnection> findByProviderId(final String providerId);
// 
//     Iterable<SocialRedisConnection> findByUserIdAndProviderId(final String userId, final String providerId);
// 
//     SocialRedisConnection findOneByUserIdAndProviderIdAndProviderUserId(String userId, String providerId, String providerUserId);
// 
//     // TODO: will be available once DATAKV-135 is finished
//     void deleteByUserIdAndProviderIdAndProviderUserId(final String userId, final String providerId, final String providerUserId);
// 
//     Iterable<SocialRedisConnection> findByUserId(final String userId);
// 
//     Iterable<SocialRedisConnection> findByProviderIdAndProviderUserId(final String providerId, final String providerUserId);
// }


import be.ordina.jworks.social.redis.connection.data.SocialRedisConnection;
import be.ordina.jworks.social.redis.connection.data.SocialRedisConnectionRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.social.connect.*;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
        redisConnection.setExpireTime(data.getExpireTime());

        socialRedisConnectionRepository.save(redisConnection);
    }

    public void removeConnections(String providerId) {
        Iterable<SocialRedisConnection> connections = socialRedisConnectionRepository.findByUserIdAndProviderId(userId, providerId);

        for (SocialRedisConnection redisConnection : connections) {
            socialRedisConnectionRepository.delete(redisConnection);
        }
    }

    public void removeConnection(ConnectionKey connectionKey) {
        // TODO: Wait for DATAKV-135 in order to use this:
        // socialRedisConnectionRepository.deleteByUserIdAndProviderIdAndProviderUserId
        // (userId, connectionKey.getProviderId(), connectionKey.getProviderUserId());

        SocialRedisConnection socialRedisConnection = socialRedisConnectionRepository.findOneByUserIdAndProviderIdAndProviderUserId(userId, connectionKey.getProviderId(), connectionKey.getProviderUserId());
        socialRedisConnectionRepository.delete(socialRedisConnection);
    }

    private final RedisConnectionMapper connectionMapper = new RedisConnectionMapper();

    private final class RedisConnectionMapper {

        Connection<?> mapConnection(final SocialRedisConnection redisConnection) {
            if (redisConnection == null) {
                throw new EmptyResultDataAccessException(1);
            }
// Path: social-redis-spring-boot-starter/src/main/java/be/ordina/jworks/social/redis/connection/data/SocialRedisConnection.java
// @Getter
// @Setter
// @AllArgsConstructor
// @RedisHash("socialRedisConnection")
// public class SocialRedisConnection {
// 
//     @Id
//     @Indexed
//     private String providerUserId;
// 
//     @Indexed
//     private String userId;
// 
//     @Indexed
//     private String providerId;
// 
//     private String displayName;
// 
//     private String profileUrl;
// 
//     private String imageUrl;
// 
//     private String accessToken;
// 
//     private String secret;
// 
//     private String refreshToken;
// 
//     private Long expireTime;
// }
// 
// Path: social-redis-spring-boot-starter/src/main/java/be/ordina/jworks/social/redis/connection/data/SocialRedisConnectionRepository.java
// @Repository
// public interface SocialRedisConnectionRepository extends CrudRepository<SocialRedisConnection, String> {
// 
//     Iterable<SocialRedisConnection> findByProviderId(final String providerId);
// 
//     Iterable<SocialRedisConnection> findByUserIdAndProviderId(final String userId, final String providerId);
// 
//     SocialRedisConnection findOneByUserIdAndProviderIdAndProviderUserId(String userId, String providerId, String providerUserId);
// 
//     // TODO: will be available once DATAKV-135 is finished
//     void deleteByUserIdAndProviderIdAndProviderUserId(final String userId, final String providerId, final String providerUserId);
// 
//     Iterable<SocialRedisConnection> findByUserId(final String userId);
// 
//     Iterable<SocialRedisConnection> findByProviderIdAndProviderUserId(final String providerId, final String providerUserId);
// }


// Path: social-redis-spring-boot-starter/src/main/java/be/ordina/jworks/social/redis/connection/RedisConnectionRepository.java
import be.ordina.jworks.social.redis.connection.data.SocialRedisConnection;
import be.ordina.jworks.social.redis.connection.data.SocialRedisConnectionRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.social.connect.*;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

        redisConnection.setExpireTime(data.getExpireTime());

        socialRedisConnectionRepository.save(redisConnection);
    }

    public void removeConnections(String providerId) {
        Iterable<SocialRedisConnection> connections = socialRedisConnectionRepository.findByUserIdAndProviderId(userId, providerId);

        for (SocialRedisConnection redisConnection : connections) {
            socialRedisConnectionRepository.delete(redisConnection);
        }
    }

    public void removeConnection(ConnectionKey connectionKey) {
        // TODO: Wait for DATAKV-135 in order to use this:
        // socialRedisConnectionRepository.deleteByUserIdAndProviderIdAndProviderUserId
        // (userId, connectionKey.getProviderId(), connectionKey.getProviderUserId());

        SocialRedisConnection socialRedisConnection = socialRedisConnectionRepository.findOneByUserIdAndProviderIdAndProviderUserId(userId, connectionKey.getProviderId(), connectionKey.getProviderUserId());
        socialRedisConnectionRepository.delete(socialRedisConnection);
    }

    private final RedisConnectionMapper connectionMapper = new RedisConnectionMapper();

    private final class RedisConnectionMapper {

        Connection<?> mapConnection(final SocialRedisConnection redisConnection) {
            if (redisConnection == null) {
                throw new EmptyResultDataAccessException(1);
            }
            ConnectionData connectionData = mapConnectionData(redisConnection);