Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. package com.github.scarpex.argus.common.database.mongodb.handler;
  2.  
  3. import com.github.scarpex.argus.Argus;
  4. import com.github.scarpex.argus.common.database.mongodb.DocumentHandler;
  5. import com.github.scarpex.argus.common.database.mongodb.collection.DiscordClient;
  6. import com.github.scarpex.argus.common.language.Language;
  7. import com.mongodb.MongoClient;
  8. import com.mongodb.client.MongoCursor;
  9. import org.bson.Document;
  10.  
  11. /**
  12. * @author Florian Heitzmann
  13. */
  14. public class DiscordClientHandler extends DocumentHandler<String> {
  15.  
  16. public DiscordClientHandler(MongoClient client) {
  17. super(client, Argus.DATABASE, "clients");
  18. loadAll();
  19. }
  20.  
  21. /**
  22. * Returns true if the discord user is already loaded in cache.
  23. *
  24. * @param id the id of the discord user
  25. * @return
  26. */
  27. public boolean contains(String id) {
  28. return getKeys().contains(id);
  29. }
  30.  
  31. /**
  32. * Returns the {@link DiscordClient} object of the discord user.
  33. *
  34. * @param id the id of the discord user
  35. * @return
  36. */
  37. public DiscordClient getDiscordClient(String id) {
  38. return toClass(getDocument("id", id), DiscordClient.class);
  39. }
  40.  
  41. /**
  42. * Creates a {@link DiscordClient} object which get saved and cached.
  43. *
  44. * @param id the id of the discord user
  45. * @param language the language of the discord user
  46. */
  47. public void createDiscordClient(String id, Language language) {
  48. DiscordClient client = new DiscordClient(id, language);
  49. Document document = toDocument(client);
  50. insertDocumentIfNotExists("id", id, document);
  51. cacheDocument(id, document);
  52. }
  53.  
  54. /**
  55. * Puts the {@link DiscordClient} object back to cache and allows to save the
  56. * {@link Document} instantly in the database.
  57. *
  58. * @param client the id of the discord user
  59. * @param saveInDatabase true if the document should saved in database
  60. */
  61. public void updateDiscordClient(DiscordClient client, boolean saveInDatabase) {
  62. Document document = toDocument(client);
  63. cacheDocument(client.getId(), document);
  64.  
  65. if (saveInDatabase) {
  66. updateDocument("id", client.getId(), document);
  67. }
  68. }
  69.  
  70. /**
  71. * Loads all profiles.
  72. */
  73. private void loadAll() {
  74. for (MongoCursor<Document> it = getMongoCollection().find().iterator(); it.hasNext();) {
  75. Document document = it.next();
  76. DiscordClient client = toClass(document, DiscordClient.class);
  77. cacheDocument(client.getId(), document);
  78. }
  79. }
  80.  
  81. /**
  82. * Saves all cached profiles.
  83. */
  84. public void savesAll() {
  85. getValues().forEach(document -> {
  86. DiscordClient client = toClass(document, DiscordClient.class);
  87. updateDocument("id", client.getId(), document);
  88. });
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement