forked from roghughe/captaindebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugUsersConnectionRepository.java
97 lines (78 loc) · 2.95 KB
/
DebugUsersConnectionRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright 2012 Marin Solutions
*/
package com.captaindebug.social.facebookposts.implementation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionKey;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
/**
* This is a demo class, responsible for showing how the app's users are connected to Spring
* social without all the tedious mucking about with JDBC and SQL databases.
*
* @author Roger
*
*/
public class DebugUsersConnectionRepository implements UsersConnectionRepository {
private static final Logger logger = LoggerFactory
.getLogger(DebugUsersConnectionRepository.class);
/** Map the user id to the connection (one user has one connection) */
private final Map<String, ConnectionRepository> repositoryMap = new HashMap<String, ConnectionRepository>();
/**
* Suggest that this loops through the map values, getting the connection respositories. If
* this connection is in the repo then add the repos user id to the list
*/
@Override
public List<String> findUserIdsWithConnection(Connection<?> connection) {
logger.info("Finding users Ids with connection: " + connection.getKey().toString());
List<String> userIds = new ArrayList<String>();
ConnectionKey connectionKey = connection.getKey();
String providerId = connectionKey.getProviderId();
Set<String> keys = repositoryMap.keySet();
for (String userId : keys) {
ConnectionRepository repo = repositoryMap.get(userId);
List<Connection<?>> connections = repo.findConnections(providerId);
for (Connection<?> con : connections) {
if (con.equals(connection)) {
userIds.add(userId);
}
}
}
return userIds;
}
/**
* @see org.springframework.social.connect.UsersConnectionRepository#findUserIdsConnectedTo(java.lang.String,
* java.util.Set)
*/
@Override
public Set<String> findUserIdsConnectedTo(String providerId, Set<String> providerUserIds) {
StringBuilder sb = new StringBuilder("Finding users Ids connected to: " + providerId);
for (String providerUserId : providerUserIds) {
sb.append("\nProviderUserId: ");
sb.append(providerUserId);
}
logger.info(sb.toString());
return Collections.emptySet();
}
/**
* @see org.springframework.social.connect.UsersConnectionRepository#createConnectionRepository(java.lang.String)
*/
@Override
public ConnectionRepository createConnectionRepository(String userId) {
logger.info("Creating/Getting connection repository for user: " + userId);
ConnectionRepository repository = repositoryMap.get(userId);
if (repository == null) {
repository = new DebugConnectionRespository();
repositoryMap.put(userId, repository);
}
return repository;
}
}