Use new template engine.
[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.io.IOException;
21 import java.io.StringWriter;
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.Reply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.template.SoneAccessor;
35 import net.pterodactylus.sone.web.WebInterface;
36 import net.pterodactylus.util.json.JsonArray;
37 import net.pterodactylus.util.json.JsonObject;
38 import net.pterodactylus.util.notify.Notification;
39 import net.pterodactylus.util.notify.TemplateNotification;
40
41 /**
42  * The “get status” AJAX handler returns all information that is necessary to
43  * update the web interface in real-time.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class GetStatusAjaxPage extends JsonPage {
48
49         /** Date formatter. */
50         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
51
52         /**
53          * Creates a new “get status” AJAX handler.
54          *
55          * @param webInterface
56          *            The Sone web interface
57          */
58         public GetStatusAjaxPage(WebInterface webInterface) {
59                 super("getStatus.ajax", webInterface);
60         }
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         protected JsonObject createJsonObject(Request request) {
67                 /* load Sones. */
68                 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
69                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
70                 if (loadAllSones) {
71                         sones.addAll(webInterface.getCore().getSones());
72                 }
73                 JsonArray jsonSones = new JsonArray();
74                 for (Sone sone : sones) {
75                         if (sone == null) {
76                                 continue;
77                         }
78                         JsonObject jsonSone = createJsonSone(sone);
79                         jsonSones.add(jsonSone);
80                 }
81                 /* load notifications. */
82                 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
83                 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
84                 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
85                 JsonArray jsonNotifications = new JsonArray();
86                 for (Notification notification : notifications) {
87                         jsonNotifications.add(createJsonNotification(notification));
88                 }
89                 JsonArray jsonRemovedNotifications = new JsonArray();
90                 for (Notification notification : removedNotifications) {
91                         jsonRemovedNotifications.add(createJsonNotification(notification));
92                 }
93                 /* load new posts. */
94                 Set<Post> newPosts = webInterface.getNewPosts();
95                 JsonArray jsonPosts = new JsonArray();
96                 for (Post post : newPosts) {
97                         JsonObject jsonPost = new JsonObject();
98                         jsonPost.put("id", post.getId());
99                         jsonPost.put("sone", post.getSone().getId());
100                         jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
101                         jsonPost.put("time", post.getTime());
102                         jsonPosts.add(jsonPost);
103                 }
104                 /* load new replies. */
105                 Set<Reply> newReplies = webInterface.getNewReplies();
106                 JsonArray jsonReplies = new JsonArray();
107                 for (Reply reply : newReplies) {
108                         JsonObject jsonReply = new JsonObject();
109                         jsonReply.put("id", reply.getId());
110                         jsonReply.put("sone", reply.getSone().getId());
111                         jsonReply.put("post", reply.getPost().getId());
112                         jsonReply.put("postSone", reply.getPost().getSone().getId());
113                         jsonReplies.add(jsonReply);
114                 }
115                 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
116         }
117
118         /**
119          * {@inheritDoc}
120          */
121         @Override
122         protected boolean needsFormPassword() {
123                 return false;
124         }
125
126         /**
127          * {@inheritDoc}
128          */
129         @Override
130         protected boolean requiresLogin() {
131                 return false;
132         }
133
134         //
135         // PRIVATE METHODS
136         //
137
138         /**
139          * Creates a JSON object from the given Sone.
140          *
141          * @param sone
142          *            The Sone to convert to a JSON object
143          * @return The JSON representation of the given Sone
144          */
145         private JsonObject createJsonSone(Sone sone) {
146                 JsonObject jsonSone = new JsonObject();
147                 jsonSone.put("id", sone.getId());
148                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
149                 jsonSone.put("local", sone.getInsertUri() != null);
150                 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
151                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
152                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
153                 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
154                 synchronized (dateFormat) {
155                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
156                 }
157                 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
158                 return jsonSone;
159         }
160
161         /**
162          * Creates a JSON object from the given notification.
163          *
164          * @param notification
165          *            The notification to create a JSON object
166          * @return The JSON object
167          */
168         private JsonObject createJsonNotification(Notification notification) {
169                 JsonObject jsonNotification = new JsonObject();
170                 jsonNotification.put("id", notification.getId());
171                 StringWriter notificationWriter = new StringWriter();
172                 try {
173                         if (notification instanceof TemplateNotification) {
174                                 ((TemplateNotification) notification).render(webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext()), notificationWriter);
175                         } else {
176                                 notification.render(notificationWriter);
177                         }
178                 } catch (IOException ioe1) {
179                         /* StringWriter never throws, ignore. */
180                 }
181                 jsonNotification.put("text", notificationWriter.toString());
182                 jsonNotification.put("createdTime", notification.getCreatedTime());
183                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
184                 jsonNotification.put("dismissable", notification.isDismissable());
185                 return jsonNotification;
186         }
187
188 }