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.notify.ListNotificationFilters;
33 import net.pterodactylus.sone.template.SoneAccessor;
34 import net.pterodactylus.sone.web.WebInterface;
35 import net.pterodactylus.util.filter.Filter;
36 import net.pterodactylus.util.filter.Filters;
37 import net.pterodactylus.util.json.JsonArray;
38 import net.pterodactylus.util.json.JsonObject;
39 import net.pterodactylus.util.notify.Notification;
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");
53 * Creates a new “get status” AJAX handler.
56 * The Sone web interface
58 public GetStatusAjaxPage(WebInterface webInterface) {
59 super("getStatus.ajax", webInterface);
66 protected JsonObject createJsonObject(Request request) {
67 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
69 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "false"));
70 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
72 sones.addAll(webInterface.getCore().getSones());
74 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
75 if (loadSoneIds.length() > 0) {
76 String[] soneIds = loadSoneIds.split(",");
77 for (String soneId : soneIds) {
78 /* just add it, we skip null further down. */
79 sones.add(webInterface.getCore().getSone(soneId, false));
83 JsonArray jsonSones = new JsonArray();
84 for (Sone sone : sones) {
88 JsonObject jsonSone = createJsonSone(sone);
89 jsonSones.add(jsonSone);
91 /* load notifications. */
92 List<Notification> notifications = ListNotificationFilters.filterNotifications(new ArrayList<Notification>(webInterface.getNotifications().getNotifications()), currentSone);
93 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
94 JsonArray jsonNotificationInformations = new JsonArray();
95 for (Notification notification : notifications) {
96 jsonNotificationInformations.add(createJsonNotificationInformation(notification));
99 Set<Post> newPosts = webInterface.getNewPosts();
100 if (currentSone != null) {
101 newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
104 public boolean filterObject(Post post) {
105 return ListNotificationFilters.isPostVisible(currentSone, post);
110 JsonArray jsonPosts = new JsonArray();
111 for (Post post : newPosts) {
112 JsonObject jsonPost = new JsonObject();
113 jsonPost.put("id", post.getId());
114 jsonPost.put("sone", post.getSone().getId());
115 jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
116 jsonPost.put("time", post.getTime());
117 jsonPosts.add(jsonPost);
119 /* load new replies. */
120 Set<Reply> newReplies = webInterface.getNewReplies();
121 if (currentSone != null) {
122 newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
125 public boolean filterObject(Reply reply) {
126 return ListNotificationFilters.isReplyVisible(currentSone, reply);
131 JsonArray jsonReplies = new JsonArray();
132 for (Reply reply : newReplies) {
133 JsonObject jsonReply = new JsonObject();
134 jsonReply.put("id", reply.getId());
135 jsonReply.put("sone", reply.getSone().getId());
136 jsonReply.put("post", reply.getPost().getId());
137 jsonReply.put("postSone", reply.getPost().getSone().getId());
138 jsonReplies.add(jsonReply);
140 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
147 protected boolean needsFormPassword() {
155 protected boolean requiresLogin() {
164 * Creates a JSON object from the given Sone.
167 * The Sone to convert to a JSON object
168 * @return The JSON representation of the given Sone
170 private JsonObject createJsonSone(Sone sone) {
171 JsonObject jsonSone = new JsonObject();
172 jsonSone.put("id", sone.getId());
173 jsonSone.put("name", SoneAccessor.getNiceName(sone));
174 jsonSone.put("local", sone.getInsertUri() != null);
175 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
176 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
177 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
178 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
179 synchronized (dateFormat) {
180 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
182 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, System.currentTimeMillis() - sone.getTime()).getText());
187 * Creates a JSON object that only contains the ID and the last-updated time
188 * of the given notification.
190 * @see Notification#getId()
191 * @see Notification#getLastUpdatedTime()
192 * @param notification
194 * @return A JSON object containing the notification ID and last-updated
197 private JsonObject createJsonNotificationInformation(Notification notification) {
198 JsonObject jsonNotification = new JsonObject();
199 jsonNotification.put("id", notification.getId());
200 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
201 return jsonNotification;