2 * Sone - AlbumAccessor.java - Copyright © 2011–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.template;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Random;
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.util.template.Accessor;
27 import net.pterodactylus.util.template.ReflectionAccessor;
28 import net.pterodactylus.util.template.TemplateContext;
31 * {@link Accessor} implementation for {@link Album}s. A property named
32 * “backlinks” is added, it returns links to all parents and the owner Sone of
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class AlbumAccessor extends ReflectionAccessor {
39 private final Random random = new Random();
45 public Object get(TemplateContext templateContext, Object object, String member) {
46 Album album = (Album) object;
47 if ("backlinks".equals(member)) {
48 List<Link> backlinks = new ArrayList<Link>();
49 Album currentAlbum = album;
50 while (!currentAlbum.isRoot()) {
51 backlinks.add(0, new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
52 currentAlbum = currentAlbum.getParent();
54 backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
56 } else if ("albumImage".equals(member)) {
57 List<Image> images = album.getImages();
58 return images.isEmpty() ? null : images.get(random.nextInt(images.size()));
60 return super.get(templateContext, object, member);
64 * Container for links.
66 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
68 private static class Link {
70 /** The target of the link. */
71 private final String target;
73 /** The name of the link. */
74 private final String name;
80 * The target of the link
82 * The name of the link
84 private Link(String target, String name) {
94 * Returns the target of the link.
96 * @return The target of the link
98 public String getTarget() {
103 * Returns the name of the link.
105 * @return The name of the link
107 public String getName() {