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.io.Serializable;
22  import java.util.Comparator;
23  
24  import org.apache.commons.lang.builder.CompareToBuilder;
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  
27  /**
28   * A notification to a user about an Issue.
29   * 
30   * <p>
31   * An Notification can only belong to 1 Issue (composition).
32   * </p>
33   * 
34   * @author ready
35   */
36  public class Notification extends AbstractEntity implements Comparable<Entity> {
37  
38  	/**
39  	 * 
40  	 */
41  	private static final long serialVersionUID = 1L;
42  
43  	public static final Comparator<Notification> TYPE_COMPARATOR = new RoleComparator();
44  
45  	public static final Comparator<Notification> USER_COMPARATOR = new UserComparator();
46  
47  	public static final Comparator<Notification> ISSUE_USER_ROLE_COMPARATOR = new IssueUserRoleComparator();
48  
49  	private Issue issue;
50  
51  	private User user;
52  
53  	private Role role;
54  
55  	/**
56  	 * Default constructor (required by Hibernate).
57  	 * 
58  	 * <p>
59  	 * PENDING: should be <code>private</code> so that it can only be used by
60  	 * Hibernate, to ensure that the fields which form an instance's identity
61  	 * are always initialized/never <tt>null</tt>.
62  	 * </p>
63  	 */
64  	public Notification() {
65  	}
66  
67  	/**
68  	 * @deprecated use Role instead int for role
69  	 * @param user
70  	 * @param issue
71  	 * @param role
72  	 */
73  	public Notification(User user, Issue issue, int role) {
74  		this.setUser(user);
75  		this.setIssue(issue);
76  		this.setNotificationRole(role);
77  
78  	}
79  
80  	public Notification(User user, Issue issue, Role role) {
81  		this.setUser(user);
82  		this.setIssue(issue);
83  		this.setRole(role);
84  	}
85  
86  	public Issue getIssue() {
87  		return issue;
88  	}
89  
90  	public void setIssue(Issue issue) {
91  		if (issue == null) {
92  			throw new IllegalArgumentException("null issue");
93  		}
94  		this.issue = issue;
95  	}
96  
97  	public User getUser() {
98  		return user;
99  	}
100 
101 	public void setUser(User user) {
102 		if (user == null) {
103 			throw new IllegalArgumentException("null user");
104 		}
105 		this.user = user;
106 	}
107 
108 	/**
109 	 * @deprecated use getRole instead
110 	 * @return
111 	 */
112 	public int getNotificationRole() {
113 		Role r = getRole();
114 		if (null == r) {
115 			r = Role.ANY;
116 		}
117 		return r.code;
118 	}
119 
120 	/**
121 	 * @deprecated
122 	 * @param role
123 	 */
124 	public void setNotificationRole(int role) {
125 		Role r = getRoleForCode(role);
126 		if (null == r) {
127 			r = Role.ANY;
128 		}
129 		this.setRole(r);
130 	}
131 
132 	private Role getRoleForCode(int code) {
133 		for (int i = 0; i < Role.values().length; i++) {
134 			if (Role.values()[i].code == code) {
135 				return Role.values()[i];
136 			}
137 		}
138 		return Role.ANY;
139 	}
140 
141 	public Role getRole() {
142 		return this.role;
143 	}
144 
145 	public void setRole(Role role) {
146 		this.role = role;
147 	}
148 
149 	@Override
150 	public String toString() {
151 		return new ToStringBuilder(this).append("id", getId()).append("issue",
152 				getIssue()).append("user", getUser()).append("role", getRole())
153 				.toString();
154 	}
155 
156 	/**
157 	 * Compares by properties issue, user, role.
158 	 * 
159 	 * @author ranks
160 	 * 
161 	 */
162 	public static final class IssueUserRoleComparator implements
163 			Comparator<Notification>, Serializable {
164 		/**
165 		 * 
166 		 */
167 		private static final long serialVersionUID = 1L;
168 
169 		public int compare(Notification o1, Notification o2) {
170 			return new CompareToBuilder().append(o1.getIssue(), o2.getIssue())
171 					.append(o1.getUser(), o2.getUser(), User.NAME_COMPARATOR)
172 					.append(o1.getRole().getCode(), o2.getRole().getCode())
173 					.toComparison();
174 		}
175 	}
176 
177 	private static class UserComparator implements Comparator<Notification>,
178 			Serializable {
179 		/**
180 		 * 
181 		 */
182 		private static final long serialVersionUID = 1L;
183 
184 		public int compare(Notification a, Notification b) {
185 			return new CompareToBuilder().append(a.getUser(), b.getUser(),
186 					User.NAME_COMPARATOR).append(a.getRole(), b.getRole(),
187 					Notification.TYPE_COMPARATOR).toComparison();
188 		}
189 
190 	}
191 
192 	private static class RoleComparator implements Comparator<Notification>,
193 			Serializable {
194 		/**
195 		 * 
196 		 */
197 		private static final long serialVersionUID = 1L;
198 
199 		public int compare(Notification a, Notification b) {
200 			if (null == a.getRole()) {
201 				return null == b.getRole()?0 : -1;
202 			} else if (b.getRole() == null) {
203 				return 1;
204 			}
205 			return new CompareToBuilder().append(a.getRole().getCode(),
206 					b.getRole().getCode()).toComparison();
207 		}
208 
209 	}
210 
211 	public static enum Role {
212 
213 		ANY(-1),
214 
215 		CREATOR(1),
216 
217 		OWNER(2),
218 
219 		CONTRIBUTER(3),
220 
221 		QA(4),
222 
223 		PM(5),
224 
225 		PO(6),
226 
227 		CO(7),
228 
229 		VO(8),
230 
231 		IP(9);
232 
233 		private final int code;
234 
235 		private Role(int code) {
236 			this.code = code;
237 		}
238 
239 		public Integer getCode() {
240 			return this.code;
241 		}
242 
243 	}
244 
245 	public static enum Type {
246 
247 		CREATED(1),
248 
249 		UPDATED(2),
250 
251 		ASSIGNED(3),
252 
253 		CLOSED(4),
254 
255 		SELF_REGISTER(5),
256 
257 		ISSUE_REMINDER(6);
258 
259 		private final int code;
260 
261 		private Type(int code) {
262 			this.code = code;
263 		}
264 
265 		public Integer getCode() {
266 			return code;
267 		}
268 
269 	}
270 
271 }