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