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 * Creates a simple field set from the given collection of Sones.
103 * The Sones to encode
104 * @return The simple field set containing the given Sones
106 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
107 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
110 soneBuilder.put("Sones.Count", sones.size());
111 for (Sone sone : sones) {
112 String sonePrefix = "Sones." + soneIndex++;
113 soneBuilder.put(sonePrefix + ".ID", sone.getId());
114 soneBuilder.put(sonePrefix + ".Name", sone.getName());
115 soneBuilder.put(sonePrefix + ".NiceName", SoneAccessor.getNiceName(sone));
116 soneBuilder.put(sonePrefix + ".Time", sone.getTime());
119 return soneBuilder.get();
123 * Creates a simple field set from the given collection of posts.
126 * The posts to encode
127 * @param includeReplies
128 * {@code true} to include the replies, {@code false} to not
129 * include the replies
130 * @return The simple field set containing the posts
132 public SimpleFieldSet encodePosts(Collection<? extends Post> posts, boolean includeReplies) {
133 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
136 postBuilder.put("Posts.Count", posts.size());
137 for (Post post : posts) {
138 String postPrefix = "Posts." + postIndex++;
139 postBuilder.put(postPrefix + ".ID", post.getId());
140 postBuilder.put(postPrefix + ".Sone", post.getSone().getId());
141 if (post.getRecipient() != null) {
142 postBuilder.put(postPrefix + ".Recipient", post.getRecipient().getId());
144 postBuilder.put(postPrefix + ".Time", post.getTime());
145 postBuilder.put(postPrefix + ".Text", post.getText());
146 if (includeReplies) {
147 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
151 return postBuilder.get();
155 * Creates a simple field set from the given collection of replies.
158 * The replies to encode
160 * The prefix for the field names (may be empty, but not
162 * @return The simple field set containing the replies
164 public SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
165 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
168 replyBuilder.put(prefix + "Replies.Count", replies.size());
169 for (Reply reply : replies) {
170 String replyPrefix = prefix + "Replies." + replyIndex++;
171 replyBuilder.put(replyPrefix + ".ID", reply.getId());
172 replyBuilder.put(replyPrefix + ".Sone", reply.getSone().getId());
173 replyBuilder.put(replyPrefix + ".Time", reply.getTime());
174 replyBuilder.put(replyPrefix + ".Text", reply.getText());
177 return replyBuilder.get();