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.HashSet;
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.json.JsonArray;
35 import net.pterodactylus.util.json.JsonObject;
36 import net.pterodactylus.util.notify.Notification;
39 * The āget statusā AJAX handler returns all information that is necessary to
40 * update the web interface in real-time.
42 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
44 public class GetStatusAjaxPage extends JsonPage {
46 /** Date formatter. */
47 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
50 * Creates a new āget statusā AJAX handler.
53 * The Sone web interface
55 public GetStatusAjaxPage(WebInterface webInterface) {
56 super("getStatus.ajax", webInterface);
63 protected JsonObject createJsonObject(Request request) {
65 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
66 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext())));
68 sones.addAll(webInterface.getCore().getSones());
70 JsonArray jsonSones = new JsonArray();
71 for (Sone sone : sones) {
72 JsonObject jsonSone = createJsonSone(sone);
73 jsonSones.add(jsonSone);
75 /* load notifications. */
76 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
77 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
78 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
79 JsonArray jsonNotifications = new JsonArray();
80 for (Notification notification : notifications) {
81 jsonNotifications.add(createJsonNotification(notification));
83 JsonArray jsonRemovedNotifications = new JsonArray();
84 for (Notification notification : removedNotifications) {
85 jsonRemovedNotifications.add(createJsonNotification(notification));
88 Set<Post> newPosts = webInterface.getNewPosts();
89 JsonArray jsonPosts = new JsonArray();
90 for (Post post : newPosts) {
91 jsonPosts.add(post.getId());
93 /* load new replies. */
94 Set<Reply> newReplies = webInterface.getNewReplies();
95 JsonArray jsonReplies = new JsonArray();
96 for (Reply reply : newReplies) {
97 jsonReplies.add(reply.getId());
99 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
106 protected boolean needsFormPassword() {
115 * Creates a JSON object from the given Sone.
118 * The Sone to convert to a JSON object
119 * @return The JSON representation of the given Sone
121 private JsonObject createJsonSone(Sone sone) {
122 JsonObject jsonSone = new JsonObject();
123 jsonSone.put("id", sone.getId());
124 jsonSone.put("name", SoneAccessor.getNiceName(sone));
125 jsonSone.put("local", sone.getInsertUri() != null);
126 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
127 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
128 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
129 synchronized (dateFormat) {
130 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
132 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
137 * Creates a JSON object from the given notification.
139 * @param notification
140 * The notification to create a JSON object
141 * @return The JSON object
143 private static JsonObject createJsonNotification(Notification notification) {
144 JsonObject jsonNotification = new JsonObject();
145 jsonNotification.put("id", notification.getId());
146 jsonNotification.put("text", notification.toString());
147 jsonNotification.put("createdTime", notification.getCreatedTime());
148 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
149 jsonNotification.put("dismissable", notification.isDismissable());
150 return jsonNotification;