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