Require a more recent version of utils.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
1 /*
2  * Sone - SearchPage.java - Copyright © 2010 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.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.Profile;
32 import net.pterodactylus.sone.data.Profile.Field;
33 import net.pterodactylus.sone.data.Reply;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.util.collection.Converter;
36 import net.pterodactylus.util.collection.Converters;
37 import net.pterodactylus.util.collection.Pagination;
38 import net.pterodactylus.util.filter.Filter;
39 import net.pterodactylus.util.filter.Filters;
40 import net.pterodactylus.util.logging.Logging;
41 import net.pterodactylus.util.number.Numbers;
42 import net.pterodactylus.util.template.Template;
43 import net.pterodactylus.util.template.TemplateContext;
44 import net.pterodactylus.util.text.StringEscaper;
45 import net.pterodactylus.util.text.TextException;
46
47 /**
48  * This page lets the user search for posts and replies that contain certain
49  * words.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class SearchPage extends SoneTemplatePage {
54
55         /** The logger. */
56         private static final Logger logger = Logging.getLogger(SearchPage.class);
57
58         /**
59          * Creates a new search page.
60          *
61          * @param template
62          *            The template to render
63          * @param webInterface
64          *            The Sone web interface
65          */
66         public SearchPage(Template template, WebInterface webInterface) {
67                 super("search.html", template, "Page.Search.Title", webInterface);
68         }
69
70         //
71         // SONETEMPLATEPAGE METHODS
72         //
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
79                 super.processTemplate(request, templateContext);
80                 String query = request.getHttpRequest().getParam("query").trim();
81                 if (query.length() == 0) {
82                         throw new RedirectException("index.html");
83                 }
84
85                 List<Phrase> phrases = parseSearchPhrases(query);
86
87                 Set<Sone> sones = webInterface.getCore().getSones();
88                 Set<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
89
90                 Set<Post> posts = new HashSet<Post>();
91                 for (Sone sone : sones) {
92                         posts.addAll(sone.getPosts());
93                 }
94                 @SuppressWarnings("synthetic-access")
95                 Set<Hit<Post>> postHits = getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
96
97                 /* now filter. */
98                 soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);
99                 postHits = Filters.filteredSet(postHits, Hit.POSITIVE_FILTER);
100
101                 /* now sort. */
102                 List<Hit<Sone>> sortedSoneHits = new ArrayList<Hit<Sone>>(soneHits);
103                 Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR);
104                 List<Hit<Post>> sortedPostHits = new ArrayList<Hit<Post>>(postHits);
105                 Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
106
107                 /* extract Sones and posts. */
108                 List<Sone> resultSones = Converters.convertList(sortedSoneHits, new HitConverter<Sone>());
109                 List<Post> resultPosts = Converters.convertList(sortedPostHits, new HitConverter<Post>());
110
111                 /* pagination. */
112                 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
113                 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
114
115                 templateContext.set("sonePagination", sonePagination);
116                 templateContext.set("soneHits", sonePagination.getItems());
117                 templateContext.set("postPagination", postPagination);
118                 templateContext.set("postHits", postPagination.getItems());
119         }
120
121         //
122         // PRIVATE METHODS
123         //
124
125         /**
126          * Collects hit information for the given objects. The objects are converted
127          * to a {@link String} using the given {@link StringGenerator}, and the
128          * {@link #calculateScore(List, String) calculated score} is stored together
129          * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
130          * returned.
131          *
132          * @param <T>
133          *            The type of the objects
134          * @param objects
135          *            The objects to search over
136          * @param phrases
137          *            The phrases to search for
138          * @param stringGenerator
139          *            The string generator for the objects
140          * @return The hits for the given phrases
141          */
142         private <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
143                 Set<Hit<T>> hits = new HashSet<Hit<T>>();
144                 for (T object : objects) {
145                         String objectString = stringGenerator.generateString(object);
146                         double score = calculateScore(phrases, objectString);
147                         hits.add(new Hit<T>(object, score));
148                 }
149                 return hits;
150         }
151
152         /**
153          * Parses the given query into search phrases. The query is split on
154          * whitespace while allowing to group words using single or double quotes.
155          * Isolated phrases starting with a “+” are
156          * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
157          * {@link Phrase.Optionality#FORBIDDEN}.
158          *
159          * @param query
160          *            The query to parse
161          * @return The parsed phrases
162          */
163         private List<Phrase> parseSearchPhrases(String query) {
164                 List<String> parsedPhrases = null;
165                 try {
166                         parsedPhrases = StringEscaper.parseLine(query);
167                 } catch (TextException te1) {
168                         /* invalid query. */
169                         return Collections.emptyList();
170                 }
171
172                 List<Phrase> phrases = new ArrayList<Phrase>();
173                 for (String phrase : parsedPhrases) {
174                         if (phrase.startsWith("+")) {
175                                 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
176                         } else if (phrase.startsWith("-")) {
177                                 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
178                         }
179                         phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
180                 }
181                 return phrases;
182         }
183
184         /**
185          * Calculates the score for the given expression when using the given
186          * phrases.
187          *
188          * @param phrases
189          *            The phrases to search for
190          * @param expression
191          *            The expression to search
192          * @return The score of the expression
193          */
194         private double calculateScore(List<Phrase> phrases, String expression) {
195                 logger.log(Level.FINEST, "Calculating Score for “%s”…", expression);
196                 double optionalHits = 0;
197                 double requiredHits = 0;
198                 int forbiddenHits = 0;
199                 int requiredPhrases = 0;
200                 for (Phrase phrase : phrases) {
201                         String phraseString = phrase.getPhrase().toLowerCase();
202                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
203                                 ++requiredPhrases;
204                         }
205                         int matches = 0;
206                         int index = 0;
207                         double score = 0;
208                         while (index < expression.length()) {
209                                 int position = expression.toLowerCase().indexOf(phraseString, index);
210                                 if (position == -1) {
211                                         break;
212                                 }
213                                 score += Math.pow(1 - position / (double) expression.length(), 2);
214                                 index = position + phraseString.length();
215                                 logger.log(Level.FINEST, "Got hit at position %d.", position);
216                                 ++matches;
217                         }
218                         logger.log(Level.FINEST, "Score: %f", score);
219                         if (matches == 0) {
220                                 continue;
221                         }
222                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
223                                 requiredHits += score;
224                         }
225                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
226                                 optionalHits += score;
227                         }
228                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
229                                 forbiddenHits += matches;
230                         }
231                 }
232                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
233         }
234
235         /**
236          * Converts a given object into a {@link String}.
237          *
238          * @param <T>
239          *            The type of the objects
240          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
241          */
242         private static interface StringGenerator<T> {
243
244                 /**
245                  * Generates a {@link String} for the given object.
246                  *
247                  * @param object
248                  *            The object to generate the {@link String} for
249                  * @return The generated {@link String}
250                  */
251                 public String generateString(T object);
252
253         }
254
255         /**
256          * Generates a {@link String} from a {@link Sone}, concatenating the name of
257          * the Sone and all {@link Profile} {@link Field} values.
258          *
259          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
260          */
261         private static class SoneStringGenerator implements StringGenerator<Sone> {
262
263                 /** A static instance of a complete Sone string generator. */
264                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
265
266                 /**
267                  * A static instance of a Sone string generator that will only use the
268                  * name of the Sone.
269                  */
270                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
271
272                 /** Whether to generate a string from all data of a Sone. */
273                 private final boolean complete;
274
275                 /**
276                  * Creates a new Sone string generator.
277                  *
278                  * @param complete
279                  *            {@code true} to use the profile’s fields, {@code false} to
280                  *            not to use the profile‘s fields
281                  */
282                 private SoneStringGenerator(boolean complete) {
283                         this.complete = complete;
284                 }
285
286                 /**
287                  * {@inheritDoc}
288                  */
289                 @Override
290                 public String generateString(Sone sone) {
291                         StringBuilder soneString = new StringBuilder();
292                         soneString.append(sone.getName());
293                         Profile soneProfile = sone.getProfile();
294                         if (soneProfile.getFirstName() != null) {
295                                 soneString.append(' ').append(soneProfile.getFirstName());
296                         }
297                         if (soneProfile.getMiddleName() != null) {
298                                 soneString.append(' ').append(soneProfile.getMiddleName());
299                         }
300                         if (soneProfile.getLastName() != null) {
301                                 soneString.append(' ').append(soneProfile.getLastName());
302                         }
303                         if (complete) {
304                                 for (Field field : soneProfile.getFields()) {
305                                         soneString.append(' ').append(field.getValue());
306                                 }
307                         }
308                         return soneString.toString();
309                 }
310
311         }
312
313         /**
314          * Generates a {@link String} from a {@link Post}, concatenating the text of
315          * the post, the text of all {@link Reply}s, and the name of all
316          * {@link Sone}s that have replied.
317          *
318          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
319          */
320         private class PostStringGenerator implements StringGenerator<Post> {
321
322                 /**
323                  * {@inheritDoc}
324                  */
325                 @Override
326                 public String generateString(Post post) {
327                         StringBuilder postString = new StringBuilder();
328                         postString.append(post.getText());
329                         if (post.getRecipient() != null) {
330                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient()));
331                         }
332                         for (Reply reply : Filters.filteredList(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLIES_FILTER)) {
333                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
334                                 postString.append(' ').append(reply.getText());
335                         }
336                         return postString.toString();
337                 }
338
339         }
340
341         /**
342          * A search phrase.
343          *
344          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
345          */
346         private static class Phrase {
347
348                 /**
349                  * The optionality of a search phrase.
350                  *
351                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
352                  *         Roden</a>
353                  */
354                 public enum Optionality {
355
356                         /** The phrase is optional. */
357                         OPTIONAL,
358
359                         /** The phrase is required. */
360                         REQUIRED,
361
362                         /** The phrase is forbidden. */
363                         FORBIDDEN
364
365                 }
366
367                 /** The phrase to search for. */
368                 private final String phrase;
369
370                 /** The optionality of the phrase. */
371                 private final Optionality optionality;
372
373                 /**
374                  * Creates a new phrase.
375                  *
376                  * @param phrase
377                  *            The phrase to search for
378                  * @param optionality
379                  *            The optionality of the phrase
380                  */
381                 public Phrase(String phrase, Optionality optionality) {
382                         this.optionality = optionality;
383                         this.phrase = phrase;
384                 }
385
386                 /**
387                  * Returns the phrase to search for.
388                  *
389                  * @return The phrase to search for
390                  */
391                 public String getPhrase() {
392                         return phrase;
393                 }
394
395                 /**
396                  * Returns the optionality of the phrase.
397                  *
398                  * @return The optionality of the phrase
399                  */
400                 public Optionality getOptionality() {
401                         return optionality;
402                 }
403
404         }
405
406         /**
407          * A hit consists of a searched object and the score it got for the phrases
408          * of the search.
409          *
410          * @see SearchPage#calculateScore(List, String)
411          * @param <T>
412          *            The type of the searched object
413          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
414          */
415         private static class Hit<T> {
416
417                 /** Filter for {@link Hit}s with a score of more than 0. */
418                 public static final Filter<Hit<?>> POSITIVE_FILTER = new Filter<Hit<?>>() {
419
420                         @Override
421                         public boolean filterObject(Hit<?> hit) {
422                                 return hit.getScore() > 0;
423                         }
424
425                 };
426
427                 /** Comparator that sorts {@link Hit}s descending by score. */
428                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
429
430                         @Override
431                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
432                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
433                         }
434
435                 };
436
437                 /** The object that was searched. */
438                 private final T object;
439
440                 /** The score of the object. */
441                 private final double score;
442
443                 /**
444                  * Creates a new hit.
445                  *
446                  * @param object
447                  *            The object that was searched
448                  * @param score
449                  *            The score of the object
450                  */
451                 public Hit(T object, double score) {
452                         this.object = object;
453                         this.score = score;
454                 }
455
456                 /**
457                  * Returns the object that was searched.
458                  *
459                  * @return The object that was searched
460                  */
461                 public T getObject() {
462                         return object;
463                 }
464
465                 /**
466                  * Returns the score of the object.
467                  *
468                  * @return The score of the object
469                  */
470                 public double getScore() {
471                         return score;
472                 }
473
474         }
475
476         /**
477          * Extracts the object from a {@link Hit}.
478          *
479          * @param <T>
480          *            The type of the object to extract
481          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
482          */
483         public static class HitConverter<T> implements Converter<Hit<T>, T> {
484
485                 /**
486                  * {@inheritDoc}
487                  */
488                 @Override
489                 public T convert(Hit<T> input) {
490                         return input.getObject();
491                 }
492
493         }
494
495 }