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 * {@code true} to only return local Sones, {@code false} to
86 * @throws FcpException
87 * if there is no Sone ID stored under the given parameter name,
88 * or if the Sone ID is invalid
90 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName, boolean localOnly) throws FcpException {
92 String soneId = simpleFieldSet.getString(parameterName);
93 Sone sone = localOnly ? core.getLocalSone(soneId, false) : core.getSone(soneId, false);
95 throw new FcpException("Could not load Sone from “" + soneId + "”.");
98 } catch (FSParseException fspe1) {
99 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
104 * Returns a post whose ID is a parameter in the given simple field set.
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
112 * @throws FcpException
113 * if there is no post ID stored under the given parameter name,
114 * or if the post ID is invalid
116 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
118 String postId = simpleFieldSet.getString(parameterName);
119 Post post = core.getPost(postId, false);
121 throw new FcpException("Could not load post from “" + postId + "”.");
124 } catch (FSParseException fspe1) {
125 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
130 * Returns a reply whose ID is a parameter in the given simple field set.
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
138 * @throws FcpException
139 * if there is no reply ID stored under the given parameter
140 * name, or if the reply ID is invalid
142 protected Reply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
144 String replyId = simpleFieldSet.getString(parameterName);
145 Reply reply = core.getReply(replyId, false);
147 throw new FcpException("Could not load reply from “" + replyId + "”.");
150 } catch (FSParseException fspe1) {
151 throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
156 * Creates a simple field set from the given collection of Sones.
159 * The Sones to encode
161 * The prefix for the field names (may be empty but not
163 * @return The simple field set containing the given Sones
165 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
166 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
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());
178 return soneBuilder.get();
182 * Creates a simple field set from the given post.
187 * The prefix for the field names (may be empty but not
189 * @param includeReplies
190 * {@code true} to include replies, {@code false} to not include
192 * @return The simple field set containing the post
194 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
195 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
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());
202 postBuilder.put(prefix + "Time", post.getTime());
203 postBuilder.put(prefix + "Text", post.getText());
204 postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
206 if (includeReplies) {
207 List<Reply> replies = core.getReplies(post);
208 postBuilder.put(encodeReplies(replies, prefix));
211 return postBuilder.get();
215 * Creates a simple field set from the given collection of posts.
218 * The posts to encode
220 * The prefix for the field names (may be empty but not
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
227 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
228 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
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 + "."));
240 return postBuilder.get();
244 * Creates a simple field set from the given collection of replies.
247 * The replies to encode
249 * The prefix for the field names (may be empty, but not
251 * @return The simple field set containing the replies
253 protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
254 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
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());
266 return replyBuilder.get();
270 * Creates a simple field set from the given collection of Sones that like
276 * The prefix for the field names (may be empty but not
278 * @return The simple field set containing the likes
280 protected SimpleFieldSet encodeLikes(Collection<? extends Sone> likes, String prefix) {
281 SimpleFieldSetBuilder likesBuilder = new SimpleFieldSetBuilder();
284 likesBuilder.put(prefix + "Count", likes.size());
285 for (Sone sone : likes) {
286 String sonePrefix = prefix + likeIndex++ + ".";
287 likesBuilder.put(sonePrefix + "ID", sone.getId());
290 return likesBuilder.get();