📄 Update year in file headers
[Sone.git] / src / main / java / net / pterodactylus / sone / template / AlbumAccessor.java
index 7a5d6d3..87378cc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - AlbumAccessor.java - Copyright © 2011–2012 David Roden
+ * Sone - AlbumAccessor.java - Copyright © 2011–2020 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
 package net.pterodactylus.sone.template;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
+import java.util.Random;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.util.template.Accessor;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.TemplateContext;
@@ -31,11 +31,11 @@ import net.pterodactylus.util.template.TemplateContext;
  * {@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 {
 
+       private final Random random = new Random();
+
        /**
         * {@inheritDoc}
         */
@@ -43,36 +43,67 @@ public class AlbumAccessor extends ReflectionAccessor {
        public Object get(TemplateContext templateContext, Object object, String member) {
                Album album = (Album) object;
                if ("backlinks".equals(member)) {
-                       List<Map<String, String>> backlinks = new ArrayList<Map<String, String>>();
+                       List<Link> backlinks = new ArrayList<>();
                        Album currentAlbum = album;
-                       while (currentAlbum != null) {
-                               backlinks.add(0, createLink("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
+                       while (!currentAlbum.isRoot()) {
+                               backlinks.add(0, new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
                                currentAlbum = currentAlbum.getParent();
                        }
-                       backlinks.add(0, createLink("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
+                       backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
                        return backlinks;
+               } else if ("albumImage".equals(member)) {
+                       List<Image> images = album.getImages();
+                       return images.isEmpty() ? null : images.get(random.nextInt(images.size()));
                }
                return super.get(templateContext, 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
+        * Container for links.
         */
-       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;
+       private 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;
+               }
+
        }
 
 }