Start moving parsing a Sone from a configuration to a specialized parser.
[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.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.util.config.Configuration;
15
16 /**
17  * Parses a {@link Sone}’s data from a {@link Configuration}.
18  *
19  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20  */
21 public class ConfigurationSoneParser {
22
23         private final Configuration configuration;
24         private final Sone sone;
25         private final String sonePrefix;
26
27         public ConfigurationSoneParser(Configuration configuration, Sone sone) {
28                 this.configuration = configuration;
29                 this.sone = sone;
30                 sonePrefix = "Sone/" + sone.getId();
31         }
32
33         public Profile parseProfile() {
34                 Profile profile = new Profile(sone);
35                 profile.setFirstName(getString("/Profile/FirstName", null));
36                 profile.setMiddleName(getString("/Profile/MiddleName", null));
37                 profile.setLastName(getString("/Profile/LastName", null));
38                 profile.setBirthDay(getInt("/Profile/BirthDay", null));
39                 profile.setBirthMonth(getInt("/Profile/BirthMonth", null));
40                 profile.setBirthYear(getInt("/Profile/BirthYear", null));
41
42                 /* load profile fields. */
43                 int fieldCount = 0;
44                 while (true) {
45                         String fieldPrefix = "/Profile/Fields/" + fieldCount++;
46                         String fieldName = getString(fieldPrefix + "/Name", null);
47                         if (fieldName == null) {
48                                 break;
49                         }
50                         String fieldValue = getString(fieldPrefix + "/Value", "");
51                         profile.addField(fieldName).setValue(fieldValue);
52                 }
53
54                 return profile;
55         }
56
57         private String getString(String nodeName, @Nullable String defaultValue) {
58                 return configuration.getStringValue(sonePrefix + nodeName)
59                                 .getValue(defaultValue);
60         }
61
62         private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
63                 return configuration.getIntValue(sonePrefix + nodeName)
64                                 .getValue(defaultValue);
65         }
66
67         private Long getLong(String nodeName, @Nullable Long defaultValue) {
68                 return configuration.getLongValue(sonePrefix + nodeName)
69                                 .getValue(defaultValue);
70         }
71
72         public Collection<Post> parsePosts(PostBuilderFactory postBuilderFactory)
73         throws InvalidPostFound {
74                 Set<Post> posts = new HashSet<Post>();
75                 while (true) {
76                         String postPrefix = "/Posts/" + posts.size();
77                         String postId = getString(postPrefix + "/ID", null);
78                         if (postId == null) {
79                                 break;
80                         }
81                         long postTime = getLong(postPrefix + "/Time", 0L);
82                         String postText = getString(postPrefix + "/Text", null);
83                         if (postAttributesAreInvalid(postTime, postText)) {
84                                 throw new InvalidPostFound();
85                         }
86                         PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
87                                         .withId(postId)
88                                         .from(sone.getId())
89                                         .withTime(postTime)
90                                         .withText(postText);
91                         String postRecipientId =
92                                         getString(postPrefix + "/Recipient", null);
93                         if (postRecipientIsValid(postRecipientId)) {
94                                 postBuilder.to(postRecipientId);
95                         }
96                         posts.add(postBuilder.build());
97                 }
98                 return posts;
99         }
100
101         private boolean postAttributesAreInvalid(long postTime, String postText) {
102                 return (postTime == 0) || (postText == null);
103         }
104
105         private boolean postRecipientIsValid(String postRecipientId) {
106                 return (postRecipientId != null) && (postRecipientId.length() == 43);
107         }
108
109         public static class InvalidPostFound extends RuntimeException { }
110
111 }