|
| 1 | +package com.baeldung.tagging; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | +import java.util.stream.StreamSupport; |
| 8 | + |
| 9 | +import org.bson.Document; |
| 10 | + |
| 11 | +import com.mongodb.BasicDBObject; |
| 12 | +import com.mongodb.DBCollection; |
| 13 | +import com.mongodb.MongoClient; |
| 14 | +import com.mongodb.client.FindIterable; |
| 15 | +import com.mongodb.client.MongoCollection; |
| 16 | +import com.mongodb.client.MongoDatabase; |
| 17 | +import com.mongodb.client.model.Filters; |
| 18 | +import com.mongodb.client.model.Updates; |
| 19 | +import com.mongodb.client.result.UpdateResult; |
| 20 | + |
| 21 | +/** |
| 22 | + * Repository used to manage tags for a blog post. |
| 23 | + * |
| 24 | + * @author Donato Rimenti |
| 25 | + * |
| 26 | + */ |
| 27 | +public class TagRepository implements Closeable { |
| 28 | + |
| 29 | + /** |
| 30 | + * Document field which holds the blog tags. |
| 31 | + */ |
| 32 | + private static final String TAGS_FIELD = "tags"; |
| 33 | + |
| 34 | + /** |
| 35 | + * The post collection. |
| 36 | + */ |
| 37 | + private MongoCollection<Document> collection; |
| 38 | + |
| 39 | + /** |
| 40 | + * The MongoDB client, stored in a field in order to close it later. |
| 41 | + */ |
| 42 | + private MongoClient mongoClient; |
| 43 | + |
| 44 | + /** |
| 45 | + * Instantiates a new TagRepository by opening the DB connection. |
| 46 | + */ |
| 47 | + public TagRepository() { |
| 48 | + mongoClient = new MongoClient("localhost", 27018); |
| 49 | + MongoDatabase database = mongoClient.getDatabase("blog"); |
| 50 | + collection = database.getCollection("posts"); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a list of posts which contains one or more of the tags passed as |
| 55 | + * argument. |
| 56 | + * |
| 57 | + * @param tags |
| 58 | + * a list of tags |
| 59 | + * @return a list of posts which contains at least one of the tags passed as |
| 60 | + * argument |
| 61 | + */ |
| 62 | + public List<Post> postsWithAtLeastOneTag(String... tags) { |
| 63 | + FindIterable<Document> results = collection.find(Filters.in(TAGS_FIELD, tags)); |
| 64 | + return StreamSupport.stream(results.spliterator(), false).map(TagRepository::documentToPost) |
| 65 | + .collect(Collectors.toList()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a list of posts which contains all the tags passed as argument. |
| 70 | + * |
| 71 | + * @param tags |
| 72 | + * a list of tags |
| 73 | + * @return a list of posts which contains all the tags passed as argument |
| 74 | + */ |
| 75 | + public List<Post> postsWithAllTags(String... tags) { |
| 76 | + FindIterable<Document> results = collection.find(Filters.all(TAGS_FIELD, tags)); |
| 77 | + return StreamSupport.stream(results.spliterator(), false).map(TagRepository::documentToPost) |
| 78 | + .collect(Collectors.toList()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns a list of posts which contains none of the tags passed as |
| 83 | + * argument. |
| 84 | + * |
| 85 | + * @param tags |
| 86 | + * a list of tags |
| 87 | + * @return a list of posts which contains none of the tags passed as |
| 88 | + * argument |
| 89 | + */ |
| 90 | + public List<Post> postsWithoutTags(String... tags) { |
| 91 | + FindIterable<Document> results = collection.find(Filters.nin(TAGS_FIELD, tags)); |
| 92 | + return StreamSupport.stream(results.spliterator(), false).map(TagRepository::documentToPost) |
| 93 | + .collect(Collectors.toList()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Adds a list of tags to the blog post with the given title. |
| 98 | + * |
| 99 | + * @param title |
| 100 | + * the title of the blog post |
| 101 | + * @param tags |
| 102 | + * a list of tags to add |
| 103 | + * @return the outcome of the operation |
| 104 | + */ |
| 105 | + public boolean addTags(String title, List<String> tags) { |
| 106 | + UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title), |
| 107 | + Updates.addEachToSet(TAGS_FIELD, tags)); |
| 108 | + return result.getModifiedCount() == 1; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Removes a list of tags to the blog post with the given title. |
| 113 | + * |
| 114 | + * @param title |
| 115 | + * the title of the blog post |
| 116 | + * @param tags |
| 117 | + * a list of tags to remove |
| 118 | + * @return the outcome of the operation |
| 119 | + */ |
| 120 | + public boolean removeTags(String title, List<String> tags) { |
| 121 | + UpdateResult result = collection.updateOne(new BasicDBObject(DBCollection.ID_FIELD_NAME, title), |
| 122 | + Updates.pullAll(TAGS_FIELD, tags)); |
| 123 | + return result.getModifiedCount() == 1; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Utility method used to map a MongoDB document into a {@link Post}. |
| 128 | + * |
| 129 | + * @param document |
| 130 | + * the document to map |
| 131 | + * @return a {@link Post} object equivalent to the document passed as |
| 132 | + * argument |
| 133 | + */ |
| 134 | + @SuppressWarnings("unchecked") |
| 135 | + private static Post documentToPost(Document document) { |
| 136 | + Post post = new Post(); |
| 137 | + post.setTitle(document.getString(DBCollection.ID_FIELD_NAME)); |
| 138 | + post.setArticle(document.getString("article")); |
| 139 | + post.setAuthor(document.getString("author")); |
| 140 | + post.setTags((List<String>) document.get(TAGS_FIELD)); |
| 141 | + return post; |
| 142 | + } |
| 143 | + |
| 144 | + /* |
| 145 | + * (non-Javadoc) |
| 146 | + * |
| 147 | + * @see java.io.Closeable#close() |
| 148 | + */ |
| 149 | + @Override |
| 150 | + public void close() throws IOException { |
| 151 | + mongoClient.close(); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments