Return rendered HTML for the reply element.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Nov 2010 23:43:58 +0000 (00:43 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 12 Nov 2010 23:43:58 +0000 (00:43 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java

index 1b4654b..2f10567 100644 (file)
@@ -216,6 +216,7 @@ public class WebInterface {
                Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
                Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
                Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
+               Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
 
                PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
                pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
@@ -245,7 +246,7 @@ public class WebInterface {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
-               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
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());
                }
        }