2 * Sone - SoneAccessor.java - Copyright © 2010–2012 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.data.Profile;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.data.Sone.SoneStatus;
27 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
28 import net.pterodactylus.sone.freenet.wot.Trust;
29 import net.pterodactylus.sone.web.WebInterface;
30 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
31 import net.pterodactylus.util.logging.Logging;
32 import net.pterodactylus.util.template.Accessor;
33 import net.pterodactylus.util.template.ReflectionAccessor;
34 import net.pterodactylus.util.template.TemplateContext;
37 * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
40 * <dd>Will show a combination of first name, middle name, and last name, if
41 * available, otherwise the username of the Sone is returned.</dd>
43 * <dd>Will return {@code true} if the sone in question is a friend of the
44 * currently logged in Sone (as determined by accessing the “currentSone”
45 * variable of the given {@link TemplateContext}).</dd>
47 * <dd>Will return {@code true} if the sone in question is the currently logged
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class SoneAccessor extends ReflectionAccessor {
56 private static final Logger logger = Logging.getLogger(SoneAccessor.class);
59 private final Core core;
62 * Creates a new Sone accessor.
67 public SoneAccessor(Core core) {
75 public Object get(TemplateContext templateContext, Object object, String member) {
76 Sone sone = (Sone) object;
77 if (member.equals("niceName")) {
78 return getNiceName(sone);
79 } else if (member.equals("local")) {
80 return core.isLocalSone(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) {
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));
113 return new Trust(null, null, null);
117 return super.get(templateContext, object, member);
125 * Returns the nice name of the given Sone.
128 * The Sone to get the nice name for
129 * @return The nice name of the Sone
131 public static String getNiceName(Sone sone) {
132 Profile profile = sone.getProfile();
133 String firstName = profile.getFirstName();
134 String middleName = profile.getMiddleName();
135 String lastName = profile.getLastName();
136 if (firstName == null) {
137 if (lastName == null) {
138 return String.valueOf(sone.getName());
142 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");