Add method to get reply from the command parameters.
[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          * Returns a reply whose ID is a parameter in the given simple field set.
128          *
129          * @param simpleFieldSet
130          *            The simple field set containing the ID of the reply
131          * @param parameterName
132          *            The name under which the reply ID is stored in the simple
133          *            field set
134          * @return The reply
135          * @throws FcpException
136          *             if there is no reply ID stored under the given parameter
137          *             name, or if the reply ID is invalid
138          */
139         protected Reply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
140                 try {
141                         String replyId = simpleFieldSet.getString(parameterName);
142                         Reply reply = core.getReply(replyId, false);
143                         if (reply == null) {
144                                 throw new FcpException("Could not load reply from “" + replyId + "”.");
145                         }
146                         return reply;
147                 } catch (FSParseException fspe1) {
148                         throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
149                 }
150         }
151
152         /**
153          * Creates a simple field set from the given collection of Sones.
154          *
155          * @param sones
156          *            The Sones to encode
157          * @param prefix
158          *            The prefix for the field names (may be empty but not
159          *            {@code null})
160          * @return The simple field set containing the given Sones
161          */
162         protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
163                 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
164
165                 int soneIndex = 0;
166                 soneBuilder.put(prefix + "Count", sones.size());
167                 for (Sone sone : sones) {
168                         String sonePrefix = prefix + soneIndex++ + ".";
169                         soneBuilder.put(sonePrefix + "ID", sone.getId());
170                         soneBuilder.put(sonePrefix + "Name", sone.getName());
171                         soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
172                         soneBuilder.put(sonePrefix + "Time", sone.getTime());
173                 }
174
175                 return soneBuilder.get();
176         }
177
178         /**
179          * Creates a simple field set from the given post.
180          *
181          * @param post
182          *            The post to encode
183          * @param prefix
184          *            The prefix for the field names (may be empty but not
185          *            {@code null})
186          * @param includeReplies
187          *            {@code true} to include replies, {@code false} to not include
188          *            replies
189          * @return The simple field set containing the post
190          */
191         protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
192                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
193
194                 postBuilder.put(prefix + "ID", post.getId());
195                 postBuilder.put(prefix + "Sone", post.getSone().getId());
196                 if (post.getRecipient() != null) {
197                         postBuilder.put(prefix + "Recipient", post.getRecipient().getId());
198                 }
199                 postBuilder.put(prefix + "Time", post.getTime());
200                 postBuilder.put(prefix + "Text", post.getText());
201                 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
202
203                 if (includeReplies) {
204                         List<Reply> replies = core.getReplies(post);
205                         postBuilder.put(encodeReplies(replies, prefix));
206                 }
207
208                 return postBuilder.get();
209         }
210
211         /**
212          * Creates a simple field set from the given collection of posts.
213          *
214          * @param posts
215          *            The posts to encode
216          * @param prefix
217          *            The prefix for the field names (may be empty but not
218          *            {@code null})
219          * @param includeReplies
220          *            {@code true} to include the replies, {@code false} to not
221          *            include the replies
222          * @return The simple field set containing the posts
223          */
224         protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
225                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
226
227                 int postIndex = 0;
228                 postBuilder.put(prefix + "Count", posts.size());
229                 for (Post post : posts) {
230                         String postPrefix = prefix + postIndex++;
231                         postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
232                         if (includeReplies) {
233                                 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
234                         }
235                 }
236
237                 return postBuilder.get();
238         }
239
240         /**
241          * Creates a simple field set from the given collection of replies.
242          *
243          * @param replies
244          *            The replies to encode
245          * @param prefix
246          *            The prefix for the field names (may be empty, but not
247          *            {@code null})
248          * @return The simple field set containing the replies
249          */
250         protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
251                 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
252
253                 int replyIndex = 0;
254                 replyBuilder.put(prefix + "Replies.Count", replies.size());
255                 for (Reply reply : replies) {
256                         String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
257                         replyBuilder.put(replyPrefix + "ID", reply.getId());
258                         replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
259                         replyBuilder.put(replyPrefix + "Time", reply.getTime());
260                         replyBuilder.put(replyPrefix + "Text", reply.getText());
261                 }
262
263                 return replyBuilder.get();
264         }
265
266         /**
267          * Creates a simple field set from the given collection of Sones that like
268          * an element.
269          *
270          * @param likes
271          *            The liking Sones
272          * @param prefix
273          *            The prefix for the field names (may be empty but not
274          *            {@code null})
275          * @return The simple field set containing the likes
276          */
277         protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
278                 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
279
280                 int likeIndex = 0;
281                 likesBuilder.put(prefix + "Count", likes.size());
282                 for (Sone sone : likes) {
283                         String sonePrefix = prefix + likeIndex++ + ".";
284                         likesBuilder.put(sonePrefix + "ID", sone.getId());
285                 }
286
287                 return likesBuilder.get();
288         }
289
290 }