2 * Sone - GetReplyAjaxPage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.ajax;
20 import java.io.StringWriter;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
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;
34 * This AJAX page returns the details of a reply.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class GetReplyAjaxPage extends JsonPage {
40 /** Date formatter. */
41 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
43 /** The template to render. */
44 private final Template replyTemplate;
47 * Creates a new “get reply” page.
50 * The Sone web interface
51 * @param replyTemplate
52 * The template to render
54 public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
55 super("ajax/getReply.ajax", webInterface);
56 this.replyTemplate = replyTemplate;
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");
73 replyTemplate.set("reply", reply);
74 replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
75 StringWriter templateWriter = new StringWriter();
77 replyTemplate.render(templateWriter);
78 } catch (TemplateException te1) {
79 /* TODO - shouldn’t happen. */
81 Closer.close(templateWriter);
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());
92 protected boolean needsFormPassword() {