Add method to return a Post from the simple field set.
[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.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;
34
35 /**
36  * Abstract base implementation of a {@link Command} with Sone-related helper
37  * methods.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public abstract class AbstractSoneCommand extends AbstractCommand {
42
43         /** The Sone core. */
44         private final Core core;
45
46         /**
47          * Creates a new abstract Sone FCP command.
48          *
49          * @param core
50          *            The Sone core
51          */
52         protected AbstractSoneCommand(Core core) {
53                 this.core = core;
54         }
55
56         //
57         // ACCESSORS
58         //
59
60         /**
61          * Returns the Sone core.
62          *
63          * @return The Sone core
64          */
65         protected Core getCore() {
66                 return core;
67         }
68
69         //
70         // PROTECTED METHODS
71         //
72
73         /**
74          * Returns a Sone whose ID is a parameter in the given simple field set.
75          *
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
80          *            set
81          * @return The Sone
82          * @throws FcpException
83          *             if there is no Sone ID stored under the given parameter name,
84          *             or if the Sone ID is invalid
85          */
86         protected Sone getSone(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
87                 try {
88                         String soneId = simpleFieldSet.getString(parameterName);
89                         Sone sone = core.getSone(soneId, false);
90                         if (sone == null) {
91                                 throw new FcpException("Could not load Sone from “" + soneId + "”.");
92                         }
93                         return sone;
94                 } catch (FSParseException fspe1) {
95                         throw new FcpException("Could not load Sone ID from “" + parameterName + "”.", fspe1);
96                 }
97         }
98
99         /**
100          * Returns a post whose ID is a parameter in the given simple field set.
101          *
102          * @param simpleFieldSet
103          *            The simple field set containing the ID of the post
104          * @param parameterName
105          *            The name under which the post ID is stored in the simple field
106          *            set
107          * @return The post
108          * @throws FcpException
109          *             if there is no post ID stored under the given parameter name,
110          *             or if the post ID is invalid
111          */
112         protected Post getPost(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
113                 try {
114                         String postId = simpleFieldSet.getString(parameterName);
115                         Post post = core.getPost(postId, false);
116                         if (post == null) {
117                                 throw new FcpException("Could not load post from “" + postId + "”.");
118                         }
119                         return post;
120                 } catch (FSParseException fspe1) {
121                         throw new FcpException("Could not post ID from “" + parameterName + "”.", fspe1);
122                 }
123         }
124
125         /**
126          * Creates a simple field set from the given collection of Sones.
127          *
128          * @param sones
129          *            The Sones to encode
130          * @return The simple field set containing the given Sones
131          */
132         protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
133                 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
134
135                 int soneIndex = 0;
136                 soneBuilder.put("Sones.Count", sones.size());
137                 for (Sone sone : sones) {
138                         String sonePrefix = "Sones." + soneIndex++;
139                         soneBuilder.put(sonePrefix + ".ID", sone.getId());
140                         soneBuilder.put(sonePrefix + ".Name", sone.getName());
141                         soneBuilder.put(sonePrefix + ".NiceName", SoneAccessor.getNiceName(sone));
142                         soneBuilder.put(sonePrefix + ".Time", sone.getTime());
143                 }
144
145                 return soneBuilder.get();
146         }
147
148         /**
149          * Creates a simple field set from the given collection of posts.
150          *
151          * @param posts
152          *            The posts to encode
153          * @param includeReplies
154          *            {@code true} to include the replies, {@code false} to not
155          *            include the replies
156          * @return The simple field set containing the posts
157          */
158         public SimpleFieldSet encodePosts(Collection<? extends Post> posts, boolean includeReplies) {
159                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
160
161                 int postIndex = 0;
162                 postBuilder.put("Posts.Count", posts.size());
163                 for (Post post : posts) {
164                         String postPrefix = "Posts." + postIndex++;
165                         postBuilder.put(postPrefix + ".ID", post.getId());
166                         postBuilder.put(postPrefix + ".Sone", post.getSone().getId());
167                         if (post.getRecipient() != null) {
168                                 postBuilder.put(postPrefix + ".Recipient", post.getRecipient().getId());
169                         }
170                         postBuilder.put(postPrefix + ".Time", post.getTime());
171                         postBuilder.put(postPrefix + ".Text", post.getText());
172                         if (includeReplies) {
173                                 postBuilder.put(encodeReplies(Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER), postPrefix + "."));
174                         }
175                 }
176
177                 return postBuilder.get();
178         }
179
180         /**
181          * Creates a simple field set from the given collection of replies.
182          *
183          * @param replies
184          *            The replies to encode
185          * @param prefix
186          *            The prefix for the field names (may be empty, but not
187          *            {@code null})
188          * @return The simple field set containing the replies
189          */
190         public SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
191                 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
192
193                 int replyIndex = 0;
194                 replyBuilder.put(prefix + "Replies.Count", replies.size());
195                 for (Reply reply : replies) {
196                         String replyPrefix = prefix + "Replies." + replyIndex++;
197                         replyBuilder.put(replyPrefix + ".ID", reply.getId());
198                         replyBuilder.put(replyPrefix + ".Sone", reply.getSone().getId());
199                         replyBuilder.put(replyPrefix + ".Time", reply.getTime());
200                         replyBuilder.put(replyPrefix + ".Text", reply.getText());
201                 }
202
203                 return replyBuilder.get();
204         }
205
206 }