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