Filter empty albums on gallery pages.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ImageBrowserPage.java
1 /*
2  * Sone - ImageBrowserPage.java - Copyright © 2011–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import static net.pterodactylus.sone.data.Album.FLATTENER;
21 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
22 import java.net.URI;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
27 import com.google.common.base.Optional;
28 import com.google.common.collect.FluentIterable;
29
30 import net.pterodactylus.sone.data.Album;
31 import net.pterodactylus.sone.data.Image;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.sone.web.page.FreenetRequest;
34 import net.pterodactylus.util.collection.Pagination;
35 import net.pterodactylus.util.number.Numbers;
36 import net.pterodactylus.util.template.Template;
37 import net.pterodactylus.util.template.TemplateContext;
38
39 /**
40  * The image browser page is the entry page for the image management.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class ImageBrowserPage extends SoneTemplatePage {
45
46         /**
47          * Creates a new image browser page.
48          *
49          * @param template
50          *            The template to render
51          * @param webInterface
52          *            The Sone web interface
53          */
54         public ImageBrowserPage(Template template, WebInterface webInterface) {
55                 super("imageBrowser.html", template, "Page.ImageBrowser.Title", webInterface, true);
56         }
57
58         //
59         // SONETEMPLATEPAGE METHODS
60         //
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
67                 super.processTemplate(request, templateContext);
68                 String albumId = request.getHttpRequest().getParam("album", null);
69                 if (albumId != null) {
70                         Album album = webInterface.getCore().getAlbum(albumId, false);
71                         templateContext.set("albumRequested", true);
72                         templateContext.set("album", album);
73                         templateContext.set("page", request.getHttpRequest().getParam("page"));
74                         return;
75                 }
76                 String imageId = request.getHttpRequest().getParam("image", null);
77                 if (imageId != null) {
78                         Image image = webInterface.getCore().getImage(imageId, false);
79                         templateContext.set("imageRequested", true);
80                         templateContext.set("image", image);
81                         return;
82                 }
83                 String soneId = request.getHttpRequest().getParam("sone", null);
84                 if (soneId != null) {
85                         Optional<Sone> sone = webInterface.getCore().getSone(soneId);
86                         templateContext.set("soneRequested", true);
87                         templateContext.set("sone", sone.orNull());
88                         return;
89                 }
90                 String mode = request.getHttpRequest().getParam("mode", null);
91                 if ("gallery".equals(mode)) {
92                         templateContext.set("galleryRequested", true);
93                         List<Album> albums = new ArrayList<Album>();
94                         for (Sone sone : webInterface.getCore().getSones()) {
95                                 albums.addAll(FluentIterable.from(sone.getAlbums()).transformAndConcat(FLATTENER).filter(NOT_EMPTY).toList());
96                         }
97                         Collections.sort(albums, Album.TITLE_COMPARATOR);
98                         Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
99                         templateContext.set("albumPagination", albumPagination);
100                         templateContext.set("albums", albumPagination.getItems());
101                         return;
102                 }
103                 Sone sone = getCurrentSone(request.getToadletContext(), false);
104                 templateContext.set("soneRequested", true);
105                 templateContext.set("sone", sone);
106         }
107
108         /**
109          * {@inheritDoc}
110          */
111         @Override
112         public boolean isLinkExcepted(URI link) {
113                 return true;
114         }
115
116 }