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.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;
17 * Parses a {@link Sone}’s data from a {@link Configuration}.
19 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21 public class ConfigurationSoneParser {
23 private final Configuration configuration;
24 private final Sone sone;
25 private final String sonePrefix;
27 public ConfigurationSoneParser(Configuration configuration, Sone sone) {
28 this.configuration = configuration;
30 sonePrefix = "Sone/" + sone.getId();
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));
42 /* load profile fields. */
45 String fieldPrefix = "/Profile/Fields/" + fieldCount++;
46 String fieldName = getString(fieldPrefix + "/Name", null);
47 if (fieldName == null) {
50 String fieldValue = getString(fieldPrefix + "/Value", "");
51 profile.addField(fieldName).setValue(fieldValue);
57 private String getString(String nodeName, @Nullable String defaultValue) {
58 return configuration.getStringValue(sonePrefix + nodeName)
59 .getValue(defaultValue);
62 private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
63 return configuration.getIntValue(sonePrefix + nodeName)
64 .getValue(defaultValue);
67 private Long getLong(String nodeName, @Nullable Long defaultValue) {
68 return configuration.getLongValue(sonePrefix + nodeName)
69 .getValue(defaultValue);
72 public Collection<Post> parsePosts(PostBuilderFactory postBuilderFactory)
73 throws InvalidPostFound {
74 Set<Post> posts = new HashSet<Post>();
76 String postPrefix = "/Posts/" + posts.size();
77 String postId = getString(postPrefix + "/ID", null);
81 long postTime = getLong(postPrefix + "/Time", 0L);
82 String postText = getString(postPrefix + "/Text", null);
83 if (postAttributesAreInvalid(postTime, postText)) {
84 throw new InvalidPostFound();
86 PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
91 String postRecipientId =
92 getString(postPrefix + "/Recipient", null);
93 if (postRecipientIsValid(postRecipientId)) {
94 postBuilder.to(postRecipientId);
96 posts.add(postBuilder.build());
101 private boolean postAttributesAreInvalid(long postTime, String postText) {
102 return (postTime == 0) || (postText == null);
105 private boolean postRecipientIsValid(String postRecipientId) {
106 return (postRecipientId != null) && (postRecipientId.length() == 43);
109 public static class InvalidPostFound extends RuntimeException { }