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