Return the enumeration value itself.
[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 net.pterodactylus.sone.core.Core;
21 import net.pterodactylus.sone.core.Core.SoneStatus;
22 import net.pterodactylus.sone.data.Profile;
23 import net.pterodactylus.sone.data.Sone;
24 import net.pterodactylus.util.template.Accessor;
25 import net.pterodactylus.util.template.DataProvider;
26 import net.pterodactylus.util.template.ReflectionAccessor;
27
28 /**
29  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
30  * <dl>
31  * <dt>niceName</dt>
32  * <dd>Will show a combination of first name, middle name, and last name, if
33  * available, otherwise the username of the Sone is returned.</dd>
34  * <dt>friend</dt>
35  * <dd>Will return {@code true} if the sone in question is a friend of the
36  * currently logged in Sone (as determined by accessing the “currentSone”
37  * variable of the given {@link DataProvider}).</dd>
38  * <dt>current</dt>
39  * <dd>Will return {@code true} if the sone in question is the currently logged
40  * in Sone.</dd>
41  * </dl>
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class SoneAccessor extends ReflectionAccessor {
46
47         /** The core. */
48         private final Core core;
49
50         /**
51          * Creates a new Sone accessor.
52          *
53          * @param core
54          *            The Sone core
55          */
56         public SoneAccessor(Core core) {
57                 this.core = core;
58         }
59
60         /**
61          * {@inheritDoc}
62          */
63         @Override
64         public Object get(DataProvider dataProvider, Object object, String member) {
65                 Sone sone = (Sone) object;
66                 if (member.equals("niceName")) {
67                         return getNiceName(sone);
68                 } else if (member.equals("local")) {
69                         return sone.getInsertUri() != null;
70                 } else if (member.equals("friend")) {
71                         Sone currentSone = (Sone) dataProvider.getData("currentSone");
72                         return (currentSone != null) && currentSone.hasFriend(sone);
73                 } else if (member.equals("current")) {
74                         Sone currentSone = (Sone) dataProvider.getData("currentSone");
75                         return (currentSone != null) && currentSone.equals(sone);
76                 } else if (member.equals("modified")) {
77                         return sone.getModificationCounter() > 0;
78                 } else if (member.equals("status")) {
79                         return core.getSoneStatus(sone);
80                 } else if (member.equals("unknown")) {
81                         return core.getSoneStatus(sone) == SoneStatus.unknown;
82                 } else if (member.equals("idle")) {
83                         return core.getSoneStatus(sone) == SoneStatus.idle;
84                 } else if (member.equals("inserting")) {
85                         return core.getSoneStatus(sone) == SoneStatus.inserting;
86                 } else if (member.equals("downloading")) {
87                         return core.getSoneStatus(sone) == SoneStatus.downloading;
88                 }
89                 return super.get(dataProvider, object, member);
90         }
91
92         //
93         // STATIC METHODS
94         //
95
96         /**
97          * Returns the nice name of the given Sone.
98          *
99          * @param sone
100          *            The Sone to get the nice name for
101          * @return The nice name of the Sone
102          */
103         public static String getNiceName(Sone sone) {
104                 Profile profile = sone.getProfile();
105                 String firstName = profile.getFirstName();
106                 String middleName = profile.getMiddleName();
107                 String lastName = profile.getLastName();
108                 if (firstName == null) {
109                         if (lastName == null) {
110                                 return sone.getName();
111                         }
112                         return lastName;
113                 }
114                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
115         }
116
117 }