Add method to create a “success” JSON object.
[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 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.template.SoneAccessor;
27 import net.pterodactylus.sone.web.WebInterface;
28 import net.pterodactylus.util.io.Closer;
29 import net.pterodactylus.util.json.JsonObject;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateException;
32
33 /**
34  * This AJAX page returns the details of a reply.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class GetReplyAjaxPage extends JsonPage {
39
40         /** Date formatter. */
41         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
42
43         /** The template to render. */
44         private final Template replyTemplate;
45
46         /**
47          * Creates a new “get reply” page.
48          *
49          * @param webInterface
50          *            The Sone web interface
51          * @param replyTemplate
52          *            The template to render
53          */
54         public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
55                 super("ajax/getReply.ajax", webInterface);
56                 this.replyTemplate = replyTemplate;
57         }
58
59         //
60         // JSONPAGE METHODS
61         //
62
63         /**
64          * {@inheritDoc}
65          */
66         @Override
67         protected JsonObject createJsonObject(Request request) {
68                 String replyId = request.getHttpRequest().getParam("reply");
69                 Reply reply = webInterface.getCore().getReply(replyId);
70                 if ((reply == null) || (reply.getSone() == null)) {
71                         return createErrorJsonObject("invalid-reply-id");
72                 }
73                 replyTemplate.set("reply", reply);
74                 replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
75                 StringWriter templateWriter = new StringWriter();
76                 try {
77                         replyTemplate.render(templateWriter);
78                 } catch (TemplateException te1) {
79                         /* TODO - shouldn’t happen. */
80                 } finally {
81                         Closer.close(templateWriter);
82                 }
83                 synchronized (dateFormat) {
84                         return createSuccessJsonObject().put("soneId", reply.getSone().getId()).put("soneName", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("displayTime", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()).put("html", templateWriter.toString());
85                 }
86         }
87
88         /**
89          * {@inheritDoc}
90          */
91         @Override
92         protected boolean needsFormPassword() {
93                 return false;
94         }
95
96 }