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.Sone;
29 import net.pterodactylus.sone.template.SoneAccessor;
30 import net.pterodactylus.sone.web.WebInterface;
31 import net.pterodactylus.util.json.JsonArray;
32 import net.pterodactylus.util.json.JsonObject;
33 import net.pterodactylus.util.notify.Notification;
36 * The “get status” AJAX handler returns all information that is necessary to
37 * update the web interface in real-time.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class GetStatusAjaxPage extends JsonPage {
43 /** Date formatter. */
44 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
47 * Creates a new “get status” AJAX handler.
50 * The Sone web interface
52 public GetStatusAjaxPage(WebInterface webInterface) {
53 super("ajax/getStatus.ajax", webInterface);
60 protected JsonObject createJsonObject(Request request) {
62 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
63 Set<Sone> sones = loadAllSones ? webInterface.getCore().getSones() : Collections.singleton(getCurrentSone(request.getToadletContext()));
64 JsonArray jsonSones = new JsonArray();
65 for (Sone sone : sones) {
66 JsonObject jsonSone = createJsonSone(sone);
67 jsonSones.add(jsonSone);
69 /* load notifications. */
70 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
71 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
72 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
73 JsonArray jsonNotifications = new JsonArray();
74 for (Notification notification : notifications) {
75 jsonNotifications.add(createJsonNotification(notification));
77 JsonArray jsonRemovedNotifications = new JsonArray();
78 for (Notification notification : removedNotifications) {
79 jsonRemovedNotifications.add(createJsonNotification(notification));
81 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications);
88 protected boolean needsFormPassword() {
97 * Creates a JSON object from the given Sone.
100 * The Sone to convert to a JSON object
101 * @return The JSON representation of the given Sone
103 private JsonObject createJsonSone(Sone sone) {
104 JsonObject jsonSone = new JsonObject();
105 jsonSone.put("id", sone.getId());
106 jsonSone.put("name", SoneAccessor.getNiceName(sone));
107 jsonSone.put("local", sone.getInsertUri() != null);
108 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
109 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
110 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
111 synchronized (dateFormat) {
112 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
114 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
119 * Creates a JSON object from the given notification.
121 * @param notification
122 * The notification to create a JSON object
123 * @return The JSON object
125 private static JsonObject createJsonNotification(Notification notification) {
126 JsonObject jsonNotification = new JsonObject();
127 jsonNotification.put("id", notification.getId());
128 jsonNotification.put("text", notification.toString());
129 jsonNotification.put("createdTime", notification.getCreatedTime());
130 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
131 jsonNotification.put("dismissable", notification.isDismissable());
132 return jsonNotification;