2 * Sone - AlbumAccessor.java - Copyright © 2011–2012 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.HashMap;
22 import java.util.List;
25 import net.pterodactylus.sone.data.Album;
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 {
43 public Object get(TemplateContext templateContext, Object object, String member) {
44 Album album = (Album) object;
45 if ("backlinks".equals(member)) {
46 List<Map<String, String>> backlinks = new ArrayList<Map<String, String>>();
47 Album currentAlbum = album;
48 while (currentAlbum != null) {
49 backlinks.add(0, createLink("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
50 currentAlbum = currentAlbum.getParent();
52 backlinks.add(0, createLink("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
55 return super.get(templateContext, object, member);
63 * Creates a map containing mappings for “target” and “link.”
66 * The target to link to
68 * The name of the link
69 * @return The created map containing the mappings
71 private static Map<String, String> createLink(String target, String name) {
72 Map<String, String> link = new HashMap<String, String>();
73 link.put("target", target);
74 link.put("name", name);