Skip to content

Commit 53ec7a8

Browse files
committed
BAEL-1591: Tagging implementation with MongoDB.
1 parent 67c0572 commit 53ec7a8

3 files changed

Lines changed: 422 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.
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.MongoClient;
12+
import com.mongodb.client.FindIterable;
13+
import com.mongodb.client.MongoCollection;
14+
import com.mongodb.client.MongoDatabase;
15+
import com.mongodb.client.model.Filters;
16+
17+
/**
18+
* Repository used to manage tags for a blog post.
19+
*
20+
* @author Donato Rimenti
21+
*
22+
*/
23+
public class TagRepository implements Closeable {
24+
25+
/**
26+
* Document field which holds the blog tags.
27+
*/
28+
private static final String TAGS_FIELD = "tags";
29+
30+
/**
31+
* The post collection.
32+
*/
33+
private MongoCollection<Document> collection;
34+
35+
/**
36+
* The MongoDB client, stored in a field in order to close it later.
37+
*/
38+
private MongoClient mongoClient;
39+
40+
/**
41+
* Instantiates a new TagRepository by opening the DB connection.
42+
*/
43+
public TagRepository() {
44+
mongoClient = new MongoClient("localhost", 27018);
45+
MongoDatabase database = mongoClient.getDatabase("blog");
46+
collection = database.getCollection("posts");
47+
}
48+
49+
/**
50+
* Returns a list of posts which contains one or more of the tags passed as
51+
* argument.
52+
*
53+
* @param tags
54+
* a list of tags
55+
* @return a list of posts which contains at least one of the tags passed as
56+
* argument
57+
*/
58+
public List<Post> postsWithAtLeastOneTag(String... tags) {
59+
FindIterable<Document> results = collection.find(Filters.in(TAGS_FIELD, tags));
60+
return StreamSupport.stream(results.spliterator(), false).map(TagRepository::documentToPost)
61+
.collect(Collectors.toList());
62+
}
63+
64+
/**
65+
* Returns a list of posts which contains all the tags passed as argument.
66+
*
67+
* @param tags
68+
* a list of tags
69+
* @return a list of posts which contains all the tags passed as argument
70+
*/
71+
public List<Post> postsWithAllTags(String... tags) {
72+
FindIterable<Document> results = collection.find(Filters.all(TAGS_FIELD, tags));
73+
return StreamSupport.stream(results.spliterator(), false).map(TagRepository::documentToPost)
74+
.collect(Collectors.toList());
75+
}
76+
77+
/**
78+
* Returns a list of posts which contains none of the tags passed as
79+
* argument.
80+
*
81+
* @param tags
82+
* a list of tags
83+
* @return a list of posts which contains none of the tags passed as
84+
* argument
85+
*/
86+
public List<Post> postsWithoutTags(String... tags) {
87+
FindIterable<Document> results = collection.find(Filters.nin(TAGS_FIELD, tags));
88+
return StreamSupport.stream(results.spliterator(), false).map(TagRepository::documentToPost)
89+
.collect(Collectors.toList());
90+
}
91+
92+
/**
93+
* Utility method used to map a MongoDB document into a {@link Post}.
94+
*
95+
* @param document
96+
* the document to map
97+
* @return a {@link Post} object equivalent to the document passed as
98+
* argument
99+
*/
100+
@SuppressWarnings("unchecked")
101+
private static Post documentToPost(Document document) {
102+
Post post = new Post();
103+
post.setArticle(document.getString("article"));
104+
post.setAuthor(document.getString("author"));
105+
post.setTitle(document.getString("title"));
106+
post.setTags((List<String>) document.get(TAGS_FIELD));
107+
return post;
108+
}
109+
110+
/*
111+
* (non-Javadoc)
112+
*
113+
* @see java.io.Closeable#close()
114+
*/
115+
@Override
116+
public void close() throws IOException {
117+
mongoClient.close();
118+
}
119+
120+
}

0 commit comments

Comments
 (0)