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.Sone;
25 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
26 import net.pterodactylus.sone.freenet.fcp.AbstractCommand;
27 import net.pterodactylus.sone.freenet.fcp.Command;
28 import net.pterodactylus.sone.freenet.fcp.FcpException;
29 import freenet.node.FSParseException;
30 import freenet.support.SimpleFieldSet;
33 * Abstract base implementation of a {@link Command} with Sone-related helper
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public abstract class AbstractSoneCommand extends AbstractCommand {
41 private final Core core;
44 * Creates a new abstract Sone FCP command.
49 protected AbstractSoneCommand(Core core) {
58 * Returns the Sone core.
60 * @return The Sone core
62 protected Core getCore() {
71 * Returns a Sone whose ID is a parameter in the given simple field set.
73 * @param simpleFieldSet
74 * The simple field set containing the ID of the Sone
75 * @param parameterName
76 * The name under which the Sone ID is stored in the simple field
79 * @throws FcpException
80 * if there is no Sone ID stored under the given parameter name,
81 * or if the Sone ID is invalid
83 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
85 String soneId = simpleFieldSet.getString(parameterName);
86 Sone sone = core.getSone(soneId, false);
88 throw new FcpException("Could not load Sone from “" + soneId + "”.");
91 } catch (FSParseException fspe1) {
92 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
97 * Creates a simple field set from the given collection of posts.
100 * The posts to encode
101 * @return The simple field set containing the posts
103 public SimpleFieldSet encodePosts(Collection<? extends Post> posts) {
104 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
107 postBuilder.put("Posts.Count", posts.size());
108 for (Post post : posts) {
109 String postPrefix = "Posts." + postIndex++;
110 postBuilder.put(postPrefix + ".ID", post.getId());
111 postBuilder.put(postPrefix + ".Sone", post.getSone().getId());
112 if (post.getRecipient() != null) {
113 postBuilder.put(postPrefix + ".Recipient", post.getRecipient().getId());
115 postBuilder.put(postPrefix + ".Time", post.getTime());
116 postBuilder.put(postPrefix + ".Text", post.getText());
119 return postBuilder.get();