2 * Sone - ViewSonePage.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.Collections;
22 import java.util.Comparator;
23 import java.util.HashMap;
24 import java.util.List;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.Reply;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.collection.Pagination;
33 import net.pterodactylus.util.number.Numbers;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateContext;
38 * Lets the user browser another Sone.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class ViewSonePage extends SoneTemplatePage {
45 * Creates a new “view Sone” page.
48 * The template to render
50 * The Sone web interface
52 public ViewSonePage(Template template, WebInterface webInterface) {
53 super("viewSone.html", template, webInterface, false);
57 // TEMPLATEPAGE METHODS
64 protected String getPageTitle(Request request) {
65 String soneId = request.getHttpRequest().getParam("sone");
66 Sone sone = webInterface.getCore().getSone(soneId, false);
67 if ((sone != null) && (sone.getTime() > 0)) {
68 String soneName = SoneAccessor.getNiceName(sone);
69 return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title");
71 return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone");
78 protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
79 super.processTemplate(request, templateContext);
80 String soneId = request.getHttpRequest().getParam("sone");
81 Sone sone = webInterface.getCore().getSone(soneId, false);
82 templateContext.set("sone", sone);
83 templateContext.set("soneId", soneId);
87 List<Post> sonePosts = sone.getPosts();
88 sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone));
89 Collections.sort(sonePosts, Post.TIME_COMPARATOR);
90 Pagination<Post> postPagination = new Pagination<Post>(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
91 templateContext.set("postPagination", postPagination);
92 templateContext.set("posts", postPagination.getItems());
93 Set<Reply> replies = sone.getReplies();
94 final Map<Post, List<Reply>> repliedPosts = new HashMap<Post, List<Reply>>();
95 for (Reply reply : replies) {
96 Post post = reply.getPost();
97 if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) {
100 repliedPosts.put(post, webInterface.getCore().getReplies(post));
102 List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
103 Collections.sort(posts, new Comparator<Post>() {
106 public int compare(Post leftPost, Post rightPost) {
107 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime() - repliedPosts.get(leftPost).get(0).getTime()));
112 Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
113 templateContext.set("repliedPostPagination", repliedPostPagination);
114 templateContext.set("repliedPosts", repliedPostPagination.getItems());