X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSearchPage.java;h=8b444a7cbd3ff5e52478403fcf6f57ff7d1b3ff9;hp=c037baffebc54237d7f5ad2802f186e85a76ead6;hb=7b55e0be6a3283e43a9bbab98f82aebdd948eb33;hpb=7f024734546973cd592e6cbf04604705477f15f6 diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index c037baf..8b444a7 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -1,5 +1,5 @@ /* - * Sone - SearchPage.java - Copyright © 2010–2013 David Roden + * Sone - SearchPage.java - Copyright © 2010–2016 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +17,10 @@ package net.pterodactylus.sone.web; +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.primitives.Ints.tryParse; +import static java.util.logging.Logger.getLogger; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -36,14 +40,13 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; -import net.pterodactylus.util.logging.Logging; -import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.text.StringEscaper; import net.pterodactylus.util.text.TextException; import com.google.common.base.Function; +import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -61,7 +64,7 @@ import com.google.common.collect.Ordering; public class SearchPage extends SoneTemplatePage { /** The logger. */ - private static final Logger logger = Logging.getLogger(SearchPage.class); + private static final Logger logger = getLogger(SearchPage.class.getName()); /** Short-term cache. */ private final LoadingCache, Set>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader, Set>>() { @@ -98,8 +101,7 @@ public class SearchPage extends SoneTemplatePage { */ @Override @SuppressWarnings("synthetic-access") - protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - super.processTemplate(request, templateContext); + protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { String query = request.getHttpRequest().getParam("query").trim(); if (query.length() == 0) { throw new RedirectException("index.html"); @@ -130,7 +132,7 @@ public class SearchPage extends SoneTemplatePage { redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image="); } - Set sones = webInterface.getCore().getSones(); + Collection sones = webInterface.getCore().getSones(); Collection> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); Collection> postHits = hitCache.getUnchecked(phrases); @@ -148,8 +150,8 @@ public class SearchPage extends SoneTemplatePage { List resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper()).toList(); /* pagination. */ - Pagination sonePagination = new Pagination(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0)); - Pagination postPagination = new Pagination(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); + Pagination sonePagination = new Pagination(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(fromNullable(tryParse(request.getHttpRequest().getParam("sonePage"))).or(0)); + Pagination postPagination = new Pagination(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(fromNullable(tryParse(request.getHttpRequest().getParam("postPage"))).or(0)); templateContext.set("sonePagination", sonePagination); templateContext.set("soneHits", sonePagination.getItems()); @@ -200,7 +202,7 @@ public class SearchPage extends SoneTemplatePage { * @return The parsed phrases */ private static List parseSearchPhrases(String query) { - List parsedPhrases = null; + List parsedPhrases; try { parsedPhrases = StringEscaper.parseLine(query); } catch (TextException te1) { @@ -309,7 +311,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getSoneId(String phrase) { String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getSone(soneId) != null) ? soneId : null; + return (webInterface.getCore().getSone(soneId).isPresent()) ? soneId : null; } /** @@ -322,7 +324,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getPostId(String phrase) { String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getPost(postId) != null) ? postId : null; + return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null; } /** @@ -336,7 +338,11 @@ public class SearchPage extends SoneTemplatePage { */ private String getReplyPostId(String phrase) { String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getPostReply(replyId) != null) ? webInterface.getCore().getPostReply(replyId).getPost().getId() : null; + Optional postReply = webInterface.getCore().getPostReply(replyId); + if (!postReply.isPresent()) { + return null; + } + return postReply.get().getPostId(); } /** @@ -349,7 +355,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getAlbumId(String phrase) { String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getAlbum(albumId, false) != null) ? albumId : null; + return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null; } /** @@ -459,10 +465,10 @@ public class SearchPage extends SoneTemplatePage { public String generateString(Post post) { StringBuilder postString = new StringBuilder(); postString.append(post.getText()); - if (post.getRecipient() != null) { - postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient())); + if (post.getRecipient().isPresent()) { + postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient().get())); } - for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) { + for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER)) { postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone())); postString.append(' ').append(reply.getText()); } @@ -576,7 +582,7 @@ public class SearchPage extends SoneTemplatePage { @Override public boolean apply(Hit hit) { - return hit.getScore() > 0; + return (hit != null) && (hit.getScore() > 0); } }; @@ -586,7 +592,7 @@ public class SearchPage extends SoneTemplatePage { @Override public int compare(Hit leftHit, Hit rightHit) { - return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0); + return Double.compare(rightHit.getScore(), leftHit.getScore()); } };