/src/main/java/com/ciphertool/sentencebuilder/dao/WordDao.java

https://github.com/beldenge/SentenceBuilder · Java · 211 lines · 102 code · 43 blank · 66 comment · 13 complexity · c11e1b13e329d09bd642e10ad67da424 MD5 · raw file

  1. /**
  2. * Copyright 2015 George Belden
  3. *
  4. * This file is part of SentenceBuilder.
  5. *
  6. * SentenceBuilder is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation, either version 3 of the License, or (at your option) any
  9. * later version.
  10. *
  11. * SentenceBuilder is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. * details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * SentenceBuilder. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.ciphertool.sentencebuilder.dao;
  20. import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
  21. import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
  22. import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
  23. import java.util.ArrayList;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.springframework.beans.factory.annotation.Required;
  30. import org.springframework.data.domain.Sort;
  31. import org.springframework.data.mongodb.core.MongoOperations;
  32. import org.springframework.data.mongodb.core.aggregation.Aggregation;
  33. import org.springframework.data.mongodb.core.aggregation.AggregationResults;
  34. import org.springframework.data.mongodb.core.query.Criteria;
  35. import org.springframework.data.mongodb.core.query.Query;
  36. import org.springframework.data.mongodb.core.query.Update;
  37. import com.ciphertool.sentencebuilder.entities.Word;
  38. public class WordDao {
  39. private Logger log = LoggerFactory.getLogger(getClass());
  40. private MongoOperations mongoOperations;
  41. /**
  42. * Returns a list of all Words, so words will be duplicated if they have multiple parts of speech.
  43. */
  44. public List<Word> findAll() {
  45. List<Word> result = mongoOperations.findAll(Word.class);
  46. return result;
  47. }
  48. /**
  49. * Returns a list of top N Words, and words can be duplicated if they have multiple parts of speech.
  50. */
  51. public List<Word> findTopByFrequency(int top) {
  52. Query query = new Query().limit(top).with(new Sort(Sort.Direction.DESC, "frequencyWeight"));
  53. List<Word> result = mongoOperations.find(query, Word.class);
  54. return result;
  55. }
  56. /**
  57. * Returns a list of all unique Words, irrespective of parts of speech.
  58. */
  59. public List<Word> findAllUniqueWords() {
  60. Query query = new Query();
  61. query.fields().include("word");
  62. query.fields().include("frequencyWeight");
  63. Set<Word> result = new HashSet<Word>(mongoOperations.find(query, Word.class));
  64. // We have to convert between set and list in order to achieve "distinct" functionality not supported by mongoOperations
  65. return new ArrayList<Word>(result);
  66. }
  67. /**
  68. * Returns a list of top N unique Words, irrespective of parts of speech.
  69. */
  70. public List<Word> findTopUniqueWordsByFrequency(int top) {
  71. Aggregation aggregation = Aggregation.newAggregation(sort(Sort.Direction.DESC, "frequencyWeight"), group("word", "frequencyWeight"), limit(top));
  72. AggregationResults<Word> result = mongoOperations.aggregate(aggregation, Word.class, Word.class);
  73. return result.getMappedResults();
  74. }
  75. /**
  76. * Finds all occurrences of a particular Word by its String value.
  77. *
  78. * @param word
  79. * the String value of the word to find
  80. * @return the List of matching Words
  81. */
  82. public List<Word> findByWordString(String word) {
  83. if (word == null) {
  84. log.warn("Attempted to find Word by null String. Unable to continue, thus returning null.");
  85. return null;
  86. }
  87. Query query = new Query();
  88. query.addCriteria(Criteria.where("word").is(word));
  89. List<Word> words = mongoOperations.find(query, Word.class);
  90. return words;
  91. }
  92. /**
  93. * Inserts a word.
  94. *
  95. * @param word
  96. * the Word to insert
  97. * @return whether the insert was successful
  98. */
  99. public boolean insert(Word word) {
  100. if (word == null) {
  101. log.warn("Attempted to insert null Word. Unable to continue, thus returning false.");
  102. return false;
  103. }
  104. mongoOperations.insert(word);
  105. return true;
  106. }
  107. /**
  108. * Inserts a List of Words in batch.
  109. *
  110. * @param wordBatch
  111. * the batch of Words to insert
  112. * @return whether the insert was successful
  113. */
  114. public boolean insertBatch(List<Word> wordBatch) {
  115. if (wordBatch == null || wordBatch.isEmpty()) {
  116. log.warn("Attempted to insert Words in batch which was found to be null or empty. Unable to continue, thus returning false.");
  117. return false;
  118. }
  119. mongoOperations.insert(wordBatch, Word.class);
  120. return true;
  121. }
  122. /**
  123. * Updates a Word.
  124. *
  125. * @param word
  126. * the Word to update
  127. * @return whether the update was successful
  128. */
  129. public boolean update(Word word) {
  130. if (word == null) {
  131. log.warn("Attempted to update null Word. Unable to continue, thus returning false.");
  132. return false;
  133. }
  134. Query query = new Query();
  135. query.addCriteria(Criteria.where("word").is(word.getWord()));
  136. query.addCriteria(Criteria.where("partOfSpeech").is(word.getPartOfSpeech()));
  137. Update update = new Update();
  138. update.set("frequencyWeight", word.getFrequencyWeight());
  139. mongoOperations.updateFirst(query, update, Word.class);
  140. return true;
  141. }
  142. /**
  143. * Updates a List of Words in batch.
  144. *
  145. * @param wordBatch
  146. * the batch of Words
  147. * @return whether the update was successful
  148. */
  149. public boolean updateBatch(List<Word> wordBatch) {
  150. if (wordBatch == null || wordBatch.isEmpty()) {
  151. log.warn("Attempted to update Words in batch which was found to be null or empty. Unable to continue, thus returning false.");
  152. return false;
  153. }
  154. for (Word word : wordBatch) {
  155. Query query = new Query();
  156. query.addCriteria(Criteria.where("word").is(word.getWord()));
  157. query.addCriteria(Criteria.where("partOfSpeech").is(word.getPartOfSpeech()));
  158. Update update = new Update();
  159. update.set("frequencyWeight", word.getFrequencyWeight());
  160. mongoOperations.updateFirst(query, update, Word.class);
  161. }
  162. return true;
  163. }
  164. @Required
  165. public void setMongoTemplate(MongoOperations mongoOperations) {
  166. this.mongoOperations = mongoOperations;
  167. }
  168. }