X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FAlbumAccessor.java;h=0ab114b712e5cd1e82ace315d7dc17b23f19e05e;hb=40e4cfe73a346f7dc9d46aedbeccd6a38798f175;hp=0eea2312fce4e57cd7cc83f7d9e69834f9c56dfc;hpb=da49879f2d335afde8f913f13458a62692fa141c;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java index 0eea231..0ab114b 100644 --- a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java @@ -1,5 +1,5 @@ /* - * Sone - AlbumAccessor.java - Copyright © 2011 David Roden + * Sone - AlbumAccessor.java - Copyright © 2011–2013 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 @@ -17,12 +17,13 @@ package net.pterodactylus.sone.template; +import static net.pterodactylus.sone.template.SoneAccessor.getNiceName; + import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.Sone; import net.pterodactylus.util.template.Accessor; import net.pterodactylus.util.template.ReflectionAccessor; import net.pterodactylus.util.template.TemplateContext; @@ -36,43 +37,84 @@ import net.pterodactylus.util.template.TemplateContext; */ public class AlbumAccessor extends ReflectionAccessor { - /** - * {@inheritDoc} - */ @Override public Object get(TemplateContext templateContext, Object object, String member) { Album album = (Album) object; if ("backlinks".equals(member)) { - List> backlinks = new ArrayList>(); - Album currentAlbum = album; - while (currentAlbum != null) { - backlinks.add(0, createLink("imageBrowser.html?album=" + album.getId(), album.getName())); - currentAlbum = currentAlbum.getParent(); - } - backlinks.add(0, createLink("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone()))); - return backlinks; + return generateBacklinks(album); + } else if ("albumImage".equals(member)) { + return album.getAlbumImage().orNull(); } return super.get(templateContext, object, member); } - // - // PRIVATE METHODS - // + private Object generateBacklinks(Album album) { + List backlinks = new ArrayList(); + Album currentAlbum = album; + while (!currentAlbum.isRoot()) { + backlinks.add(0, generateLinkToAlbum(currentAlbum)); + currentAlbum = currentAlbum.getParent(); + } + backlinks.add(0, generateLinkToSone(album.getSone())); + return backlinks; + } + + private Link generateLinkToSone(Sone sone) { + return new Link("imageBrowser.html?sone=" + sone.getId(), getNiceName(sone)); + } + + private Link generateLinkToAlbum(Album currentAlbum) { + return new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()); + } /** - * Creates a map containing mappings for “target” and “link.” + * Container for links. * - * @param target - * The target to link to - * @param name - * The name of the link - * @return The created map containing the mappings + * @author David ‘Bombe’ Roden */ - private Map createLink(String target, String name) { - Map link = new HashMap(); - link.put("target", target); - link.put("name", name); - return link; + static class Link { + + /** The target of the link. */ + private final String target; + + /** The name of the link. */ + private final String name; + + /** + * Creates a new link. + * + * @param target + * The target of the link + * @param name + * The name of the link + */ + private Link(String target, String name) { + this.target = target; + this.name = name; + } + + // + // ACCESSORS + // + + /** + * Returns the target of the link. + * + * @return The target of the link + */ + public String getTarget() { + return target; + } + + /** + * Returns the name of the link. + * + * @return The name of the link + */ + public String getName() { + return name; + } + } }