package net.pterodactylus.sone.web.ajax;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.io.StringWriter;
import net.pterodactylus.sone.data.Reply;
-import net.pterodactylus.sone.template.SoneAccessor;
+import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.util.template.DataProvider;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateException;
/**
* This AJAX page returns the details of a reply.
*/
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) {
- super("ajax/getReply.ajax", webInterface);
+ public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
+ super("getReply.ajax", webInterface);
+ this.replyTemplate = replyTemplate;
}
//
if ((reply == null) || (reply.getSone() == null)) {
return createErrorJsonObject("invalid-reply-id");
}
- 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 createSuccessJsonObject().put("reply", createJsonReply(reply, getCurrentSone(request.getToadletContext())));
}
/**
return false;
}
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Creates a JSON representation of the given reply.
+ *
+ * @param reply
+ * The reply to convert
+ * @param currentSone
+ * The currently logged in Sone (to store in the template)
+ * @return The JSON representation of the reply
+ */
+ private JsonObject createJsonReply(Reply reply, Sone currentSone) {
+ JsonObject jsonReply = new JsonObject();
+ jsonReply.put("id", reply.getId());
+ jsonReply.put("postId", reply.getPost().getId());
+ jsonReply.put("soneId", reply.getSone().getId());
+ jsonReply.put("time", reply.getTime());
+ StringWriter stringWriter = new StringWriter();
+ DataProvider dataProvider = replyTemplate.createDataProvider();
+ dataProvider.setData("reply", reply);
+ dataProvider.setData("currentSone", currentSone);
+ try {
+ replyTemplate.render(dataProvider, stringWriter);
+ } catch (TemplateException te1) {
+ /* TODO - shouldn’t happen. */
+ } finally {
+ Closer.close(stringWriter);
+ }
+ return jsonReply.put("html", stringWriter.toString());
+ }
+
}