2 * Sone - SoneAccessor.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.template;
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.core.Core.SoneStatus;
25 import net.pterodactylus.sone.data.Profile;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.freenet.wot.Trust;
28 import net.pterodactylus.sone.web.WebInterface;
29 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
30 import net.pterodactylus.util.logging.Logging;
31 import net.pterodactylus.util.template.Accessor;
32 import net.pterodactylus.util.template.ReflectionAccessor;
33 import net.pterodactylus.util.template.TemplateContext;
36 * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
39 * <dd>Will show a combination of first name, middle name, and last name, if
40 * available, otherwise the username of the Sone is returned.</dd>
42 * <dd>Will return {@code true} if the sone in question is a friend of the
43 * currently logged in Sone (as determined by accessing the “currentSone”
44 * variable of the given {@link TemplateContext}).</dd>
46 * <dd>Will return {@code true} if the sone in question is the currently logged
50 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52 public class SoneAccessor extends ReflectionAccessor {
55 private static final Logger logger = Logging.getLogger(SoneAccessor.class);
58 private final Core core;
61 * Creates a new Sone accessor.
66 public SoneAccessor(Core core) {
74 public Object get(TemplateContext templateContext, Object object, String member) {
75 Sone sone = (Sone) object;
76 if (member.equals("niceName")) {
77 return getNiceName(sone);
78 } else if (member.equals("local")) {
79 return core.isLocalSone(sone);
80 } else if (member.equals("friend")) {
81 Sone currentSone = (Sone) templateContext.get("currentSone");
82 return (currentSone != null) && currentSone.hasFriend(sone.getId());
83 } else if (member.equals("current")) {
84 Sone currentSone = (Sone) templateContext.get("currentSone");
85 return (currentSone != null) && currentSone.equals(sone);
86 } else if (member.equals("modified")) {
87 return core.isModifiedSone(sone);
88 } else if (member.equals("status")) {
89 return core.getSoneStatus(sone);
90 } else if (member.equals("unknown")) {
91 return core.getSoneStatus(sone) == SoneStatus.unknown;
92 } else if (member.equals("idle")) {
93 return core.getSoneStatus(sone) == SoneStatus.idle;
94 } else if (member.equals("inserting")) {
95 return core.getSoneStatus(sone) == SoneStatus.inserting;
96 } else if (member.equals("downloading")) {
97 return core.getSoneStatus(sone) == SoneStatus.downloading;
98 } else if (member.equals("new")) {
99 return core.isNewSone(sone.getId());
100 } else if (member.equals("locked")) {
101 return core.isLocked(sone);
102 } else if (member.equals("lastUpdatedText")) {
103 return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
104 } else if (member.equals("trust")) {
105 Sone currentSone = (Sone) templateContext.get("currentSone");
106 if (currentSone == null) {
109 Trust trust = core.getTrust(currentSone, sone);
110 logger.log(Level.FINEST, "Trust for %s by %s: %s", new Object[] { sone, currentSone, trust });
112 return new Trust(null, null, null);
116 return super.get(templateContext, object, member);
124 * Returns the nice name of the given Sone.
127 * The Sone to get the nice name for
128 * @return The nice name of the Sone
130 public static String getNiceName(Sone sone) {
131 Profile profile = sone.getProfile();
132 String firstName = profile.getFirstName();
133 String middleName = profile.getMiddleName();
134 String lastName = profile.getLastName();
135 if (firstName == null) {
136 if (lastName == null) {
137 return String.valueOf(sone.getName());
141 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");