1 package net.pterodactylus.sone.core;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
10 import javax.annotation.Nullable;
12 import net.pterodactylus.sone.data.Album;
13 import net.pterodactylus.sone.data.Post;
14 import net.pterodactylus.sone.data.PostReply;
15 import net.pterodactylus.sone.data.Profile;
16 import net.pterodactylus.sone.data.Sone;
17 import net.pterodactylus.sone.database.AlbumBuilderFactory;
18 import net.pterodactylus.sone.database.PostBuilder;
19 import net.pterodactylus.sone.database.PostBuilderFactory;
20 import net.pterodactylus.sone.database.PostReplyBuilder;
21 import net.pterodactylus.sone.database.PostReplyBuilderFactory;
22 import net.pterodactylus.util.config.Configuration;
25 * Parses a {@link Sone}’s data from a {@link Configuration}.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public class ConfigurationSoneParser {
31 private final Configuration configuration;
32 private final Sone sone;
33 private final String sonePrefix;
35 public ConfigurationSoneParser(Configuration configuration, Sone sone) {
36 this.configuration = configuration;
38 sonePrefix = "Sone/" + sone.getId();
41 public Profile parseProfile() {
42 Profile profile = new Profile(sone);
43 profile.setFirstName(getString("/Profile/FirstName", null));
44 profile.setMiddleName(getString("/Profile/MiddleName", null));
45 profile.setLastName(getString("/Profile/LastName", null));
46 profile.setBirthDay(getInt("/Profile/BirthDay", null));
47 profile.setBirthMonth(getInt("/Profile/BirthMonth", null));
48 profile.setBirthYear(getInt("/Profile/BirthYear", null));
50 /* load profile fields. */
53 String fieldPrefix = "/Profile/Fields/" + fieldCount++;
54 String fieldName = getString(fieldPrefix + "/Name", null);
55 if (fieldName == null) {
58 String fieldValue = getString(fieldPrefix + "/Value", "");
59 profile.addField(fieldName).setValue(fieldValue);
65 private String getString(String nodeName, @Nullable String defaultValue) {
66 return configuration.getStringValue(sonePrefix + nodeName)
67 .getValue(defaultValue);
70 private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
71 return configuration.getIntValue(sonePrefix + nodeName)
72 .getValue(defaultValue);
75 private Long getLong(String nodeName, @Nullable Long defaultValue) {
76 return configuration.getLongValue(sonePrefix + nodeName)
77 .getValue(defaultValue);
80 public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
81 throws InvalidPostFound {
82 Set<Post> posts = new HashSet<Post>();
84 String postPrefix = "/Posts/" + posts.size();
85 String postId = getString(postPrefix + "/ID", null);
89 long postTime = getLong(postPrefix + "/Time", 0L);
90 String postText = getString(postPrefix + "/Text", null);
91 if (postAttributesAreInvalid(postTime, postText)) {
92 throw new InvalidPostFound();
94 PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
99 String postRecipientId =
100 getString(postPrefix + "/Recipient", null);
101 if (postRecipientIsValid(postRecipientId)) {
102 postBuilder.to(postRecipientId);
104 posts.add(postBuilder.build());
109 private boolean postAttributesAreInvalid(long postTime, String postText) {
110 return (postTime == 0) || (postText == null);
113 private boolean postRecipientIsValid(String postRecipientId) {
114 return (postRecipientId != null) && (postRecipientId.length() == 43);
117 public Set<PostReply> parsePostReplies(
118 PostReplyBuilderFactory postReplyBuilderFactory) {
119 Set<PostReply> replies = new HashSet<PostReply>();
121 String replyPrefix = "/Replies/" + replies.size();
122 String replyId = getString(replyPrefix + "/ID", null);
123 if (replyId == null) {
126 String postId = getString(replyPrefix + "/Post/ID", null);
127 long replyTime = getLong(replyPrefix + "/Time", 0L);
128 String replyText = getString(replyPrefix + "/Text", null);
129 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
130 throw new InvalidPostReplyFound();
132 PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
133 .newPostReplyBuilder()
138 .withText(replyText);
139 replies.add(postReplyBuilder.build());
144 public Set<String> parseLikedPostIds() {
145 Set<String> likedPostIds = new HashSet<String>();
148 getString("/Likes/Post/" + likedPostIds.size() + "/ID",
150 if (likedPostId == null) {
153 likedPostIds.add(likedPostId);
158 public Set<String> parseLikedPostReplyIds() {
159 Set<String> likedPostReplyIds = new HashSet<String>();
161 String likedReplyId = getString(
162 "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null);
163 if (likedReplyId == null) {
166 likedPostReplyIds.add(likedReplyId);
168 return likedPostReplyIds;
171 public Set<String> parseFriends() {
172 Set<String> friends = new HashSet<String>();
175 getString("/Friends/" + friends.size() + "/ID", null);
176 if (friendId == null) {
179 friends.add(friendId);
184 public List<Album> parseTopLevelAlbums(
185 AlbumBuilderFactory albumBuilderFactory) {
186 Map<String, Album> albums = new HashMap<String, Album>();
187 List<Album> topLevelAlbums = new ArrayList<Album>();
188 int albumCounter = 0;
190 String albumPrefix = "/Albums/" + albumCounter++;
191 String albumId = getString(albumPrefix + "/ID", null);
192 if (albumId == null) {
195 String albumTitle = getString(albumPrefix + "/Title", null);
196 String albumDescription =
197 getString(albumPrefix + "/Description", null);
198 String albumParentId = getString(albumPrefix + "/Parent", null);
199 String albumImageId =
200 getString(albumPrefix + "/AlbumImage", null);
201 if ((albumTitle == null) || (albumDescription == null)) {
202 throw new InvalidAlbumFound();
204 Album album = albumBuilderFactory.newAlbumBuilder()
209 .setTitle(albumTitle)
210 .setDescription(albumDescription)
211 .setAlbumImage(albumImageId)
213 if (albumParentId != null) {
214 Album parentAlbum = albums.get(albumParentId);
215 if (parentAlbum == null) {
216 throw new InvalidParentAlbumFound(albumParentId);
218 parentAlbum.addAlbum(album);
220 topLevelAlbums.add(album);
222 albums.put(albumId, album);
224 return topLevelAlbums;
227 public static class InvalidPostFound extends RuntimeException { }
229 public static class InvalidPostReplyFound extends RuntimeException { }
231 public static class InvalidAlbumFound extends RuntimeException { }
233 public static class InvalidParentAlbumFound extends RuntimeException {
235 private final String albumParentId;
237 public InvalidParentAlbumFound(String albumParentId) {
238 this.albumParentId = albumParentId;
241 public String getAlbumParentId() {
242 return albumParentId;