1 package net.pterodactylus.sone.core;
3 import java.util.HashSet;
6 import javax.annotation.Nullable;
8 import net.pterodactylus.sone.data.Post;
9 import net.pterodactylus.sone.data.PostReply;
10 import net.pterodactylus.sone.data.Profile;
11 import net.pterodactylus.sone.data.Sone;
12 import net.pterodactylus.sone.database.PostBuilder;
13 import net.pterodactylus.sone.database.PostBuilderFactory;
14 import net.pterodactylus.sone.database.PostReplyBuilder;
15 import net.pterodactylus.sone.database.PostReplyBuilderFactory;
16 import net.pterodactylus.util.config.Configuration;
19 * Parses a {@link Sone}’s data from a {@link Configuration}.
21 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23 public class ConfigurationSoneParser {
25 private final Configuration configuration;
26 private final Sone sone;
27 private final String sonePrefix;
29 public ConfigurationSoneParser(Configuration configuration, Sone sone) {
30 this.configuration = configuration;
32 sonePrefix = "Sone/" + sone.getId();
35 public Profile parseProfile() {
36 Profile profile = new Profile(sone);
37 profile.setFirstName(getString("/Profile/FirstName", null));
38 profile.setMiddleName(getString("/Profile/MiddleName", null));
39 profile.setLastName(getString("/Profile/LastName", null));
40 profile.setBirthDay(getInt("/Profile/BirthDay", null));
41 profile.setBirthMonth(getInt("/Profile/BirthMonth", null));
42 profile.setBirthYear(getInt("/Profile/BirthYear", null));
44 /* load profile fields. */
47 String fieldPrefix = "/Profile/Fields/" + fieldCount++;
48 String fieldName = getString(fieldPrefix + "/Name", null);
49 if (fieldName == null) {
52 String fieldValue = getString(fieldPrefix + "/Value", "");
53 profile.addField(fieldName).setValue(fieldValue);
59 private String getString(String nodeName, @Nullable String defaultValue) {
60 return configuration.getStringValue(sonePrefix + nodeName)
61 .getValue(defaultValue);
64 private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
65 return configuration.getIntValue(sonePrefix + nodeName)
66 .getValue(defaultValue);
69 private Long getLong(String nodeName, @Nullable Long defaultValue) {
70 return configuration.getLongValue(sonePrefix + nodeName)
71 .getValue(defaultValue);
74 public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
75 throws InvalidPostFound {
76 Set<Post> posts = new HashSet<Post>();
78 String postPrefix = "/Posts/" + posts.size();
79 String postId = getString(postPrefix + "/ID", null);
83 long postTime = getLong(postPrefix + "/Time", 0L);
84 String postText = getString(postPrefix + "/Text", null);
85 if (postAttributesAreInvalid(postTime, postText)) {
86 throw new InvalidPostFound();
88 PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
93 String postRecipientId =
94 getString(postPrefix + "/Recipient", null);
95 if (postRecipientIsValid(postRecipientId)) {
96 postBuilder.to(postRecipientId);
98 posts.add(postBuilder.build());
103 private boolean postAttributesAreInvalid(long postTime, String postText) {
104 return (postTime == 0) || (postText == null);
107 private boolean postRecipientIsValid(String postRecipientId) {
108 return (postRecipientId != null) && (postRecipientId.length() == 43);
111 public Set<PostReply> parsePostReplies(
112 PostReplyBuilderFactory postReplyBuilderFactory) {
113 Set<PostReply> replies = new HashSet<PostReply>();
115 String replyPrefix = "/Replies/" + replies.size();
116 String replyId = getString(replyPrefix + "/ID", null);
117 if (replyId == null) {
120 String postId = getString(replyPrefix + "/Post/ID", null);
121 long replyTime = getLong(replyPrefix + "/Time", 0L);
122 String replyText = getString(replyPrefix + "/Text", null);
123 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
124 throw new InvalidPostReplyFound();
126 PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
127 .newPostReplyBuilder()
132 .withText(replyText);
133 replies.add(postReplyBuilder.build());
138 public Set<String> parseLikedPostIds() {
139 Set<String> likedPostIds = new HashSet<String>();
142 getString("/Likes/Post/" + likedPostIds.size() + "/ID",
144 if (likedPostId == null) {
147 likedPostIds.add(likedPostId);
152 public Set<String> parseLikedPostReplyIds() {
153 Set<String> likedPostReplyIds = new HashSet<String>();
155 String likedReplyId = getString(
156 "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null);
157 if (likedReplyId == null) {
160 likedPostReplyIds.add(likedReplyId);
162 return likedPostReplyIds;
165 public Set<String> parseFriends() {
166 Set<String> friends = new HashSet<String>();
169 getString("/Friends/" + friends.size() + "/ID", null);
170 if (friendId == null) {
173 friends.add(friendId);
178 public static class InvalidPostFound extends RuntimeException { }
180 public static class InvalidPostReplyFound extends RuntimeException { }