Fix loading of local Sones without posts and replies.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 18 Dec 2014 19:43:13 +0000 (20:43 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 18 Dec 2014 19:43:13 +0000 (20:43 +0100)
src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/test/java/net/pterodactylus/sone/core/ConfigurationSoneParserTest.java

index a29856b..f592c1e 100644 (file)
@@ -33,19 +33,19 @@ import net.pterodactylus.util.config.Configuration;
 public class ConfigurationSoneParser {
 
        private final Configuration configuration;
 public class ConfigurationSoneParser {
 
        private final Configuration configuration;
-       private final Sone sone;
+       private final String soneId;
        private final String sonePrefix;
        private final Map<String, Album> albums = new HashMap<String, Album>();
        private final List<Album> topLevelAlbums = new ArrayList<Album>();
        private final Map<String, Image> images = new HashMap<String, Image>();
 
        private final String sonePrefix;
        private final Map<String, Album> albums = new HashMap<String, Album>();
        private final List<Album> topLevelAlbums = new ArrayList<Album>();
        private final Map<String, Image> images = new HashMap<String, Image>();
 
-       public ConfigurationSoneParser(Configuration configuration, Sone sone) {
+       public ConfigurationSoneParser(Configuration configuration, String soneId) {
                this.configuration = configuration;
                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));
                Profile profile = new Profile(sone);
                profile.setFirstName(getString("/Profile/FirstName", null));
                profile.setMiddleName(getString("/Profile/MiddleName", null));
@@ -100,7 +100,7 @@ public class ConfigurationSoneParser {
                        }
                        PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
                                        .withId(postId)
                        }
                        PostBuilder postBuilder = postBuilderFactory.newPostBuilder()
                                        .withId(postId)
-                                       .from(sone.getId())
+                                       .from(soneId)
                                        .withTime(postTime)
                                        .withText(postText);
                        String postRecipientId =
                                        .withTime(postTime)
                                        .withText(postText);
                        String postRecipientId =
@@ -139,7 +139,7 @@ public class ConfigurationSoneParser {
                        PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
                                        .newPostReplyBuilder()
                                        .withId(replyId)
                        PostReplyBuilder postReplyBuilder = postReplyBuilderFactory
                                        .newPostReplyBuilder()
                                        .withId(replyId)
-                                       .from(sone.getId())
+                                       .from(soneId)
                                        .to(postId)
                                        .withTime(replyTime)
                                        .withText(replyText);
                                        .to(postId)
                                        .withTime(replyTime)
                                        .withText(replyText);
@@ -189,7 +189,7 @@ public class ConfigurationSoneParser {
        }
 
        public List<Album> parseTopLevelAlbums(
        }
 
        public List<Album> parseTopLevelAlbums(
-                       AlbumBuilderFactory albumBuilderFactory) {
+                       AlbumBuilderFactory albumBuilderFactory, Sone sone) {
                int albumCounter = 0;
                while (true) {
                        String albumPrefix = "/Albums/" + albumCounter++;
                int albumCounter = 0;
                while (true) {
                        String albumPrefix = "/Albums/" + albumCounter++;
@@ -233,7 +233,7 @@ public class ConfigurationSoneParser {
                return unmodifiableMap(albums);
        }
 
                return unmodifiableMap(albums);
        }
 
-       public void parseImages(ImageBuilderFactory imageBuilderFactory) {
+       public void parseImages(ImageBuilderFactory imageBuilderFactory, Sone sone) {
                int imageCounter = 0;
                while (true) {
                        String imagePrefix = "/Images/" + imageCounter++;
                int imageCounter = 0;
                while (true) {
                        String imagePrefix = "/Images/" + imageCounter++;
index 8815abc..561f14f 100644 (file)
@@ -182,45 +182,56 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        private LocalSone loadLocalSone(OwnIdentity ownIdentity) {
        }
 
        private LocalSone loadLocalSone(OwnIdentity ownIdentity) {
-               LocalSone localSone = newSoneBuilder().from(ownIdentity).using(
-                               new Client("Sone", SonePlugin.VERSION.toString())).buildLocal();
+               final SoneBuilder soneBuilder = newSoneBuilder().from(ownIdentity).using(
+                               new Client("Sone", SonePlugin.VERSION.toString()));
+
+               loadElements(soneBuilder, ownIdentity.getId());
+
+               LocalSone localSone = soneBuilder.buildLocal();
+               loadSone(localSone);
+               localSone.setKnown(true);
                localSone.setLatestEdition(
                                Optional.fromNullable(
                                                Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY)))
                                .or(0L));
                localSone.setLatestEdition(
                                Optional.fromNullable(
                                                Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY)))
                                .or(0L));
-               localSone.setKnown(true);
-
-               loadSone(localSone);
                return localSone;
        }
 
                return localSone;
        }
 
-       private void loadSone(LocalSone sone) {
-               long soneTime = configurationLoader.getLocalSoneTime(sone.getId());
-               if (soneTime == -1) {
-                       return;
-               }
+       private void loadElements(SoneBuilder soneBuilder, String soneId) {
+               ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, soneId);
 
 
-               /* load profile. */
-               ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
-               Profile profile = configurationSoneParser.parseProfile();
-
-               /* load posts. */
-               Collection<Post> posts;
                try {
                try {
-                       posts = configurationSoneParser.parsePosts(this);
+                       Set<Post> posts = configurationSoneParser.parsePosts(this);
+                       soneBuilder.withPosts(posts);
+                       for (Post post : posts) {
+                               post.setKnown(true);
+                       }
                } catch (InvalidPostFound ipf) {
                        logger.log(Level.WARNING, "Invalid post found, aborting load!");
                        return;
                }
 
                } catch (InvalidPostFound ipf) {
                        logger.log(Level.WARNING, "Invalid post found, aborting load!");
                        return;
                }
 
-               /* load replies. */
-               Collection<PostReply> postReplies;
                try {
                try {
-                       postReplies = configurationSoneParser.parsePostReplies(this);
+                       Set<PostReply> postReplies = configurationSoneParser.parsePostReplies(this);
+                       soneBuilder.withPostReplies(postReplies);
+                       for (PostReply reply : postReplies) {
+                               reply.setKnown(true);
+                       }
                } catch (InvalidPostReplyFound iprf) {
                        logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                        return;
                }
                } catch (InvalidPostReplyFound iprf) {
                        logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                        return;
                }
+       }
+
+       private void loadSone(LocalSone sone) {
+               long soneTime = configurationLoader.getLocalSoneTime(sone.getId());
+               if (soneTime == -1) {
+                       return;
+               }
+
+               /* load profile. */
+               ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone.getId());
+               Profile profile = configurationSoneParser.parseProfile(sone);
 
                /* load post likes. */
                Set<String> likedPostIds = configurationSoneParser.parseLikedPostIds();
 
                /* load post likes. */
                Set<String> likedPostIds = configurationSoneParser.parseLikedPostIds();
@@ -231,7 +242,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                /* load albums. */
                List<Album> topLevelAlbums;
                try {
                /* load albums. */
                List<Album> topLevelAlbums;
                try {
-                       topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this);
+                       topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this, sone);
                } catch (InvalidAlbumFound iaf) {
                        logger.log(Level.WARNING, "Invalid album found, aborting load!");
                        return;
                } catch (InvalidAlbumFound iaf) {
                        logger.log(Level.WARNING, "Invalid album found, aborting load!");
                        return;
@@ -243,7 +254,7 @@ public class MemoryDatabase extends AbstractService implements Database {
 
                /* load images. */
                try {
 
                /* load images. */
                try {
-                       configurationSoneParser.parseImages(this);
+                       configurationSoneParser.parseImages(this, sone);
                } catch (InvalidImageFound iif) {
                        logger.log(WARNING, "Invalid image found, aborting load!");
                        return;
                } catch (InvalidImageFound iif) {
                        logger.log(WARNING, "Invalid image found, aborting load!");
                        return;
@@ -284,19 +295,13 @@ public class MemoryDatabase extends AbstractService implements Database {
                        lastInsertFingerprints.put(sone.getId(), lastInsertFingerprint);
 
                        allSones.put(sone.getId(), sone);
                        lastInsertFingerprints.put(sone.getId(), lastInsertFingerprint);
 
                        allSones.put(sone.getId(), sone);
-                       storePosts(sone.getId(), posts);
-                       storePostReplies(sone.getId(), postReplies);
+                       storePosts(sone.getId(), sone.getPosts());
+                       storePostReplies(sone.getId(), sone.getReplies());
                        storeAlbums(sone.getId(), topLevelAlbums);
                        storeImages(sone.getId(), from(topLevelAlbums).transformAndConcat(Album.FLATTENER).transformAndConcat(Album.IMAGES).toList());
                } finally {
                        lock.writeLock().unlock();
                }
                        storeAlbums(sone.getId(), topLevelAlbums);
                        storeImages(sone.getId(), from(topLevelAlbums).transformAndConcat(Album.FLATTENER).transformAndConcat(Album.IMAGES).toList());
                } finally {
                        lock.writeLock().unlock();
                }
-               for (Post post : posts) {
-                       post.setKnown(true);
-               }
-               for (PostReply reply : postReplies) {
-                       reply.setKnown(true);
-               }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
        }
 
                logger.info(String.format("Sone loaded successfully: %s", sone));
        }
index 7bbfae8..0df4836 100644 (file)
@@ -69,13 +69,13 @@ public class ConfigurationSoneParserTest {
        public ConfigurationSoneParserTest() {
                when(sone.getId()).thenReturn("1");
                configurationSoneParser =
        public ConfigurationSoneParserTest() {
                when(sone.getId()).thenReturn("1");
                configurationSoneParser =
-                               new ConfigurationSoneParser(configuration, sone);
+                               new ConfigurationSoneParser(configuration, "1");
        }
 
        @Test
        public void emptyProfileIsLoadedCorrectly() {
                setupEmptyProfile();
        }
 
        @Test
        public void emptyProfileIsLoadedCorrectly() {
                setupEmptyProfile();
-               Profile profile = configurationSoneParser.parseProfile();
+               Profile profile = configurationSoneParser.parseProfile(sone);
                assertThat(profile, notNullValue());
                assertThat(profile.getFirstName(), nullValue());
                assertThat(profile.getMiddleName(), nullValue());
                assertThat(profile, notNullValue());
                assertThat(profile.getFirstName(), nullValue());
                assertThat(profile.getMiddleName(), nullValue());
@@ -96,7 +96,7 @@ public class ConfigurationSoneParserTest {
        @Test
        public void filledProfileWithFieldsIsParsedCorrectly() {
                setupFilledProfile();
        @Test
        public void filledProfileWithFieldsIsParsedCorrectly() {
                setupFilledProfile();
-               Profile profile = configurationSoneParser.parseProfile();
+               Profile profile = configurationSoneParser.parseProfile(sone);
                assertThat(profile, notNullValue());
                assertThat(profile.getFirstName(), is("First"));
                assertThat(profile.getMiddleName(), is("M."));
                assertThat(profile, notNullValue());
                assertThat(profile.getFirstName(), is("First"));
                assertThat(profile.getMiddleName(), is("M."));
@@ -330,7 +330,7 @@ public class ConfigurationSoneParserTest {
                AlbumBuilderFactory albumBuilderFactory = createAlbumBuilderFactory();
                List<Album> topLevelAlbums =
                                configurationSoneParser.parseTopLevelAlbums(
                AlbumBuilderFactory albumBuilderFactory = createAlbumBuilderFactory();
                List<Album> topLevelAlbums =
                                configurationSoneParser.parseTopLevelAlbums(
-                                               albumBuilderFactory);
+                                               albumBuilderFactory, sone);
                assertThat(topLevelAlbums, hasSize(2));
                Album firstAlbum = topLevelAlbums.get(0);
                assertThat(firstAlbum, isAlbum("A1", null, "T1", "D1", "I1"));
                assertThat(topLevelAlbums, hasSize(2));
                Album firstAlbum = topLevelAlbums.get(0);
                assertThat(firstAlbum, isAlbum("A1", null, "T1", "D1", "I1"));
@@ -381,30 +381,30 @@ public class ConfigurationSoneParserTest {
        public void albumWithInvalidTitleIsRecognized() {
                setupAlbum(0, "A1", null, null, "D1", "I1");
                configurationSoneParser.parseTopLevelAlbums(
        public void albumWithInvalidTitleIsRecognized() {
                setupAlbum(0, "A1", null, null, "D1", "I1");
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidAlbumFound.class)
        public void albumWithInvalidDescriptionIsRecognized() {
                setupAlbum(0, "A1", null, "T1", null, "I1");
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidAlbumFound.class)
        public void albumWithInvalidDescriptionIsRecognized() {
                setupAlbum(0, "A1", null, "T1", null, "I1");
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidParentAlbumFound.class)
        public void albumWithInvalidParentIsRecognized() {
                setupAlbum(0, "A1", "A0", "T1", "D1", "I1");
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidParentAlbumFound.class)
        public void albumWithInvalidParentIsRecognized() {
                setupAlbum(0, "A1", "A0", "T1", "D1", "I1");
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
        }
 
        @Test
        public void imagesAreParsedCorrectly() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test
        public void imagesAreParsedCorrectly() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImages();
                setupImages();
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
                Map<String, Album> albums = configurationSoneParser.getAlbums();
                assertThat(albums.get("A1").getImages(),
                                contains(isImage("I1", 1000L, "K1", "T1", "D1", 16, 9)));
                Map<String, Album> albums = configurationSoneParser.getAlbums();
                assertThat(albums.get("A1").getImages(),
                                contains(isImage("I1", 1000L, "K1", "T1", "D1", 16, 9)));
@@ -453,72 +453,72 @@ public class ConfigurationSoneParserTest {
        public void missingAlbumIdIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        public void missingAlbumIdIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", null, 1000L, "K1", "T1", "D1", 16, 9);
                setupImage(0, "I1", null, 1000L, "K1", "T1", "D1", 16, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidParentAlbumFound.class)
        public void invalidAlbumIdIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidParentAlbumFound.class)
        public void invalidAlbumIdIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A4", 1000L, "K1", "T1", "D1", 16, 9);
                setupImage(0, "I1", "A4", 1000L, "K1", "T1", "D1", 16, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingCreationTimeIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingCreationTimeIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A1", null, "K1", "T1", "D1", 16, 9);
                setupImage(0, "I1", "A1", null, "K1", "T1", "D1", 16, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingKeyIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingKeyIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A1", 1000L, null, "T1", "D1", 16, 9);
                setupImage(0, "I1", "A1", 1000L, null, "T1", "D1", 16, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingTitleIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingTitleIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A1", 1000L, "K1", null, "D1", 16, 9);
                setupImage(0, "I1", "A1", 1000L, "K1", null, "D1", 16, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingDescriptionIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingDescriptionIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A1", 1000L, "K1", "T1", null, 16, 9);
                setupImage(0, "I1", "A1", 1000L, "K1", "T1", null, 16, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingWidthIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingWidthIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", null, 9);
                setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", null, 9);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingHeightIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
        }
 
        @Test(expected = InvalidImageFound.class)
        public void missingHeightIsRecognized() {
                setupTopLevelAlbums();
                configurationSoneParser.parseTopLevelAlbums(
-                               createAlbumBuilderFactory());
+                               createAlbumBuilderFactory(), sone);
                setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", 16, null);
                setupImage(0, "I1", "A1", 1000L, "K1", "T1", "D1", 16, null);
-               configurationSoneParser.parseImages(createImageBuilderFactory());
+               configurationSoneParser.parseImages(createImageBuilderFactory(), sone);
        }
 
 }
        }
 
 }