Return sets instead of collections for posts and replies.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / ConfigurationSoneParser.java
1 package net.pterodactylus.sone.core;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import javax.annotation.Nullable;
7
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;
17
18 /**
19  * Parses a {@link Sone}’s data from a {@link Configuration}.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22  */
23 public class ConfigurationSoneParser {
24
25         private final Configuration configuration;
26         private final Sone sone;
27         private final String sonePrefix;
28
29         public ConfigurationSoneParser(Configuration configuration, Sone sone) {
30                 this.configuration = configuration;
31                 this.sone = sone;
32                 sonePrefix = "Sone/" + sone.getId();
33         }
34
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));
43
44                 /* load profile fields. */
45                 int fieldCount = 0;
46                 while (true) {
47                         String fieldPrefix = "/Profile/Fields/" + fieldCount++;
48                         String fieldName = getString(fieldPrefix + "/Name", null);
49                         if (fieldName == null) {
50                                 break;
51                         }
52                         String fieldValue = getString(fieldPrefix + "/Value", "");
53                         profile.addField(fieldName).setValue(fieldValue);
54                 }
55
56                 return profile;
57         }
58
59         private String getString(String nodeName, @Nullable String defaultValue) {
60                 return configuration.getStringValue(sonePrefix + nodeName)
61                                 .getValue(defaultValue);
62         }
63
64         private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
65                 return configuration.getIntValue(sonePrefix + nodeName)
66                                 .getValue(defaultValue);
67         }
68
69         private Long getLong(String nodeName, @Nullable Long defaultValue) {
70                 return configuration.getLongValue(sonePrefix + nodeName)
71                                 .getValue(defaultValue);
72         }
73
74         public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
75         throws InvalidPostFound {
76                 Set<Post> posts = new HashSet<Post>();
77                 while (true) {
78                         String postPrefix = "/Posts/" + posts.size();
79                         String postId = getString(postPrefix + "/ID", null);
80                         if (postId == null) {
81                                 break;
82                         }
83                         long postTime = getLong(postPrefix + "/Time", 0L);
84                         String postText = getString(postPrefix + "/Text", null);
85                         if (postAttributesAreInvalid(postTime, postText)) {
86                                 throw new InvalidPostFound();
87                         }
88                         PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
89                                         .withId(postId)
90                                         .from(sone.getId())
91                                         .withTime(postTime)
92                                         .withText(postText);
93                         String postRecipientId =
94                                         getString(postPrefix + "/Recipient", null);
95                         if (postRecipientIsValid(postRecipientId)) {
96                                 postBuilder.to(postRecipientId);
97                         }
98                         posts.add(postBuilder.build());
99                 }
100                 return posts;
101         }
102
103         private boolean postAttributesAreInvalid(long postTime, String postText) {
104                 return (postTime == 0) || (postText == null);
105         }
106
107         private boolean postRecipientIsValid(String postRecipientId) {
108                 return (postRecipientId != null) && (postRecipientId.length() == 43);
109         }
110
111         public Set<PostReply> parsePostReplies(
112                         PostReplyBuilderFactory postReplyBuilderFactory) {
113                 Set<PostReply> replies = new HashSet<PostReply>();
114                 while (true) {
115                         String replyPrefix = "/Replies/" + replies.size();
116                         String replyId = getString(replyPrefix + "/ID", null);
117                         if (replyId == null) {
118                                 break;
119                         }
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();
125                         }
126                         PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
127                                         .newPostReplyBuilder()
128                                         .withId(replyId)
129                                         .from(sone.getId())
130                                         .to(postId)
131                                         .withTime(replyTime)
132                                         .withText(replyText);
133                         replies.add(postReplyBuilder.build());
134                 }
135                 return replies;
136         }
137
138         public Set<String> parseLikedPostIds() {
139                 Set<String> likedPostIds = new HashSet<String>();
140                 while (true) {
141                         String likedPostId =
142                                         getString("/Likes/Post/" + likedPostIds.size() + "/ID",
143                                                         null);
144                         if (likedPostId == null) {
145                                 break;
146                         }
147                         likedPostIds.add(likedPostId);
148                 }
149                 return likedPostIds;
150         }
151
152         public static class InvalidPostFound extends RuntimeException { }
153
154         public static class InvalidPostReplyFound extends RuntimeException { }
155
156 }