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 freenet.node.FSParseException;
32 import freenet.support.SimpleFieldSet;
35 * Abstract base implementation of a {@link Command} with Sone-related helper
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public abstract class AbstractSoneCommand extends AbstractCommand {
43 private final Core core;
46 * Creates a new abstract Sone FCP command.
51 protected AbstractSoneCommand(Core core) {
60 * Returns the Sone core.
62 * @return The Sone core
64 protected Core getCore() {
73 * Returns a Sone whose ID is a parameter in the given simple field set.
75 * @param simpleFieldSet
76 * The simple field set containing the ID of the Sone
77 * @param parameterName
78 * The name under which the Sone ID is stored in the simple field
81 * @throws FcpException
82 * if there is no Sone ID stored under the given parameter name,
83 * or if the Sone ID is invalid
85 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
87 String soneId = simpleFieldSet.getString(parameterName);
88 Sone sone = core.getSone(soneId, false);
90 throw new FcpException("Could not load Sone from “" + soneId + "”.");
93 } catch (FSParseException fspe1) {
94 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
99 * Creates a simple field set from the given collection of Sones.
102 * The Sones to encode
103 * @return The simple field set containing the given Sones
105 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
106 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
109 soneBuilder.put("Sones.Count", sones.size());
110 for (Sone sone : sones) {
111 String sonePrefix = "Sones." + soneIndex++;
112 soneBuilder.put(sonePrefix + ".ID", sone.getId());
113 soneBuilder.put(sonePrefix + ".Name", sone.getName());
114 soneBuilder.put(sonePrefix + ".NiceName", SoneAccessor.getNiceName(sone));
115 soneBuilder.put(sonePrefix + ".Time", sone.getTime());
118 return soneBuilder.get();
122 * Creates a simple field set from the given collection of posts.
125 * The posts to encode
126 * @return The simple field set containing the posts
128 public SimpleFieldSet encodePosts(Collection<? extends Post> posts) {
129 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
132 postBuilder.put("Posts.Count", posts.size());
133 for (Post post : posts) {
134 String postPrefix = "Posts." + postIndex++;
135 postBuilder.put(postPrefix + ".ID", post.getId());
136 postBuilder.put(postPrefix + ".Sone", post.getSone().getId());
137 if (post.getRecipient() != null) {
138 postBuilder.put(postPrefix + ".Recipient", post.getRecipient().getId());
140 postBuilder.put(postPrefix + ".Time", post.getTime());
141 postBuilder.put(postPrefix + ".Text", post.getText());
144 return postBuilder.get();
148 * Creates a simple field set from the given collection of replies.
151 * The replies to encode
153 * The prefix for the field names (may be empty, but not
155 * @return The simple field set containing the replies
157 public SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
158 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
161 replyBuilder.put(prefix + "Replies.Count", replies.size());
162 for (Reply reply : replies) {
163 String replyPrefix = prefix + "Replies." + replyIndex++;
164 replyBuilder.put(replyPrefix + ".ID", reply.getId());
165 replyBuilder.put(replyPrefix + ".Sone", reply.getSone().getId());
166 replyBuilder.put(replyPrefix + ".Time", reply.getTime());
167 replyBuilder.put(replyPrefix + ".Text", reply.getText());
170 return replyBuilder.get();