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