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