Get the trust directly from the identity.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
1 /*
2  * Sone - SoneAccessor.java - Copyright © 2010–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.logging.Level;
21 import java.util.logging.Logger;
22
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;
35
36 /**
37  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
38  * <dl>
39  * <dt>niceName</dt>
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>
42  * <dt>friend</dt>
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>
46  * <dt>current</dt>
47  * <dd>Will return {@code true} if the sone in question is the currently logged
48  * in Sone.</dd>
49  * </dl>
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class SoneAccessor extends ReflectionAccessor {
54
55         /** The logger. */
56         private static final Logger logger = Logging.getLogger(SoneAccessor.class);
57
58         /** The core. */
59         private final Core core;
60
61         /**
62          * Creates a new Sone accessor.
63          *
64          * @param core
65          *            The Sone core
66          */
67         public SoneAccessor(Core core) {
68                 this.core = core;
69         }
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
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) {
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                 }
117                 return super.get(templateContext, object, member);
118         }
119
120         //
121         // STATIC METHODS
122         //
123
124         /**
125          * Returns the nice name of the given Sone.
126          *
127          * @param sone
128          *            The Sone to get the nice name for
129          * @return The nice name of the Sone
130          */
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());
139                         }
140                         return lastName;
141                 }
142                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
143         }
144
145 }