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 net.pterodactylus.sone.template.SoneAccessor;
30 import freenet.node.FSParseException;
31 import freenet.support.SimpleFieldSet;
34 * Abstract base implementation of a {@link Command} with Sone-related helper
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public abstract class AbstractSoneCommand extends AbstractCommand {
42 private final Core core;
45 * Creates a new abstract Sone FCP command.
50 protected AbstractSoneCommand(Core core) {
59 * Returns the Sone core.
61 * @return The Sone core
63 protected Core getCore() {
72 * Returns a Sone whose ID is a parameter in the given simple field set.
74 * @param simpleFieldSet
75 * The simple field set containing the ID of the Sone
76 * @param parameterName
77 * The name under which the Sone ID is stored in the simple field
80 * @throws FcpException
81 * if there is no Sone ID stored under the given parameter name,
82 * or if the Sone ID is invalid
84 protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
86 String soneId = simpleFieldSet.getString(parameterName);
87 Sone sone = core.getSone(soneId, false);
89 throw new FcpException("Could not load Sone from “" + soneId + "”.");
92 } catch (FSParseException fspe1) {
93 throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
98 * Creates a simple field set from the given collection of Sones.
101 * The Sones to encode
102 * @return The simple field set containing the given Sones
104 protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
105 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
108 soneBuilder.put("Sones.Count", sones.size());
109 for (Sone sone : sones) {
110 String sonePrefix = "Sones." + soneIndex++;
111 soneBuilder.put(sonePrefix + ".ID", sone.getId());
112 soneBuilder.put(sonePrefix + ".Name", sone.getName());
113 soneBuilder.put(sonePrefix + ".NiceName", SoneAccessor.getNiceName(sone));
114 soneBuilder.put(sonePrefix + ".Time", sone.getTime());
117 return soneBuilder.get();
121 * Creates a simple field set from the given collection of posts.
124 * The posts to encode
125 * @return The simple field set containing the posts
127 public SimpleFieldSet encodePosts(Collection<? extends Post> posts) {
128 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
131 postBuilder.put("Posts.Count", posts.size());
132 for (Post post : posts) {
133 String postPrefix = "Posts." + postIndex++;
134 postBuilder.put(postPrefix + ".ID", post.getId());
135 postBuilder.put(postPrefix + ".Sone", post.getSone().getId());
136 if (post.getRecipient() != null) {
137 postBuilder.put(postPrefix + ".Recipient", post.getRecipient().getId());
139 postBuilder.put(postPrefix + ".Time", post.getTime());
140 postBuilder.put(postPrefix + ".Text", post.getText());
143 return postBuilder.get();