Turn Sone into an interface, add in-memory implementation of Sone.
[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.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;
34
35 /**
36  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
37  * <dl>
38  * <dt>niceName</dt>
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>
41  * <dt>friend</dt>
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>
45  * <dt>current</dt>
46  * <dd>Will return {@code true} if the sone in question is the currently logged
47  * in Sone.</dd>
48  * </dl>
49  *
50  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51  */
52 public class SoneAccessor extends ReflectionAccessor {
53
54         /** The logger. */
55         private static final Logger logger = Logging.getLogger(SoneAccessor.class);
56
57         /** The core. */
58         private final Core core;
59
60         /**
61          * Creates a new Sone accessor.
62          *
63          * @param core
64          *            The Sone core
65          */
66         public SoneAccessor(Core core) {
67                 this.core = core;
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         @Override
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("friend")) {
79                         Sone currentSone = (Sone) templateContext.get("currentSone");
80                         return (currentSone != null) && currentSone.hasFriend(sone.getId());
81                 } else if (member.equals("current")) {
82                         Sone currentSone = (Sone) templateContext.get("currentSone");
83                         return (currentSone != null) && currentSone.equals(sone);
84                 } else if (member.equals("modified")) {
85                         return core.isModifiedSone(sone);
86                 } else if (member.equals("status")) {
87                         return sone.getStatus();
88                 } else if (member.equals("unknown")) {
89                         return sone.getStatus() == SoneStatus.unknown;
90                 } else if (member.equals("idle")) {
91                         return sone.getStatus() == SoneStatus.idle;
92                 } else if (member.equals("inserting")) {
93                         return sone.getStatus() == SoneStatus.inserting;
94                 } else if (member.equals("downloading")) {
95                         return sone.getStatus() == SoneStatus.downloading;
96                 } else if (member.equals("new")) {
97                         return !sone.isKnown();
98                 } else if (member.equals("locked")) {
99                         return core.isLocked(sone);
100                 } else if (member.equals("lastUpdatedText")) {
101                         return GetTimesAjaxPage.getTime((WebInterface) templateContext.get("webInterface"), sone.getTime());
102                 } else if (member.equals("trust")) {
103                         Sone currentSone = (Sone) templateContext.get("currentSone");
104                         if (currentSone == null) {
105                                 return null;
106                         }
107                         Trust trust = core.getTrust(currentSone, sone);
108                         logger.log(Level.FINEST, String.format("Trust for %s by %s: %s", sone, currentSone, trust));
109                         if (trust == null) {
110                                 return new Trust(null, null, null);
111                         }
112                         return trust;
113                 }
114                 return super.get(templateContext, object, member);
115         }
116
117         //
118         // STATIC METHODS
119         //
120
121         /**
122          * Returns the nice name of the given Sone.
123          *
124          * @param sone
125          *            The Sone to get the nice name for
126          * @return The nice name of the Sone
127          */
128         public static String getNiceName(Sone sone) {
129                 Profile profile = sone.getProfile();
130                 String firstName = profile.getFirstName();
131                 String middleName = profile.getMiddleName();
132                 String lastName = profile.getLastName();
133                 if (firstName == null) {
134                         if (lastName == null) {
135                                 return String.valueOf(sone.getName());
136                         }
137                         return lastName;
138                 }
139                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
140         }
141
142 }