8b7f5132613243af67de5328df87413e36051a12
[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.StringWriter;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Set;
28
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.io.Closer;
35 import net.pterodactylus.util.json.JsonArray;
36 import net.pterodactylus.util.json.JsonObject;
37 import net.pterodactylus.util.notify.Notification;
38 import net.pterodactylus.util.template.Template;
39 import net.pterodactylus.util.template.TemplateException;
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         /** The template to render posts. */
53         private final Template postTemplate;
54
55         /** The template to render replies. */
56         private final Template replyTemplate;
57
58         /**
59          * Creates a new “get status” AJAX handler.
60          *
61          * @param webInterface
62          *            The Sone web interface
63          * @param postTemplate
64          *            The template to render for posts
65          * @param replyTemplate
66          *            The template to render for replies
67          */
68         public GetStatusAjaxPage(WebInterface webInterface, Template postTemplate, Template replyTemplate) {
69                 super("ajax/getStatus.ajax", webInterface);
70                 this.postTemplate = postTemplate;
71                 this.replyTemplate = replyTemplate;
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         protected JsonObject createJsonObject(Request request) {
79                 /* load Sones. */
80                 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
81                 Set<Sone> sones = loadAllSones ? webInterface.getCore().getSones() : Collections.singleton(getCurrentSone(request.getToadletContext()));
82                 JsonArray jsonSones = new JsonArray();
83                 for (Sone sone : sones) {
84                         JsonObject jsonSone = createJsonSone(sone);
85                         jsonSones.add(jsonSone);
86                 }
87                 /* load notifications. */
88                 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
89                 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
90                 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
91                 JsonArray jsonNotifications = new JsonArray();
92                 for (Notification notification : notifications) {
93                         jsonNotifications.add(createJsonNotification(notification));
94                 }
95                 JsonArray jsonRemovedNotifications = new JsonArray();
96                 for (Notification notification : removedNotifications) {
97                         jsonRemovedNotifications.add(createJsonNotification(notification));
98                 }
99                 /* load new posts. */
100                 postTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
101                 Set<Post> newPosts = webInterface.getNewPosts();
102                 JsonArray jsonPosts = new JsonArray();
103                 for (Post post : newPosts) {
104                         jsonPosts.add(createJsonPost(post));
105                 }
106                 /* load new replies. */
107                 replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
108                 Set<Reply> newReplies = webInterface.getNewReplies();
109                 JsonArray jsonReplies = new JsonArray();
110                 for (Reply reply : newReplies) {
111                         jsonReplies.add(createJsonReply(reply));
112                 }
113                 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         protected boolean needsFormPassword() {
121                 return false;
122         }
123
124         //
125         // PRIVATE METHODS
126         //
127
128         /**
129          * Creates a JSON object from the given Sone.
130          *
131          * @param sone
132          *            The Sone to convert to a JSON object
133          * @return The JSON representation of the given Sone
134          */
135         private JsonObject createJsonSone(Sone sone) {
136                 JsonObject jsonSone = new JsonObject();
137                 jsonSone.put("id", sone.getId());
138                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
139                 jsonSone.put("local", sone.getInsertUri() != null);
140                 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
141                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
142                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
143                 synchronized (dateFormat) {
144                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
145                 }
146                 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
147                 return jsonSone;
148         }
149
150         /**
151          * Creates a JSON object from the given post. The JSON object will only
152          * contain the ID of the post, its time, and its rendered HTML code.
153          *
154          * @param post
155          *            The post to create a JSON object from
156          * @return The JSON representation of the post
157          */
158         private JsonObject createJsonPost(Post post) {
159                 JsonObject jsonPost = new JsonObject();
160                 jsonPost.put("id", post.getId());
161                 jsonPost.put("time", post.getTime());
162                 StringWriter stringWriter = new StringWriter();
163                 postTemplate.set("post", post);
164                 try {
165                         postTemplate.render(stringWriter);
166                 } catch (TemplateException te1) {
167                         /* TODO - shouldn’t happen. */
168                 } finally {
169                         Closer.close(stringWriter);
170                 }
171                 return jsonPost.put("html", stringWriter.toString());
172         }
173
174         /**
175          * Creates a JSON object from the given reply. The JSON object will only
176          * contain the ID of the reply, the ID of its post, its time, and its
177          * rendered HTML code.
178          *
179          * @param reply
180          *            The reply to create a JSON object from
181          * @return The JSON representation of the reply
182          */
183         private JsonObject createJsonReply(Reply reply) {
184                 JsonObject jsonPost = new JsonObject();
185                 jsonPost.put("postId", reply.getPost().getId());
186                 jsonPost.put("id", reply.getId());
187                 jsonPost.put("time", reply.getTime());
188                 StringWriter stringWriter = new StringWriter();
189                 replyTemplate.set("reply", reply);
190                 try {
191                         replyTemplate.render(stringWriter);
192                 } catch (TemplateException te1) {
193                         /* TODO - shouldn’t happen. */
194                 } finally {
195                         Closer.close(stringWriter);
196                 }
197                 return jsonPost.put("html", stringWriter.toString());
198         }
199
200         /**
201          * Creates a JSON object from the given notification.
202          *
203          * @param notification
204          *            The notification to create a JSON object
205          * @return The JSON object
206          */
207         private static JsonObject createJsonNotification(Notification notification) {
208                 JsonObject jsonNotification = new JsonObject();
209                 jsonNotification.put("id", notification.getId());
210                 jsonNotification.put("text", notification.toString());
211                 jsonNotification.put("createdTime", notification.getCreatedTime());
212                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
213                 jsonNotification.put("dismissable", notification.isDismissable());
214                 return jsonNotification;
215         }
216
217 }