X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FConfigurationSoneParser.java;h=f592c1eaf80888ea85c561c6e6f26e504f9ed152;hb=851ef7a7f4e25fe3c57c2d9c67349acce58f1ddc;hp=7b7be9ff421107bd366dfb4bad3ba88c19cb3385;hpb=ffb2ea1773cf7e3d1b7fc41ab0e9c3c1eed514e0;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java index 7b7be9f..f592c1e 100644 --- a/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java @@ -1,5 +1,7 @@ package net.pterodactylus.sone.core; +import static java.util.Collections.unmodifiableMap; + import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -10,11 +12,13 @@ import java.util.Set; import javax.annotation.Nullable; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.AlbumBuilderFactory; +import net.pterodactylus.sone.database.ImageBuilderFactory; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostBuilderFactory; import net.pterodactylus.sone.database.PostReplyBuilder; @@ -29,16 +33,19 @@ import net.pterodactylus.util.config.Configuration; public class ConfigurationSoneParser { private final Configuration configuration; - private final Sone sone; + private final String soneId; private final String sonePrefix; + private final Map albums = new HashMap(); + private final List topLevelAlbums = new ArrayList(); + private final Map images = new HashMap(); - public ConfigurationSoneParser(Configuration configuration, Sone sone) { + public ConfigurationSoneParser(Configuration configuration, String soneId) { this.configuration = configuration; - this.sone = sone; - sonePrefix = "Sone/" + sone.getId(); + this.soneId = soneId; + sonePrefix = "Sone/" + soneId; } - public Profile parseProfile() { + public Profile parseProfile(Sone sone) { Profile profile = new Profile(sone); profile.setFirstName(getString("/Profile/FirstName", null)); profile.setMiddleName(getString("/Profile/MiddleName", null)); @@ -93,7 +100,7 @@ public class ConfigurationSoneParser { } PostBuilder postBuilder = postBuilderFactory.newPostBuilder() .withId(postId) - .from(sone.getId()) + .from(soneId) .withTime(postTime) .withText(postText); String postRecipientId = @@ -132,7 +139,7 @@ public class ConfigurationSoneParser { PostReplyBuilder postReplyBuilder = postReplyBuilderFactory .newPostReplyBuilder() .withId(replyId) - .from(sone.getId()) + .from(soneId) .to(postId) .withTime(replyTime) .withText(replyText); @@ -182,9 +189,7 @@ public class ConfigurationSoneParser { } public List parseTopLevelAlbums( - AlbumBuilderFactory albumBuilderFactory) { - Map albums = new HashMap(); - List topLevelAlbums = new ArrayList(); + AlbumBuilderFactory albumBuilderFactory, Sone sone) { int albumCounter = 0; while (true) { String albumPrefix = "/Albums/" + albumCounter++; @@ -224,6 +229,64 @@ public class ConfigurationSoneParser { return topLevelAlbums; } + public Map getAlbums() { + return unmodifiableMap(albums); + } + + public void parseImages(ImageBuilderFactory imageBuilderFactory, Sone sone) { + int imageCounter = 0; + while (true) { + String imagePrefix = "/Images/" + imageCounter++; + String imageId = getString(imagePrefix + "/ID", null); + if (imageId == null) { + break; + } + String albumId = getString(imagePrefix + "/Album", null); + String key = getString(imagePrefix + "/Key", null); + String title = getString(imagePrefix + "/Title", null); + String description = + getString(imagePrefix + "/Description", null); + Long creationTime = getLong(imagePrefix + "/CreationTime", null); + Integer width = getInt(imagePrefix + "/Width", null); + Integer height = getInt(imagePrefix + "/Height", null); + if (albumAttributesAreInvalid(albumId, key, title, description, + creationTime, + width, height)) { + throw new InvalidImageFound(); + } + Album album = albums.get(albumId); + if (album == null) { + throw new InvalidParentAlbumFound(albumId); + } + Image image = imageBuilderFactory.newImageBuilder() + .withId(imageId) + .build() + .modify() + .setSone(sone) + .setCreationTime(creationTime) + .setKey(key) + .setTitle(title) + .setDescription(description) + .setWidth(width) + .setHeight(height) + .update(); + album.addImage(image); + images.put(image.getId(), image); + } + } + + public Map getImages() { + return images; + } + + private boolean albumAttributesAreInvalid(String albumId, String key, + String title, String description, Long creationTime, + Integer width, Integer height) { + return (albumId == null) || (key == null) || (title == null) || ( + description == null) || (creationTime == null) || (width + == null) || (height == null); + } + public static class InvalidPostFound extends RuntimeException { } public static class InvalidPostReplyFound extends RuntimeException { } @@ -244,4 +307,6 @@ public class ConfigurationSoneParser { } + public static class InvalidImageFound extends RuntimeException { } + }