2 * Sone - GetStatusAjaxPage.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.ArrayList;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.Reply;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.template.SoneAccessor;
33 import net.pterodactylus.sone.web.WebInterface;
34 import net.pterodactylus.util.io.Closer;
35 import net.pterodactylus.util.json.JsonArray;
36 import net.pterodactylus.util.json.JsonObject;
37 import net.pterodactylus.util.notify.Notification;
38 import net.pterodactylus.util.template.Template;
39 import net.pterodactylus.util.template.TemplateException;
42 * The “get status” AJAX handler returns all information that is necessary to
43 * update the web interface in real-time.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class GetStatusAjaxPage extends JsonPage {
49 /** Date formatter. */
50 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
52 /** The template to render posts. */
53 private final Template postTemplate;
55 /** The template to render replies. */
56 private final Template replyTemplate;
59 * Creates a new “get status” AJAX handler.
62 * The Sone web interface
64 * The template to render for posts
65 * @param replyTemplate
66 * The template to render for replies
68 public GetStatusAjaxPage(WebInterface webInterface, Template postTemplate, Template replyTemplate) {
69 super("ajax/getStatus.ajax", webInterface);
70 this.postTemplate = postTemplate;
71 this.replyTemplate = replyTemplate;
78 protected JsonObject createJsonObject(Request request) {
80 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
81 Set<Sone> sones = loadAllSones ? webInterface.getCore().getSones() : Collections.singleton(getCurrentSone(request.getToadletContext()));
82 JsonArray jsonSones = new JsonArray();
83 for (Sone sone : sones) {
84 JsonObject jsonSone = createJsonSone(sone);
85 jsonSones.add(jsonSone);
87 /* load notifications. */
88 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
89 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
90 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
91 JsonArray jsonNotifications = new JsonArray();
92 for (Notification notification : notifications) {
93 jsonNotifications.add(createJsonNotification(notification));
95 JsonArray jsonRemovedNotifications = new JsonArray();
96 for (Notification notification : removedNotifications) {
97 jsonRemovedNotifications.add(createJsonNotification(notification));
100 postTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
101 Set<Post> newPosts = webInterface.getNewPosts();
102 JsonArray jsonPosts = new JsonArray();
103 for (Post post : newPosts) {
104 jsonPosts.add(createJsonPost(post));
106 /* load new replies. */
107 replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
108 Set<Reply> newReplies = webInterface.getNewReplies();
109 JsonArray jsonReplies = new JsonArray();
110 for (Reply reply : newReplies) {
111 jsonReplies.add(createJsonReply(reply));
113 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
120 protected boolean needsFormPassword() {
129 * Creates a JSON object from the given Sone.
132 * The Sone to convert to a JSON object
133 * @return The JSON representation of the given Sone
135 private JsonObject createJsonSone(Sone sone) {
136 JsonObject jsonSone = new JsonObject();
137 jsonSone.put("id", sone.getId());
138 jsonSone.put("name", SoneAccessor.getNiceName(sone));
139 jsonSone.put("local", sone.getInsertUri() != null);
140 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
141 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
142 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
143 synchronized (dateFormat) {
144 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
146 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
151 * Creates a JSON object from the given post. The JSON object will only
152 * contain the ID of the post, its time, and its rendered HTML code.
155 * The post to create a JSON object from
156 * @return The JSON representation of the post
158 private JsonObject createJsonPost(Post post) {
159 JsonObject jsonPost = new JsonObject();
160 jsonPost.put("id", post.getId());
161 jsonPost.put("time", post.getTime());
162 StringWriter stringWriter = new StringWriter();
163 postTemplate.set("post", post);
165 postTemplate.render(stringWriter);
166 } catch (TemplateException te1) {
167 /* TODO - shouldn’t happen. */
169 Closer.close(stringWriter);
171 return jsonPost.put("html", stringWriter.toString());
175 * Creates a JSON object from the given reply. The JSON object will only
176 * contain the ID of the reply, the ID of its post, its time, and its
177 * rendered HTML code.
180 * The reply to create a JSON object from
181 * @return The JSON representation of the reply
183 private JsonObject createJsonReply(Reply reply) {
184 JsonObject jsonPost = new JsonObject();
185 jsonPost.put("postId", reply.getPost().getId());
186 jsonPost.put("id", reply.getId());
187 jsonPost.put("time", reply.getTime());
188 StringWriter stringWriter = new StringWriter();
189 replyTemplate.set("reply", reply);
191 replyTemplate.render(stringWriter);
192 } catch (TemplateException te1) {
193 /* TODO - shouldn’t happen. */
195 Closer.close(stringWriter);
197 return jsonPost.put("html", stringWriter.toString());
201 * Creates a JSON object from the given notification.
203 * @param notification
204 * The notification to create a JSON object
205 * @return The JSON object
207 private static JsonObject createJsonNotification(Notification notification) {
208 JsonObject jsonNotification = new JsonObject();
209 jsonNotification.put("id", notification.getId());
210 jsonNotification.put("text", notification.toString());
211 jsonNotification.put("createdTime", notification.getCreatedTime());
212 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
213 jsonNotification.put("dismissable", notification.isDismissable());
214 return jsonNotification;