Move methods to move an image up and down to Image.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 13 Oct 2013 00:18:29 +0000 (02:18 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:24 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/data/Album.java
src/main/java/net/pterodactylus/sone/data/Image.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java
src/main/java/net/pterodactylus/sone/web/EditImagePage.java
src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java

index df514d4..e8efb59 100644 (file)
@@ -128,28 +128,6 @@ public interface Album extends Identified, Fingerprintable {
        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.
         *
index 3fbcc02..571eb4f 100644 (file)
@@ -106,6 +106,10 @@ public interface Image extends Identified, Fingerprintable {
 
        Modifier modify() throws IllegalStateException;
 
+       void moveUp() throws IllegalStateException;
+
+       void moveDown() throws IllegalStateException;
+
        void remove() throws IllegalStateException;
 
        interface Modifier {
index 161cdd9..32052d5 100644 (file)
@@ -17,8 +17,6 @@
 
 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;
@@ -104,36 +102,6 @@ public class DefaultAlbum extends AbstractAlbum {
        }
 
        @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;
index 156eb81..26f6355 100644 (file)
@@ -50,6 +50,20 @@ public class DefaultImage extends AbstractImage {
        }
 
        @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());
index c778de2..6cb0027 100644 (file)
@@ -581,6 +581,66 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       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
        //
index 338da0a..d2c743e 100644 (file)
@@ -51,6 +51,16 @@ public class MemoryImage extends AbstractImage {
        }
 
        @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);
        }
index cec6990..c9bd705 100644 (file)
@@ -66,9 +66,9 @@ public class EditImagePage extends SoneTemplatePage {
                                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();
index ace775d..18dccf4 100644 (file)
@@ -68,14 +68,14 @@ public class EditImageAjaxPage extends JsonPage {
                        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();