Replace getSoneStatus and getNotifications with getStatus.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.java
1 /*
2  * Sone - GetStatusAjaxPage.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.ajax;
19
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;
26 import java.util.Set;
27
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;
34
35 /**
36  * The “get status” AJAX handler returns all information that is necessary to
37  * update the web interface in real-time.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class GetStatusAjaxPage extends JsonPage {
42
43         /** Date formatter. */
44         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
45
46         /**
47          * Creates a new “get status” AJAX handler.
48          *
49          * @param webInterface
50          *            The Sone web interface
51          */
52         public GetStatusAjaxPage(WebInterface webInterface) {
53                 super("ajax/getStatus.ajax", webInterface);
54         }
55
56         /**
57          * {@inheritDoc}
58          */
59         @Override
60         protected JsonObject createJsonObject(Request request) {
61                 /* load Sones. */
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);
68                 }
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));
76                 }
77                 JsonArray jsonRemovedNotifications = new JsonArray();
78                 for (Notification notification : removedNotifications) {
79                         jsonRemovedNotifications.add(createJsonNotification(notification));
80                 }
81                 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications);
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         protected boolean needsFormPassword() {
89                 return false;
90         }
91
92         //
93         // PRIVATE METHODS
94         //
95
96         /**
97          * Creates a JSON object from the given Sone.
98          *
99          * @param sone
100          *            The Sone to convert to a JSON object
101          * @return The JSON representation of the given Sone
102          */
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())));
113                 }
114                 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
115                 return jsonSone;
116         }
117
118         /**
119          * Creates a JSON object from the given notification.
120          *
121          * @param notification
122          *            The notification to create a JSON object
123          * @return The JSON object
124          */
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;
133         }
134
135 }