Add parameter to include replies in encodePosts().
[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          * Creates a simple field set from the given collection of Sones.
101          *
102          * @param sones
103          *            The Sones to encode
104          * @return The simple field set containing the given Sones
105          */
106         protected SimpleFieldSet encodeSones(Collection<? extends Sone> sones) {
107                 SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
108
109                 int soneIndex = 0;
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());
117                 }
118
119                 return soneBuilder.get();
120         }
121
122         /**
123          * Creates a simple field set from the given collection of posts.
124          *
125          * @param 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
131          */
132         public SimpleFieldSet encodePosts(Collection<? extends Post> posts, boolean includeReplies) {
133                 SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
134
135                 int postIndex = 0;
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());
143                         }
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 + "."));
148                         }
149                 }
150
151                 return postBuilder.get();
152         }
153
154         /**
155          * Creates a simple field set from the given collection of replies.
156          *
157          * @param replies
158          *            The replies to encode
159          * @param prefix
160          *            The prefix for the field names (may be empty, but not
161          *            {@code null})
162          * @return The simple field set containing the replies
163          */
164         public SimpleFieldSet encodeReplies(Collection<? extends Reply> replies, String prefix) {
165                 SimpleFieldSetBuilder replyBuilder = new SimpleFieldSetBuilder();
166
167                 int replyIndex = 0;
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());
175                 }
176
177                 return replyBuilder.get();
178         }
179
180 }