Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ViewSonePage.java
1 /*
2  * Sone - ViewSonePage.java - Copyright © 2010–2013 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.net.URI;
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;
26 import java.util.Map;
27 import java.util.Set;
28
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.template.Template;
36 import net.pterodactylus.util.template.TemplateContext;
37
38 import com.google.common.base.Optional;
39 import com.google.common.primitives.Ints;
40
41 /**
42  * Lets the user browser another Sone.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class ViewSonePage extends SoneTemplatePage {
47
48         /**
49          * Creates a new “view Sone” page.
50          *
51          * @param template
52          *            The template to render
53          * @param webInterface
54          *            The Sone web interface
55          */
56         public ViewSonePage(Template template, WebInterface webInterface) {
57                 super("viewSone.html", template, webInterface, false);
58         }
59
60         //
61         // TEMPLATEPAGE METHODS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         protected String getPageTitle(FreenetRequest request) {
69                 String soneId = request.getHttpRequest().getParam("sone");
70                 Sone sone = webInterface.getCore().getSone(soneId, false);
71                 if ((sone != null) && (sone.getTime() > 0)) {
72                         String soneName = SoneAccessor.getNiceName(sone);
73                         return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title");
74                 }
75                 return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone");
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
82         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
83                 super.processTemplate(request, templateContext);
84                 String soneId = request.getHttpRequest().getParam("sone");
85                 Sone sone = webInterface.getCore().getSone(soneId, false);
86                 templateContext.set("sone", sone);
87                 templateContext.set("soneId", soneId);
88                 if (sone == null) {
89                         return;
90                 }
91                 List<Post> sonePosts = sone.getPosts();
92                 sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone));
93                 Collections.sort(sonePosts, Post.TIME_COMPARATOR);
94                 Pagination<Post> postPagination = new Pagination<Post>(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("postPage"))).or(0));
95                 templateContext.set("postPagination", postPagination);
96                 templateContext.set("posts", postPagination.getItems());
97                 Set<PostReply> replies = sone.getReplies();
98                 final Map<Post, List<PostReply>> repliedPosts = new HashMap<Post, List<PostReply>>();
99                 for (PostReply reply : replies) {
100                         Post post = reply.getPost();
101                         if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) {
102                                 continue;
103                         }
104                         repliedPosts.put(post, webInterface.getCore().getReplies(post));
105                 }
106                 List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
107                 Collections.sort(posts, new Comparator<Post>() {
108
109                         @Override
110                         public int compare(Post leftPost, Post rightPost) {
111                                 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                         }
113
114                 });
115
116                 Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("repliedPostPage"))).or(0));
117                 templateContext.set("repliedPostPagination", repliedPostPagination);
118                 templateContext.set("repliedPosts", repliedPostPagination.getItems());
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public boolean isLinkExcepted(URI link) {
126                 return true;
127         }
128
129 }