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.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.List;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.Reply;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.sone.web.WebInterface;
33 import net.pterodactylus.util.json.JsonArray;
34 import net.pterodactylus.util.json.JsonObject;
35 import net.pterodactylus.util.notify.Notification;
38 * The “get status” AJAX handler returns all information that is necessary to
39 * update the web interface in real-time.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class GetStatusAjaxPage extends JsonPage {
45 /** Date formatter. */
46 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
49 * Creates a new “get status” AJAX handler.
52 * The Sone web interface
54 public GetStatusAjaxPage(WebInterface webInterface) {
55 super("ajax/getStatus.ajax", webInterface);
62 protected JsonObject createJsonObject(Request request) {
64 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
65 Set<Sone> sones = loadAllSones ? webInterface.getCore().getSones() : Collections.singleton(getCurrentSone(request.getToadletContext()));
66 JsonArray jsonSones = new JsonArray();
67 for (Sone sone : sones) {
68 JsonObject jsonSone = createJsonSone(sone);
69 jsonSones.add(jsonSone);
71 /* load notifications. */
72 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
73 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
74 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
75 JsonArray jsonNotifications = new JsonArray();
76 for (Notification notification : notifications) {
77 jsonNotifications.add(createJsonNotification(notification));
79 JsonArray jsonRemovedNotifications = new JsonArray();
80 for (Notification notification : removedNotifications) {
81 jsonRemovedNotifications.add(createJsonNotification(notification));
84 Set<Post> newPosts = webInterface.getNewPosts();
85 JsonArray jsonPosts = new JsonArray();
86 for (Post post : newPosts) {
87 jsonPosts.add(post.getId());
89 /* load new replies. */
90 Set<Reply> newReplies = webInterface.getNewReplies();
91 JsonArray jsonReplies = new JsonArray();
92 for (Reply reply : newReplies) {
93 jsonReplies.add(reply.getId());
95 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
102 protected boolean needsFormPassword() {
111 * Creates a JSON object from the given Sone.
114 * The Sone to convert to a JSON object
115 * @return The JSON representation of the given Sone
117 private JsonObject createJsonSone(Sone sone) {
118 JsonObject jsonSone = new JsonObject();
119 jsonSone.put("id", sone.getId());
120 jsonSone.put("name", SoneAccessor.getNiceName(sone));
121 jsonSone.put("local", sone.getInsertUri() != null);
122 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
123 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
124 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
125 synchronized (dateFormat) {
126 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
128 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
133 * Creates a JSON object from the given notification.
135 * @param notification
136 * The notification to create a JSON object
137 * @return The JSON object
139 private static JsonObject createJsonNotification(Notification notification) {
140 JsonObject jsonNotification = new JsonObject();
141 jsonNotification.put("id", notification.getId());
142 jsonNotification.put("text", notification.toString());
143 jsonNotification.put("createdTime", notification.getCreatedTime());
144 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
145 jsonNotification.put("dismissable", notification.isDismissable());
146 return jsonNotification;