1 package net.pterodactylus.sone.core;
3 import java.util.Collection;
4 import java.util.HashSet;
7 import javax.annotation.Nullable;
9 import net.pterodactylus.sone.data.Post;
10 import net.pterodactylus.sone.data.PostReply;
11 import net.pterodactylus.sone.data.Profile;
12 import net.pterodactylus.sone.data.Sone;
13 import net.pterodactylus.sone.database.PostBuilder;
14 import net.pterodactylus.sone.database.PostBuilderFactory;
15 import net.pterodactylus.sone.database.PostReplyBuilder;
16 import net.pterodactylus.sone.database.PostReplyBuilderFactory;
17 import net.pterodactylus.util.config.Configuration;
20 * Parses a {@link Sone}’s data from a {@link Configuration}.
22 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24 public class ConfigurationSoneParser {
26 private final Configuration configuration;
27 private final Sone sone;
28 private final String sonePrefix;
30 public ConfigurationSoneParser(Configuration configuration, Sone sone) {
31 this.configuration = configuration;
33 sonePrefix = "Sone/" + sone.getId();
36 public Profile parseProfile() {
37 Profile profile = new Profile(sone);
38 profile.setFirstName(getString("/Profile/FirstName", null));
39 profile.setMiddleName(getString("/Profile/MiddleName", null));
40 profile.setLastName(getString("/Profile/LastName", null));
41 profile.setBirthDay(getInt("/Profile/BirthDay", null));
42 profile.setBirthMonth(getInt("/Profile/BirthMonth", null));
43 profile.setBirthYear(getInt("/Profile/BirthYear", null));
45 /* load profile fields. */
48 String fieldPrefix = "/Profile/Fields/" + fieldCount++;
49 String fieldName = getString(fieldPrefix + "/Name", null);
50 if (fieldName == null) {
53 String fieldValue = getString(fieldPrefix + "/Value", "");
54 profile.addField(fieldName).setValue(fieldValue);
60 private String getString(String nodeName, @Nullable String defaultValue) {
61 return configuration.getStringValue(sonePrefix + nodeName)
62 .getValue(defaultValue);
65 private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
66 return configuration.getIntValue(sonePrefix + nodeName)
67 .getValue(defaultValue);
70 private Long getLong(String nodeName, @Nullable Long defaultValue) {
71 return configuration.getLongValue(sonePrefix + nodeName)
72 .getValue(defaultValue);
75 public Collection<Post> parsePosts(PostBuilderFactory postBuilderFactory)
76 throws InvalidPostFound {
77 Set<Post> posts = new HashSet<Post>();
79 String postPrefix = "/Posts/" + posts.size();
80 String postId = getString(postPrefix + "/ID", null);
84 long postTime = getLong(postPrefix + "/Time", 0L);
85 String postText = getString(postPrefix + "/Text", null);
86 if (postAttributesAreInvalid(postTime, postText)) {
87 throw new InvalidPostFound();
89 PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
94 String postRecipientId =
95 getString(postPrefix + "/Recipient", null);
96 if (postRecipientIsValid(postRecipientId)) {
97 postBuilder.to(postRecipientId);
99 posts.add(postBuilder.build());
104 private boolean postAttributesAreInvalid(long postTime, String postText) {
105 return (postTime == 0) || (postText == null);
108 private boolean postRecipientIsValid(String postRecipientId) {
109 return (postRecipientId != null) && (postRecipientId.length() == 43);
112 public Collection<PostReply> parsePostReplies(
113 PostReplyBuilderFactory postReplyBuilderFactory) {
114 Set<PostReply> replies = new HashSet<PostReply>();
116 String replyPrefix = "/Replies/" + replies.size();
117 String replyId = getString(replyPrefix + "/ID", null);
118 if (replyId == null) {
121 String postId = getString(replyPrefix + "/Post/ID", null);
122 long replyTime = getLong(replyPrefix + "/Time", 0L);
123 String replyText = getString(replyPrefix + "/Text", null);
124 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
125 throw new InvalidPostReplyFound();
127 PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
128 .newPostReplyBuilder()
133 .withText(replyText);
134 replies.add(postReplyBuilder.build());
139 public static class InvalidPostFound extends RuntimeException { }
141 public static class InvalidPostReplyFound extends RuntimeException { }