Remove javadoc comments from overriding methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
1 /*
2  * Sone - SoneAccessor.java - Copyright © 2010–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 com.google.common.collect.FluentIterable.from;
21 import static java.util.Arrays.asList;
22 import static net.pterodactylus.sone.data.Album.FLATTENER;
23 import static net.pterodactylus.sone.data.Album.IMAGES;
24
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.sone.core.Core;
29 import net.pterodactylus.sone.data.Profile;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.data.Sone.SoneStatus;
32 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
33 import net.pterodactylus.sone.freenet.wot.Trust;
34 import net.pterodactylus.sone.web.WebInterface;
35 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
36 import net.pterodactylus.util.logging.Logging;
37 import net.pterodactylus.util.template.Accessor;
38 import net.pterodactylus.util.template.ReflectionAccessor;
39 import net.pterodactylus.util.template.TemplateContext;
40
41 /**
42  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
43  * <dl>
44  * <dt>niceName</dt>
45  * <dd>Will show a combination of first name, middle name, and last name, if
46  * available, otherwise the username of the Sone is returned.</dd>
47  * <dt>friend</dt>
48  * <dd>Will return {@code true} if the sone in question is a friend of the
49  * currently logged in Sone (as determined by accessing the “currentSone”
50  * variable of the given {@link TemplateContext}).</dd>
51  * <dt>current</dt>
52  * <dd>Will return {@code true} if the sone in question is the currently logged
53  * in Sone.</dd>
54  * </dl>
55  *
56  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57  */
58 public class SoneAccessor extends ReflectionAccessor {
59
60         /** The logger. */
61         private static final Logger logger = Logging.getLogger(SoneAccessor.class);
62
63         /** The core. */
64         private final Core core;
65
66         /**
67          * Creates a new Sone accessor.
68          *
69          * @param core
70          *            The Sone core
71          */
72         public SoneAccessor(Core core) {
73                 this.core = core;
74         }
75
76         @Override
77         public Object get(TemplateContext templateContext, Object object, String member) {
78                 Sone sone = (Sone) object;
79                 if (member.equals("niceName")) {
80                         return getNiceName(sone);
81                 } else if (member.equals("friend")) {
82                         Sone currentSone = (Sone) templateContext.get("currentSone");
83                         return (currentSone != null) && currentSone.hasFriend(sone.getId());
84                 } else if (member.equals("current")) {
85                         Sone currentSone = (Sone) templateContext.get("currentSone");
86                         return (currentSone != null) && currentSone.equals(sone);
87                 } else if (member.equals("modified")) {
88                         return core.isModifiedSone(sone);
89                 } else if (member.equals("status")) {
90                         return sone.getStatus();
91                 } else if (member.equals("unknown")) {
92                         return sone.getStatus() == SoneStatus.unknown;
93                 } else if (member.equals("idle")) {
94                         return sone.getStatus() == SoneStatus.idle;
95                 } else if (member.equals("inserting")) {
96                         return sone.getStatus() == SoneStatus.inserting;
97                 } else if (member.equals("downloading")) {
98                         return sone.getStatus() == SoneStatus.downloading;
99                 } else if (member.equals("new")) {
100                         return !sone.isKnown();
101                 } else if (member.equals("locked")) {
102                         return core.isLocked(sone);
103                 } else if (member.equals("lastUpdatedText")) {
104                         return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
105                 } else if (member.equals("trust")) {
106                         Sone currentSone = (Sone) templateContext.get("currentSone");
107                         if (currentSone == null) {
108                                 return null;
109                         }
110                         Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
111                         logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
112                         if (trust == null) {
113                                 return new Trust(null, null, null);
114                         }
115                         return trust;
116                 } else if (member.equals("allImages")) {
117                         return from(asList(sone.getRootAlbum())).transformAndConcat(FLATTENER).transformAndConcat(IMAGES);
118                 } else if (member.equals("albums")) {
119                         return sone.getRootAlbum().getAlbums();
120                 }
121                 return super.get(templateContext, object, member);
122         }
123
124         //
125         // STATIC METHODS
126         //
127
128         /**
129          * Returns the nice name of the given Sone.
130          *
131          * @param sone
132          *            The Sone to get the nice name for
133          * @return The nice name of the Sone
134          */
135         public static String getNiceName(Sone sone) {
136                 Profile profile = sone.getProfile();
137                 String firstName = profile.getFirstName();
138                 String middleName = profile.getMiddleName();
139                 String lastName = profile.getLastName();
140                 if (firstName == null) {
141                         if (lastName == null) {
142                                 return String.valueOf(sone.getName());
143                         }
144                         return lastName;
145                 }
146                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
147         }
148
149 }