1 package org.varienaja.comments;
2
3 import java.util.Date;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.Set;
9 import java.util.TreeSet;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12 import java.util.regex.PatternSyntaxException;
13
14 import org.apache.log4j.Logger;
15 import org.musicontroller.core.Band;
16 import org.musicontroller.core.Contract_PS;
17 import org.musicontroller.core.Keywordbag;
18 import org.musicontroller.core.Playlist;
19 import org.musicontroller.dao.Dao;
20 import org.musicontroller.model.Linkable;
21 import org.varienaja.util.wikipedia.WikipediaException;
22 import org.varienaja.util.wikipedia.WikipediaSearcher;
23
24
25
26
27
28
29
30 public class CommentService {
31 private static final Logger log = Logger.getLogger(CommentService.class);
32 private static Dao _dao;
33 private static final String plPrefix = "review";
34 private static final String bndPrefix = "band";
35
36 public void setDao(Dao dao) {
37 _dao = dao;
38 }
39
40 public Dao getDao() {
41 return _dao;
42 }
43
44
45
46
47
48
49
50
51 public Set<Comment> getComments(Playlist playlist) {
52 Set<Comment> reviews = CommentDAO.getCommentsFromCache(plPrefix, playlist.getId());
53 if (reviews==null) {
54 reviews = downloadComments(playlist);
55 CommentDAO.saveCommentsToCache(plPrefix, playlist.getId(),reviews);
56 }
57 return reviews;
58 }
59
60
61
62
63
64
65
66
67 public Set<Comment> getComments(Band band) {
68 Set<Comment> reviews = CommentDAO.getCommentsFromCache(bndPrefix, band.getId());
69 if (reviews==null) {
70 reviews = downloadComments(band);
71 CommentDAO.saveCommentsToCache(bndPrefix, band.getId(),reviews);
72 }
73 return reviews;
74 }
75
76
77
78
79
80
81
82 public boolean refreshComments(Playlist playlist) {
83 log.debug("Refreshing comments for playlist: "+playlist.getName());
84 Set<Comment> comments = CommentDAO.getCommentsFromCache(plPrefix, playlist.getId());
85 if (comments==null) {
86 comments = new TreeSet<Comment>();
87 }
88 int oldcount = comments.size();
89 if (comments.addAll(downloadComments(playlist))) {
90 log.debug("Comments refreshed, we had "+oldcount+" reviews, now we have "+comments.size());
91 for (Comment review : comments) {
92 enhanceComment(review, playlist);
93 }
94 CommentDAO.saveCommentsToCache(plPrefix, playlist.getId(),comments);
95 return true;
96 } else {
97 log.debug("No new comments found online.");
98 return false;
99 }
100 }
101
102
103
104
105
106
107 private Set<Comment> downloadComments(Playlist playlist) {
108 Set<Comment> comments = new TreeSet<Comment>();
109
110 AmazonCommentService amazonService = new AmazonCommentService();
111 comments.addAll(amazonService.getComments(playlist));
112
113 return comments;
114 }
115
116
117
118
119
120
121 private Set<Comment> downloadComments(Band band) {
122 Set<Comment> reviews = new TreeSet<Comment>();
123
124 String source = WikipediaSearcher.getExternalBandURL(band.getName());
125 Comment comment = new Comment(source, "", new Date());
126 String content = "";
127 try {
128 content = WikipediaSearcher.getBandInfo(band.getName());
129 } catch (WikipediaException e) {
130 log.error("Could not get comment for band: "+band.getName()+" from Wikipedia: "+e);
131 }
132 CommentElement elt = new CommentElement();
133 elt.setText(content);
134 elt.setType('T');
135 comment.addElement(elt);
136
137 reviews.add(comment);
138 return reviews;
139 }
140
141
142
143
144
145
146
147 protected void enhanceComment(Comment review, Playlist playlist) {
148 log.debug("Enhancing comment: "+review.getTitle());
149
150 StringBuilder sb = new StringBuilder();
151 for (CommentElement elt : review.getElements()) {
152 sb.append(elt.getText());
153 sb.append(" ");
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168 Set<Linkable>[] mappings = new Set[8];
169 mappings[0] = getBands(playlist);
170 mappings[1] = getSongs(playlist);
171 mappings[2] = getPlaylists(playlist);
172 mappings[3] = getAllSongs(playlist);
173 mappings[4] = getArtists(playlist);
174 mappings[5] = getKeywords(playlist);
175 mappings[6] = getAllKeywords();
176 mappings[7] = getAllBands();
177
178 for (Set<Linkable> mapping : mappings) {
179 Iterator<Linkable> it = mapping.iterator();
180 while (it.hasNext()) {
181 Linkable link = it.next();
182 try {
183 Pattern p = Pattern.compile("\\b\\Q" + link.getName() + "\\E\\b");
184
185 boolean somethingHyperlinked = true;
186 while (somethingHyperlinked) {
187
188
189 CommentElement[] all = new CommentElement[0];
190 all = review.getElements().toArray(all);
191 somethingHyperlinked = false;
192 for (CommentElement elt : all) {
193 if (elt.isText()) {
194 String txt = elt.getText();
195
196
197
198 Matcher matcher = p.matcher(txt.toLowerCase());
199 if (matcher.find()) {
200 int index = matcher.start();
201 List<CommentElement> newElts = new LinkedList<CommentElement>();
202
203 CommentElement before = new CommentElement();
204 before.setType('T');
205 before.setText(txt.substring(0,index));
206
207 CommentElement changed = new CommentElement();
208 changed.setType(link.getType().charAt(0));
209 changed.setText(link.getName());
210 changed.setId(link.getId());
211
212 CommentElement after = new CommentElement();
213 after.setType('T');
214 after.setText(txt.substring(index+link.getName().length()));
215 log.debug("Enhanced review with: "+changed.toString());
216
217 newElts.add(before);
218 newElts.add(changed);
219 newElts.add(after);
220
221 review.splitElement(elt, newElts);
222 somethingHyperlinked = true;
223 }
224 }
225 }
226 }
227 } catch (PatternSyntaxException e) {
228 log.error(e.getMessage());
229 } catch (Exception e) {
230 log.error(e.getMessage());
231 } finally {
232 it.remove();
233 }
234 }
235 }
236 log.debug("Enhancing comment done.");
237 }
238
239
240
241
242
243 private Set<Linkable> getAllBands() {
244 return new HashSet<Linkable>();
245 }
246
247
248
249
250
251 private Set<Linkable> getAllKeywords() {
252 return new HashSet<Linkable>();
253 }
254
255
256
257
258
259
260 private Set<Linkable> getKeywords(Playlist playlist) {
261 Set<Linkable> result = new HashSet<Linkable>();
262 for (Contract_PS cps : playlist.getSongs()) {
263 Keywordbag kwb = cps.getSong().getKeywordbag();
264 if (kwb!=null) {
265 result.addAll(kwb.getKeywords());
266 }
267 }
268 return result;
269 }
270
271
272
273
274
275
276 private Set<Linkable> getArtists(Playlist playlist) {
277 return new HashSet<Linkable>();
278 }
279
280
281
282
283
284
285 private Set<Linkable> getAllSongs(Playlist playlist) {
286 Set<Linkable> result = new HashSet<Linkable>();
287 for (Linkable band : getBands(playlist)) {
288 Playlist allSongs = getDao().songsByBand(band.getId());
289 for (Contract_PS cps : allSongs.getSongs()) {
290 result.add(cps.getSong());
291 }
292 }
293 return result;
294 }
295
296
297
298
299
300
301 private Set<Linkable> getPlaylists(Playlist playlist) {
302 Set<Linkable> result = new HashSet<Linkable>();
303 for (Linkable band : getBands(playlist)) {
304 result.addAll(getDao().listPlaylists((Band) band));
305 }
306 return result;
307 }
308
309
310
311
312
313
314 private Set<Linkable> getSongs(Playlist playlist) {
315 Set<Linkable> result = new HashSet<Linkable>();
316 for (Contract_PS cps : playlist.getSongs()) {
317 result.add(cps.getSong());
318 }
319 return result;
320 }
321
322
323
324
325
326
327 private Set<Linkable> getBands(Playlist playlist) {
328 Set<Linkable> result = new HashSet<Linkable>();
329 for (Contract_PS cps : playlist.getSongs()) {
330 result.add(cps.getSong().getBand());
331 }
332 return result;
333 }
334
335 }