Add posts a Sone replied to to “view Sone” page.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ViewSonePage.java
1 /*
2  * Sone - ViewSonePage.java - Copyright © 2010 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.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
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.util.collection.Pagination;
32 import net.pterodactylus.util.number.Numbers;
33 import net.pterodactylus.util.template.Template;
34 import net.pterodactylus.util.template.TemplateContext;
35
36 /**
37  * Lets the user browser another Sone.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class ViewSonePage extends SoneTemplatePage {
42
43         /**
44          * Creates a new “view Sone” page.
45          *
46          * @param template
47          *            The template to render
48          * @param webInterface
49          *            The Sone web interface
50          */
51         public ViewSonePage(Template template, WebInterface webInterface) {
52                 super("viewSone.html", template, "Page.ViewSone.Title", webInterface, false);
53         }
54
55         //
56         // TEMPLATEPAGE METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
64                 super.processTemplate(request, templateContext);
65                 String soneId = request.getHttpRequest().getParam("sone");
66                 Sone sone = webInterface.getCore().getSone(soneId, false);
67                 templateContext.set("sone", sone);
68                 Set<Reply> replies = sone.getReplies();
69                 final Map<Post, List<Reply>> repliedPosts = new HashMap<Post, List<Reply>>();
70                 for (Reply reply : replies) {
71                         Post post = reply.getPost();
72                         if (repliedPosts.containsKey(post) || sone.equals(post.getSone())) {
73                                 continue;
74                         }
75                         repliedPosts.put(post, webInterface.getCore().getReplies(post));
76                 }
77                 List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
78                 Collections.sort(posts, new Comparator<Post>() {
79
80                         @Override
81                         public int compare(Post leftPost, Post rightPost) {
82                                 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime() - repliedPosts.get(leftPost).get(0).getTime()));
83                         }
84
85                 });
86
87                 Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, 10).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
88                 templateContext.set("repliedPostPagination", repliedPostPagination);
89                 templateContext.set("repliedPosts", repliedPostPagination.getItems());
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         @SuppressWarnings("unchecked")
97         protected void postProcess(Request request, TemplateContext templateContext) {
98                 Sone sone = (Sone) templateContext.get("sone");
99                 if (sone == null) {
100                         return;
101                 }
102                 webInterface.getCore().markSoneKnown(sone);
103                 List<Post> posts = sone.getPosts();
104                 posts.addAll((List<Post>) templateContext.get("repliedPosts"));
105                 for (Post post : posts) {
106                         if (post.getSone() != null) {
107                                 webInterface.getCore().markPostKnown(post);
108                         }
109                         for (Reply reply : webInterface.getCore().getReplies(post)) {
110                                 webInterface.getCore().markReplyKnown(reply);
111                         }
112                 }
113         }
114
115 }