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 org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  
24  import java.io.Serializable;
25  import java.util.Comparator;
26  
27  /**
28   * A notification to a user about an Issue.
29   * <p/>
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       * <p/>
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       */
70      public Notification(User user, Issue issue, int role) {
71          this.setUser(user);
72          this.setIssue(issue);
73          this.setNotificationRole(role);
74  
75      }
76  
77      public Notification(User user, Issue issue, Role role) {
78          this.setUser(user);
79          this.setIssue(issue);
80          this.setRole(role);
81      }
82  
83      public Issue getIssue() {
84          return issue;
85      }
86  
87      public void setIssue(Issue issue) {
88          if (issue == null) {
89              throw new IllegalArgumentException("null issue");
90          }
91          this.issue = issue;
92      }
93  
94      public User getUser() {
95          return user;
96      }
97  
98      public void setUser(User user) {
99          if (user == null) {
100             throw new IllegalArgumentException("null user");
101         }
102         this.user = user;
103     }
104 
105     /**
106      * @deprecated use getRole instead
107      */
108     public int getNotificationRole() {
109         Role r = getRole();
110         if (null == r) {
111             r = Role.ANY;
112         }
113         return r.code;
114     }
115 
116     /**
117      * @deprecated
118      */
119     public void setNotificationRole(int role) {
120         Role r = getRoleForCode(role);
121         if (null == r) {
122             r = Role.ANY;
123         }
124         this.setRole(r);
125     }
126 
127     private Role getRoleForCode(int code) {
128         for (int i = 0; i < Role.values().length; i++) {
129             if (Role.values()[i].code == code) {
130                 return Role.values()[i];
131             }
132         }
133         return Role.ANY;
134     }
135 
136     public Role getRole() {
137         return this.role;
138     }
139 
140     public void setRole(Role role) {
141         this.role = role;
142     }
143 
144     @Override
145     public String toString() {
146         return new ToStringBuilder(this).append("id", getId()).append("issue",
147                 getIssue()).append("user", getUser()).append("role", getRole())
148                 .toString();
149     }
150 
151     /**
152      * Compares by properties issue, user, role.
153      *
154      * @author ranks
155      */
156     public static final class IssueUserRoleComparator implements
157             Comparator<Notification>, Serializable {
158         /**
159          *
160          */
161         private static final long serialVersionUID = 1L;
162 
163         public int compare(Notification../../../org/itracker/model/Notification.html#Notification">Notification o1, Notification o2) {
164             return new CompareToBuilder().append(o1.getIssue(), o2.getIssue())
165                     .append(o1.getUser(), o2.getUser(), User.NAME_COMPARATOR)
166                     .append(o1.getRole().getCode(), o2.getRole().getCode())
167                     .toComparison();
168         }
169     }
170 
171     private static class UserComparator implements Comparator<Notification>,
172             Serializable {
173         /**
174          *
175          */
176         private static final long serialVersionUID = 1L;
177 
178         public int compare(Notification"../../../org/itracker/model/Notification.html#Notification">Notification a, Notification b) {
179             return new CompareToBuilder().append(a.getUser(), b.getUser(),
180                     User.NAME_COMPARATOR).append(a.getRole(), b.getRole(),
181                     Notification.TYPE_COMPARATOR).toComparison();
182         }
183 
184     }
185 
186     private static class RoleComparator implements Comparator<Notification>,
187             Serializable {
188         /**
189          *
190          */
191         private static final long serialVersionUID = 1L;
192 
193         public int compare(Notification"../../../org/itracker/model/Notification.html#Notification">Notification a, Notification b) {
194             if (null == a.getRole()) {
195                 return null == b.getRole() ? 0 : -1;
196             } else if (b.getRole() == null) {
197                 return 1;
198             }
199             return new CompareToBuilder().append(a.getRole().getCode(),
200                     b.getRole().getCode()).toComparison();
201         }
202 
203     }
204 
205     public static enum Role {
206 
207         ANY(-1),
208 
209         CREATOR(1),
210 
211         OWNER(2),
212 
213         CONTRIBUTER(3),
214 
215         QA(4),
216 
217         PM(5),
218 
219         PO(6),
220 
221         CO(7),
222 
223         VO(8),
224 
225         IP(9);
226 
227         private final int code;
228 
229         private Role(int code) {
230             this.code = code;
231         }
232 
233         public Integer getCode() {
234             return this.code;
235         }
236 
237     }
238 
239     public static enum Type {
240 
241         CREATED(1),
242 
243         UPDATED(2),
244 
245         ASSIGNED(3),
246 
247         CLOSED(4),
248 
249         SELF_REGISTER(5),
250 
251         ISSUE_REMINDER(6);
252 
253         private final int code;
254 
255         private Type(int code) {
256             this.code = code;
257         }
258 
259         public Integer getCode() {
260             return code;
261         }
262 
263     }
264 
265 }