Use an album modifier for setting title, description, and album image.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 10 Oct 2013 06:01:27 +0000 (08:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 10 Oct 2013 06:01:27 +0000 (08:01 +0200)
The rest of modifications will follow.

src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/data/Album.java
src/main/java/net/pterodactylus/sone/data/AlbumImpl.java
src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java
src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java
src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java

index 0f0b6d3..1ec4d7b 100644 (file)
@@ -1247,7 +1247,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid album found, aborting load!");
                                return;
                        }
-                       Album album = getAlbum(albumId).setSone(sone).setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId);
+                       Album album = getAlbum(albumId).setSone(sone).modify().setTitle(albumTitle).setDescription(albumDescription).setAlbumImage(albumImageId).update();
                        if (albumParentId != null) {
                                Album parentAlbum = getAlbum(albumParentId, false);
                                if (parentAlbum == null) {
index 9af6328..eaf56a9 100644 (file)
@@ -468,7 +468,7 @@ public class SoneDownloader extends AbstractService {
                                                return null;
                                        }
                                }
-                               Album album = core.getAlbum(id).setSone(sone).setTitle(title).setDescription(description);
+                               Album album = core.getAlbum(id).setSone(sone).modify().setTitle(title).setDescription(description).update();
                                if (parent != null) {
                                        parent.addAlbum(album);
                                } else {
@@ -501,7 +501,7 @@ public class SoneDownloader extends AbstractService {
                                                album.addImage(image);
                                        }
                                }
-                               album.setAlbumImage(albumImageId);
+                               album.modify().setAlbumImage(albumImageId).update();
                        }
                }
 
index 1e79f89..09a7da4 100644 (file)
@@ -224,15 +224,6 @@ public interface Album extends Identified, Fingerprintable {
        Image getAlbumImage();
 
        /**
-        * Sets the ID of the album image.
-        *
-        * @param id
-        *              The ID of the album image
-        * @return This album
-        */
-       Album setAlbumImage(String id);
-
-       /**
         * Returns whether this album contains any other albums or images.
         *
         * @return {@code true} if this album is empty, {@code false} otherwise
@@ -279,28 +270,38 @@ public interface Album extends Identified, Fingerprintable {
        String getTitle();
 
        /**
-        * Sets the title of this album.
+        * Returns the description of this album.
         *
-        * @param title
-        *              The title of this album
-        * @return This album
+        * @return The description of this album
         */
-       Album setTitle(String title);
+       String getDescription();
 
        /**
-        * Returns the description of this album.
+        * Returns a modifier for this album.
         *
-        * @return The description of this album
+        * @return A modifier for this album
+        * @throws IllegalStateException
+        *              if this album can not be modified
         */
-       String getDescription();
+       Modifier modify() throws IllegalStateException;
 
        /**
-        * Sets the description of this album.
+        * Allows modifying an album. Modifications are only performed once {@link
+        * #update()} has succesfully returned a new album with the modifications
+        * made.
         *
-        * @param description
-        *              The description of this album
-        * @return This album
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       Album setDescription(String description);
+       interface Modifier {
+
+               Modifier setTitle(String title);
+
+               Modifier setDescription(String description);
+
+               Modifier setAlbumImage(String imageId);
+
+               Album update() throws IllegalStateException;
+
+       }
 
 }
index 56c44aa..93cf2d9 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.data;
 
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
@@ -29,6 +31,7 @@ import java.util.UUID;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
 import com.google.common.hash.Hasher;
@@ -242,12 +245,6 @@ public class AlbumImpl implements Album {
        }
 
        @Override
-       public AlbumImpl setAlbumImage(String id) {
-               this.albumImage = id;
-               return this;
-       }
-
-       @Override
        public boolean isEmpty() {
                return albums.isEmpty() && images.isEmpty();
        }
@@ -280,20 +277,53 @@ public class AlbumImpl implements Album {
        }
 
        @Override
-       public Album setTitle(String title) {
-               this.title = checkNotNull(title, "title must not be null");
-               return this;
-       }
-
-       @Override
        public String getDescription() {
                return description;
        }
 
        @Override
-       public AlbumImpl setDescription(String description) {
-               this.description = checkNotNull(description, "description must not be null");
-               return this;
+       public Modifier modify() throws IllegalStateException {
+               Preconditions.checkState(getSone().isLocal(), "album must belong to a local Sone");
+               return new Modifier() {
+                       private Optional<String> title = absent();
+
+                       private Optional<String> description = absent();
+
+                       private Optional<String> albumImage = absent();
+
+                       @Override
+                       public Modifier setTitle(String title) {
+                               this.title = fromNullable(title);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setDescription(String description) {
+                               this.description = fromNullable(description);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setAlbumImage(String imageId) {
+                               this.albumImage = fromNullable(imageId);
+                               return this;
+                       }
+
+                       @Override
+                       public Album update() throws IllegalStateException {
+                               checkState(!albumImage.isPresent() || images.containsKey(albumImage.get()), "album image must belong to this album");
+                               if (title.isPresent()) {
+                                       AlbumImpl.this.title = title.get();
+                               }
+                               if (description.isPresent()) {
+                                       AlbumImpl.this.description = description.get();
+                               }
+                               if (albumImage.isPresent()) {
+                                       AlbumImpl.this.albumImage = albumImage.get();
+                               }
+                               return AlbumImpl.this;
+                       }
+               };
        }
 
        //
index 9e9cd9e..a8d024a 100644 (file)
@@ -68,7 +68,7 @@ public class CreateAlbumPage extends SoneTemplatePage {
                                parent = currentSone.getRootAlbum();
                        }
                        Album album = webInterface.getCore().createAlbum(currentSone, parent);
-                       album.setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description));
+                       album.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
                        webInterface.getCore().touchConfiguration();
                        throw new RedirectException("imageBrowser.html?album=" + album.getId());
                }
index 3db14d5..dfd75b3 100644 (file)
@@ -72,14 +72,14 @@ public class EditAlbumPage extends SoneTemplatePage {
                        if (webInterface.getCore().getImage(albumImageId, false) == null) {
                                albumImageId = null;
                        }
-                       album.setAlbumImage(albumImageId);
+                       album.modify().setAlbumImage(albumImageId).update();
                        String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim();
                        if (title.length() == 0) {
                                templateContext.set("titleMissing", true);
                                return;
                        }
                        String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1000).trim();
-                       album.setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description));
+                       album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
                        webInterface.getCore().touchConfiguration();
                        throw new RedirectException("imageBrowser.html?album=" + album.getId());
                }
index 5d587db..74a73b8 100644 (file)
@@ -68,7 +68,7 @@ public class EditAlbumAjaxPage extends JsonPage {
                }
                String title = request.getHttpRequest().getParam("title").trim();
                String description = request.getHttpRequest().getParam("description").trim();
-               album.setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description));
+               album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
                webInterface.getCore().touchConfiguration();
                return createSuccessJsonObject().put("albumId", album.getId()).put("title", album.getTitle()).put("description", album.getDescription());
        }