The rest of modifications will follow.
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) {
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 {
album.addImage(image);
}
}
- album.setAlbumImage(albumImageId);
+ album.modify().setAlbumImage(albumImageId).update();
}
}
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
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;
+
+ }
}
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;
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;
}
@Override
- public AlbumImpl setAlbumImage(String id) {
- this.albumImage = id;
- return this;
- }
-
- @Override
public boolean isEmpty() {
return albums.isEmpty() && images.isEmpty();
}
}
@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;
+ }
+ };
}
//
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());
}
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());
}
}
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());
}