Return optionals of fields.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / MarkAsKnownPage.java
1 /*
2  * Sone - MarkAsKnownPage.java - Copyright © 2011–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.util.StringTokenizer;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.PostReply;
24 import net.pterodactylus.sone.data.Reply;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.web.page.FreenetRequest;
27 import net.pterodactylus.util.template.Template;
28 import net.pterodactylus.util.template.TemplateContext;
29
30 import com.google.common.base.Optional;
31
32 /**
33  * Page that lets the user mark a number of {@link Sone}s, {@link Post}s, or
34  * {@link Reply Replie}s as known.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class MarkAsKnownPage extends SoneTemplatePage {
39
40         /**
41          * Creates a new “mark as known” page.
42          *
43          * @param template
44          *            The template to render
45          * @param webInterface
46          *            The Sone web interface
47          */
48         public MarkAsKnownPage(Template template, WebInterface webInterface) {
49                 super("markAsKnown.html", template, "Page.MarkAsKnown.Title", webInterface);
50         }
51
52         //
53         // SONETEMPLATEPAGE METHODS
54         //
55
56         @Override
57         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
58                 super.processTemplate(request, templateContext);
59                 String type = request.getHttpRequest().getPartAsStringFailsafe("type", 5);
60                 if (!type.equals("sone") && !type.equals("post") && !type.equals("reply")) {
61                         throw new RedirectException("invalid.html");
62                 }
63                 String ids = request.getHttpRequest().getPartAsStringFailsafe("id", 65536);
64                 for (StringTokenizer idTokenizer = new StringTokenizer(ids); idTokenizer.hasMoreTokens();) {
65                         String id = idTokenizer.nextToken();
66                         if (type.equals("post")) {
67                                 Optional<Post> post = webInterface.getCore().getDatabase().getPost(id);
68                                 if (!post.isPresent()) {
69                                         continue;
70                                 }
71                                 webInterface.getCore().markPostKnown(post.get());
72                         } else if (type.equals("reply")) {
73                                 Optional<PostReply> reply = webInterface.getCore().getDatabase().getPostReply(id);
74                                 if (!reply.isPresent()) {
75                                         continue;
76                                 }
77                                 reply.get().modify().setKnown().update(webInterface.getCore().postReplyUpdated());
78                         } else if (type.equals("sone")) {
79                                 Optional<Sone> sone = webInterface.getCore().getSone(id);
80                                 if (!sone.isPresent()) {
81                                         continue;
82                                 }
83                                 webInterface.getCore().markSoneKnown(sone.get());
84                         }
85                 }
86                 String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
87                 throw new RedirectException(returnPage);
88         }
89
90 }