Skip to content

Commit 8cb6225

Browse files
authored
Merge pull request eugenp#3861 from aurasphere/BAEL-1591-mongodb-tagging
Bael 1591 mongodb tagging
2 parents b8d9a18 + d192cf8 commit 8cb6225

3 files changed

Lines changed: 503 additions & 0 deletions

File tree

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.baeldung.tagging;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Model for a blog post.
7+
*
8+
* @author Donato Rimenti
9+
*
10+
*/
11+
public class Post {
12+
13+
/**
14+
* Title of the post. Must be unique.
15+
*/
16+
private String title;
17+
18+
/**
19+
* Full article.
20+
*/
21+
private String article;
22+
23+
/**
24+
* Author of the post.
25+
*/
26+
private String author;
27+
28+
/**
29+
* Tags of the post.
30+
*/
31+
private List<String> tags;
32+
33+
/**
34+
* Gets the {@link #title}.
35+
*
36+
* @return the {@link #title}
37+
*/
38+
public String getTitle() {
39+
return title;
40+
}
41+
42+
/**
43+
* Sets the {@link #title}.
44+
*
45+
* @param title
46+
* the new {@link #title}
47+
*/
48+
public void setTitle(String title) {
49+
this.title = title;
50+
}
51+
52+
/**
53+
* Gets the {@link #article}.
54+
*
55+
* @return the {@link #article}
56+
*/
57+
public String getArticle() {
58+
return article;
59+
}
60+
61+
/**
62+
* Sets the {@link #article}.
63+
*
64+
* @param article
65+
* the new {@link #article}
66+
*/
67+
public void setArticle(String article) {
68+
this.article = article;
69+
}
70+
71+
/**
72+
* Gets the {@link #author}.
73+
*
74+
* @return the {@link #author}
75+
*/
76+
public String getAuthor() {
77+
return author;
78+
}
79+
80+
/**
81+
* Sets the {@link #author}.
82+
*
83+
* @param author
84+
* the new {@link #author}
85+
*/
86+
public void setAuthor(String author) {
87+
this.author = author;
88+
}
89+
90+
/**
91+
* Gets the {@link #tags}.
92+
*
93+
* @return the {@link #tags}
94+
*/
95+
public List<String> getTags() {
96+
return tags;
97+
}
98+
99+
/**
100+
* Sets the {@link #tags}.
101+
*
102+
* @param tags
103+
* the new {@link #tags}
104+
*/
105+
public void setTags(List<String> tags) {
106+
this.tags = tags;
107+
}
108+
109+
/*
110+
* (non-Javadoc)
111+
*
112+
* @see java.lang.Object#hashCode()
113+
*/
114+
@Override
115+
public int hashCode() {
116+
final int prime = 31;
117+
int result = 1;
118+
result = prime * result + ((article == null) ? 0 : article.hashCode());
119+
result = prime * result + ((author == null) ? 0 : author.hashCode());
120+
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
121+
result = prime * result + ((title == null) ? 0 : title.hashCode());
122+
return result;
123+
}
124+
125+
/*
126+
* (non-Javadoc)
127+
*
128+
* @see java.lang.Object#equals(java.lang.Object)
129+
*/
130+
@Override
131+
public boolean equals(Object obj) {
132+
if (this == obj)
133+
return true;
134+
if (obj == null)
135+
return false;
136+
if (getClass() != obj.getClass())
137+
return false;
138+
Post other = (Post) obj;
139+
if (article == null) {
140+
if (other.article != null)
141+
return false;
142+
} else if (!article.equals(other.article))
143+
return false;
144+
if (author == null) {
145+
if (other.author != null)
146+
return false;
147+
} else if (!author.equals(other.author))
148+
return false;
149+
if (tags == null) {
150+
if (other.tags != null)
151+
return false;
152+
} else if (!tags.equals(other.tags))
153+
return false;
154+
if (title == null) {
155+
if (other.title != null)
156+
return false;
157+
} else if (!title.equals(other.title))
158+
return false;
159+
return true;
160+
}
161+
162+
/*
163+
* (non-Javadoc)
164+
*
165+
* @see java.lang.Object#toString()
166+
*/
167+
@Override
168+
public String toString() {
169+
return "Post [title=" + title + ", article=" + article + ", author=" + author + ", tags=" + tags + "]";
170+
}
171+
172+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)