Remove javadoc comments from overriding methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
1 /*
2  * Sone - SearchPage.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.concurrent.TimeUnit;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Profile;
34 import net.pterodactylus.sone.data.Profile.Field;
35 import net.pterodactylus.sone.data.Reply;
36 import net.pterodactylus.sone.data.Sone;
37 import net.pterodactylus.sone.web.page.FreenetRequest;
38 import net.pterodactylus.util.collection.Pagination;
39 import net.pterodactylus.util.logging.Logging;
40 import net.pterodactylus.util.number.Numbers;
41 import net.pterodactylus.util.template.Template;
42 import net.pterodactylus.util.template.TemplateContext;
43 import net.pterodactylus.util.text.StringEscaper;
44 import net.pterodactylus.util.text.TextException;
45
46 import com.google.common.base.Function;
47 import com.google.common.base.Optional;
48 import com.google.common.base.Predicate;
49 import com.google.common.cache.CacheBuilder;
50 import com.google.common.cache.CacheLoader;
51 import com.google.common.cache.LoadingCache;
52 import com.google.common.collect.Collections2;
53 import com.google.common.collect.FluentIterable;
54 import com.google.common.collect.Ordering;
55
56 /**
57  * This page lets the user search for posts and replies that contain certain
58  * words.
59  *
60  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61  */
62 public class SearchPage extends SoneTemplatePage {
63
64         /** The logger. */
65         private static final Logger logger = Logging.getLogger(SearchPage.class);
66
67         /** Short-term cache. */
68         private final LoadingCache<List<Phrase>, Set<Hit<Post>>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader<List<Phrase>, Set<Hit<Post>>>() {
69
70                 @Override
71                 @SuppressWarnings("synthetic-access")
72                 public Set<Hit<Post>> load(List<Phrase> phrases) {
73                         Set<Post> posts = new HashSet<Post>();
74                         for (Sone sone : webInterface.getCore().getSones()) {
75                                 posts.addAll(sone.getPosts());
76                         }
77                         return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
78                 }
79         });
80
81         /**
82          * Creates a new search page.
83          *
84          * @param template
85          *            The template to render
86          * @param webInterface
87          *            The Sone web interface
88          */
89         public SearchPage(Template template, WebInterface webInterface) {
90                 super("search.html", template, "Page.Search.Title", webInterface);
91         }
92
93         //
94         // SONETEMPLATEPAGE METHODS
95         //
96
97         @Override
98         @SuppressWarnings("synthetic-access")
99         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
100                 super.processTemplate(request, templateContext);
101                 String query = request.getHttpRequest().getParam("query").trim();
102                 if (query.length() == 0) {
103                         throw new RedirectException("index.html");
104                 }
105
106                 List<Phrase> phrases = parseSearchPhrases(query);
107                 if (phrases.isEmpty()) {
108                         throw new RedirectException("index.html");
109                 }
110
111                 /* check for a couple of shortcuts. */
112                 if (phrases.size() == 1) {
113                         String phrase = phrases.get(0).getPhrase();
114
115                         /* is it a Sone ID? */
116                         redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
117
118                         /* is it a post ID? */
119                         redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
120
121                         /* is it a reply ID? show the post. */
122                         redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
123
124                         /* is it an album ID? */
125                         redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
126
127                         /* is it an image ID? */
128                         redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
129                 }
130
131                 Collection<Sone> sones = webInterface.getCore().getSones();
132                 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
133
134                 Collection<Hit<Post>> postHits = hitCache.getUnchecked(phrases);
135
136                 /* now filter. */
137                 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
138                 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
139
140                 /* now sort. */
141                 List<Hit<Sone>> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits);
142                 List<Hit<Post>> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits);
143
144                 /* extract Sones and posts. */
145                 List<Sone> resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper<Sone>()).toList();
146                 List<Post> resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper<Post>()).toList();
147
148                 /* pagination. */
149                 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
150                 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
151
152                 templateContext.set("sonePagination", sonePagination);
153                 templateContext.set("soneHits", sonePagination.getItems());
154                 templateContext.set("postPagination", postPagination);
155                 templateContext.set("postHits", postPagination.getItems());
156         }
157
158         //
159         // PRIVATE METHODS
160         //
161
162         /**
163          * Collects hit information for the given objects. The objects are converted
164          * to a {@link String} using the given {@link StringGenerator}, and the
165          * {@link #calculateScore(List, String) calculated score} is stored together
166          * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
167          * returned.
168          *
169          * @param <T>
170          *            The type of the objects
171          * @param objects
172          *            The objects to search over
173          * @param phrases
174          *            The phrases to search for
175          * @param stringGenerator
176          *            The string generator for the objects
177          * @return The hits for the given phrases
178          */
179         private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
180                 Set<Hit<T>> hits = new HashSet<Hit<T>>();
181                 for (T object : objects) {
182                         String objectString = stringGenerator.generateString(object);
183                         double score = calculateScore(phrases, objectString);
184                         hits.add(new Hit<T>(object, score));
185                 }
186                 return hits;
187         }
188
189         /**
190          * Parses the given query into search phrases. The query is split on
191          * whitespace while allowing to group words using single or double quotes.
192          * Isolated phrases starting with a “+” are
193          * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
194          * {@link Phrase.Optionality#FORBIDDEN}.
195          *
196          * @param query
197          *            The query to parse
198          * @return The parsed phrases
199          */
200         private static List<Phrase> parseSearchPhrases(String query) {
201                 List<String> parsedPhrases = null;
202                 try {
203                         parsedPhrases = StringEscaper.parseLine(query);
204                 } catch (TextException te1) {
205                         /* invalid query. */
206                         return Collections.emptyList();
207                 }
208
209                 List<Phrase> phrases = new ArrayList<Phrase>();
210                 for (String phrase : parsedPhrases) {
211                         if (phrase.startsWith("+")) {
212                                 if (phrase.length() > 1) {
213                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
214                                 } else {
215                                         phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
216                                 }
217                         } else if (phrase.startsWith("-")) {
218                                 if (phrase.length() > 1) {
219                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
220                                 } else {
221                                         phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
222                                 }
223                         } else {
224                                 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
225                         }
226                 }
227                 return phrases;
228         }
229
230         /**
231          * Calculates the score for the given expression when using the given
232          * phrases.
233          *
234          * @param phrases
235          *            The phrases to search for
236          * @param expression
237          *            The expression to search
238          * @return The score of the expression
239          */
240         private static double calculateScore(List<Phrase> phrases, String expression) {
241                 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
242                 double optionalHits = 0;
243                 double requiredHits = 0;
244                 int forbiddenHits = 0;
245                 int requiredPhrases = 0;
246                 for (Phrase phrase : phrases) {
247                         String phraseString = phrase.getPhrase().toLowerCase();
248                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
249                                 ++requiredPhrases;
250                         }
251                         int matches = 0;
252                         int index = 0;
253                         double score = 0;
254                         while (index < expression.length()) {
255                                 int position = expression.toLowerCase().indexOf(phraseString, index);
256                                 if (position == -1) {
257                                         break;
258                                 }
259                                 score += Math.pow(1 - position / (double) expression.length(), 2);
260                                 index = position + phraseString.length();
261                                 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
262                                 ++matches;
263                         }
264                         logger.log(Level.FINEST, String.format("Score: %f", score));
265                         if (matches == 0) {
266                                 continue;
267                         }
268                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
269                                 requiredHits += score;
270                         }
271                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
272                                 optionalHits += score;
273                         }
274                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
275                                 forbiddenHits += matches;
276                         }
277                 }
278                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
279         }
280
281         /**
282          * Throws a
283          * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
284          * if the given object is not {@code null}, appending the object to the
285          * given target URL.
286          *
287          * @param object
288          *            The object on which to redirect
289          * @param target
290          *            The target of the redirect
291          * @throws RedirectException
292          *             if {@code object} is not {@code null}
293          */
294         private static void redirectIfNotNull(String object, String target) throws RedirectException {
295                 if (object != null) {
296                         throw new RedirectException(target + object);
297                 }
298         }
299
300         /**
301          * If the given phrase contains a Sone ID (optionally prefixed by
302          * “sone://”), returns said Sone ID, otherwise return {@code null}.
303          *
304          * @param phrase
305          *            The phrase that maybe is a Sone ID
306          * @return The Sone ID, or {@code null}
307          */
308         private String getSoneId(String phrase) {
309                 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
310                 return (webInterface.getCore().getSone(soneId).isPresent()) ? soneId : null;
311         }
312
313         /**
314          * If the given phrase contains a post ID (optionally prefixed by
315          * “post://”), returns said post ID, otherwise return {@code null}.
316          *
317          * @param phrase
318          *            The phrase that maybe is a post ID
319          * @return The post ID, or {@code null}
320          */
321         private String getPostId(String phrase) {
322                 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
323                 return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null;
324         }
325
326         /**
327          * If the given phrase contains a reply ID (optionally prefixed by
328          * “reply://”), returns the ID of the post the reply belongs to, otherwise
329          * return {@code null}.
330          *
331          * @param phrase
332          *            The phrase that maybe is a reply ID
333          * @return The reply’s post ID, or {@code null}
334          */
335         private String getReplyPostId(String phrase) {
336                 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
337                 Optional<PostReply> postReply = webInterface.getCore().getPostReply(replyId);
338                 if (!postReply.isPresent()) {
339                         return null;
340                 }
341                 return postReply.get().getPostId();
342         }
343
344         /**
345          * If the given phrase contains an album ID (optionally prefixed by
346          * “album://”), returns said album ID, otherwise return {@code null}.
347          *
348          * @param phrase
349          *            The phrase that maybe is an album ID
350          * @return The album ID, or {@code null}
351          */
352         private String getAlbumId(String phrase) {
353                 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
354                 return webInterface.getCore().getAlbum(albumId).isPresent() ? albumId : null;
355         }
356
357         /**
358          * If the given phrase contains an image ID (optionally prefixed by
359          * “image://”), returns said image ID, otherwise return {@code null}.
360          *
361          * @param phrase
362          *            The phrase that maybe is an image ID
363          * @return The image ID, or {@code null}
364          */
365         private String getImageId(String phrase) {
366                 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
367                 return webInterface.getCore().getImage(imageId).isPresent() ? imageId : null;
368         }
369
370         /**
371          * Converts a given object into a {@link String}.
372          *
373          * @param <T>
374          *            The type of the objects
375          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
376          */
377         private static interface StringGenerator<T> {
378
379                 /**
380                  * Generates a {@link String} for the given object.
381                  *
382                  * @param object
383                  *            The object to generate the {@link String} for
384                  * @return The generated {@link String}
385                  */
386                 public String generateString(T object);
387
388         }
389
390         /**
391          * Generates a {@link String} from a {@link Sone}, concatenating the name of
392          * the Sone and all {@link Profile} {@link Field} values.
393          *
394          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
395          */
396         private static class SoneStringGenerator implements StringGenerator<Sone> {
397
398                 /** A static instance of a complete Sone string generator. */
399                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
400
401                 /**
402                  * A static instance of a Sone string generator that will only use the
403                  * name of the Sone.
404                  */
405                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
406
407                 /** Whether to generate a string from all data of a Sone. */
408                 private final boolean complete;
409
410                 /**
411                  * Creates a new Sone string generator.
412                  *
413                  * @param complete
414                  *            {@code true} to use the profile’s fields, {@code false} to
415                  *            not to use the profile‘s fields
416                  */
417                 private SoneStringGenerator(boolean complete) {
418                         this.complete = complete;
419                 }
420
421                 @Override
422                 public String generateString(Sone sone) {
423                         StringBuilder soneString = new StringBuilder();
424                         soneString.append(sone.getName());
425                         Profile soneProfile = sone.getProfile();
426                         if (soneProfile.getFirstName() != null) {
427                                 soneString.append(' ').append(soneProfile.getFirstName());
428                         }
429                         if (soneProfile.getMiddleName() != null) {
430                                 soneString.append(' ').append(soneProfile.getMiddleName());
431                         }
432                         if (soneProfile.getLastName() != null) {
433                                 soneString.append(' ').append(soneProfile.getLastName());
434                         }
435                         if (complete) {
436                                 for (Field field : soneProfile.getFields()) {
437                                         soneString.append(' ').append(field.getValue());
438                                 }
439                         }
440                         return soneString.toString();
441                 }
442
443         }
444
445         /**
446          * Generates a {@link String} from a {@link Post}, concatenating the text of
447          * the post, the text of all {@link Reply}s, and the name of all
448          * {@link Sone}s that have replied.
449          *
450          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
451          */
452         private class PostStringGenerator implements StringGenerator<Post> {
453
454                 @Override
455                 public String generateString(Post post) {
456                         StringBuilder postString = new StringBuilder();
457                         postString.append(post.getText());
458                         if (post.getRecipient().isPresent()) {
459                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient().get()));
460                         }
461                         for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER)) {
462                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
463                                 postString.append(' ').append(reply.getText());
464                         }
465                         return postString.toString();
466                 }
467
468         }
469
470         /**
471          * A search phrase.
472          *
473          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
474          */
475         private static class Phrase {
476
477                 /**
478                  * The optionality of a search phrase.
479                  *
480                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
481                  *         Roden</a>
482                  */
483                 public enum Optionality {
484
485                         /** The phrase is optional. */
486                         OPTIONAL,
487
488                         /** The phrase is required. */
489                         REQUIRED,
490
491                         /** The phrase is forbidden. */
492                         FORBIDDEN
493
494                 }
495
496                 /** The phrase to search for. */
497                 private final String phrase;
498
499                 /** The optionality of the phrase. */
500                 private final Optionality optionality;
501
502                 /**
503                  * Creates a new phrase.
504                  *
505                  * @param phrase
506                  *            The phrase to search for
507                  * @param optionality
508                  *            The optionality of the phrase
509                  */
510                 public Phrase(String phrase, Optionality optionality) {
511                         this.optionality = optionality;
512                         this.phrase = phrase;
513                 }
514
515                 /**
516                  * Returns the phrase to search for.
517                  *
518                  * @return The phrase to search for
519                  */
520                 public String getPhrase() {
521                         return phrase;
522                 }
523
524                 /**
525                  * Returns the optionality of the phrase.
526                  *
527                  * @return The optionality of the phrase
528                  */
529                 public Optionality getOptionality() {
530                         return optionality;
531                 }
532
533                 //
534                 // OBJECT METHODS
535                 //
536
537                 @Override
538                 public int hashCode() {
539                         return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
540                 }
541
542                 @Override
543                 public boolean equals(Object object) {
544                         if (!(object instanceof Phrase)) {
545                                 return false;
546                         }
547                         Phrase phrase = (Phrase) object;
548                         return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
549                 }
550
551         }
552
553         /**
554          * A hit consists of a searched object and the score it got for the phrases
555          * of the search.
556          *
557          * @see SearchPage#calculateScore(List, String)
558          * @param <T>
559          *            The type of the searched object
560          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
561          */
562         private static class Hit<T> {
563
564                 /** Filter for {@link Hit}s with a score of more than 0. */
565                 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
566
567                         @Override
568                         public boolean apply(Hit<?> hit) {
569                                 return (hit == null) ? false : hit.getScore() > 0;
570                         }
571
572                 };
573
574                 /** Comparator that sorts {@link Hit}s descending by score. */
575                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
576
577                         @Override
578                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
579                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
580                         }
581
582                 };
583
584                 /** The object that was searched. */
585                 private final T object;
586
587                 /** The score of the object. */
588                 private final double score;
589
590                 /**
591                  * Creates a new hit.
592                  *
593                  * @param object
594                  *            The object that was searched
595                  * @param score
596                  *            The score of the object
597                  */
598                 public Hit(T object, double score) {
599                         this.object = object;
600                         this.score = score;
601                 }
602
603                 /**
604                  * Returns the object that was searched.
605                  *
606                  * @return The object that was searched
607                  */
608                 public T getObject() {
609                         return object;
610                 }
611
612                 /**
613                  * Returns the score of the object.
614                  *
615                  * @return The score of the object
616                  */
617                 public double getScore() {
618                         return score;
619                 }
620
621         }
622
623         /**
624          * Extracts the object from a {@link Hit}.
625          *
626          * @param <T>
627          *            The type of the object to extract
628          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
629          */
630         private static class HitMapper<T> implements Function<Hit<T>, T> {
631
632                 @Override
633                 public T apply(Hit<T> input) {
634                         return input.getObject();
635                 }
636
637         }
638
639 }