3d384c1b5260cd3aba484cdb06052cd07101417f
[Sone.git] / src / main / java / net / pterodactylus / sone / core / ConfigurationSoneParser.java
1 package net.pterodactylus.sone.core;
2
3 import static java.util.Collections.unmodifiableMap;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import javax.annotation.Nullable;
13
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;
27
28 /**
29  * Parses a {@link Sone}’s data from a {@link Configuration}.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class ConfigurationSoneParser {
34
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
41         public ConfigurationSoneParser(Configuration configuration, Sone sone) {
42                 this.configuration = configuration;
43                 this.sone = sone;
44                 sonePrefix = "Sone/" + sone.getId();
45         }
46
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));
55
56                 /* load profile fields. */
57                 int fieldCount = 0;
58                 while (true) {
59                         String fieldPrefix = "/Profile/Fields/" + fieldCount++;
60                         String fieldName = getString(fieldPrefix + "/Name", null);
61                         if (fieldName == null) {
62                                 break;
63                         }
64                         String fieldValue = getString(fieldPrefix + "/Value", "");
65                         profile.addField(fieldName).setValue(fieldValue);
66                 }
67
68                 return profile;
69         }
70
71         private String getString(String nodeName, @Nullable String defaultValue) {
72                 return configuration.getStringValue(sonePrefix + nodeName)
73                                 .getValue(defaultValue);
74         }
75
76         private Integer getInt(String nodeName, @Nullable Integer defaultValue) {
77                 return configuration.getIntValue(sonePrefix + nodeName)
78                                 .getValue(defaultValue);
79         }
80
81         private Long getLong(String nodeName, @Nullable Long defaultValue) {
82                 return configuration.getLongValue(sonePrefix + nodeName)
83                                 .getValue(defaultValue);
84         }
85
86         public Set<Post> parsePosts(PostBuilderFactory postBuilderFactory)
87         throws InvalidPostFound {
88                 Set<Post> posts = new HashSet<Post>();
89                 while (true) {
90                         String postPrefix = "/Posts/" + posts.size();
91                         String postId = getString(postPrefix + "/ID", null);
92                         if (postId == null) {
93                                 break;
94                         }
95                         long postTime = getLong(postPrefix + "/Time", 0L);
96                         String postText = getString(postPrefix + "/Text", null);
97                         if (postAttributesAreInvalid(postTime, postText)) {
98                                 throw new InvalidPostFound();
99                         }
100                         PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
101                                         .withId(postId)
102                                         .from(sone.getId())
103                                         .withTime(postTime)
104                                         .withText(postText);
105                         String postRecipientId =
106                                         getString(postPrefix + "/Recipient", null);
107                         if (postRecipientIsValid(postRecipientId)) {
108                                 postBuilder.to(postRecipientId);
109                         }
110                         posts.add(postBuilder.build());
111                 }
112                 return posts;
113         }
114
115         private boolean postAttributesAreInvalid(long postTime, String postText) {
116                 return (postTime == 0) || (postText == null);
117         }
118
119         private boolean postRecipientIsValid(String postRecipientId) {
120                 return (postRecipientId != null) && (postRecipientId.length() == 43);
121         }
122
123         public Set<PostReply> parsePostReplies(
124                         PostReplyBuilderFactory postReplyBuilderFactory) {
125                 Set<PostReply> replies = new HashSet<PostReply>();
126                 while (true) {
127                         String replyPrefix = "/Replies/" + replies.size();
128                         String replyId = getString(replyPrefix + "/ID", null);
129                         if (replyId == null) {
130                                 break;
131                         }
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();
137                         }
138                         PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
139                                         .newPostReplyBuilder()
140                                         .withId(replyId)
141                                         .from(sone.getId())
142                                         .to(postId)
143                                         .withTime(replyTime)
144                                         .withText(replyText);
145                         replies.add(postReplyBuilder.build());
146                 }
147                 return replies;
148         }
149
150         public Set<String> parseLikedPostIds() {
151                 Set<String> likedPostIds = new HashSet<String>();
152                 while (true) {
153                         String likedPostId =
154                                         getString("/Likes/Post/" + likedPostIds.size() + "/ID",
155                                                         null);
156                         if (likedPostId == null) {
157                                 break;
158                         }
159                         likedPostIds.add(likedPostId);
160                 }
161                 return likedPostIds;
162         }
163
164         public Set<String> parseLikedPostReplyIds() {
165                 Set<String> likedPostReplyIds = new HashSet<String>();
166                 while (true) {
167                         String likedReplyId = getString(
168                                         "/Likes/Reply/" + likedPostReplyIds.size() + "/ID", null);
169                         if (likedReplyId == null) {
170                                 break;
171                         }
172                         likedPostReplyIds.add(likedReplyId);
173                 }
174                 return likedPostReplyIds;
175         }
176
177         public Set<String> parseFriends() {
178                 Set<String> friends = new HashSet<String>();
179                 while (true) {
180                         String friendId =
181                                         getString("/Friends/" + friends.size() + "/ID", null);
182                         if (friendId == null) {
183                                 break;
184                         }
185                         friends.add(friendId);
186                 }
187                 return friends;
188         }
189
190         public List<Album> parseTopLevelAlbums(
191                         AlbumBuilderFactory albumBuilderFactory) {
192                 int albumCounter = 0;
193                 while (true) {
194                         String albumPrefix = "/Albums/" + albumCounter++;
195                         String albumId = getString(albumPrefix + "/ID", null);
196                         if (albumId == null) {
197                                 break;
198                         }
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();
207                         }
208                         Album album = albumBuilderFactory.newAlbumBuilder()
209                                         .withId(albumId)
210                                         .by(sone)
211                                         .build()
212                                         .modify()
213                                         .setTitle(albumTitle)
214                                         .setDescription(albumDescription)
215                                         .setAlbumImage(albumImageId)
216                                         .update();
217                         if (albumParentId != null) {
218                                 Album parentAlbum = albums.get(albumParentId);
219                                 if (parentAlbum == null) {
220                                         throw new InvalidParentAlbumFound(albumParentId);
221                                 }
222                                 parentAlbum.addAlbum(album);
223                         } else {
224                                 topLevelAlbums.add(album);
225                         }
226                         albums.put(albumId, album);
227                 }
228                 return topLevelAlbums;
229         }
230
231         public Map<String, Album> getAlbums() {
232                 return unmodifiableMap(albums);
233         }
234
235         public void parseImages(ImageBuilderFactory imageBuilderFactory) {
236                 int imageCounter = 0;
237                 while (true) {
238                         String imagePrefix = "/Images/" + imageCounter++;
239                         String imageId = getString(imagePrefix + "/ID", null);
240                         if (imageId == null) {
241                                 break;
242                         }
243                         String albumId = getString(imagePrefix + "/Album", null);
244                         String key = getString(imagePrefix + "/Key", null);
245                         String title = getString(imagePrefix + "/Title", null);
246                         String description =
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,
252                                         creationTime,
253                                         width, height)) {
254                                 throw new InvalidImageFound();
255                         }
256                         Album album = albums.get(albumId);
257                         if (album == null) {
258                                 throw new InvalidParentAlbumFound(albumId);
259                         }
260                         Image image = imageBuilderFactory.newImageBuilder()
261                                         .withId(imageId)
262                                         .build()
263                                         .modify()
264                                         .setSone(sone)
265                                         .setCreationTime(creationTime)
266                                         .setKey(key)
267                                         .setTitle(title)
268                                         .setDescription(description)
269                                         .setWidth(width)
270                                         .setHeight(height)
271                                         .update();
272                         album.addImage(image);
273                 }
274         }
275
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);
282         }
283
284         public static class InvalidPostFound extends RuntimeException { }
285
286         public static class InvalidPostReplyFound extends RuntimeException { }
287
288         public static class InvalidAlbumFound extends RuntimeException { }
289
290         public static class InvalidParentAlbumFound extends RuntimeException {
291
292                 private final String albumParentId;
293
294                 public InvalidParentAlbumFound(String albumParentId) {
295                         this.albumParentId = albumParentId;
296                 }
297
298                 public String getAlbumParentId() {
299                         return albumParentId;
300                 }
301
302         }
303
304         public static class InvalidImageFound extends RuntimeException { }
305
306 }