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.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
29
30
31
32
33
34
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
57
58
59
60
61
62
63
64 public Notification() {
65 }
66
67
68
69
70
71
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
110
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
122
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
158
159
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 }