View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
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   * A user.
37   * 
38   * @author ready
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  	/** The system configuration for this User. */
67  	private UserPreferences userPreferences;
68  
69  	/** The Permissions of this User on all Projects. */
70  	// TODO: it would be a Set, not list
71  	private Set<Permission> permissions = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);
72  
73  	/** The Projects owned by this User. */
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  	 * This class used to have an <code>activities</code> attribute, which was
87  	 * a Collection<IssueActivity>. This has been removed because the
88  	 * association User - IssueActivity doesn't need to be navigatable in this
89  	 * direction.
90  	 */
91  
92  	/*
93  	 * This class used to have a <code>notifications</code> attribute, which
94  	 * was a Collection<Notification>. This has been removed because the
95  	 * association User - Notification doesn't need to be navigatable in this
96  	 * direction.
97  	 */
98  
99  	/*
100 	 * This class used to have an <code>attachments</code> attribute, which
101 	 * was a Collection<IssueAttachment>. This has been removed because the
102 	 * association User - IssueAttachment doesn't need to be navigatable in this
103 	 * direction.
104 	 */
105 
106 	/*
107 	 * This class used to have a <code>history</code> attribute, which was a
108 	 * Collection<IssueHistory>. This has been removed because the association
109 	 * User - IssueHistory doesn't need to be navigatable in this direction.
110 	 */
111 
112 	/**
113 	 * Default constructor (required by Hibernate).
114 	 * 
115 	 * <p>
116 	 * PENDING: should be <code>private</code> so that it can only be used by
117 	 * Hibernate, to ensure that the fields which form an instance's identity
118 	 * are always initialized/never <tt>null</tt>.
119 	 * </p>
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 	 * Compares 2 users by last and first name.
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 }