Add likes to posts.
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / AbstractSoneCommand.java
1 /*
2  * Sone - FcpInterface.java - Copyright © 2011 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.fcp;
19
20 import java.util.Collection;
21 import java.util.List;
22
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
28 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
29 import net.pterodactylus.sone.freenet.fcp.Command;
30 import net.pterodactylus.sone.freenet.fcp.FcpException;
31 import net.pterodactylus.sone.template.SoneAccessor;
32 import net.pterodactylus.util.filter.Filters;
33 import freenet.node.FSParseException;
34 import freenet.support.SimpleFieldSet;
35
36 /**
37  * Abstract base implementation of a {@link Command} with Sone-related helper
38  * methods.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public abstract class AbstractSoneCommand extends AbstractCommand {
43
44         /** The Sone core. */
45         private final Core core;
46
47         /**
48          * Creates a new abstract Sone FCP command.
49          *
50          * @param core
51          *            The Sone core
52          */
53         protected AbstractSoneCommand(Core core) {
54                 this.core = core;
55         }
56
57         //
58         // ACCESSORS
59         //
60
61         /**
62          * Returns the Sone core.
63          *
64          * @return The Sone core
65          */
66         protected Core getCore() {
67                 return core;
68         }
69
70         //
71         // PROTECTED METHODS
72         //
73
74         /**
75          * Returns a Sone whose ID is a parameter in the given simple field set.
76          *
77          * @param simpleFieldSet
78          *            The simple field set containing the ID of the Sone
79          * @param parameterName
80          *            The name under which the Sone ID is stored in the simple field
81          *            set
82          * @return The Sone
83          * @throws FcpException
84          *             if there is no Sone ID stored under the given parameter name,
85          *             or if the Sone ID is invalid
86          */
87         protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
88                 try {
89                         String soneId = simpleFieldSet.getString(parameterName);
90                         Sone sone = core.getSone(soneId, false);
91                         if (sone == null) {
92                                 throw new FcpException("Could not load Sone from “" + soneId + "”.");
93                         }
94                         return sone;
95                 } catch (FSParseException fspe1) {
96                         throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
97                 }
98         }
99
100         /**
101          * Returns a post whose ID is a parameter in the given simple field set.
102          *
103          * @param simpleFieldSet
104          *            The simple field set containing the ID of the post
105          * @param parameterName
106          *            The name under which the post ID is stored in the simple field
107          *            set
108          * @return The post
109          * @throws FcpException
110          *             if there is no post ID stored under the given parameter name,
111          *             or if the post ID is invalid
112          */
113         protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
114                 try {
115                         String postId = simpleFieldSet.getString(parameterName);
116                         Post post = core.getPost(postId, false);
117                         if (post == null) {
118                                 throw new FcpException("Could not load post from “" + postId + "”.");
119                         }
120                         return post;
121                 } catch (FSParseException fspe1) {
122                         throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
123                 }
124         }
125
126         /**
127          * Creates a simple field set from the given collection of Sones.
128          *
129          * @param sones
130          *            The Sones to encode
131          * @param prefix
132          *            The prefix for the field names (may be empty but not
133          *            {@code null})
134          * @return The simple field set containing the given Sones
135          */
136         protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
137                 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
138
139                 int soneIndex = 0;
140                 soneBuilder.put(prefix + "Count", sones.size());
141                 for (Sone sone : sones) {
142                         String sonePrefix = prefix + soneIndex++ + ".";
143                         soneBuilder.put(sonePrefix + "ID", sone.getId());
144                         soneBuilder.put(sonePrefix + "Name", sone.getName());
145                         soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
146                         soneBuilder.put(sonePrefix + "Time", sone.getTime());
147                 }
148
149                 return soneBuilder.get();
150         }
151
152         /**
153          * Creates a simple field set from the given post.
154          *
155          * @param post
156          *            The post to encode
157          * @param prefix
158          *            The prefix for the field names (may be empty but not
159          *            {@code null})
160          * @param includeReplies
161          *            {@code true} to include replies, {@code false} to not include
162          *            replies
163          * @return The simple field set containing the post
164          */
165         protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
166                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
167
168                 postBuilder.put(prefix + "ID", post.getId());
169                 postBuilder.put(prefix + "Sone", post.getSone().getId());
170                 if (post.getRecipient() != null) {
171                         postBuilder.put(prefix + "Recipient", post.getRecipient().getId());
172                 }
173                 postBuilder.put(prefix + "Time", post.getTime());
174                 postBuilder.put(prefix + "Text", post.getText());
175                 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
176
177                 if (includeReplies) {
178                         List<Reply> replies = core.getReplies(post);
179                         postBuilder.put(encodeReplies(replies, prefix));
180                 }
181
182                 return postBuilder.get();
183         }
184
185         /**
186          * Creates a simple field set from the given collection of posts.
187          *
188          * @param posts
189          *            The posts to encode
190          * @param prefix
191          *            The prefix for the field names (may be empty but not
192          *            {@code null})
193          * @param includeReplies
194          *            {@code true} to include the replies, {@code false} to not
195          *            include the replies
196          * @return The simple field set containing the posts
197          */
198         protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
199                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
200
201                 int postIndex = 0;
202                 postBuilder.put(prefix + "Count", posts.size());
203                 for (Post post : posts) {
204                         String postPrefix = prefix + postIndex++;
205                         postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
206                         if (includeReplies) {
207                                 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
208                         }
209                 }
210
211                 return postBuilder.get();
212         }
213
214         /**
215          * Creates a simple field set from the given collection of replies.
216          *
217          * @param replies
218          *            The replies to encode
219          * @param prefix
220          *            The prefix for the field names (may be empty, but not
221          *            {@code null})
222          * @return The simple field set containing the replies
223          */
224         protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
225                 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
226
227                 int replyIndex = 0;
228                 replyBuilder.put(prefix + "Replies.Count", replies.size());
229                 for (Reply reply : replies) {
230                         String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
231                         replyBuilder.put(replyPrefix + "ID", reply.getId());
232                         replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
233                         replyBuilder.put(replyPrefix + "Time", reply.getTime());
234                         replyBuilder.put(replyPrefix + "Text", reply.getText());
235                 }
236
237                 return replyBuilder.get();
238         }
239
240         /**
241          * Creates a simple field set from the given collection of Sones that like
242          * an element.
243          *
244          * @param likes
245          *            The liking Sones
246          * @param prefix
247          *            The prefix for the field names (may be empty but not
248          *            {@code null})
249          * @return The simple field set containing the likes
250          */
251         protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
252                 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
253
254                 int likeIndex = 0;
255                 likesBuilder.put(prefix + "Count", likes.size());
256                 for (Sone sone : likes) {
257                         String sonePrefix = prefix + likeIndex++ + ".";
258                         likesBuilder.put(sonePrefix + "ID", sone.getId());
259                 }
260
261                 return likesBuilder.get();
262         }
263
264 }