Simplify album removal.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 12 Oct 2013 19:57:30 +0000 (21:57 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:24 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Album.java
src/main/java/net/pterodactylus/sone/data/SoneImpl.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java
src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java

index 4e7f18d..e61a58f 100644 (file)
@@ -1224,7 +1224,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                followSone(sone, friendId);
                        }
                        for (Album album : sone.getRootAlbum().getAlbums()) {
                                followSone(sone, friendId);
                        }
                        for (Album album : sone.getRootAlbum().getAlbums()) {
-                               sone.getRootAlbum().removeAlbum(album);
+                               album.remove();
                        }
                        soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
                        }
                        soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
@@ -1483,24 +1483,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
        }
 
        /**
-        * Deletes the given album. The owner of the album has to be a local Sone, and
-        * the album has to be {@link Album#isEmpty() empty} to be deleted.
-        *
-        * @param album
-        *              The album to remove
-        */
-       public void deleteAlbum(Album album) {
-               checkNotNull(album, "album must not be null");
-               checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone");
-               if (!album.isEmpty()) {
-                       return;
-               }
-               album.getParent().removeAlbum(album);
-               database.removeAlbum(album);
-               touchConfiguration();
-       }
-
-       /**
         * Creates a new image.
         *
         * @param sone
         * Creates a new image.
         *
         * @param sone
index 5b9d368..26da9b7 100644 (file)
@@ -121,14 +121,6 @@ public interface Album extends Identified, Fingerprintable {
        List<Album> getAlbums();
 
        /**
        List<Album> getAlbums();
 
        /**
-        * Removes an album from this album.
-        *
-        * @param album
-        *              The album to remove
-        */
-       void removeAlbum(Album album);
-
-       /**
         * Moves the given album up in this album’s albums. If the album is already the
         * first album, nothing happens.
         *
         * Moves the given album up in this album’s albums. If the album is already the
         * first album, nothing happens.
         *
@@ -211,22 +203,6 @@ public interface Album extends Identified, Fingerprintable {
        Album getParent();
 
        /**
        Album getParent();
 
        /**
-        * Sets the parent album of this album.
-        *
-        * @param parent
-        *              The new parent album of this album
-        * @return This album
-        */
-       Album setParent(Album parent);
-
-       /**
-        * Removes the parent album of this album.
-        *
-        * @return This album
-        */
-       Album removeParent();
-
-       /**
         * Returns the title of this album.
         *
         * @return The title of this album
         * Returns the title of this album.
         *
         * @return The title of this album
@@ -253,6 +229,8 @@ public interface Album extends Identified, Fingerprintable {
         */
        Modifier modify() throws IllegalStateException;
 
         */
        Modifier modify() throws IllegalStateException;
 
+       void remove() throws IllegalStateException;
+
        /**
         * Allows modifying an album. Modifications are only performed once {@link
         * #update()} has succesfully returned a new album with the modifications
        /**
         * Allows modifying an album. Modifications are only performed once {@link
         * #update()} has succesfully returned a new album with the modifications
index e929025..1f7ee1a 100644 (file)
@@ -100,7 +100,7 @@ public class SoneImpl implements Sone {
        private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
 
        /** The root album containing all albums. */
        private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
 
        /** The root album containing all albums. */
-       private final Album rootAlbum = new DefaultAlbum(this);
+       private final Album rootAlbum = new DefaultAlbum(this, null);
 
        /** Sone-specific options. */
        private Options options = new Options();
 
        /** Sone-specific options. */
        private Options options = new Options();
