X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FAlbumAccessor.java;h=0ab114b712e5cd1e82ace315d7dc17b23f19e05e;hb=563b924d21a02faa60409d21cc6ec1fd2740347c;hp=e2c6f16d78bd7c7e060f2a3b88a77a4173ecb121;hpb=89afd4d07bb120cbabd91d07493f4e5f2f74d816;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 e2c6f16..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,15 +17,16 @@ 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.DataProvider; import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateContext; /** * {@link Accessor} implementation for {@link Album}s. A property named @@ -36,43 +37,84 @@ import net.pterodactylus.util.template.ReflectionAccessor; */ public class AlbumAccessor extends ReflectionAccessor { - /** - * {@inheritDoc} - */ @Override - public Object get(DataProvider dataProvider, Object object, String member) { + 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("viewSone.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 Object generateBacklinks(Album album) { + List backlinks = new ArrayList(); + Album currentAlbum = album; + while (!currentAlbum.isRoot()) { + backlinks.add(0, generateLinkToAlbum(currentAlbum)); + currentAlbum = currentAlbum.getParent(); } - return super.get(dataProvider, object, member); + backlinks.add(0, generateLinkToSone(album.getSone())); + return backlinks; } - // - // PRIVATE METHODS - // + 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; + } + } }