Mark all elements as known in the processing method, not after rendering.
[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.sone.template.SoneAccessor;
32 import net.pterodactylus.util.collection.ListBuilder;
33 import net.pterodactylus.util.collection.Pagination;
34 import net.pterodactylus.util.number.Numbers;
35 import net.pterodactylus.util.template.Template;
36 import net.pterodactylus.util.template.TemplateContext;
37
38 /**
39  * Lets the user browser another Sone.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class ViewSonePage extends SoneTemplatePage {
44
45         /**
46          * Creates a new “view Sone” page.
47          *
48          * @param template
49          *            The template to render
50          * @param webInterface
51          *            The Sone web interface
52          */
53         public ViewSonePage(Template template, WebInterface webInterface) {
54                 super("viewSone.html", template, webInterface, false);
55         }
56
57         //
58         // TEMPLATEPAGE METHODS
59         //
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
65         protected String getPageTitle(Request request) {
66                 String soneId = request.getHttpRequest().getParam("sone");
67                 Sone sone = webInterface.getCore().getSone(soneId, false);
68                 if ((sone != null) && (sone.getTime() > 0)) {
69                         String soneName = SoneAccessor.getNiceName(sone);
70                         return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title");
71                 }
72                 return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone");
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
80                 super.processTemplate(request, templateContext);
81                 String soneId = request.getHttpRequest().getParam("sone");
82                 Sone sone = webInterface.getCore().getSone(soneId, false);
83                 templateContext.set("sone", sone);
84                 List<Post> sonePosts = sone.getPosts();
85                 sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone));
86                 Collections.sort(sonePosts, Post.TIME_COMPARATOR);
87                 Pagination<Post> postPagination = new Pagination<Post>(sonePosts, 10).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
88                 templateContext.set("postPagination", postPagination);
89                 templateContext.set("posts", postPagination.getItems());
90                 Set<Reply> replies = sone.getReplies();
91                 final Map<Post, List<Reply>> repliedPosts = new HashMap<Post, List<Reply>>();
92                 for (Reply reply : replies) {
93                         Post post = reply.getPost();
94                         if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) {
95                                 continue;
96                         }
97                         repliedPosts.put(post, webInterface.getCore().getReplies(post));
98                 }
99                 List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
100                 Collections.sort(posts, new Comparator<Post>() {
101
102                         @Override
103                         public int compare(Post leftPost, Post rightPost) {
104                                 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime() - repliedPosts.get(leftPost).get(0).getTime()));
105                         }
106
107                 });
108
109                 Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, 10).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
110                 templateContext.set("repliedPostPagination", repliedPostPagination);
111                 templateContext.set("repliedPosts", repliedPostPagination.getItems());
112
113                 /* mark Sone and posts and replies as known. */
114                 webInterface.getCore().markSoneKnown(sone);
115                 for (Post post : new ListBuilder<Post>().addAll(postPagination.getItems()).addAll(repliedPostPagination.getItems()).get()) {
116                         if (post.getSone() != null) {
117                                 webInterface.getCore().markPostKnown(post);
118                         }
119                         for (Reply reply : webInterface.getCore().getReplies(post)) {
120                                 webInterface.getCore().markReplyKnown(reply);
121                         }
122                 }
123         }
124
125 }