- * Loads and updates the given Sone from the configuration. If any error is
- * encountered, loading is aborted and the given Sone is not changed.
- *
- * @param sone
- * The Sone to load and update
- */
- public void loadSone(Sone sone) {
- if (!sone.isLocal()) {
- logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
- return;
- }
- logger.info(String.format("Loading local Sone: %s", sone));
-
- /* load Sone. */
- String sonePrefix = "Sone/" + sone.getId();
- Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
- if (soneTime == null) {
- logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
- return;
- }
- String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
-
- /* load profile. */
- ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
- Profile profile = configurationSoneParser.parseProfile();
-
- /* load posts. */
- Collection<Post> posts;
- try {
- posts = configurationSoneParser.parsePosts(database);
- } catch (InvalidPostFound ipf) {
- logger.log(Level.WARNING, "Invalid post found, aborting load!");
- return;
- }
-
- /* load replies. */
- Collection<PostReply> replies;
- try {
- replies = configurationSoneParser.parsePostReplies(database);
- } catch (InvalidPostReplyFound iprf) {
- logger.log(Level.WARNING, "Invalid reply found, aborting load!");
- return;
- }
-
- /* load post likes. */
- Set<String> likedPostIds =
- configurationSoneParser.parseLikedPostIds();
-
- /* load reply likes. */
- Set<String> likedReplyIds =
- configurationSoneParser.parseLikedPostReplyIds();
-
- /* load friends. */
- Set<String> friends = configurationSoneParser.parseFriends();
-
- /* load albums. */
- List<Album> topLevelAlbums = new ArrayList<Album>();
- int albumCounter = 0;
- while (true) {
- String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
- String albumId = configuration.getStringValue(albumPrefix + "/ID").getValue(null);
- if (albumId == null) {
- break;
- }
- String albumTitle = configuration.getStringValue(albumPrefix + "/Title").getValue(null);
- String albumDescription = configuration.getStringValue(albumPrefix + "/Description").getValue(null);
- String albumParentId = configuration.getStringValue(albumPrefix + "/Parent").getValue(null);
- String albumImageId = configuration.getStringValue(albumPrefix + "/AlbumImage").getValue(null);
- if ((albumTitle == null) || (albumDescription == null)) {
- logger.log(Level.WARNING, "Invalid album found, aborting load!");
- return;
- }
- Album album = database.newAlbumBuilder()
- .withId(albumId)
- .by(sone)
- .build()
- .modify()
- .setTitle(albumTitle)
- .setDescription(albumDescription)
- .setAlbumImage(albumImageId)
- .update();
- if (albumParentId != null) {
- Album parentAlbum = getAlbum(albumParentId);
- if (parentAlbum == null) {
- logger.log(Level.WARNING, String.format("Invalid parent album ID: %s", albumParentId));
- return;
- }
- parentAlbum.addAlbum(album);
- } else {
- if (!topLevelAlbums.contains(album)) {
- topLevelAlbums.add(album);
- }
- }
- }
-
- /* load images. */
- int imageCounter = 0;
- while (true) {
- String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
- String imageId = configuration.getStringValue(imagePrefix + "/ID").getValue(null);
- if (imageId == null) {
- break;
- }
- String albumId = configuration.getStringValue(imagePrefix + "/Album").getValue(null);
- String key = configuration.getStringValue(imagePrefix + "/Key").getValue(null);
- String title = configuration.getStringValue(imagePrefix + "/Title").getValue(null);
- String description = configuration.getStringValue(imagePrefix + "/Description").getValue(null);
- Long creationTime = configuration.getLongValue(imagePrefix + "/CreationTime").getValue(null);
- Integer width = configuration.getIntValue(imagePrefix + "/Width").getValue(null);
- Integer height = configuration.getIntValue(imagePrefix + "/Height").getValue(null);
- if ((albumId == null) || (key == null) || (title == null) || (description == null) || (creationTime == null) || (width == null) || (height == null)) {
- logger.log(Level.WARNING, "Invalid image found, aborting load!");
- return;
- }
- Album album = getAlbum(albumId);
- if (album == null) {
- logger.log(Level.WARNING, "Invalid album image encountered, aborting load!");
- return;
- }
- Image image = getImage(imageId).modify().setSone(sone).setCreationTime(creationTime).setKey(key).setTitle(title).setDescription(description).setWidth(width).setHeight(height).update();
- album.addImage(image);
- }
-
- /* load avatar. */
- String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
- if (avatarId != null) {
- profile.setAvatar(getImage(avatarId, false));
- }
-
- /* load options. */
- sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
- sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
- sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
- sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
- sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
- sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
-
- /* if we’re still here, Sone was loaded successfully. */
- synchronized (sone) {
- sone.setTime(soneTime);
- sone.setProfile(profile);
- sone.setPosts(posts);
- sone.setReplies(replies);
- sone.setLikePostIds(likedPostIds);
- sone.setLikeReplyIds(likedReplyIds);
- for (String friendId : friends) {
- followSone(sone, friendId);
- }
- for (Album album : sone.getRootAlbum().getAlbums()) {
- sone.getRootAlbum().removeAlbum(album);
- }
- for (Album album : topLevelAlbums) {
- sone.getRootAlbum().addAlbum(album);
- }
- soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
- }
- synchronized (knownSones) {
- for (String friend : friends) {
- knownSones.add(friend);
- }
- }
- database.storePosts(sone, posts);
- for (Post post : posts) {
- post.setKnown(true);
- }
- database.storePostReplies(sone, replies);
- for (PostReply reply : replies) {
- reply.setKnown(true);
- }
-
- logger.info(String.format("Sone loaded successfully: %s", sone));
- }
-
- /**