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 * Creates a simple field set from the given collection of Sones.
130 * The Sones to encode
132 * The prefix for the field names (may be empty but not
134 * @return The simple field set containing the given Sones
136 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
137 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
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());
149 return soneBuilder.get();
153 * Creates a simple field set from the given post.
158 * The prefix for the field names (may be empty but not
160 * @param includeReplies
161 * {@code true} to include replies, {@code false} to not include
163 * @return The simple field set containing the post
165 protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
166 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
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());
173 postBuilder.put(prefix + "Time", post.getTime());
174 postBuilder.put(prefix + "Text", post.getText());
176 if (includeReplies) {
177 List<Reply> replies = core.getReplies(post);
178 postBuilder.put(encodeReplies(replies, prefix));
181 return postBuilder.get();
185 * Creates a simple field set from the given collection of posts.
188 * The posts to encode
190 * The prefix for the field names (may be empty but not
192 * @param includeReplies
193 * {@code true} to include the replies, {@code false} to not
194 * include the replies
195 * @return The simple field set containing the posts
197 protected SimpleFieldSet encodePosts(Collection<? extends Post> posts, String prefix, boolean includeReplies) {
198 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
201 postBuilder.put(prefix + "Count", posts.size());
202 for (Post post : posts) {
203 String postPrefix = prefix + postIndex++;
204 postBuilder.put(encodePost(post, postPrefix + ".", includeReplies));
205 if (includeReplies) {
206 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
210 return postBuilder.get();
214 * Creates a simple field set from the given collection of replies.
217 * The replies to encode
219 * The prefix for the field names (may be empty, but not
221 * @return The simple field set containing the replies
223 protected SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
224 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
227 replyBuilder.put(prefix + "Replies.Count", replies.size());
228 for (Reply reply : replies) {
229 String replyPrefix = prefix + "Replies." + replyIndex++ + ".";
230 replyBuilder.put(replyPrefix + "ID", reply.getId());
231 replyBuilder.put(replyPrefix + "Sone", reply.getSone().getId());
232 replyBuilder.put(replyPrefix + "Time", reply.getTime());
233 replyBuilder.put(replyPrefix + "Text", reply.getText());
236 return replyBuilder.get();