Expose Link class.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / AlbumAccessor.java
1 /*
2  * Sone - AlbumAccessor.java - Copyright © 2011–2013 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
23 import net.pterodactylus.sone.data.Album;
24 import net.pterodactylus.util.template.Accessor;
25 import net.pterodactylus.util.template.ReflectionAccessor;
26 import net.pterodactylus.util.template.TemplateContext;
27
28 /**
29  * {@link Accessor} implementation for {@link Album}s. A property named
30  * “backlinks” is added, it returns links to all parents and the owner Sone of
31  * an album.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class AlbumAccessor extends ReflectionAccessor {
36
37         @Override
38         public Object get(TemplateContext templateContext, Object object, String member) {
39                 Album album = (Album) object;
40                 if ("backlinks".equals(member)) {
41                         List<Link> backlinks = new ArrayList<Link>();
42                         Album currentAlbum = album;
43                         while (!currentAlbum.isRoot()) {
44                                 backlinks.add(0, new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
45                                 currentAlbum = currentAlbum.getParent();
46                         }
47                         backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
48                         return backlinks;
49                 } else if ("albumImage".equals(member)) {
50                         return album.getAlbumImage().orNull();
51                 }
52                 return super.get(templateContext, object, member);
53         }
54
55         /**
56          * Container for links.
57          *
58          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59          */
60         static class Link {
61
62                 /** The target of the link. */
63                 private final String target;
64
65                 /** The name of the link. */
66                 private final String name;
67
68                 /**
69                  * Creates a new link.
70                  *
71                  * @param target
72                  *              The target of the link
73                  * @param name
74                  *              The name of the link
75                  */
76                 private Link(String target, String name) {
77                         this.target = target;
78                         this.name = name;
79                 }
80
81                 //
82                 // ACCESSORS
83                 //
84
85                 /**
86                  * Returns the target of the link.
87                  *
88                  * @return The target of the link
89                  */
90                 public String getTarget() {
91                         return target;
92                 }
93
94                 /**
95                  * Returns the name of the link.
96                  *
97                  * @return The name of the link
98                  */
99                 public String getName() {
100                         return name;
101                 }
102
103         }
104
105 }