Merge branch 'release-0.4.2'
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
1 /*
2  * Sone - SoneAccessor.java - Copyright © 2010 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.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.util.logging.Logging;
29 import net.pterodactylus.util.template.Accessor;
30 import net.pterodactylus.util.template.ReflectionAccessor;
31 import net.pterodactylus.util.template.TemplateContext;
32
33 /**
34  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
35  * <dl>
36  * <dt>niceName</dt>
37  * <dd>Will show a combination of first name, middle name, and last name, if
38  * available, otherwise the username of the Sone is returned.</dd>
39  * <dt>friend</dt>
40  * <dd>Will return {@code true} if the sone in question is a friend of the
41  * currently logged in Sone (as determined by accessing the “currentSone”
42  * variable of the given {@link TemplateContext}).</dd>
43  * <dt>current</dt>
44  * <dd>Will return {@code true} if the sone in question is the currently logged
45  * in Sone.</dd>
46  * </dl>
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class SoneAccessor extends ReflectionAccessor {
51
52         /** The logger. */
53         private static final Logger logger = Logging.getLogger(SoneAccessor.class);
54
55         /** The core. */
56         private final Core core;
57
58         /**
59          * Creates a new Sone accessor.
60          *
61          * @param core
62          *            The Sone core
63          */
64         public SoneAccessor(Core core) {
65                 this.core = core;
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         public Object get(TemplateContext templateContext, Object object, String member) {
73                 Sone sone = (Sone) object;
74                 if (member.equals("niceName")) {
75                         return getNiceName(sone);
76                 } else if (member.equals("local")) {
77                         return core.isLocalSone(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 core.getSoneStatus(sone);
88                 } else if (member.equals("unknown")) {
89                         return core.getSoneStatus(sone) == SoneStatus.unknown;
90                 } else if (member.equals("idle")) {
91                         return core.getSoneStatus(sone) == SoneStatus.idle;
92                 } else if (member.equals("inserting")) {
93                         return core.getSoneStatus(sone) == SoneStatus.inserting;
94                 } else if (member.equals("downloading")) {
95                         return core.getSoneStatus(sone) == SoneStatus.downloading;
96                 } else if (member.equals("new")) {
97                         return core.isNewSone(sone.getId());
98                 } else if (member.equals("locked")) {
99                         return core.isLocked(sone);
100                 } else if (member.equals("trust")) {
101                         Sone currentSone = (Sone) templateContext.get("currentSone");
102                         if (currentSone == null) {
103                                 return null;
104                         }
105                         Trust trust = core.getTrust(currentSone, sone);
106                         logger.log(Level.FINEST, "Trust for %s by %s: %s", new Object[] { sone, currentSone, trust });
107                         if (trust == null) {
108                                 return new Trust(null, null, null);
109                         }
110                         return trust;
111                 }
112                 return super.get(templateContext, object, member);
113         }
114
115         //
116         // STATIC METHODS
117         //
118
119         /**
120          * Returns the nice name of the given Sone.
121          *
122          * @param sone
123          *            The Sone to get the nice name for
124          * @return The nice name of the Sone
125          */
126         public static String getNiceName(Sone sone) {
127                 Profile profile = sone.getProfile();
128                 String firstName = profile.getFirstName();
129                 String middleName = profile.getMiddleName();
130                 String lastName = profile.getLastName();
131                 if (firstName == null) {
132                         if (lastName == null) {
133                                 return String.valueOf(sone.getName());
134                         }
135                         return lastName;
136                 }
137                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
138         }
139
140 }