From: David ‘Bombe’ Roden Date: Tue, 11 Jan 2011 06:05:04 +0000 (+0100) Subject: Add album accessor. X-Git-Tag: beta-freefall-0.6.2-1~125 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=89afd4d07bb120cbabd91d07493f4e5f2f74d816 Add album accessor. --- diff --git a/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java new file mode 100644 index 0000000..e2c6f16 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java @@ -0,0 +1,78 @@ +/* + * Sone - AlbumAccessor.java - Copyright © 2011 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +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.util.template.Accessor; +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.ReflectionAccessor; + +/** + * {@link Accessor} implementation for {@link Album}s. A property named + * “backlinks” is added, it returns links to all parents and the owner Sone of + * an album. + * + * @author David ‘Bombe’ Roden + */ +public class AlbumAccessor extends ReflectionAccessor { + + /** + * {@inheritDoc} + */ + @Override + public Object get(DataProvider dataProvider, 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 super.get(dataProvider, object, member); + } + + // + // PRIVATE METHODS + // + + /** + * Creates a map containing mappings for “target” and “link.” + * + * @param target + * The target to link to + * @param name + * The name of the link + * @return The created map containing the mappings + */ + private Map createLink(String target, String name) { + Map link = new HashMap(); + link.put("target", target); + link.put("name", name); + return link; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 1f601ed..4cace64 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -36,6 +36,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.CoreListener; +import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; @@ -43,6 +44,7 @@ import net.pterodactylus.sone.freenet.L10nFilter; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.notify.ListNotification; +import net.pterodactylus.sone.template.AlbumAccessor; import net.pterodactylus.sone.template.CollectionAccessor; import net.pterodactylus.sone.template.CssClassNameFilter; import net.pterodactylus.sone.template.GetPagePlugin; @@ -162,6 +164,7 @@ public class WebInterface implements CoreListener { templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore())); templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory)); templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory)); + templateFactory.addAccessor(Album.class, new AlbumAccessor()); templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore())); templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor()); templateFactory.addFilter("date", new DateFilter());