1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.model;
20
21 import java.util.ArrayList;
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26
27 import javax.mail.internet.AddressException;
28 import javax.mail.internet.InternetAddress;
29
30 import org.apache.commons.lang.builder.CompareToBuilder;
31 import org.apache.commons.lang.builder.ToStringBuilder;
32 import org.apache.log4j.Logger;
33 import org.itracker.services.util.UserUtilities;
34
35
36
37
38
39
40 public class User extends AbstractEntity implements Comparable<Entity> {
41
42
43
44
45 private static final long serialVersionUID = 1L;
46 private static final Logger log = Logger.getLogger(User.class);
47 public static final Comparator<User> NAME_COMPARATOR = new NameComparator();
48 public static final Comparator<User> LOGIN_COMPARATOR = new LoginComparator();
49
50 private String login;
51
52 private String password;
53
54 private String firstName;
55
56 private String lastName;
57
58 private String email;
59
60 private int status;
61
62 private boolean superUser;
63
64 private int registrationType;
65
66
67 private UserPreferences userPreferences;
68
69
70
71 private Set<Permission> permissions = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);
72
73
74 private List<Project> projects = new ArrayList<Project>();
75 private UserPreferences preferences;
76
77 public UserPreferences getPreferences() {
78 return preferences;
79 }
80
81 public void setPreferences(UserPreferences preferences) {
82 this.preferences = preferences;
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public User() {
122 }
123
124 public User(String login) {
125 setLogin(login);
126 }
127
128 public User(String login, String password, String firstName,
129 String lastName, String email, boolean superUser) {
130 this(login, password, firstName, lastName, email,
131 UserUtilities.REGISTRATION_TYPE_ADMIN, superUser);
132 }
133
134 public User(String login, String password, String firstName,
135 String lastName, String email, int registrationType,
136 boolean superUser) {
137 this(login);
138 this.password = password;
139 this.firstName = firstName;
140 this.lastName = lastName;
141 this.email = email;
142 this.registrationType = registrationType;
143 setSuperUser(superUser);
144 }
145
146 public String getLogin() {
147 return login;
148 }
149
150 public void setLogin(String login) {
151 if (login == null) {
152 throw new IllegalArgumentException("null login");
153 }
154 this.login = login;
155 }
156
157 public String getPassword() {
158 return password;
159 }
160
161 public void setPassword(String value) {
162 this.password = value;
163 }
164
165 public String getFirstName() {
166 return firstName;
167 }
168
169 public void setFirstName(String value) {
170 firstName = value;
171 }
172
173 public String getLastName() {
174 return lastName;
175 }
176
177 public void setLastName(String value) {
178 this.lastName = value;
179 }
180
181 public String getEmail() {
182 return email;
183 }
184
185 public InternetAddress getEmailAddress() {
186
187 if (null == getEmail() || getEmail().trim().length() == 0) {
188 log.warn("getEmailAddress: failed to get eMail for user "
189 + getLogin() + " (" + getId() + ")");
190 return null;
191 }
192 try {
193 return new InternetAddress(getEmail(), getFirstName() + " "
194 + getLastName());
195 } catch (Exception e) {
196 try {
197 return new InternetAddress(getEmail());
198 } catch (AddressException e1) {
199 log.error("getEmailAddress: failed to parse email '"
200 + getEmail() + "' for user " + getLogin() + " ("
201 + getId() + "), returning null", e1);
202 return null;
203 }
204 }
205
206 }
207
208 public void setEmail(String email) {
209 this.email = email;
210 }
211
212 public Set<Permission> getPermissions() {
213 return permissions;
214 }
215
216 public void setPermissions(Set<Permission> getPermissions) {
217 this.permissions = getPermissions;
218 }
219
220 public UserPreferences getUserPreferences() {
221 return userPreferences;
222 }
223
224 public void setUserPreferences(UserPreferences getPreferences) {
225 this.userPreferences = getPreferences;
226 }
227
228 public int getRegistrationType() {
229 return registrationType;
230 }
231
232 public void setRegistrationType(int registrationType) {
233 this.registrationType = registrationType;
234 }
235
236 public int getStatus() {
237 return status;
238 }
239
240 public void setStatus(int status) {
241 this.status = status;
242 }
243
244 public boolean isSuperUser() {
245 return superUser;
246 }
247
248 public void setSuperUser(boolean superUser) {
249 this.superUser = superUser;
250 }
251
252 public String getFirstInitial() {
253 return (null != getFirstName() && getFirstName().length() > 0 ? getFirstName().substring(0, 1)
254 .toUpperCase()
255 + "." : "");
256 }
257
258 public boolean hasRequiredData() {
259 return hasRequiredData(true);
260 }
261
262 public boolean hasRequiredData(boolean passwordRequired) {
263 if (this.getLogin() == null || this.getLogin().equals("")
264 || this.getFirstName() == null
265 || this.getFirstName().equals("") || this.getLastName() == null
266 || this.getLastName().equals("") || this.getEmail() == null
267 || this.getEmail().equals("")) {
268 return false;
269 }
270 if (passwordRequired
271 && (this.getPassword() == null || this.getPassword().equals(""))) {
272 return false;
273 }
274 return true;
275 }
276
277 public List<Project> getProjects() {
278 return projects;
279 }
280
281 public void setProjects(List<Project> projects) {
282 this.projects = projects;
283 }
284
285 @Override
286 public String toString() {
287 return new ToStringBuilder(this).append("id", getId())
288 .append("login", getLogin()).toString();
289 }
290
291
292
293
294 private static class NameComparator implements Comparator<User> {
295
296 public int compare(User a, User b) {
297 return new CompareToBuilder().append(a.getLastName(), b.getLastName())
298 .append(a.getFirstName(), b.getFirstName()).append(a, b, LOGIN_COMPARATOR).toComparison();
299 }
300
301 }
302
303 public static final class LoginComparator implements Comparator<User> {
304 public int compare(User o1, User o2) {
305 return new CompareToBuilder().append(o1.getLogin(), o2.getLogin())
306 .toComparison();
307 }
308 }
309
310 }