7d0ab905a56de8023d1e96ea3259bd2342a733ac
[Sone.git] / src / main / java / net / pterodactylus / sone / template / AlbumAccessor.java
1 /*
2  * Sone - AlbumAccessor.java - Copyright © 2011–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Random;
23
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.util.template.Accessor;
27 import net.pterodactylus.util.template.ReflectionAccessor;
28 import net.pterodactylus.util.template.TemplateContext;
29
30 /**
31  * {@link Accessor} implementation for {@link Album}s. A property named
32  * “backlinks” is added, it returns links to all parents and the owner Sone of
33  * an album.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class AlbumAccessor extends ReflectionAccessor {
38
39         private final Random random = new Random();
40
41         /**
42          * {@inheritDoc}
43          */
44         @Override
45         public Object get(TemplateContext templateContext, Object object, String member) {
46                 Album album = (Album) object;
47                 if ("backlinks".equals(member)) {
48                         List<Link> backlinks = new ArrayList<Link>();
49                         Album currentAlbum = album;
50                         while (!currentAlbum.isRoot()) {
51                                 backlinks.add(0, new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
52                                 currentAlbum = currentAlbum.getParent();
53                         }
54                         backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
55                         return backlinks;
56                 } else if ("albumImage".equals(member)) {
57                         List<Image> images = album.getImages();
58                         return images.isEmpty() ? null : images.get(random.nextInt(images.size()));
59                 }
60                 return super.get(templateContext, object, member);
61         }
62
63         /**
64          * Container for links.
65          *
66          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
67          */
68         private static class Link {
69
70                 /** The target of the link. */
71                 private final String target;
72
73                 /** The name of the link. */
74                 private final String name;
75
76                 /**
77                  * Creates a new link.
78                  *
79                  * @param target
80                  *              The target of the link
81                  * @param name
82                  *              The name of the link
83                  */
84                 private Link(String target, String name) {
85                         this.target = target;
86                         this.name = name;
87                 }
88
89                 //
90                 // ACCESSORS
91                 //
92
93                 /**
94                  * Returns the target of the link.
95                  *
96                  * @return The target of the link
97                  */
98                 public String getTarget() {
99                         return target;
100                 }
101
102                 /**
103                  * Returns the name of the link.
104                  *
105                  * @return The name of the link
106                  */
107                 public String getName() {
108                         return name;
109                 }
110
111         }
112
113 }