Merge branch 'release-0.8.5'
[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 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("friend")) {
80                         Sone currentSone = (Sone) templateContext.get("currentSone");
81                         return (currentSone != null) && currentSone.hasFriend(sone.getId());
82                 } else if (member.equals("current")) {
83                         Sone currentSone = (Sone) templateContext.get("currentSone");
84                         return (currentSone != null) && currentSone.equals(sone);
85                 } else if (member.equals("modified")) {
86                         return core.isModifiedSone(sone);
87                 } else if (member.equals("status")) {
88                         return sone.getStatus();
89                 } else if (member.equals("unknown")) {
90                         return sone.getStatus() == SoneStatus.unknown;
91                 } else if (member.equals("idle")) {
92                         return sone.getStatus() == SoneStatus.idle;
93                 } else if (member.equals("inserting")) {
94                         return sone.getStatus() == SoneStatus.inserting;
95                 } else if (member.equals("downloading")) {
96                         return sone.getStatus() == SoneStatus.downloading;
97                 } else if (member.equals("new")) {
98                         return !sone.isKnown();
99                 } else if (member.equals("locked")) {
100                         return core.isLocked(sone);
101                 } else if (member.equals("lastUpdatedText")) {
102                         return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
103                 } else if (member.equals("trust")) {
104                         Sone currentSone = (Sone) templateContext.get("currentSone");
105                         if (currentSone == null) {
106                                 return null;
107                         }
108                         Trust trust = sone.getIdentity().getTrust((OwnIdentity) currentSone.getIdentity());
109                         logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
110                         if (trust == null) {
111                                 return new Trust(null, null, null);
112                         }
113                         return trust;
114                 }
115                 return super.get(templateContext, object, member);
116         }
117
118         //
119         // STATIC METHODS
120         //
121
122         /**
123          * Returns the nice name of the given Sone.
124          *
125          * @param sone
126          *            The Sone to get the nice name for
127          * @return The nice name of the Sone
128          */
129         public static String getNiceName(Sone sone) {
130                 Profile profile = sone.getProfile();
131                 String firstName = profile.getFirstName();
132                 String middleName = profile.getMiddleName();
133                 String lastName = profile.getLastName();
134                 if (firstName == null) {
135                         if (lastName == null) {
136                                 return String.valueOf(sone.getName());
137                         }
138                         return lastName;
139                 }
140                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
141         }
142
143 }