Allow sender selection on new posts.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetReplyAjaxPage.java
1 /*
2  * Sone - GetReplyAjaxPage.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.ajax;
19
20 import java.io.StringWriter;
21
22 import net.pterodactylus.sone.data.Reply;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.sone.web.WebInterface;
25 import net.pterodactylus.util.io.Closer;
26 import net.pterodactylus.util.json.JsonObject;
27 import net.pterodactylus.util.template.DataProvider;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateException;
30
31 /**
32  * This AJAX page returns the details of a reply.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class GetReplyAjaxPage extends JsonPage {
37
38         /** The template to render. */
39         private final Template replyTemplate;
40
41         /**
42          * Creates a new “get reply” page.
43          *
44          * @param webInterface
45          *            The Sone web interface
46          * @param replyTemplate
47          *            The template to render
48          */
49         public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
50                 super("getReply.ajax", webInterface);
51                 this.replyTemplate = replyTemplate;
52         }
53
54         //
55         // JSONPAGE METHODS
56         //
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         protected JsonObject createJsonObject(Request request) {
63                 String replyId = request.getHttpRequest().getParam("reply");
64                 Reply reply = webInterface.getCore().getReply(replyId);
65                 if ((reply == null) || (reply.getSone() == null)) {
66                         return createErrorJsonObject("invalid-reply-id");
67                 }
68                 return createSuccessJsonObject().put("reply", createJsonReply(reply, getCurrentSone(request.getToadletContext())));
69         }
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         protected boolean needsFormPassword() {
76                 return false;
77         }
78
79         //
80         // PRIVATE METHODS
81         //
82
83         /**
84          * Creates a JSON representation of the given reply.
85          *
86          * @param reply
87          *            The reply to convert
88          * @param currentSone
89          *            The currently logged in Sone (to store in the template)
90          * @return The JSON representation of the reply
91          */
92         private JsonObject createJsonReply(Reply reply, Sone currentSone) {
93                 JsonObject jsonReply = new JsonObject();
94                 jsonReply.put("id", reply.getId());
95                 jsonReply.put("postId", reply.getPost().getId());
96                 jsonReply.put("soneId", reply.getSone().getId());
97                 jsonReply.put("time", reply.getTime());
98                 StringWriter stringWriter = new StringWriter();
99                 DataProvider dataProvider = replyTemplate.createDataProvider();
100                 dataProvider.set("reply", reply);
101                 dataProvider.set("currentSone", currentSone);
102                 try {
103                         replyTemplate.render(dataProvider, stringWriter);
104                 } catch (TemplateException te1) {
105                         /* TODO - shouldn’t happen. */
106                 } finally {
107                         Closer.close(stringWriter);
108                 }
109                 return jsonReply.put("html", stringWriter.toString());
110         }
111
112 }