1 package net.pterodactylus.sone.core;
3 import static java.util.Collections.unmodifiableMap;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
12 import javax.annotation.Nullable;
14 import net.pterodactylus.sone.data.Album;
15 import net.pterodactylus.sone.data.Image;
16 import net.pterodactylus.sone.data.Post;
17 import net.pterodactylus.sone.data.PostReply;
18 import net.pterodactylus.sone.data.Profile;
19 import net.pterodactylus.sone.data.Sone;
20 import net.pterodactylus.sone.database.AlbumBuilderFactory;
21 import net.pterodactylus.sone.database.ImageBuilderFactory;
22 import net.pterodactylus.sone.database.PostBuilder;
23 import net.pterodactylus.sone.database.PostBuilderFactory;
24 import net.pterodactylus.sone.database.PostReplyBuilder;
25 import net.pterodactylus.sone.database.PostReplyBuilderFactory;
26 import net.pterodactylus.util.config.Configuration;
29 * Parses a {@link Sone}’s data from a {@link Configuration}.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class ConfigurationSoneParser {
35 private final Configuration configuration;
36 private final Sone sone;
37 private final String sonePrefix;
38 private final Map<String, Album> albums = new HashMap<String, Album>();
39 private final List<Album> topLevelAlbums = new ArrayList<Album>();
41 public ConfigurationSoneParser(Configuration configuration, Sone sone) {
42 this.configuration = configuration;
44 sonePrefix = "Sone/" + sone.getId();
47 public Profile parseProfile() {
48 Profile profile = new Profile(sone);
49 profile.setFirstName(getString("/Profile/FirstName", null));
50 profile.setMiddleName(getString("/Profile/MiddleName", null));
51 profile.setLastName(getString("/Profile/LastName", null));
52 profile.setBirthDay(getInt("/Profile/BirthDay", null));
53 profile.setBirthMonth(getInt("/Profile/BirthMonth", null));
54 profile.setBirthYear(getInt("/Profile/BirthYear", null));
56 /* load profile fields. */
59 String fieldPrefix = "/Profile/Fields/" + fieldCount++;
60 String fieldName = getString(fieldPrefix + "/Name", null);
61 if (fieldName == null) {
64 String fieldValue = getString(fieldPrefix + "/Value", "");
65 profile.addField(fieldName).setValue(fieldValue);
71 private String getString(String nodeName, @Nullable String defaultValue) {
72 return configuration.getStringValue(sonePrefix + nodeName)
73 .getValue(defaultValue);
76 private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
77 return configuration.getIntValue(sonePrefix + nodeName)
78 .getValue(defaultValue);
81 private Long getLong(String nodeName, @Nullable Long defaultValue) {
82 return configuration.getLongValue(sonePrefix + nodeName)
83 .getValue(defaultValue);
86 public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
87 throws InvalidPostFound {
88 Set<Post> posts = new HashSet<Post>();
90 String postPrefix = "/Posts/" + posts.size();
91 String postId = getString(postPrefix + "/ID", null);
95 long postTime = getLong(postPrefix + "/Time", 0L);
96 String postText = getString(postPrefix + "/Text", null);
97 if (postAttributesAreInvalid(postTime, postText)) {
98 throw new InvalidPostFound();
100 PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
105 String postRecipientId =
106 getString(postPrefix + "/Recipient", null);
107 if (postRecipientIsValid(postRecipientId)) {
108 postBuilder.to(postRecipientId);
110 posts.add(postBuilder.build());
115 private boolean postAttributesAreInvalid(long postTime, String postText) {
116 return (postTime == 0) || (postText == null);
119 private boolean postRecipientIsValid(String postRecipientId) {
120 return (postRecipientId != null) && (postRecipientId.length() == 43);
123 public Set<PostReply> parsePostReplies(
124 PostReplyBuilderFactory postReplyBuilderFactory) {
125 Set<PostReply> replies = new HashSet<PostReply>();
127 String replyPrefix = "/Replies/" + replies.size();
128 String replyId = getString(replyPrefix + "/ID", null);
129 if (replyId == null) {
132 String postId = getString(replyPrefix + "/Post/ID", null);
133 long replyTime = getLong(replyPrefix + "/Time", 0L);
134 String replyText = getString(replyPrefix + "/Text", null);
135 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
136 throw new InvalidPostReplyFound();
138 PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
139 .newPostReplyBuilder()
144 .withText(replyText);
145 replies.add(postReplyBuilder.build());
150 public Set<String> parseLikedPostIds() {
151 Set<String> likedPostIds = new HashSet<String>();
154 getString("/Likes/Post/" + likedPostIds.size() + "/ID",
156 if (likedPostId == null) {
159 likedPostIds.add(likedPostId);
164 public Set<String> parseLikedPostReplyIds() {
165 Set<String> likedPostReplyIds = new HashSet<String>();
167 String likedReplyId = getString(
168 "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null);
169 if (likedReplyId == null) {
172 likedPostReplyIds.add(likedReplyId);
174 return likedPostReplyIds;
177 public Set<String> parseFriends() {
178 Set<String> friends = new HashSet<String>();
181 getString("/Friends/" + friends.size() + "/ID", null);
182 if (friendId == null) {
185 friends.add(friendId);
190 public List<Album> parseTopLevelAlbums(
191 AlbumBuilderFactory albumBuilderFactory) {
192 int albumCounter = 0;
194 String albumPrefix = "/Albums/" + albumCounter++;
195 String albumId = getString(albumPrefix + "/ID", null);
196 if (albumId == null) {
199 String albumTitle = getString(albumPrefix + "/Title", null);
200 String albumDescription =
201 getString(albumPrefix + "/Description", null);
202 String albumParentId = getString(albumPrefix + "/Parent", null);
203 String albumImageId =
204 getString(albumPrefix + "/AlbumImage", null);
205 if ((albumTitle == null) || (albumDescription == null)) {
206 throw new InvalidAlbumFound();
208 Album album = albumBuilderFactory.newAlbumBuilder()
213 .setTitle(albumTitle)
214 .setDescription(albumDescription)
215 .setAlbumImage(albumImageId)
217 if (albumParentId != null) {
218 Album parentAlbum = albums.get(albumParentId);
219 if (parentAlbum == null) {
220 throw new InvalidParentAlbumFound(albumParentId);
222 parentAlbum.addAlbum(album);
224 topLevelAlbums.add(album);
226 albums.put(albumId, album);
228 return topLevelAlbums;
231 public Map<String, Album> getAlbums() {
232 return unmodifiableMap(albums);
235 public void parseImages(ImageBuilderFactory imageBuilderFactory) {
236 int imageCounter = 0;
238 String imagePrefix = "/Images/" + imageCounter++;
239 String imageId = getString(imagePrefix + "/ID", null);
240 if (imageId == null) {
243 String albumId = getString(imagePrefix + "/Album", null);
244 String key = getString(imagePrefix + "/Key", null);
245 String title = getString(imagePrefix + "/Title", null);
247 getString(imagePrefix + "/Description", null);
248 Long creationTime = getLong(imagePrefix + "/CreationTime", null);
249 Integer width = getInt(imagePrefix + "/Width", null);
250 Integer height = getInt(imagePrefix + "/Height", null);
251 if (albumAttributesAreInvalid(albumId, key, title, description,
254 throw new InvalidImageFound();
256 Album album = albums.get(albumId);
258 throw new InvalidParentAlbumFound(albumId);
260 Image image = imageBuilderFactory.newImageBuilder()
265 .setCreationTime(creationTime)
268 .setDescription(description)
272 album.addImage(image);
276 private boolean albumAttributesAreInvalid(String albumId, String key,
277 String title, String description, Long creationTime,
278 Integer width, Integer height) {
279 return (albumId == null) || (key == null) || (title == null) || (
280 description == null) || (creationTime == null) || (width
281 == null) || (height == null);
284 public static class InvalidPostFound extends RuntimeException { }
286 public static class InvalidPostReplyFound extends RuntimeException { }
288 public static class InvalidAlbumFound extends RuntimeException { }
290 public static class InvalidParentAlbumFound extends RuntimeException {
292 private final String albumParentId;
294 public InvalidParentAlbumFound(String albumParentId) {
295 this.albumParentId = albumParentId;
298 public String getAlbumParentId() {
299 return albumParentId;
304 public static class InvalidImageFound extends RuntimeException { }