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