+ * Returns a reply whose ID is a parameter in the given simple field set.
+ *
+ * @param simpleFieldSet
+ * The simple field set containing the ID of the reply
+ * @param parameterName
+ * The name under which the reply ID is stored in the simple
+ * field set
+ * @return The reply
+ * @throws FcpException
+ * if there is no reply ID stored under the given parameter
+ * name, or if the reply ID is invalid
+ */
+ protected PostReply getReply(SimpleFieldSet simpleFieldSet, String parameterName) throws FcpException {
+ try {
+ String replyId = simpleFieldSet.getString(parameterName);
+ PostReply reply = core.getPostReply(replyId);
+ if (reply == null) {
+ throw new FcpException("Could not load reply from “" + replyId + "”.");
+ }
+ return reply;
+ } catch (FSParseException fspe1) {
+ throw new FcpException("Could not reply ID from “" + parameterName + "”.", fspe1);
+ }
+ }
+
+ /**
+ * Creates a simple field set from the given Sone, including {@link Profile}
+ * information.
+ *
+ * @param sone
+ * The Sone to encode
+ * @param prefix
+ * The prefix for the field names (may be empty but not {@code
+ * null})
+ * @param localSone
+ * An optional local Sone that is used for Sone-specific data,
+ * such as if the Sone is followed by the local Sone
+ * @return The simple field set containing the given Sone
+ */
+ protected static SimpleFieldSet encodeSone(Sone sone, String prefix, Sone localSone) {
+ SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
+
+ soneBuilder.put(prefix + "Name", sone.getName());
+ soneBuilder.put(prefix + "NiceName", SoneAccessor.getNiceName(sone));
+ soneBuilder.put(prefix + "LastUpdated", sone.getTime());
+ if (localSone != null) {
+ soneBuilder.put(prefix + "Followed", String.valueOf(localSone.hasFriend(sone.getId())));
+ }
+ Profile profile = sone.getProfile();
+ soneBuilder.put(prefix + "Field.Count", profile.getFields().size());
+ int fieldIndex = 0;
+ for (Field field : profile.getFields()) {
+ soneBuilder.put(prefix + "Field." + fieldIndex + ".Name", field.getName());
+ soneBuilder.put(prefix + "Field." + fieldIndex + ".Value", field.getValue());
+ ++fieldIndex;
+ }
+
+ return soneBuilder.get();
+ }
+
+ /**
+ * Creates a simple field set from the given collection of Sones.
+ *
+ * @param sones
+ * The Sones to encode
+ * @param prefix
+ * The prefix for the field names (may be empty but not
+ * {@code null})
+ * @return The simple field set containing the given Sones
+ */
+ protected static SimpleFieldSet encodeSones(Collection<? extends Sone> sones, String prefix) {
+ SimpleFieldSetBuilder soneBuilder = new SimpleFieldSetBuilder();
+
+ int soneIndex = 0;
+ soneBuilder.put(prefix + "Count", sones.size());
+ for (Sone sone : sones) {
+ String sonePrefix = prefix + soneIndex++ + ".";
+ soneBuilder.put(sonePrefix + "ID", sone.getId());
+ soneBuilder.put(sonePrefix + "Name", sone.getName());
+ soneBuilder.put(sonePrefix + "NiceName", SoneAccessor.getNiceName(sone));
+ soneBuilder.put(sonePrefix + "Time", sone.getTime());
+ }
+
+ return soneBuilder.get();
+ }
+
+ /**
+ * Creates a simple field set from the given post.
+ *
+ * @param post
+ * The post to encode
+ * @param prefix
+ * The prefix for the field names (may be empty but not
+ * {@code null})
+ * @param includeReplies
+ * {@code true} to include replies, {@code false} to not include
+ * replies
+ * @return The simple field set containing the post
+ */
+ protected SimpleFieldSet encodePost(Post post, String prefix, boolean includeReplies) {
+ SimpleFieldSetBuilder postBuilder = new SimpleFieldSetBuilder();
+
+ postBuilder.put(prefix + "ID", post.getId());
+ postBuilder.put(prefix + "Sone", post.getSone().getId());
+ if (post.getRecipient() != null) {
+ postBuilder.put(prefix + "Recipient", post.getRecipient().getId());
+ }
+ postBuilder.put(prefix + "Time", post.getTime());
+ postBuilder.put(prefix + "Text", encodeString(post.getText()));
+ postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
+
+ if (includeReplies) {
+ List<PostReply> replies = core.getReplies(post);
+ postBuilder.put(encodeReplies(replies, prefix));
+ }
+
+ return postBuilder.get();
+ }
+
+ /**