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>();
40 private final Map<String, Image> images = new HashMap<String, Image>();
42 public ConfigurationSoneParser(Configuration configuration, Sone sone) {
43 this.configuration = configuration;
45 sonePrefix = "Sone/" + sone.getId();
48 public Profile parseProfile() {
49 Profile profile = new Profile(sone);
50 profile.setFirstName(getString("/Profile/FirstName", null));
51 profile.setMiddleName(getString("/Profile/MiddleName", null));
52 profile.setLastName(getString("/Profile/LastName", null));
53 profile.setBirthDay(getInt("/Profile/BirthDay", null));
54 profile.setBirthMonth(getInt("/Profile/BirthMonth", null));
55 profile.setBirthYear(getInt("/Profile/BirthYear", null));
57 /* load profile fields. */
60 String fieldPrefix = "/Profile/Fields/" + fieldCount++;
61 String fieldName = getString(fieldPrefix + "/Name", null);
62 if (fieldName == null) {
65 String fieldValue = getString(fieldPrefix + "/Value", "");
66 profile.addField(fieldName).setValue(fieldValue);
72 private String getString(String nodeName, @Nullable String defaultValue) {
73 return configuration.getStringValue(sonePrefix + nodeName)
74 .getValue(defaultValue);
77 private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
78 return configuration.getIntValue(sonePrefix + nodeName)
79 .getValue(defaultValue);
82 private Long getLong(String nodeName, @Nullable Long defaultValue) {
83 return configuration.getLongValue(sonePrefix + nodeName)
84 .getValue(defaultValue);
87 public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
88 throws InvalidPostFound {
89 Set<Post> posts = new HashSet<Post>();
91 String postPrefix = "/Posts/" + posts.size();
92 String postId = getString(postPrefix + "/ID", null);
96 long postTime = getLong(postPrefix + "/Time", 0L);
97 String postText = getString(postPrefix + "/Text", null);
98 if (postAttributesAreInvalid(postTime, postText)) {
99 throw new InvalidPostFound();
101 PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
106 String postRecipientId =
107 getString(postPrefix + "/Recipient", null);
108 if (postRecipientIsValid(postRecipientId)) {
109 postBuilder.to(postRecipientId);
111 posts.add(postBuilder.build());
116 private boolean postAttributesAreInvalid(long postTime, String postText) {
117 return (postTime == 0) || (postText == null);
120 private boolean postRecipientIsValid(String postRecipientId) {
121 return (postRecipientId != null) && (postRecipientId.length() == 43);
124 public Set<PostReply> parsePostReplies(
125 PostReplyBuilderFactory postReplyBuilderFactory) {
126 Set<PostReply> replies = new HashSet<PostReply>();
128 String replyPrefix = "/Replies/" + replies.size();
129 String replyId = getString(replyPrefix + "/ID", null);
130 if (replyId == null) {
133 String postId = getString(replyPrefix + "/Post/ID", null);
134 long replyTime = getLong(replyPrefix + "/Time", 0L);
135 String replyText = getString(replyPrefix + "/Text", null);
136 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
137 throw new InvalidPostReplyFound();
139 PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
140 .newPostReplyBuilder()
145 .withText(replyText);
146 replies.add(postReplyBuilder.build());
151 public Set<String> parseLikedPostIds() {
152 Set<String> likedPostIds = new HashSet<String>();
155 getString("/Likes/Post/" + likedPostIds.size() + "/ID",
157 if (likedPostId == null) {
160 likedPostIds.add(likedPostId);
165 public Set<String> parseLikedPostReplyIds() {
166 Set<String> likedPostReplyIds = new HashSet<String>();
168 String likedReplyId = getString(
169 "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null);
170 if (likedReplyId == null) {
173 likedPostReplyIds.add(likedReplyId);
175 return likedPostReplyIds;
178 public Set<String> parseFriends() {
179 Set<String> friends = new HashSet<String>();
182 getString("/Friends/" + friends.size() + "/ID", null);
183 if (friendId == null) {
186 friends.add(friendId);
191 public List<Album> parseTopLevelAlbums(
192 AlbumBuilderFactory albumBuilderFactory) {
193 int albumCounter = 0;
195 String albumPrefix = "/Albums/" + albumCounter++;
196 String albumId = getString(albumPrefix + "/ID", null);
197 if (albumId == null) {
200 String albumTitle = getString(albumPrefix + "/Title", null);
201 String albumDescription =
202 getString(albumPrefix + "/Description", null);
203 String albumParentId = getString(albumPrefix + "/Parent", null);
204 String albumImageId =
205 getString(albumPrefix + "/AlbumImage", null);
206 if ((albumTitle == null) || (albumDescription == null)) {
207 throw new InvalidAlbumFound();
209 Album album = albumBuilderFactory.newAlbumBuilder()
214 .setTitle(albumTitle)
215 .setDescription(albumDescription)
216 .setAlbumImage(albumImageId)
218 if (albumParentId != null) {
219 Album parentAlbum = albums.get(albumParentId);
220 if (parentAlbum == null) {
221 throw new InvalidParentAlbumFound(albumParentId);
223 parentAlbum.addAlbum(album);
225 topLevelAlbums.add(album);
227 albums.put(albumId, album);
229 return topLevelAlbums;
232 public Map<String, Album> getAlbums() {
233 return unmodifiableMap(albums);
236 public void parseImages(ImageBuilderFactory imageBuilderFactory) {
237 int imageCounter = 0;
239 String imagePrefix = "/Images/" + imageCounter++;
240 String imageId = getString(imagePrefix + "/ID", null);
241 if (imageId == null) {
244 String albumId = getString(imagePrefix + "/Album", null);
245 String key = getString(imagePrefix + "/Key", null);
246 String title = getString(imagePrefix + "/Title", null);
248 getString(imagePrefix + "/Description", null);
249 Long creationTime = getLong(imagePrefix + "/CreationTime", null);
250 Integer width = getInt(imagePrefix + "/Width", null);
251 Integer height = getInt(imagePrefix + "/Height", null);
252 if (albumAttributesAreInvalid(albumId, key, title, description,
255 throw new InvalidImageFound();
257 Album album = albums.get(albumId);
259 throw new InvalidParentAlbumFound(albumId);
261 Image image = imageBuilderFactory.newImageBuilder()
266 .setCreationTime(creationTime)
269 .setDescription(description)
273 album.addImage(image);
274 images.put(image.getId(), image);
278 public Map<String, Image> getImages() {
282 private boolean albumAttributesAreInvalid(String albumId, String key,
283 String title, String description, Long creationTime,
284 Integer width, Integer height) {
285 return (albumId == null) || (key == null) || (title == null) || (
286 description == null) || (creationTime == null) || (width
287 == null) || (height == null);
290 public static class InvalidPostFound extends RuntimeException { }
292 public static class InvalidPostReplyFound extends RuntimeException { }
294 public static class InvalidAlbumFound extends RuntimeException { }
296 public static class InvalidParentAlbumFound extends RuntimeException {
298 private final String albumParentId;
300 public InvalidParentAlbumFound(String albumParentId) {
301 this.albumParentId = albumParentId;
304 public String getAlbumParentId() {
305 return albumParentId;
310 public static class InvalidImageFound extends RuntimeException { }