2 * Sone - OptionsPage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
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;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.Profile;
30 import net.pterodactylus.sone.data.Profile.Field;
31 import net.pterodactylus.sone.data.Reply;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.util.collection.Converter;
34 import net.pterodactylus.util.collection.Converters;
35 import net.pterodactylus.util.collection.Pagination;
36 import net.pterodactylus.util.filter.Filter;
37 import net.pterodactylus.util.filter.Filters;
38 import net.pterodactylus.util.number.Numbers;
39 import net.pterodactylus.util.template.Template;
40 import net.pterodactylus.util.template.TemplateContext;
41 import net.pterodactylus.util.text.StringEscaper;
42 import net.pterodactylus.util.text.TextException;
45 * This page lets the user search for posts and replies that contain certain
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class SearchPage extends SoneTemplatePage {
53 * Creates a new search page.
56 * The template to render
58 * The Sone web interface
60 public SearchPage(Template template, WebInterface webInterface) {
61 super("search.html", template, "Page.Search.Title", webInterface);
65 // SONETEMPLATEPAGE METHODS
72 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
73 super.processTemplate(request, templateContext);
74 String query = request.getHttpRequest().getParam("query").trim();
75 if (query.length() == 0) {
76 throw new RedirectException("index.html");
79 List<Phrase> phrases = parseSearchPhrases(query);
81 Set<Sone> sones = webInterface.getCore().getSones();
82 Set<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
84 Set<Post> posts = new HashSet<Post>();
85 for (Sone sone : sones) {
86 posts.addAll(sone.getPosts());
88 @SuppressWarnings("synthetic-access")
89 Set<Hit<Post>> postHits = getHits(posts, phrases, new PostStringGenerator());
92 soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);
93 postHits = Filters.filteredSet(postHits, Hit.POSITIVE_FILTER);
96 List<Hit<Sone>> sortedSoneHits = new ArrayList<Hit<Sone>>(soneHits);
97 Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR);
98 List<Hit<Post>> sortedPostHits = new ArrayList<Hit<Post>>(postHits);
99 Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
101 /* extract Sones and posts. */
102 List<Sone> resultSones = Converters.convertList(sortedSoneHits, new HitConverter<Sone>());
103 List<Post> resultPosts = Converters.convertList(sortedPostHits, new HitConverter<Post>());
106 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, 10).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
107 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, 10).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
109 templateContext.set("sonePagination", sonePagination);
110 templateContext.set("soneHits", sonePagination.getItems());
111 templateContext.set("postPagination", postPagination);
112 templateContext.set("postHits", postPagination.getItems());
114 /* mark found posts and Sones as knew. */
115 for (Sone sone : sonePagination.getItems()) {
116 webInterface.getCore().markSoneKnown(sone);
118 for (Post post : postPagination.getItems()) {
119 webInterface.getCore().markPostKnown(post);
120 for (Reply reply : webInterface.getCore().getReplies(post)) {
121 webInterface.getCore().markReplyKnown(reply);
131 * Collects hit information for the given objects. The objects are converted
132 * to a {@link String} using the given {@link StringGenerator}, and the
133 * {@link #calculateScore(List, String) calculated score} is stored together
134 * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
138 * The type of the objects
140 * The objects to search over
142 * The phrases to search for
143 * @param stringGenerator
144 * The string generator for the objects
145 * @return The hits for the given phrases
147 private <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
148 Set<Hit<T>> hits = new HashSet<Hit<T>>();
149 for (T object : objects) {
150 String objectString = stringGenerator.generateString(object);
151 int score = calculateScore(phrases, objectString);
152 hits.add(new Hit<T>(object, score));
158 * Parses the given query into search phrases. The query is split on
159 * whitespace while allowing to group words using single or double quotes.
160 * Isolated phrases starting with a “+” are
161 * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
162 * {@link Phrase.Optionality#FORBIDDEN}.
166 * @return The parsed phrases
168 private List<Phrase> parseSearchPhrases(String query) {
169 List<String> parsedPhrases = null;
171 parsedPhrases = StringEscaper.parseLine(query);
172 } catch (TextException te1) {
174 return Collections.emptyList();
177 List<Phrase> phrases = new ArrayList<Phrase>();
178 for (String phrase : parsedPhrases) {
179 if (phrase.startsWith("+")) {
180 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
181 } else if (phrase.startsWith("-")) {
182 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
184 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
190 * Calculates the score for the given expression when using the given
194 * The phrases to search for
196 * The expression to search
197 * @return The score of the expression
199 private int calculateScore(List<Phrase> phrases, String expression) {
200 int optionalHits = 0;
201 int requiredHits = 0;
202 int forbiddenHits = 0;
203 int requiredPhrases = 0;
204 for (Phrase phrase : phrases) {
205 String phraseString = phrase.getPhrase().toLowerCase();
206 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
211 while (index < expression.length()) {
212 int position = expression.toLowerCase().indexOf(phraseString, index);
213 if (position == -1) {
216 index = position + phraseString.length();
222 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
223 requiredHits += matches;
225 if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
226 optionalHits += matches;
228 if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
229 forbiddenHits += matches;
232 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
236 * Converts a given object into a {@link String}.
239 * The type of the objects
240 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
242 private static interface StringGenerator<T> {
245 * Generates a {@link String} for the given object.
248 * The object to generate the {@link String} for
249 * @return The generated {@link String}
251 public String generateString(T object);
256 * Generates a {@link String} from a {@link Sone}, concatenating the name of
257 * the Sone and all {@link Profile} {@link Field} values.
259 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
261 private static class SoneStringGenerator implements StringGenerator<Sone> {
263 /** A static instance of a complete Sone string generator. */
264 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
267 * A static instance of a Sone string generator that will only use the
270 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
272 /** Whether to generate a string from all data of a Sone. */
273 private final boolean complete;
276 * Creates a new Sone string generator.
279 * {@code true} to use the profile’s fields, {@code false} to
280 * not to use the profile‘s fields
282 private SoneStringGenerator(boolean complete) {
283 this.complete = complete;
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());
297 if (soneProfile.getMiddleName() != null) {
298 soneString.append(' ').append(soneProfile.getMiddleName());
300 if (soneProfile.getLastName() != null) {
301 soneString.append(' ').append(soneProfile.getLastName());
304 for (Field field : soneProfile.getFields()) {
305 soneString.append(' ').append(field.getValue());
308 return soneString.toString();
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.
318 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
320 private class PostStringGenerator implements StringGenerator<Post> {
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()));
332 for (Reply reply : webInterface.getCore().getReplies(post)) {
333 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
334 postString.append(' ').append(reply.getText());
336 return postString.toString();
344 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
346 private static class Phrase {
349 * The optionality of a search phrase.
351 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
354 public enum Optionality {
356 /** The phrase is optional. */
359 /** The phrase is required. */
362 /** The phrase is forbidden. */
367 /** The phrase to search for. */
368 private final String phrase;
370 /** The optionality of the phrase. */
371 private final Optionality optionality;
374 * Creates a new phrase.
377 * The phrase to search for
379 * The optionality of the phrase
381 public Phrase(String phrase, Optionality optionality) {
382 this.optionality = optionality;
383 this.phrase = phrase;
387 * Returns the phrase to search for.
389 * @return The phrase to search for
391 public String getPhrase() {
396 * Returns the optionality of the phrase.
398 * @return The optionality of the phrase
400 public Optionality getOptionality() {
407 * A hit consists of a searched object and the score it got for the phrases
410 * @see SearchPage#calculateScore(List, String)
412 * The type of the searched object
413 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
415 private static class Hit<T> {
417 /** Filter for {@link Hit}s with a score of more than 0. */
418 public static final Filter<Hit<?>> POSITIVE_FILTER = new Filter<Hit<?>>() {
421 public boolean filterObject(Hit<?> hit) {
422 return hit.getScore() > 0;
427 /** Comparator that sorts {@link Hit}s descending by score. */
428 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
431 public int compare(Hit<?> leftHit, Hit<?> rightHit) {
432 return rightHit.getScore() - leftHit.getScore();
437 /** The object that was searched. */
438 private final T object;
440 /** The score of the object. */
441 private final int score;
447 * The object that was searched
449 * The score of the object
451 public Hit(T object, int score) {
452 this.object = object;
457 * Returns the object that was searched.
459 * @return The object that was searched
461 public T getObject() {
466 * Returns the score of the object.
468 * @return The score of the object
470 public int getScore() {
477 * Extracts the object from a {@link Hit}.
480 * The type of the object to extract
481 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
483 public static class HitConverter<T> implements Converter<Hit<T>, T> {
489 public T convert(Hit<T> input) {
490 return input.getObject();