Add album accessor.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Jan 2011 06:05:04 +0000 (07:05 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 11 Jan 2011 06:05:04 +0000 (07:05 +0100)
src/main/java/net/pterodactylus/sone/template/AlbumAccessor.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.java

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 (file)
index 0000000..e2c6f16
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<Map<String, String>> backlinks = new ArrayList<Map<String, String>>();
+                       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<String, String> createLink(String target, String name) {
+               Map<String, String> link = new HashMap<String, String>();
+               link.put("target", target);
+               link.put("name", name);
+               return link;
+       }
+
+}
index 1f601ed..4cace64 100644 (file)
@@ -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());