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