List<Image> getImages();
/**
- * Moves the given image up in this album’s images. If the image is already the
- * first image, nothing happens.
- *
- * @param image
- * The image to move up
- * @return The image that the given image swapped the place with, or
- * <code>null</code> if the image did not change its place
- */
- Image moveImageUp(Image image);
-
- /**
- * Moves the given image down in this album’s images. If the image is already
- * the last image, nothing happens.
- *
- * @param image
- * The image to move down
- * @return The image that the given image swapped the place with, or
- * <code>null</code> if the image did not change its place
- */
- Image moveImageDown(Image image);
-
- /**
* Returns the album image of this album, or {@code null} if no album image has
* been set.
*
Modifier modify() throws IllegalStateException;
+ void moveUp() throws IllegalStateException;
+
+ void moveDown() throws IllegalStateException;
+
void remove() throws IllegalStateException;
interface Modifier {
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.checkState;
import java.util.ArrayList;
}
@Override
- public Image moveImageUp(Image image) {
- checkNotNull(image, "image must not be null");
- checkNotNull(image.getSone(), "image must have an owner");
- checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
- checkArgument(image.getAlbum().equals(this), "image must belong to this album");
- int oldIndex = imageIds.indexOf(image.getId());
- if (oldIndex <= 0) {
- return null;
- }
- imageIds.remove(image.getId());
- imageIds.add(oldIndex - 1, image.getId());
- return images.get(imageIds.get(oldIndex));
- }
-
- @Override
- public Image moveImageDown(Image image) {
- checkNotNull(image, "image must not be null");
- checkNotNull(image.getSone(), "image must have an owner");
- checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
- checkArgument(image.getAlbum().equals(this), "image must belong to this album");
- int oldIndex = imageIds.indexOf(image.getId());
- if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
- return null;
- }
- imageIds.remove(image.getId());
- imageIds.add(oldIndex + 1, image.getId());
- return images.get(imageIds.get(oldIndex));
- }
-
- @Override
public Image getAlbumImage() {
if (albumImage == null) {
return null;
}
@Override
+ public void moveUp() throws IllegalStateException {
+ int oldIndex = album.imageIds.indexOf(getId());
+ album.imageIds.remove(getId());
+ album.imageIds.add(Math.max(0, oldIndex - 1), getId());
+ }
+
+ @Override
+ public void moveDown() throws IllegalStateException {
+ int oldIndex = album.imageIds.indexOf(getId());
+ album.imageIds.remove(getId());
+ album.imageIds.add(Math.min(album.imageIds.size(), oldIndex + 1), getId());
+ }
+
+ @Override
public void remove() throws IllegalStateException {
synchronized (album) {
album.images.remove(getId());
}
}
+ void moveUp(Album album) {
+ lock.writeLock().lock();
+ try {
+ List<String> albums = albumChildren.get(album.getParent().getId());
+ int currentIndex = albums.indexOf(album.getId());
+ if (currentIndex == 0) {
+ return;
+ }
+ albums.remove(album.getId());
+ albums.add(currentIndex - 1, album.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ void moveDown(Album album) {
+ lock.writeLock().lock();
+ try {
+ List<String> albums = albumChildren.get(album.getParent().getId());
+ int currentIndex = albums.indexOf(album.getId());
+ if (currentIndex == (albums.size() - 1)) {
+ return;
+ }
+ albums.remove(album.getId());
+ albums.add(currentIndex + 1, album.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ void moveUp(Image image) {
+ lock.writeLock().lock();
+ try {
+ List<String> images = albumImages.get(image.getAlbum().getId());
+ int currentIndex = images.indexOf(image.getId());
+ if (currentIndex == 0) {
+ return;
+ }
+ images.remove(image.getId());
+ images.add(currentIndex - 1, image.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ void moveDown(Image image) {
+ lock.writeLock().lock();
+ try {
+ List<String> images = albumChildren.get(image.getAlbum().getId());
+ int currentIndex = images.indexOf(image.getId());
+ if (currentIndex == (images.size() - 1)) {
+ return;
+ }
+ images.remove(image.getId());
+ images.add(currentIndex + 1, image.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
//
// PRIVATE METHODS
//
}
@Override
+ public void moveUp() throws IllegalStateException {
+ memoryDatabase.moveUp(this);
+ }
+
+ @Override
+ public void moveDown() throws IllegalStateException {
+ memoryDatabase.moveDown(this);
+ }
+
+ @Override
public void remove() throws IllegalStateException {
memoryDatabase.removeImage(this);
}
throw new RedirectException("noPermission.html");
}
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) {
- image.get().getAlbum().moveImageUp(image.get());
+ image.get().moveUp();
} else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) {
- image.get().getAlbum().moveImageDown(image.get());
+ image.get().moveDown();
} else {
String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim();
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1024).trim();
return createErrorJsonObject("not-authorized");
}
if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
- Image swappedImage = image.get().getAlbum().moveImageUp(image.get());
+ image.get().moveUp();
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceImageId", image.get().getId()).put("destinationImageId", swappedImage.getId());
+ return createSuccessJsonObject(); // TODO - fix javascript
}
if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
- Image swappedImage = image.get().getAlbum().moveImageDown(image.get());
+ image.get().moveDown();
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceImageId", image.get().getId()).put("destinationImageId", swappedImage.getId());
+ return createSuccessJsonObject(); // TODO - fix javascript
}
String title = request.getHttpRequest().getParam("title").trim();
String description = request.getHttpRequest().getParam("description").trim();