f56c5b7966da54e44390014a0c55d41dedafcc64
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetPostAjaxPage.java
1 /*
2  * Sone - GetPostAjaxPage.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.Post;
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.Template;
28 import net.pterodactylus.util.template.TemplateContext;
29 import net.pterodactylus.util.template.TemplateException;
30
31 /**
32  * This AJAX handler retrieves information and rendered representation of a
33  * {@link Post}.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class GetPostAjaxPage extends JsonPage {
38
39         /** The template to render for posts. */
40         private final Template postTemplate;
41
42         /**
43          * Creates a new “get post” AJAX handler.
44          *
45          * @param webInterface
46          *            The Sone web interface
47          * @param postTemplate
48          *            The template to render for posts
49          */
50         public GetPostAjaxPage(WebInterface webInterface, Template postTemplate) {
51                 super("getPost.ajax", webInterface);
52                 this.postTemplate = postTemplate;
53         }
54
55         /**
56          * {@inheritDoc}
57          */
58         @Override
59         protected JsonObject createJsonObject(Request request) {
60                 String postId = request.getHttpRequest().getParam("post");
61                 Post post = webInterface.getCore().getPost(postId, false);
62                 if (post == null) {
63                         return createErrorJsonObject("invalid-post-id");
64                 }
65                 return createSuccessJsonObject().put("post", createJsonPost(request, post, getCurrentSone(request.getToadletContext())));
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         protected boolean needsFormPassword() {
73                 return false;
74         }
75
76         //
77         // PRIVATE METHODS
78         //
79
80         /**
81          * Creates a JSON object from the given post. The JSON object will only
82          * contain the ID of the post, its time, and its rendered HTML code.
83          *
84          * @param request
85          *            The request being processed
86          * @param post
87          *            The post to create a JSON object from
88          * @param currentSone
89          *            The currently logged in Sone (to store in the template)
90          * @return The JSON representation of the post
91          */
92         private JsonObject createJsonPost(Request request, Post post, Sone currentSone) {
93                 JsonObject jsonPost = new JsonObject();
94                 jsonPost.put("id", post.getId());
95                 jsonPost.put("sone", post.getSone().getId());
96                 jsonPost.put("recipient", (post.getRecipient() == null) ? null : post.getRecipient().getId());
97                 jsonPost.put("time", post.getTime());
98                 StringWriter stringWriter = new StringWriter();
99                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext();
100                 templateContext.set("core", webInterface.getCore());
101                 templateContext.set("request", request);
102                 templateContext.set("post", post);
103                 templateContext.set("currentSone", currentSone);
104                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
105                 try {
106                         postTemplate.render(templateContext, stringWriter);
107                 } catch (TemplateException te1) {
108                         /* TODO - shouldn’t happen. */
109                 } finally {
110                         Closer.close(stringWriter);
111                 }
112                 jsonPost.put("html", stringWriter.toString());
113                 return jsonPost;
114         }
115
116 }