2 * Sone - ViewSonePage.java - Copyright © 2010–2012 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;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.List;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.PostReply;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.template.SoneAccessor;
33 import net.pterodactylus.sone.web.page.FreenetRequest;
34 import net.pterodactylus.util.collection.Pagination;
35 import net.pterodactylus.util.number.Numbers;
36 import net.pterodactylus.util.template.Template;
37 import net.pterodactylus.util.template.TemplateContext;
40 * Lets the user browser another Sone.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class ViewSonePage extends SoneTemplatePage {
47 * Creates a new “view Sone” page.
50 * The template to render
52 * The Sone web interface
54 public ViewSonePage(Template template, WebInterface webInterface) {
55 super("viewSone.html", template, webInterface, false);
59 // TEMPLATEPAGE METHODS
66 protected String getPageTitle(FreenetRequest request) {
67 String soneId = request.getHttpRequest().getParam("sone");
68 Sone sone = webInterface.getCore().getSone(soneId, false);
69 if ((sone != null) && (sone.getTime() > 0)) {
70 String soneName = SoneAccessor.getNiceName(sone);
71 return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title");
73 return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone");
80 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
81 super.processTemplate(request, templateContext);
82 String soneId = request.getHttpRequest().getParam("sone");
83 Sone sone = webInterface.getCore().getSone(soneId, false);
84 templateContext.set("sone", sone);
85 templateContext.set("soneId", soneId);
89 List<Post> sonePosts = sone.getPosts();
90 sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone));
91 Collections.sort(sonePosts, Post.TIME_COMPARATOR);
92 Pagination<Post> postPagination = new Pagination<Post>(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
93 templateContext.set("postPagination", postPagination);
94 templateContext.set("posts", postPagination.getItems());
95 Set<PostReply> replies = sone.getReplies();
96 final Map<Post, List<PostReply>> repliedPosts = new HashMap<Post, List<PostReply>>();
97 for (PostReply reply : replies) {
98 Post post = reply.getPost();
99 if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) {
102 repliedPosts.put(post, webInterface.getCore().getReplies(post));
104 List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
105 Collections.sort(posts, new Comparator<Post>() {
108 public int compare(Post leftPost, Post rightPost) {
109 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime() - repliedPosts.get(leftPost).get(0).getTime()));
114 Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
115 templateContext.set("repliedPostPagination", repliedPostPagination);
116 templateContext.set("repliedPosts", repliedPostPagination.getItems());
123 public boolean isLinkExcepted(URI link) {