index 74b66e7..cbef5da 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.data.impl;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
 
 import java.util.ArrayList;
 import java.util.HashMap;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -47,6 +48,9 @@ public class DefaultAlbum extends AbstractAlbum {
        /** The Sone this album belongs to. */
        private Sone sone;
 
        /** The Sone this album belongs to. */
        private Sone sone;
 
+       /** The parent album. */
+       private final DefaultAlbum parent;
+
        /** Nested albums. */
        private final List<Album> albums = new ArrayList<Album>();
 
        /** Nested albums. */
        private final List<Album> albums = new ArrayList<Album>();
 
@@ -56,12 +60,9 @@ public class DefaultAlbum extends AbstractAlbum {
        /** The images in this album. */
        final Map<String, Image> images = new HashMap<String, Image>();
 
        /** The images in this album. */
        final Map<String, Image> images = new HashMap<String, Image>();
 
-       /** The parent album. */
-       private Album parent;
-
        /** Creates a new album with a random ID. */
        /** Creates a new album with a random ID. */
-       public DefaultAlbum(Sone sone) {
-               this(UUID.randomUUID().toString(), sone);
+       public DefaultAlbum(Sone sone, DefaultAlbum parent) {
+               this(UUID.randomUUID().toString(), sone, parent);
        }
 
        /**
        }
 
        /**
@@ -70,9 +71,10 @@ public class DefaultAlbum extends AbstractAlbum {
         * @param id
         *              The ID of the album
         */
         * @param id
         *              The ID of the album
         */
-       public DefaultAlbum(String id, Sone sone) {
+       public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) {
                super(id);
                this.sone = sone;
                super(id);
                this.sone = sone;
+               this.parent = parent;
        }
 
        //
        }
 
        //
@@ -90,15 +92,6 @@ public class DefaultAlbum extends AbstractAlbum {
        }
 
        @Override
        }
 
        @Override
-       public void removeAlbum(Album album) {
-               checkNotNull(album, "album must not be null");
-               checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
-               checkArgument(equals(album.getParent()), "album must belong to this album");
-               albums.remove(album);
-               album.removeParent();
-       }
-
-       @Override
        public Album moveAlbumUp(Album album) {
                checkNotNull(album, "album must not be null");
                checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
        public Album moveAlbumUp(Album album) {
                checkNotNull(album, "album must not be null");
                checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
@@ -182,20 +175,8 @@ public class DefaultAlbum extends AbstractAlbum {
        }
 
        @Override
        }
 
        @Override
-       public Album setParent(Album parent) {
-               this.parent = checkNotNull(parent, "parent must not be null");
-               return this;
-       }
-
-       @Override
-       public Album removeParent() {
-               this.parent = null;
-               return this;
-       }
-
-       @Override
        public AlbumBuilder newAlbumBuilder() {
        public AlbumBuilder newAlbumBuilder() {
-               return new DefaultAlbumBuilder(sone) {
+               return new DefaultAlbumBuilder(sone, this) {
                        @Override
                        public Album build() throws IllegalStateException {
                                Album album = super.build();
                        @Override
                        public Album build() throws IllegalStateException {
                                Album album = super.build();
@@ -221,4 +202,24 @@ public class DefaultAlbum extends AbstractAlbum {
                };
        }
 
                };
        }
 
+       @Override
+       public void remove() throws IllegalStateException {
+               checkState(!isRoot(), "can not remove root album");
+               removeAllAlbums();
+               removeAllImages();
+               parent.albums.remove(this);
+       }
+
+       private void removeAllImages() {
+               for (Image image : images.values()) {
+                       image.remove();
+               }
+       }
+
+       private void removeAllAlbums() {
+               for (Album album: albums) {
+                       album.remove();
+               }
+       }
+
 }
 }
index cd6475a..5148471 100644 (file)
@@ -29,15 +29,17 @@ import net.pterodactylus.sone.database.AlbumBuilder;
 public class DefaultAlbumBuilder extends AbstractAlbumBuilder {
 
        private final Sone sone;
 public class DefaultAlbumBuilder extends AbstractAlbumBuilder {
 
        private final Sone sone;
+       private final DefaultAlbum parent;
 
 
-       public DefaultAlbumBuilder(Sone sone) {
+       public DefaultAlbumBuilder(Sone sone, DefaultAlbum parent) {
                this.sone = sone;
                this.sone = sone;
+               this.parent = parent;
        }
 
        @Override
        public Album build() throws IllegalStateException {
                validate();
        }
 
        @Override
        public Album build() throws IllegalStateException {
                validate();
-               return new DefaultAlbum(getId(), sone);
+               return new DefaultAlbum(getId(), sone, parent);
        }
 
 }
        }
 
 }
index dfb384b..0250971 100644 (file)
@@ -63,7 +63,7 @@ public class DeleteAlbumPage extends SoneTemplatePage {
                                throw new RedirectException("imageBrowser.html?album=" + album.get().getId());
                        }
                        Album parentAlbum = album.get().getParent();
                                throw new RedirectException("imageBrowser.html?album=" + album.get().getId());
                        }
                        Album parentAlbum = album.get().getParent();
-                       webInterface.getCore().deleteAlbum(album.get());
+                       album.get().remove();
                        if (parentAlbum.equals(album.get().getSone().getRootAlbum())) {
                                throw new RedirectException("imageBrowser.html?sone=" + album.get().getSone().getId());
                        }
                        if (parentAlbum.equals(album.get().getSone().getRootAlbum())) {
                                throw new RedirectException("imageBrowser.html?sone=" + album.get().getSone().getId());
                        }