Replace image browser page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 2 May 2017 20:14:49 +0000 (22:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 5 May 2017 20:51:31 +0000 (22:51 +0200)
src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/web/pages/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPageTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/pages/ImageBrowserPage.java
deleted file mode 100644 (file)
index c8ee0d0..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Sone - ImageBrowserPage.java - Copyright © 2011–2016 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.web.pages;
-
-import static com.google.common.collect.FluentIterable.from;
-import static net.pterodactylus.sone.data.Album.FLATTENER;
-import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
-import static net.pterodactylus.sone.data.Album.TITLE_COMPARATOR;
-import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
-
-import java.net.URI;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import com.google.common.base.Optional;
-
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.collection.Pagination;
-import net.pterodactylus.util.template.Template;
-import net.pterodactylus.util.template.TemplateContext;
-
-/**
- * The image browser page is the entry page for the image management.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class ImageBrowserPage extends SoneTemplatePage {
-
-       /**
-        * Creates a new image browser page.
-        *
-        * @param template
-        *            The template to render
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public ImageBrowserPage(Template template, WebInterface webInterface) {
-               super("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface, true);
-       }
-
-       //
-       // SONETEMPLATEPAGE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
-               String albumId = request.getHttpRequest().getParam("album", null);
-               if (albumId != null) {
-                       Album album = webInterface.getCore().getAlbum(albumId);
-                       templateContext.set("albumRequested", true);
-                       templateContext.set("album", album);
-                       templateContext.set("page", request.getHttpRequest().getParam("page"));
-                       return;
-               }
-               String imageId = request.getHttpRequest().getParam("image", null);
-               if (imageId != null) {
-                       Image image = webInterface.getCore().getImage(imageId, false);
-                       templateContext.set("imageRequested", true);
-                       templateContext.set("image", image);
-                       return;
-               }
-               String soneId = request.getHttpRequest().getParam("sone", null);
-               if (soneId != null) {
-                       Optional<Sone> sone = webInterface.getCore().getSone(soneId);
-                       templateContext.set("soneRequested", true);
-                       templateContext.set("sone", sone.orNull());
-                       return;
-               }
-               String mode = request.getHttpRequest().getParam("mode", null);
-               if ("gallery".equals(mode)) {
-                       templateContext.set("galleryRequested", true);
-                       List<Album> albums = new ArrayList<Album>();
-                       for (Sone sone : webInterface.getCore().getSones()) {
-                               albums.addAll(from(sone.getRootAlbum().getAlbums()).transformAndConcat(FLATTENER).filter(NOT_EMPTY).toList());
-                       }
-                       Collections.sort(albums, TITLE_COMPARATOR);
-                       Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
-                       templateContext.set("albumPagination", albumPagination);
-                       templateContext.set("albums", albumPagination.getItems());
-                       return;
-               }
-               Sone sone = getCurrentSone(request.getToadletContext(), false);
-               templateContext.set("soneRequested", true);
-               templateContext.set("sone", sone);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isLinkExcepted(URI link) {
-               return true;
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt
new file mode 100644 (file)
index 0000000..6189429
--- /dev/null
@@ -0,0 +1,47 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.data.Album
+import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+import net.pterodactylus.util.collection.Pagination
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+import java.net.URI
+
+/**
+ * The image browser page is the entry page for the image management.
+ */
+class ImageBrowserPage(template: Template, webInterface: WebInterface):
+               SoneTemplatePage("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface, true) {
+
+       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
+               if ("album" in request.parameters) {
+                       templateContext["albumRequested"] = true
+                       templateContext["album"] = webInterface.core.getAlbum(request.parameters["album"]!!)
+                       templateContext["page"] = request.parameters["page"]
+               } else if ("image" in request.parameters) {
+                       templateContext["imageRequested"] = true
+                       templateContext["image"] = webInterface.core.getImage(request.parameters["image"])
+               } else if (request.parameters["mode"] == "gallery") {
+                       templateContext["galleryRequested"] = true
+                       webInterface.core.sones
+                                       .map(Sone::getRootAlbum)
+                                       .flatMap(Album::getAlbums)
+                                       .flatMap { Album.FLATTENER.apply(it)!! }
+                                       .filterNot(Album::isEmpty)
+                                       .sortedBy(Album::getTitle)
+                                       .also { albums ->
+                                               templateContext["albums"] = albums
+                                               templateContext["albumPagination"] = Pagination(albums, 12).setPage(request.parameters["page"]?.toIntOrNull() ?: 0)
+                                       }
+               } else {
+                       templateContext["soneRequested"] = true
+                       templateContext["sone"] = webInterface.core.getSone(request.httpRequest.getParam("sone")).orNull() ?: getCurrentSone(request.toadletContext)
+               }
+       }
+
+       override fun isLinkExcepted(link: URI?) = true
+
+}
index f4936b7..e04e780 100644 (file)
@@ -316,6 +316,7 @@ public abstract class WebPageTest {
                        throw new RuntimeException(e);
                }
                when(freenetRequest.getMethod()).thenReturn(method);
+               when(httpRequest.getMethod()).thenReturn(method.name());
        }
 
        protected void addHttpRequestHeader(@Nonnull String name, String value) {
index 19d24ed..063a588 100644 (file)
@@ -5,8 +5,6 @@ import net.pterodactylus.sone.data.Image
 import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.sone.web.pages.ImageBrowserPage
-import net.pterodactylus.sone.web.pages.WebPageTest
 import net.pterodactylus.util.web.Method.GET
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.contains
@@ -43,7 +41,7 @@ class ImageBrowserPageTest : WebPageTest() {
                addAlbum("album-id", album)
                addHttpRequestParameter("album", "album-id")
                addHttpRequestParameter("page", "5")
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["albumRequested"], equalTo<Any>(true))
                assertThat(templateContext["album"], equalTo<Any>(album))
                assertThat(templateContext["page"], equalTo<Any>("5"))
@@ -55,7 +53,7 @@ class ImageBrowserPageTest : WebPageTest() {
                val image = mock<Image>()
                addImage("image-id", image)
                addHttpRequestParameter("image", "image-id")
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["imageRequested"], equalTo<Any>(true))
                assertThat(templateContext["image"], equalTo<Any>(image))
        }
@@ -66,7 +64,7 @@ class ImageBrowserPageTest : WebPageTest() {
                val sone = mock<Sone>()
                addSone("sone-id", sone)
                addHttpRequestParameter("sone", "sone-id")
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["soneRequested"], equalTo<Any>(true))
                assertThat(templateContext["sone"], equalTo<Any>(sone))
        }
@@ -79,7 +77,7 @@ class ImageBrowserPageTest : WebPageTest() {
                val secondSone = createSone("third album", "fourth album")
                addSone("sone2", secondSone)
                addHttpRequestParameter("mode", "gallery")
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
                @Suppress("UNCHECKED_CAST")
                assertThat(templateContext["albums"] as Iterable<Album>, contains(
@@ -109,7 +107,7 @@ class ImageBrowserPageTest : WebPageTest() {
        @Test
        fun `requesting nothing will show the albums of the current sone`() {
                request("", GET)
-               page.handleRequest(freenetRequest, templateContext)
+               page.processTemplate(freenetRequest, templateContext)
                assertThat(templateContext["soneRequested"], equalTo<Any>(true))
                assertThat(templateContext["sone"], equalTo<Any>(currentSone))
        }