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;
22 import net.pterodactylus.sone.core.Core;
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.data.Reply;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
27 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
28 import net.pterodactylus.sone.freenet.fcp.Command;
29 import net.pterodactylus.sone.freenet.fcp.FcpException;
30 import net.pterodactylus.sone.template.SoneAccessor;
31 import net.pterodactylus.util.filter.Filters;
32 import freenet.node.FSParseException;
33 import freenet.support.SimpleFieldSet;
36 * Abstract base implementation of a {@link Command} with Sone-related helper
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public abstract class AbstractSoneCommand extends AbstractCommand {
44 private final Core core;
47 * Creates a new abstract Sone FCP command.
52 protected AbstractSoneCommand(Core core) {
61 * Returns the Sone core.
63 * @return The Sone core
65 protected Core getCore() {
74 * Returns a Sone whose ID is a parameter in the given simple field set.
76 * @param simpleFieldSet
77 * The simple field set containing the ID of the Sone
78 * @param parameterName
79 * The name under which the Sone ID is stored in the simple field
82 * @throws FcpException
83 * if there is no Sone ID stored under the given parameter name,
84 * or if the Sone ID is invalid
86 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
88 String soneId = simpleFieldSet.getString(parameterName);
89 Sone sone = core.getSone(soneId, false);
91 throw new FcpException("Could not load Sone from “" + soneId + "”.");
94 } catch (FSParseException fspe1) {
95 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
100 * Returns a post whose ID is a parameter in the given simple field set.
102 * @param simpleFieldSet
103 * The simple field set containing the ID of the post
104 * @param parameterName
105 * The name under which the post ID is stored in the simple field
108 * @throws FcpException
109 * if there is no post ID stored under the given parameter name,
110 * or if the post ID is invalid
112 protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
114 String postId = simpleFieldSet.getString(parameterName);
115 Post post = core.getPost(postId, false);
117 throw new FcpException("Could not load post from “" + postId + "”.");
120 } catch (FSParseException fspe1) {
121 throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
126 * Creates a simple field set from the given collection of Sones.
129 * The Sones to encode
130 * @return The simple field set containing the given Sones
132 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
133 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
136 soneBuilder.put("Sones.Count", sones.size());
137 for (Sone sone : sones) {
138 String sonePrefix = "Sones." + soneIndex++;
139 soneBuilder.put(sonePrefix + ".ID", sone.getId());
140 soneBuilder.put(sonePrefix + ".Name", sone.getName());
141 soneBuilder.put(sonePrefix + ".NiceName", SoneAccessor.getNiceName(sone));
142 soneBuilder.put(sonePrefix + ".Time", sone.getTime());
145 return soneBuilder.get();
149 * Creates a simple field set from the given collection of posts.
152 * The posts to encode
153 * @param includeReplies
154 * {@code true} to include the replies, {@code false} to not
155 * include the replies
156 * @return The simple field set containing the posts
158 public SimpleFieldSet encodePosts(Collection<? extends Post> posts, boolean includeReplies) {
159 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
162 postBuilder.put("Posts.Count", posts.size());
163 for (Post post : posts) {
164 String postPrefix = "Posts." + postIndex++;
165 postBuilder.put(postPrefix + ".ID", post.getId());
166 postBuilder.put(postPrefix + ".Sone", post.getSone().getId());
167 if (post.getRecipient() != null) {
168 postBuilder.put(postPrefix + ".Recipient", post.getRecipient().getId());
170 postBuilder.put(postPrefix + ".Time", post.getTime());
171 postBuilder.put(postPrefix + ".Text", post.getText());
172 if (includeReplies) {
173 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
177 return postBuilder.get();
181 * Creates a simple field set from the given collection of replies.
184 * The replies to encode
186 * The prefix for the field names (may be empty, but not
188 * @return The simple field set containing the replies
190 public SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
191 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
194 replyBuilder.put(prefix + "Replies.Count", replies.size());
195 for (Reply reply : replies) {
196 String replyPrefix = prefix + "Replies." + replyIndex++;
197 replyBuilder.put(replyPrefix + ".ID", reply.getId());
198 replyBuilder.put(replyPrefix + ".Sone", reply.getSone().getId());
199 replyBuilder.put(replyPrefix + ".Time", reply.getTime());
200 replyBuilder.put(replyPrefix + ".Text", reply.getText());
203 return replyBuilder.get();