Move album parsing to new configuration parser.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / ConfigurationSoneParser.java
1 package net.pterodactylus.sone.core;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import javax.annotation.Nullable;
11
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;
23
24 /**
25  * Parses a {@link Sone}’s data from a {@link Configuration}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class ConfigurationSoneParser {
30
31         private final Configuration configuration;
32         private final Sone sone;
33         private final String sonePrefix;
34
35         public ConfigurationSoneParser(Configuration configuration, Sone sone) {
36                 this.configuration = configuration;
37                 this.sone = sone;
38                 sonePrefix = "Sone/" + sone.getId();
39         }
40
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));
49
50                 /* load profile fields. */
51                 int fieldCount = 0;
52                 while (true) {
53                         String fieldPrefix = "/Profile/Fields/" + fieldCount++;
54                         String fieldName = getString(fieldPrefix + "/Name", null);
55                         if (fieldName == null) {
56                                 break;
57                         }
58                         String fieldValue = getString(fieldPrefix + "/Value", "");
59                         profile.addField(fieldName).setValue(fieldValue);
60                 }
61
62                 return profile;
63         }
64
65         private String getString(String nodeName, @Nullable String defaultValue) {
66                 return configuration.getStringValue(sonePrefix + nodeName)
67                                 .getValue(defaultValue);
68         }
69
70         private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
71                 return configuration.getIntValue(sonePrefix + nodeName)
72                                 .getValue(defaultValue);
73         }
74
75         private Long getLong(String nodeName, @Nullable Long defaultValue) {
76                 return configuration.getLongValue(sonePrefix + nodeName)
77                                 .getValue(defaultValue);
78         }
79
80         public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
81         throws InvalidPostFound {
82                 Set<Post> posts = new HashSet<Post>();
83                 while (true) {
84                         String postPrefix = "/Posts/" + posts.size();
85                         String postId = getString(postPrefix + "/ID", null);
86                         if (postId == null) {
87                                 break;
88                         }
89                         long postTime = getLong(postPrefix + "/Time", 0L);
90                         String postText = getString(postPrefix + "/Text", null);
91                         if (postAttributesAreInvalid(postTime, postText)) {
92                                 throw new InvalidPostFound();
93                         }
94                         PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
95                                         .withId(postId)
96                                         .from(sone.getId())
97                                         .withTime(postTime)
98                                         .withText(postText);
99                         String postRecipientId =
100                                         getString(postPrefix + "/Recipient", null);
101                         if (postRecipientIsValid(postRecipientId)) {
102                                 postBuilder.to(postRecipientId);
103                         }
104                         posts.add(postBuilder.build());
105                 }
106                 return posts;
107         }
108
109         private boolean postAttributesAreInvalid(long postTime, String postText) {
110                 return (postTime == 0) || (postText == null);
111         }
112
113         private boolean postRecipientIsValid(String postRecipientId) {
114                 return (postRecipientId != null) && (postRecipientId.length() == 43);
115         }
116
117         public Set<PostReply> parsePostReplies(
118                         PostReplyBuilderFactory postReplyBuilderFactory) {
119                 Set<PostReply> replies = new HashSet<PostReply>();
120                 while (true) {
121                         String replyPrefix = "/Replies/" + replies.size();
122                         String replyId = getString(replyPrefix + "/ID", null);
123                         if (replyId == null) {
124                                 break;
125                         }
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();
131                         }
132                         PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
133                                         .newPostReplyBuilder()
134                                         .withId(replyId)
135                                         .from(sone.getId())
136                                         .to(postId)
137                                         .withTime(replyTime)
138                                         .withText(replyText);
139                         replies.add(postReplyBuilder.build());
140                 }
141                 return replies;
142         }
143
144         public Set<String> parseLikedPostIds() {
145                 Set<String> likedPostIds = new HashSet<String>();
146                 while (true) {
147                         String likedPostId =
148                                         getString("/Likes/Post/" + likedPostIds.size() + "/ID",
149                                                         null);
150                         if (likedPostId == null) {
151                                 break;
152                         }
153                         likedPostIds.add(likedPostId);
154                 }
155                 return likedPostIds;
156         }
157
158         public Set<String> parseLikedPostReplyIds() {
159                 Set<String> likedPostReplyIds = new HashSet<String>();
160                 while (true) {
161                         String likedReplyId = getString(
162                                         "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null);
163                         if (likedReplyId == null) {
164                                 break;
165                         }
166                         likedPostReplyIds.add(likedReplyId);
167                 }
168                 return likedPostReplyIds;
169         }
170
171         public Set<String> parseFriends() {
172                 Set<String> friends = new HashSet<String>();
173                 while (true) {
174                         String friendId =
175                                         getString("/Friends/" + friends.size() + "/ID", null);
176                         if (friendId == null) {
177                                 break;
178                         }
179                         friends.add(friendId);
180                 }
181                 return friends;
182         }
183
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;
189                 while (true) {
190                         String albumPrefix = "/Albums/" + albumCounter++;
191                         String albumId = getString(albumPrefix + "/ID", null);
192                         if (albumId == null) {
193                                 break;
194                         }
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();
203                         }
204                         Album album = albumBuilderFactory.newAlbumBuilder()
205                                         .withId(albumId)
206                                         .by(sone)
207                                         .build()
208                                         .modify()
209                                         .setTitle(albumTitle)
210                                         .setDescription(albumDescription)
211                                         .setAlbumImage(albumImageId)
212                                         .update();
213                         if (albumParentId != null) {
214                                 Album parentAlbum = albums.get(albumParentId);
215                                 if (parentAlbum == null) {
216                                         throw new InvalidParentAlbumFound(albumParentId);
217                                 }
218                                 parentAlbum.addAlbum(album);
219                         } else {
220                                 topLevelAlbums.add(album);
221                         }
222                         albums.put(albumId, album);
223                 }
224                 return topLevelAlbums;
225         }
226
227         public static class InvalidPostFound extends RuntimeException { }
228
229         public static class InvalidPostReplyFound extends RuntimeException { }
230
231         public static class InvalidAlbumFound extends RuntimeException { }
232
233         public static class InvalidParentAlbumFound extends RuntimeException {
234
235                 private final String albumParentId;
236
237                 public InvalidParentAlbumFound(String albumParentId) {
238                         this.albumParentId = albumParentId;
239                 }
240
241                 public String getAlbumParentId() {
242                         return albumParentId;
243                 }
244
245         }
246
247 }