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