PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/example/ampstutorial/TwitterLoginRecordStoreImpl.java

https://bitbucket.org/atlassian_tutorial/fecru-twitter-tutorial
Java | 93 lines | 77 code | 16 blank | 0 comment | 21 complexity | a95525592450c4ab5afe7b74f91c9222 MD5 | raw file
  1. package com.example.ampstutorial;
  2. import com.atlassian.crucible.spi.data.UserProfileData;
  3. import com.atlassian.crucible.spi.services.ServerException;
  4. import com.atlassian.crucible.spi.services.UserService;
  5. import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
  6. import org.springframework.stereotype.Component;
  7. import javax.annotation.Resource;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. public class TwitterLoginRecordStoreImpl implements TwitterLoginRecordStore {
  12. private final Map<CommitterAndRepoKey,TwitterLoginRecord> byCommitterAndRepository = new HashMap<CommitterAndRepoKey,TwitterLoginRecord>();
  13. private final Map<String,TwitterLoginRecord> byUser = new HashMap<String,TwitterLoginRecord>();
  14. private UserService userService;
  15. private PluginSettingsFactory pluginSettingsFactory;
  16. public TwitterLoginRecordStoreImpl() {
  17. }
  18. public void storeByUsername(String userName, TwitterLoginRecord record) {
  19. TwitterLoginRecord oldValue = byUser.put(userName, record);
  20. if (oldValue != null) {
  21. for (Map.Entry<CommitterAndRepoKey,TwitterLoginRecord> e : byCommitterAndRepository.entrySet()) {
  22. if (e.getValue() == oldValue) {
  23. byCommitterAndRepository.remove(e.getKey());
  24. }
  25. }
  26. }
  27. try {
  28. UserProfileData userProfile = userService.getUserProfile(userName);
  29. for (Map.Entry<String, List<String>> repoCommitters : userProfile.getMappedCommitters().entrySet()) {
  30. for (String committer : repoCommitters.getValue()) {
  31. byCommitterAndRepository.put(new CommitterAndRepoKey(committer, repoCommitters.getKey()), record);
  32. }
  33. }
  34. } catch (ServerException e) {
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. public TwitterLoginRecord getByUsername(String userName) {
  39. return byUser.get(userName);
  40. }
  41. public TwitterLoginRecord getByCommitterAndRepo(String committer, String repoName) {
  42. return byCommitterAndRepository.get(new CommitterAndRepoKey(committer, repoName));
  43. }
  44. @Resource
  45. public void setPluginSettingsFactory(PluginSettingsFactory pluginSettingsFactory) {
  46. System.out.println("Injected " + pluginSettingsFactory);
  47. this.pluginSettingsFactory = pluginSettingsFactory;
  48. }
  49. @Resource
  50. public void setUserService(UserService userService) {
  51. System.out.println("Injected " + userService);
  52. this.userService = userService;
  53. }
  54. private static class CommitterAndRepoKey {
  55. private final String committer;
  56. private final String repoName;
  57. public CommitterAndRepoKey(String committer, String repoName) {
  58. this.committer = committer;
  59. this.repoName = repoName;
  60. }
  61. @Override
  62. public boolean equals(Object o) {
  63. if (this == o) return true;
  64. if (o == null || getClass() != o.getClass()) return false;
  65. CommitterAndRepoKey that = (CommitterAndRepoKey) o;
  66. if (committer != null ? !committer.equals(that.committer) : that.committer != null) return false;
  67. if (repoName != null ? !repoName.equals(that.repoName) : that.repoName != null) return false;
  68. return true;
  69. }
  70. @Override
  71. public int hashCode() {
  72. int result = committer != null ? committer.hashCode() : 0;
  73. result = 31 * result + (repoName != null ? repoName.hashCode() : 0);
  74. return result;
  75. }
  76. }
  77. }