2 * Sone - FcpInterface.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.fcp;
20 import java.util.Collection;
21 import java.util.List;
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;
37 * Abstract base implementation of a {@link Command} with Sone-related helper
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public abstract class AbstractSoneCommand extends AbstractCommand {
45 private final Core core;
48 * Creates a new abstract Sone FCP command.
53 protected AbstractSoneCommand(Core core) {
62 * Returns the Sone core.
64 * @return The Sone core
66 protected Core getCore() {
75 * Returns a Sone whose ID is a parameter in the given simple field set.
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
83 * @throws FcpException
84 * if there is no Sone ID stored under the given parameter name,
85 * or if the Sone ID is invalid
87 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
89 String soneId = simpleFieldSet.getString(parameterName);
90 Sone sone = core.getSone(soneId, false);
92 throw new FcpException("Could not load Sone from “" + soneId + "”.");
95 } catch (FSParseException fspe1) {
96 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
101 * Returns a post whose ID is a parameter in the given simple field set.
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
109 * @throws FcpException
110 * if there is no post ID stored under the given parameter name,
111 * or if the post ID is invalid
113 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
115 String postId = simpleFieldSet.getString(parameterName);
116 Post post = core.getPost(postId, false);
118 throw new FcpException("Could not load post from “" + postId + "”.");
121 } catch (FSParseException fspe1) {
122 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
127 * Returns a reply whose ID is a parameter in the given simple field set.
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
135 * @throws FcpException
136 * if there is no reply ID stored under the given parameter
137 * name, or if the reply ID is invalid
139 protected Reply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
141 String replyId = simpleFieldSet.getString(parameterName);
142 Reply reply = core.getReply(replyId, false);
144 throw new FcpException("Could not load reply from “" + replyId + "”.");
147 } catch (FSParseException fspe1) {
148 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
153 * Creates a simple field set from the given collection of Sones.
156 * The Sones to encode
158 * The prefix for the field names (may be empty but not
160 * @return The simple field set containing the given Sones
162 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
163 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
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());
175 return soneBuilder.get();
179 * Creates a simple field set from the given post.
184 * The prefix for the field names (may be empty but not
186 * @param includeReplies
187 * {@code true} to include replies, {@code false} to not include
189 * @return The simple field set containing the post
191 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
192 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
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());
199 postBuilder.put(prefix + "Time", post.getTime());
200 postBuilder.put(prefix + "Text", post.getText());
201 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
203 if (includeReplies) {
204 List<Reply> replies = core.getReplies(post);
205 postBuilder.put(encodeReplies(replies, prefix));
208 return postBuilder.get();
212 * Creates a simple field set from the given collection of posts.
215 * The posts to encode
217 * The prefix for the field names (may be empty but not
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
224 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
225 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
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 + "."));
237 return postBuilder.get();
241 * Creates a simple field set from the given collection of replies.
244 * The replies to encode
246 * The prefix for the field names (may be empty, but not
248 * @return The simple field set containing the replies
250 protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
251 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
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());
263 return replyBuilder.get();
267 * Creates a simple field set from the given collection of Sones that like
273 * The prefix for the field names (may be empty but not
275 * @return The simple field set containing the likes
277 protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
278 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
281 likesBuilder.put(prefix + "Count", likes.size());
282 for (Sone sone : likes) {
283 String sonePrefix = prefix + likeIndex++ + ".";
284 likesBuilder.put(sonePrefix + "ID", sone.getId());
287 return likesBuilder.get();