2 * Sone - AlbumAccessor.java - Copyright © 2011–2013 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 static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
22 import java.util.ArrayList;
23 import java.util.List;
25 import net.pterodactylus.sone.data.Album;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.util.template.Accessor;
28 import net.pterodactylus.util.template.ReflectionAccessor;
29 import net.pterodactylus.util.template.TemplateContext;
32 * {@link Accessor} implementation for {@link Album}s. A property named
33 * “backlinks” is added, it returns links to all parents and the owner Sone of
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class AlbumAccessor extends ReflectionAccessor {
41 public Object get(TemplateContext templateContext, Object object, String member) {
42 Album album = (Album) object;
43 if ("backlinks".equals(member)) {
44 return generateBacklinks(album);
45 } else if ("albumImage".equals(member)) {
46 return album.getAlbumImage().orNull();
48 return super.get(templateContext, object, member);
51 private Object generateBacklinks(Album album) {
52 List<Link> backlinks = new ArrayList<Link>();
53 Album currentAlbum = album;
54 while (!currentAlbum.isRoot()) {
55 backlinks.add(0, generateLinkToAlbum(currentAlbum));
56 currentAlbum = currentAlbum.getParent();
58 backlinks.add(0, generateLinkToSone(album.getSone()));
62 private Link generateLinkToSone(Sone sone) {
63 return new Link("imageBrowser.html?sone=" + sone.getId(), getNiceName(sone));
66 private Link generateLinkToAlbum(Album currentAlbum) {
67 return new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle());
71 * Container for links.
73 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
77 /** The target of the link. */
78 private final String target;
80 /** The name of the link. */
81 private final String name;
87 * The target of the link
89 * The name of the link
91 private Link(String target, String name) {
101 * Returns the target of the link.
103 * @return The target of the link
105 public String getTarget() {
110 * Returns the name of the link.
112 * @return The name of the link
114 public String getName() {