Change all copyright headers to include 2012.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / AlbumAccessor.java
1 /*
2  * Sone - AlbumAccessor.java - Copyright © 2011–2012 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.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import net.pterodactylus.sone.data.Album;
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         /**
40          * {@inheritDoc}
41          */
42         @Override
43         public Object get(TemplateContext templateContext, Object object, String member) {
44                 Album album = (Album) object;
45                 if ("backlinks".equals(member)) {
46                         List<Map<String, String>> backlinks = new ArrayList<Map<String, String>>();
47                         Album currentAlbum = album;
48                         while (currentAlbum != null) {
49                                 backlinks.add(0, createLink("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
50                                 currentAlbum = currentAlbum.getParent();
51                         }
52                         backlinks.add(0, createLink("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
53                         return backlinks;
54                 }
55                 return super.get(templateContext, object, member);
56         }
57
58         //
59         // PRIVATE METHODS
60         //
61
62         /**
63          * Creates a map containing mappings for “target” and “link.”
64          *
65          * @param target
66          *            The target to link to
67          * @param name
68          *            The name of the link
69          * @return The created map containing the mappings
70          */
71         private Map<String, String> createLink(String target, String name) {
72                 Map<String, String> link = new HashMap<String, String>();
73                 link.put("target", target);
74                 link.put("name", name);
75                 return link;
76         }
77
78 }