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