PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/wuffy-bot/src/main/java/net/wuffy/bot/database/mongo/impl/MongoDBGuild.java

https://bitbucket.org/wuffy-official/bot
Java | 265 lines | 178 code | 69 blank | 18 comment | 5 complexity | 10a7a35b51f6aeb436e8aef5cc37086c MD5 | raw file
Possible License(s): Apache-2.0
  1. package net.wuffy.bot.database.mongo.impl;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import org.bson.Document;
  5. import org.bson.conversions.Bson;
  6. import com.google.gson.Gson;
  7. import com.google.gson.GsonBuilder;
  8. import com.mongodb.client.MongoCollection;
  9. import com.mongodb.client.model.Filters;
  10. import com.mongodb.client.model.InsertOneModel;
  11. import com.mongodb.client.model.UpdateOneModel;
  12. import net.dv8tion.jda.core.entities.Guild;
  13. import net.wuffy.bot.database.DBGuild;
  14. import net.wuffy.bot.database.mongo.MongoStorageImpl;
  15. import net.wuffy.core.Core;
  16. import net.wuffy.core.database.mongo.MongoBulkWriteSystem;
  17. public class MongoDBGuild extends DBGuild {
  18. private static final Gson GSON = new GsonBuilder().create();
  19. private static MongoCollection<Document> collection;
  20. private static MongoBulkWriteSystem bulkWriteSystem;
  21. private MongoStorageImpl storage;
  22. private MongoGuildCache guildCache;
  23. private Bson filter;
  24. public MongoDBGuild(MongoStorageImpl storage, Core core, Guild guild) {
  25. super(core, guild);
  26. this.storage = storage;
  27. if(MongoDBGuild.collection == null) {
  28. MongoDBGuild.collection = this.storage.getStorage().getCollection("guild");
  29. MongoDBGuild.bulkWriteSystem = this.storage.getBulkWriteSystemAdapter().get("guild");
  30. MongoDBGuild.bulkWriteSystem.enableBulkWrite(MongoDBGuild.collection);
  31. }
  32. this.filter = Filters.eq("_guildId", Long.toString(this.getIdLong()));
  33. Document found = MongoDBGuild.collection.find(this.filter).first();
  34. if(found == null) {
  35. this.guildCache = new MongoGuildCache(this.getIdLong()).construct();
  36. MongoDBGuild.bulkWriteSystem.queueBulkModel(new InsertOneModel<Document>(Document.parse(MongoDBGuild.GSON.toJson(this.guildCache))));
  37. } else {
  38. this.guildCache = MongoDBGuild.GSON.fromJson(found.toJson(), MongoGuildCache.class);
  39. this.filter = Filters.eq("_id", this.guildCache._id);
  40. }
  41. }
  42. public void queueBulk(Document update) {
  43. this.queueBulk(this.filter, update);
  44. }
  45. public void queueBulk(Bson filter, Document update) {
  46. MongoDBGuild.bulkWriteSystem.queueBulkModel(new UpdateOneModel<Document>(filter, update));
  47. }
  48. public void queueBulk(Document filter, Document update) {
  49. MongoDBGuild.bulkWriteSystem.queueBulkModel(new UpdateOneModel<Document>(filter, update));
  50. }
  51. public Document toDocument(Object object) {
  52. return Document.parse(MongoDBGuild.GSON.toJson(object));
  53. }
  54. @Override
  55. public void destroy() {
  56. this.storage = null;
  57. this.guildCache = null;
  58. this.filter = null;
  59. }
  60. @Override
  61. public boolean isMention() {
  62. return this.guildCache.command.mention;
  63. }
  64. @Override
  65. public void setMention(boolean mention) {
  66. this.guildCache.command.mention = mention;
  67. this.queueBulk(new Document("$set", new Document("command.mention", mention)));
  68. }
  69. @Override
  70. public List<String> getPrefixes() {
  71. return this.guildCache.command.prefixes;
  72. }
  73. @Override
  74. public void addPrefix(String prefix) {
  75. this.guildCache.command.prefixes.add(prefix);
  76. this.queueBulk(new Document("$addToSet", new Document("command.prefixes", prefix)));
  77. }
  78. @Override
  79. public void removePrefix(String prefix) {
  80. this.guildCache.command.prefixes.remove(prefix);
  81. this.queueBulk(new Document("$pull", new Document("command.prefixes", prefix)));
  82. }
  83. @Override
  84. public void setPrefix(String... prefix) {
  85. List<String> prefixes = Arrays.asList(prefix);
  86. this.guildCache.command.prefixes = prefixes;
  87. this.queueBulk(new Document("$set", new Document("command.prefixes", prefixes)));
  88. }
  89. @Override
  90. public boolean isDeleteExecuterMessage() {
  91. return this.guildCache.message.deleteExecuter;
  92. }
  93. @Override
  94. public void setDeleteExecuterMessage(boolean delete) {
  95. this.guildCache.message.deleteExecuter = delete;
  96. this.queueBulk(new Document("$set", new Document("message.deleteExecuter", delete)));
  97. }
  98. @Override
  99. public boolean isDeleteBotMessage() {
  100. return this.guildCache.message.deleteBot;
  101. }
  102. @Override
  103. public void setDeleteBotMessage(boolean delete) {
  104. this.guildCache.message.deleteBot = delete;
  105. this.queueBulk(new Document("$set", new Document("message.deleteBot", delete)));
  106. }
  107. @Override
  108. public List<EnumPermissionMode> getPermissionMode() {
  109. // TODO Auto-generated method stub
  110. return null;
  111. }
  112. @Override
  113. public void addPermissionMode(EnumPermissionMode... permissionMode) {
  114. // TODO Auto-generated method stub
  115. }
  116. @Override
  117. public void removePermissionMode(EnumPermissionMode... permissionMode) {
  118. // TODO Auto-generated method stub
  119. }
  120. @Override
  121. public void setPermissionMode(List<EnumPermissionMode> permissionModeList) {
  122. // TODO Auto-generated method stub
  123. }
  124. @Override
  125. public List<String> getPermissionChannel(EnumPermissionType type, Long channelId, String... ids) {
  126. // TODO Auto-generated method stub
  127. return null;
  128. }
  129. @Override
  130. public void setPermissionChannel(EnumPermissionType type, Long channelId, String id, String... permissions) {
  131. // TODO Auto-generated method stub
  132. }
  133. @Override
  134. public void setPermissionChannel(EnumPermissionType type, Long channelId, List<String> ids, String... permissions) {
  135. // TODO Auto-generated method stub
  136. }
  137. @Override
  138. public void addPermissionChannel(EnumPermissionType type, Long channelId, String id, String... permissions) {
  139. // TODO Auto-generated method stub
  140. }
  141. @Override
  142. public void addPermissionChannel(EnumPermissionType type, Long channelId, List<String> ids, String... permissions) {
  143. // TODO Auto-generated method stub
  144. }
  145. @Override
  146. public void removePermissionChannel(EnumPermissionType type, Long channelId, String id, String... permissions) {
  147. // TODO Auto-generated method stub
  148. }
  149. @Override
  150. public void removePermissionChannel(EnumPermissionType type, Long channelId, List<String> ids,
  151. String... permissions) {
  152. // TODO Auto-generated method stub
  153. }
  154. @Override
  155. public List<String> getPermissionGlobal(EnumPermissionType type, String... ids) {
  156. // TODO Auto-generated method stub
  157. return null;
  158. }
  159. @Override
  160. public void setPermissionGlobal(EnumPermissionType type, String id, String... permissions) {
  161. // TODO Auto-generated method stub
  162. }
  163. @Override
  164. public void setPermissionGlobal(EnumPermissionType type, List<String> ids, String... permissions) {
  165. // TODO Auto-generated method stub
  166. }
  167. @Override
  168. public void addPermissionGlobal(EnumPermissionType type, String id, String... permissions) {
  169. // TODO Auto-generated method stub
  170. }
  171. @Override
  172. public void addPermissionGlobal(EnumPermissionType type, List<String> ids, String... permissions) {
  173. // TODO Auto-generated method stub
  174. }
  175. @Override
  176. public void removePermissionGlobal(EnumPermissionType type, String id, String... permissions) {
  177. // TODO Auto-generated method stub
  178. }
  179. @Override
  180. public void removePermissionGlobal(EnumPermissionType type, List<String> ids, String... permissions) {
  181. // TODO Auto-generated method stub
  182. }
  183. @Override
  184. public Boolean isPartner() {
  185. return this.guildCache.partner;
  186. }
  187. @Override
  188. public void setPartner(boolean partner) {
  189. this.guildCache.partner = partner;
  190. this.queueBulk(new Document("$set", new Document("partner", partner)));
  191. }
  192. @Override
  193. public void deleteFromDatabase() {
  194. MongoDBGuild.collection.findOneAndDelete(this.filter);
  195. }
  196. }