Add abstract FCP command with Sone-specific helper methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / fcp / AbstractSoneCommand.java
1 /*
2  * Sone - FcpInterface.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
20 import java.util.Collection;
21
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;
31
32 /**
33  * Abstract base implementation of a {@link Command} with Sone-related helper
34  * methods.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public abstract class AbstractSoneCommand extends AbstractCommand {
39
40         /** The Sone core. */
41         private final Core core;
42
43         /**
44          * Creates a new abstract Sone FCP command.
45          *
46          * @param core
47          *            The Sone core
48          */
49         protected AbstractSoneCommand(Core core) {
50                 this.core = core;
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Returns the Sone core.
59          *
60          * @return The Sone core
61          */
62         protected Core getCore() {
63                 return core;
64         }
65
66         //
67         // PROTECTED METHODS
68         //
69
70         /**
71          * Returns a Sone whose ID is a parameter in the given simple field set.
72          *
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
77          *            set
78          * @return The Sone
79          * @throws FcpException
80          *             if there is no Sone ID stored under the given parameter name,
81          *             or if the Sone ID is invalid
82          */
83         protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
84                 try {
85                         String soneId = simpleFieldSet.getString(parameterName);
86                         Sone sone = core.getSone(soneId, false);
87                         if (sone == null) {
88                                 throw new FcpException("Could not load Sone from “" + soneId + "”.");
89                         }
90                         return sone;
91                 } catch (FSParseException fspe1) {
92                         throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
93                 }
94         }
95
96         /**
97          * Creates a simple field set from the given collection of posts.
98          *
99          * @param posts
100          *            The posts to encode
101          * @return The simple field set containing the posts
102          */
103         public SimpleFieldSet encodePosts(Collection<? extends Post> posts) {
104                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
105
106                 int postIndex = 0;
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());
114                         }
115                         postBuilder.put(postPrefix + ".Time", post.getTime());
116                         postBuilder.put(postPrefix + ".Text", post.getText());
117                 }
118
119                 return postBuilder.get();
120         }
121
122 }