Return rendered HTML for the reply element.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetReplyAjaxPage.java
index d00c7dd..292f3bf 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web.ajax;
 
+import java.io.StringWriter;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -24,7 +25,10 @@ import java.util.Date;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateException;
 
 /**
  * This AJAX page returns the details of a reply.
@@ -36,13 +40,20 @@ public class GetReplyAjaxPage extends JsonPage {
        /** Date formatter. */
        private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
 
+       /** The template to render. */
+       private final Template replyTemplate;
+
        /**
         * Creates a new “get reply” page.
         *
         * @param webInterface
+        *            The Sone web interface
+        * @param replyTemplate
+        *            The template to render
         */
-       public GetReplyAjaxPage(WebInterface webInterface) {
+       public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
                super("ajax/getReply.ajax", webInterface);
+               this.replyTemplate = replyTemplate;
        }
 
        //
@@ -59,8 +70,18 @@ public class GetReplyAjaxPage extends JsonPage {
                if ((reply == null) || (reply.getSone() == null)) {
                        return createErrorJsonObject("invalid-reply-id");
                }
+               replyTemplate.set("reply", reply);
+               replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
+               StringWriter templateWriter = new StringWriter();
+               try {
+                       replyTemplate.render(templateWriter);
+               } catch (TemplateException te1) {
+                       /* TODO - shouldn’t happen. */
+               } finally {
+                       Closer.close(templateWriter);
+               }
                synchronized (dateFormat) {
-                       return new JsonObject().put("success", true).put("sone-id", reply.getSone().getId()).put("sone-name", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("display-time", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText());
+                       return new JsonObject().put("success", true).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());
                }
        }