From c2f9f5293a81bdf2606f56f3822779a4639a1356 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 10 Oct 2013 08:01:27 +0200 Subject: [PATCH] Use an album modifier for setting title, description, and album image. The rest of modifications will follow. --- .../java/net/pterodactylus/sone/core/Core.java | 2 +- .../pterodactylus/sone/core/SoneDownloader.java | 4 +- .../java/net/pterodactylus/sone/data/Album.java | 45 ++++++++-------- .../net/pterodactylus/sone/data/AlbumImpl.java | 60 ++++++++++++++++------ .../pterodactylus/sone/web/CreateAlbumPage.java | 2 +- .../net/pterodactylus/sone/web/EditAlbumPage.java | 4 +- .../sone/web/ajax/EditAlbumAjaxPage.java | 2 +- 7 files changed, 75 insertions(+), 44 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 0f0b6d3..1ec4d7b 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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) { diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 9af6328..eaf56a9 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -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(); } } diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 1e79f89..09a7da4 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -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 David ‘Bombe’ Roden */ - Album setDescription(String description); + interface Modifier { + + Modifier setTitle(String title); + + Modifier setDescription(String description); + + Modifier setAlbumImage(String imageId); + + Album update() throws IllegalStateException; + + } } diff --git a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java index 56c44aa..93cf2d9 100644 --- a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java @@ -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 title = absent(); + + private Optional description = absent(); + + private Optional 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; + } + }; } // diff --git a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java index 9e9cd9e..a8d024a 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java @@ -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()); } diff --git a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java index 3db14d5..dfd75b3 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java @@ -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()); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java index 5d587db..74a73b8 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java @@ -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()); } -- 2.7.4