Write end marker after albums.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 7f90184..4e3df64 100644 (file)
@@ -731,7 +731,6 @@ public class Core implements IdentityListener, UpdateListener {
        }
 
        /**
-        *
         * Returns the album with the given ID, creating a new album if no album
         * with the given ID can be found.
         *
@@ -1435,6 +1434,31 @@ public class Core implements IdentityListener, UpdateListener {
                        }
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
+                       /* save albums. first, collect in a flat structure, top-level first. */
+                       List<Album> albums = new ArrayList<Album>();
+                       albums.addAll(sone.getAlbums());
+                       int lastAlbumIndex = 0;
+                       while (lastAlbumIndex < albums.size()) {
+                               int previousAlbumCount = albums.size();
+                               for (Album album : new ArrayList<Album>(albums.subList(lastAlbumIndex, albums.size()))) {
+                                       albums.addAll(album.getAlbums());
+                               }
+                               lastAlbumIndex = previousAlbumCount;
+                       }
+
+                       int albumCounter = 0;
+                       for (Album album : albums) {
+                               String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
+                               configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
+                               configuration.getStringValue(albumPrefix + "/Name").setValue(album.getName());
+                               configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
+                               configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent() == null ? null : album.getParent().getId());
+                       }
+                       configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
+
+                       /* save options. */
+                       configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
+
                        configuration.save();
                        logger.log(Level.INFO, "Sone %s saved.", sone);
                } catch (ConfigurationException ce1) {
@@ -1711,8 +1735,9 @@ public class Core implements IdentityListener, UpdateListener {
                album.setSone(sone);
                if (parent != null) {
                        parent.addAlbum(album);
+               } else {
+                       sone.addAlbum(album);
                }
-               sone.addAlbum(album);
                return album;
        }